From 9d5a07bac4fb047a55d64cfeb6fae0a2b3ee1624 Mon Sep 17 00:00:00 2001 From: sakridge Date: Fri, 13 Dec 2019 16:46:16 -0800 Subject: [PATCH] Move create_dir_all to AccountsDB::new (#7465) AppendVec create doesn't need to try and create paths every time and it can stall while snapshot create is happening. --- core/src/snapshot_packager_service.rs | 1 + runtime/src/accounts_db.rs | 9 ++++++++- runtime/src/append_vec.rs | 5 +---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core/src/snapshot_packager_service.rs b/core/src/snapshot_packager_service.rs index fb166e6108..c0f6312249 100644 --- a/core/src/snapshot_packager_service.rs +++ b/core/src/snapshot_packager_service.rs @@ -230,6 +230,7 @@ mod tests { let snapshot_package_output_path = temp_dir.join("snapshots_output"); fs::create_dir_all(&snapshot_package_output_path).unwrap(); + fs::create_dir_all(&accounts_dir).unwrap(); // Create some storage entries let storage_entries: Vec<_> = (0..5) .map(|i| Arc::new(AccountStorageEntry::new(&accounts_dir, 0, i, 10))) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index a57ff4978b..502f4d7fcb 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -407,7 +407,7 @@ impl Default for AccountsDB { impl AccountsDB { pub fn new(paths: Vec) -> Self { - if !paths.is_empty() { + let new = if !paths.is_empty() { Self { paths: RwLock::new(paths), temp_paths: None, @@ -422,7 +422,14 @@ impl AccountsDB { temp_paths: Some(temp_dirs), ..Self::default() } + }; + { + let paths = new.paths.read().unwrap(); + for path in paths.iter() { + std::fs::create_dir_all(path).expect("Create directory failed."); + } } + new } #[cfg(test)] diff --git a/runtime/src/append_vec.rs b/runtime/src/append_vec.rs index 703f530a0a..eb0115e7e7 100644 --- a/runtime/src/append_vec.rs +++ b/runtime/src/append_vec.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use solana_sdk::{account::Account, clock::Epoch, hash::Hash, pubkey::Pubkey}; use std::{ fmt, - fs::{create_dir_all, remove_file, OpenOptions}, + fs::{remove_file, OpenOptions}, io, io::{Cursor, Seek, SeekFrom, Write}, mem, @@ -95,9 +95,6 @@ impl AppendVec { pub fn new(file: &Path, create: bool, size: usize) -> Self { if create { let _ignored = remove_file(file); - if let Some(parent) = file.parent() { - create_dir_all(parent).expect("Create directory failed"); - } } let mut data = OpenOptions::new()