Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startup shrink all slots has to avoid the snapshot slot #2339

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4826,17 +4826,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 @@ -4850,7 +4861,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 @@ -12049,7 +12060,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 @@ -12107,7 +12118,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 @@ -5927,6 +5927,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