Skip to content

Commit

Permalink
startup shrink all slots has to avoid the snapshot slot (#2339)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington authored Jul 30, 2024
1 parent ba36d0b commit 83e967a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
19 changes: 15 additions & 4 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4829,17 +4829,28 @@ impl AccountsDb {
num_candidates
}

/// This is only called at startup from bank when we are being extra careful such as when we downloaded a snapshot.
/// Also called from tests.
/// `newest_slot_skip_shrink_inclusive` is used to avoid shrinking the slot we are loading a snapshot from. If we shrink that slot, we affect
/// the bank hash calculation verification at startup.
pub fn shrink_all_slots(
&self,
is_startup: bool,
last_full_snapshot_slot: Option<Slot>,
epoch_schedule: &EpochSchedule,
newest_slot_skip_shrink_inclusive: Option<Slot>,
) {
let _guard = self.active_stats.activate(ActiveStatItem::Shrink);
const DIRTY_STORES_CLEANING_THRESHOLD: usize = 10_000;
const OUTER_CHUNK_SIZE: usize = 2000;
let mut slots = self.all_slots_in_storage();
if let Some(newest_slot_skip_shrink_inclusive) = newest_slot_skip_shrink_inclusive {
// at startup, we cannot shrink the slot that we're about to replay and recalculate bank hash for.
// That storage's contents are used to verify the bank hash (and accounts delta hash) of the startup slot.
slots.retain(|slot| slot < &newest_slot_skip_shrink_inclusive);
}

if is_startup {
let slots = self.all_slots_in_storage();
let threads = num_cpus::get();
let inner_chunk_size = std::cmp::max(OUTER_CHUNK_SIZE / threads, 1);
slots.chunks(OUTER_CHUNK_SIZE).for_each(|chunk| {
Expand All @@ -4853,7 +4864,7 @@ impl AccountsDb {
}
});
} else {
for slot in self.all_slots_in_storage() {
for slot in slots {
self.shrink_slot_forced(slot);
if self.dirty_stores.len() > DIRTY_STORES_CLEANING_THRESHOLD {
self.clean_accounts(None, is_startup, last_full_snapshot_slot, epoch_schedule);
Expand Down Expand Up @@ -12052,7 +12063,7 @@ pub mod tests {
accounts.shrink_candidate_slots(&epoch_schedule);
}

accounts.shrink_all_slots(*startup, None, &EpochSchedule::default());
accounts.shrink_all_slots(*startup, None, &EpochSchedule::default(), None);
}
}

Expand Down Expand Up @@ -12110,7 +12121,7 @@ pub mod tests {
);

// Now, do full-shrink.
accounts.shrink_all_slots(false, None, &EpochSchedule::default());
accounts.shrink_all_slots(false, None, &EpochSchedule::default(), None);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_accounts_file(shrink_slot)
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5903,6 +5903,8 @@ impl Bank {
true,
Some(last_full_snapshot_slot),
self.epoch_schedule(),
// we cannot allow the snapshot slot to be shrunk
Some(self.slot()),
);
info!("Shrinking... Done.");
} else {
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/serde_snapshot/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ mod serde_snapshot_tests {
pubkey_count,
accounts.all_account_count_in_accounts_file(shrink_slot)
);
accounts.shrink_all_slots(*startup, None, &EpochSchedule::default());
accounts.shrink_all_slots(*startup, None, &EpochSchedule::default(), None);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_accounts_file(shrink_slot)
Expand Down Expand Up @@ -851,7 +851,7 @@ mod serde_snapshot_tests {
.unwrap();

// repeating should be no-op
accounts.shrink_all_slots(*startup, None, &epoch_schedule);
accounts.shrink_all_slots(*startup, None, &epoch_schedule, None);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_accounts_file(shrink_slot)
Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn test_shrink_and_clean() {
if exit_for_shrink.load(Ordering::Relaxed) {
break;
}
accounts_for_shrink.shrink_all_slots(false, None, &EpochSchedule::default());
accounts_for_shrink.shrink_all_slots(false, None, &EpochSchedule::default(), None);
});

let mut alive_accounts = vec![];
Expand Down

0 comments on commit 83e967a

Please sign in to comment.