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

Simplify Bank::clean_accounts() by removing params #27254

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
8 changes: 2 additions & 6 deletions runtime/src/accounts_background_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,7 @@ impl SnapshotRequestHandler {
};

let mut clean_time = Measure::start("clean_time");
// Don't clean the slot we're snapshotting because it may have zero-lamport
// accounts that were included in the bank delta hash when the bank was frozen,
// and if we clean them here, the newly created snapshot's hash may not match
// the frozen hash.
snapshot_root_bank.clean_accounts(true, false, *last_full_snapshot_slot);
snapshot_root_bank.clean_accounts(*last_full_snapshot_slot);
clean_time.stop();

if accounts_db_caching_enabled {
Expand Down Expand Up @@ -564,7 +560,7 @@ impl AccountsBackgroundService {
// slots >= bank.slot()
bank.force_flush_accounts_cache();
}
bank.clean_accounts(true, false, last_full_snapshot_slot);
bank.clean_accounts(last_full_snapshot_slot);
last_cleaned_block_height = bank.block_height();
}
}
Expand Down
24 changes: 14 additions & 10 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7155,7 +7155,7 @@ impl Bank {
let mut clean_time = Measure::start("clean");
if !accounts_db_skip_shrink && self.slot() > 0 {
info!("cleaning..");
self.clean_accounts(true, true, Some(last_full_snapshot_slot));
self._clean_accounts(true, true, Some(last_full_snapshot_slot));
Copy link
Contributor Author

@brooksprumo brooksprumo Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since verify_snapshot_bank() happens at startup, I wanted to preserve calling AccountsDb::clean_accounts() with is_startup = true.

This call will change in the next PR too.

}
clean_time.stop();

Expand Down Expand Up @@ -7439,23 +7439,27 @@ impl Bank {
debug!("Added precompiled program {:?}", program_id);
}

pub fn clean_accounts(
&self,
skip_last: bool,
is_startup: bool,
last_full_snapshot_slot: Option<Slot>,
) {
pub(crate) fn clean_accounts(&self, last_full_snapshot_slot: Option<Slot>) {
// Don't clean the slot we're snapshotting because it may have zero-lamport
// accounts that were included in the bank delta hash when the bank was frozen,
// and if we clean them here, any newly created snapshot's hash for this bank
// may not match the frozen hash.
//
// So when we're snapshotting, set `skip_last` to true so the highest slot to clean is
// lowered by one.
let highest_slot_to_clean = skip_last.then(|| self.slot().saturating_sub(1));
self._clean_accounts(true, false, last_full_snapshot_slot)
}

fn _clean_accounts(
&self,
skip_last: bool,
is_startup: bool,
last_full_snapshot_slot: Option<Slot>,
) {
let max_clean_root = skip_last.then(|| self.slot().saturating_sub(1));

self.rc.accounts.accounts_db.clean_accounts(
highest_slot_to_clean,
max_clean_root,
is_startup,
last_full_snapshot_slot,
);
Expand Down Expand Up @@ -16256,7 +16260,7 @@ pub(crate) mod tests {
current_bank.squash();
if current_bank.slot() % 2 == 0 {
current_bank.force_flush_accounts_cache();
current_bank.clean_accounts(true, false, None);
current_bank.clean_accounts(None);
}
prev_bank = current_bank.clone();
current_bank = Arc::new(Bank::new_from_parent(
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ pub fn bank_to_full_snapshot_archive(
assert!(bank.is_complete());
bank.squash(); // Bank may not be a root
bank.force_flush_accounts_cache();
bank.clean_accounts(true, false, Some(bank.slot()));
bank.clean_accounts(Some(bank.slot()));
bank.update_accounts_hash();
bank.rehash(); // Bank accounts may have been manually modified by the caller

Expand Down Expand Up @@ -2125,7 +2125,7 @@ pub fn bank_to_incremental_snapshot_archive(
assert!(bank.slot() > full_snapshot_slot);
bank.squash(); // Bank may not be a root
bank.force_flush_accounts_cache();
bank.clean_accounts(true, false, Some(full_snapshot_slot));
bank.clean_accounts(Some(full_snapshot_slot));
bank.update_accounts_hash();
bank.rehash(); // Bank accounts may have been manually modified by the caller

Expand Down Expand Up @@ -3771,7 +3771,7 @@ mod tests {

// Ensure account1 has been cleaned/purged from everywhere
bank4.squash();
bank4.clean_accounts(true, false, Some(full_snapshot_slot));
bank4.clean_accounts(Some(full_snapshot_slot));
assert!(
bank4.get_account_modified_slot(&key1.pubkey()).is_none(),
"Ensure Account1 has been cleaned and purged from AccountsDb"
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/system_instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,7 @@ mod tests {
.unwrap();

// super fun time; callback chooses to .clean_accounts(None) or not
let bank = Arc::new(Bank::new_from_parent(&bank, &collector, bank.slot() + 1));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test below, test_create_zero_lamport_with_clean is interesting. It probably isn't the right place to test that clean_accounts() is functioning correctly, but I didn't want to remove it as part of this PR.

So, Bank::clean_accounts(), now cleans up to but not including bank.slot(). This test relies on a specific slot getting cleaned so that the slot's zero lamport accounts are cleaned up. That means we need to make one more bank so that Bank::clean_accounts() will work on the intended slot.

Down in the test you'll see that the numbers are bumped by one; this is due to the extra bank I've added here.

callback(&*bank);

// create a normal account at the same pubkey as the zero-lamports account
Expand All @@ -1651,9 +1652,9 @@ mod tests {
bank.squash();
bank.force_flush_accounts_cache();
// do clean and assert that it actually did its job
assert_eq!(4, bank.get_snapshot_storages(None).len());
bank.clean_accounts(None);
assert_eq!(3, bank.get_snapshot_storages(None).len());
bank.clean_accounts(false, false, None);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like the only one ever calling this with the 0th param = false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I already changed all the other tests in Bank to use a test-only clean_accounts_for_tests().

I didn't know about this test in system_instruction_processor until today. I commented on it above: #27254 (comment)

assert_eq!(2, bank.get_snapshot_storages(None).len());
});
}

Expand Down