Purges old bank snapshots at startup (#31656)

Co-authored-by: Andrew Fitzgerald <apfitzge@gmail.com>
This commit is contained in:
Brooks 2023-05-23 13:18:05 -04:00 committed by GitHub
parent 5448d0b1e8
commit 61afb07dbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -565,6 +565,9 @@ impl Validator {
info!("done. {}", start);
snapshot_utils::purge_incomplete_bank_snapshots(&config.snapshot_config.bank_snapshots_dir);
snapshot_utils::purge_old_bank_snapshots_at_startup(
&config.snapshot_config.bank_snapshots_dir,
);
info!("Cleaning orphaned account snapshot directories..");
if let Err(e) = clean_orphaned_account_snapshot_dirs(

View File

@ -2992,6 +2992,23 @@ pub fn purge_old_bank_snapshots(
);
}
/// At startup, purge old (i.e. unusable) bank snapshots
///
/// Only a single bank snapshot could be needed at startup (when using fast boot), so
/// retain the highest bank snapshot "post", and purge the rest.
pub fn purge_old_bank_snapshots_at_startup(bank_snapshots_dir: impl AsRef<Path>) {
purge_old_bank_snapshots(&bank_snapshots_dir, 0, Some(BankSnapshotType::Pre));
purge_old_bank_snapshots(&bank_snapshots_dir, 1, Some(BankSnapshotType::Post));
let highest_bank_snapshot_post = get_highest_bank_snapshot_post(&bank_snapshots_dir);
if let Some(highest_bank_snapshot_post) = highest_bank_snapshot_post {
debug!(
"Retained bank snapshot for slot {}, and purged the rest.",
highest_bank_snapshot_post.slot
);
}
}
/// Purges bank snapshots that are older than `slot`
pub fn purge_bank_snapshots_older_than_slot(bank_snapshots_dir: impl AsRef<Path>, slot: Slot) {
let mut bank_snapshots = get_bank_snapshots(&bank_snapshots_dir);
@ -5644,4 +5661,22 @@ mod tests {
assert_eq!(bank_snapshots_before.len(), bank_snapshots_after.len() + 9);
assert!(bank_snapshots_after.is_empty());
}
#[test]
fn test_purge_old_bank_snapshots_at_startup() {
let genesis_config = GenesisConfig::default();
let bank_snapshots_dir = tempfile::TempDir::new().unwrap();
// The bank must stay in scope to ensure the temp dirs that it holds are not dropped
let _bank = create_snapshot_dirs_for_tests(&genesis_config, &bank_snapshots_dir, 9, 6);
purge_old_bank_snapshots_at_startup(&bank_snapshots_dir);
let bank_snapshots_pre = get_bank_snapshots_pre(&bank_snapshots_dir);
assert!(bank_snapshots_pre.is_empty());
let bank_snapshots_post = get_bank_snapshots_post(&bank_snapshots_dir);
assert_eq!(bank_snapshots_post.len(), 1);
assert_eq!(bank_snapshots_post.first().unwrap().slot, 6);
}
}