async delete contents but leave directory (#31737)

* async delete contents but leave directory

* Clarified comment
This commit is contained in:
Andrew Fitzgerald 2023-05-23 15:33:09 -07:00 committed by GitHub
parent daebea0f33
commit f52ded35f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 6 deletions

View File

@ -93,7 +93,9 @@ use {
snapshot_archive_info::SnapshotArchiveInfoGetter,
snapshot_config::SnapshotConfig,
snapshot_hash::StartingSnapshotHashes,
snapshot_utils::{self, clean_orphaned_account_snapshot_dirs, move_and_async_delete_path},
snapshot_utils::{
self, clean_orphaned_account_snapshot_dirs, move_and_async_delete_path_contents,
},
},
solana_sdk::{
clock::Slot,
@ -2238,11 +2240,11 @@ fn get_stake_percent_in_gossip(bank: &Bank, cluster_info: &ClusterInfo, log: boo
fn cleanup_accounts_paths(config: &ValidatorConfig) {
for accounts_path in &config.account_paths {
move_and_async_delete_path(accounts_path);
move_and_async_delete_path_contents(accounts_path);
}
if let Some(ref shrink_paths) = config.account_shrink_paths {
for accounts_path in shrink_paths {
move_and_async_delete_path(accounts_path);
move_and_async_delete_path_contents(accounts_path);
}
}
}

View File

@ -35,7 +35,7 @@ use {
snapshot_hash::StartingSnapshotHashes,
snapshot_utils::{
self, clean_orphaned_account_snapshot_dirs, create_all_accounts_run_and_snapshot_dirs,
move_and_async_delete_path,
move_and_async_delete_path_contents,
},
},
solana_sdk::{
@ -186,7 +186,7 @@ pub fn load_bank_forks(
let mut measure = Measure::start("clean_accounts_paths");
account_paths.iter().for_each(|path| {
if path.exists() {
move_and_async_delete_path(path);
move_and_async_delete_path_contents(path);
}
});
measure.stop();

View File

@ -469,8 +469,18 @@ fn delete_contents_of_path(path: impl AsRef<Path>) {
}
}
/// Moves and asynchronously deletes the contents of a directory to avoid blocking on it.
/// The directory is re-created after the move, and should now be empty.
pub fn move_and_async_delete_path_contents(path: impl AsRef<Path>) {
move_and_async_delete_path(&path);
// The following could fail if the rename failed.
// If that happens, the directory should be left as is.
// So we ignore errors here.
let _ = std::fs::create_dir(path);
}
/// Delete directories/files asynchronously to avoid blocking on it.
/// Fist, in sync context, rename the original path to *_deleted,
/// First, in sync context, rename the original path to *_deleted,
/// then spawn a thread to delete the renamed path.
/// If the process is killed and the deleting process is not done,
/// the leftover path will be deleted in the next process life, so