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

shink all slots in parallel on startup #17308

Merged
merged 1 commit into from
May 19, 2021
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
142 changes: 78 additions & 64 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,12 +2170,22 @@ impl AccountsDb {
num_candidates
}

pub fn shrink_all_slots(&self) {
for slot in self.all_slots_in_storage() {
if self.caching_enabled {
self.shrink_slot_forced(slot);
} else {
self.do_shrink_slot_forced_v1(slot);
pub fn shrink_all_slots(&self, is_startup: bool) {
if is_startup && self.caching_enabled {
let slots = self.all_slots_in_storage();
let chunk_size = std::cmp::max(slots.len() / 8, 1); // approximately 400k slots in a snapshot
slots.par_chunks(chunk_size).for_each(|slots| {
for slot in slots {
self.shrink_slot_forced(*slot);
}
});
} else {
for slot in self.all_slots_in_storage() {
if self.caching_enabled {
self.shrink_slot_forced(slot);
} else {
self.do_shrink_slot_forced_v1(slot);
}
}
}
}
Expand Down Expand Up @@ -8449,13 +8459,15 @@ pub mod tests {

#[test]
fn test_shrink_all_slots_none() {
let accounts = AccountsDb::new_single();
for startup in &[false, true] {
let accounts = AccountsDb::new_single();

for _ in 0..10 {
accounts.shrink_candidate_slots();
}
for _ in 0..10 {
accounts.shrink_candidate_slots();
}

accounts.shrink_all_slots();
accounts.shrink_all_slots(*startup);
}
}

#[test]
Expand Down Expand Up @@ -8536,68 +8548,70 @@ pub mod tests {
fn test_shrink_stale_slots_processed() {
solana_logger::setup();

let accounts = AccountsDb::new_single();
for startup in &[false, true] {
let accounts = AccountsDb::new_single();

let pubkey_count = 100;
let pubkeys: Vec<_> = (0..pubkey_count)
.map(|_| solana_sdk::pubkey::new_rand())
.collect();
let pubkey_count = 100;
let pubkeys: Vec<_> = (0..pubkey_count)
.map(|_| solana_sdk::pubkey::new_rand())
.collect();

let some_lamport = 223;
let no_data = 0;
let owner = *AccountSharedData::default().owner();
let some_lamport = 223;
let no_data = 0;
let owner = *AccountSharedData::default().owner();

let account = AccountSharedData::new(some_lamport, no_data, &owner);
let account = AccountSharedData::new(some_lamport, no_data, &owner);

let mut current_slot = 0;
let mut current_slot = 0;

current_slot += 1;
for pubkey in &pubkeys {
accounts.store_uncached(current_slot, &[(&pubkey, &account)]);
}
let shrink_slot = current_slot;
accounts.get_accounts_delta_hash(current_slot);
accounts.add_root(current_slot);
current_slot += 1;
for pubkey in &pubkeys {
accounts.store_uncached(current_slot, &[(&pubkey, &account)]);
}
let shrink_slot = current_slot;
accounts.get_accounts_delta_hash(current_slot);
accounts.add_root(current_slot);

current_slot += 1;
let pubkey_count_after_shrink = 10;
let updated_pubkeys = &pubkeys[0..pubkey_count - pubkey_count_after_shrink];
current_slot += 1;
let pubkey_count_after_shrink = 10;
let updated_pubkeys = &pubkeys[0..pubkey_count - pubkey_count_after_shrink];

for pubkey in updated_pubkeys {
accounts.store_uncached(current_slot, &[(&pubkey, &account)]);
}
accounts.get_accounts_delta_hash(current_slot);
accounts.add_root(current_slot);
for pubkey in updated_pubkeys {
accounts.store_uncached(current_slot, &[(&pubkey, &account)]);
}
accounts.get_accounts_delta_hash(current_slot);
accounts.add_root(current_slot);

accounts.clean_accounts(None);
accounts.clean_accounts(None);

assert_eq!(
pubkey_count,
accounts.all_account_count_in_append_vec(shrink_slot)
);
accounts.shrink_all_slots();
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_append_vec(shrink_slot)
);
assert_eq!(
pubkey_count,
accounts.all_account_count_in_append_vec(shrink_slot)
);
accounts.shrink_all_slots(*startup);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_append_vec(shrink_slot)
);

let no_ancestors = Ancestors::default();
accounts.update_accounts_hash(current_slot, &no_ancestors);
accounts
.verify_bank_hash_and_lamports(current_slot, &no_ancestors, 22300)
.unwrap();
let no_ancestors = Ancestors::default();
accounts.update_accounts_hash(current_slot, &no_ancestors);
accounts
.verify_bank_hash_and_lamports(current_slot, &no_ancestors, 22300)
.unwrap();

let accounts = reconstruct_accounts_db_via_serialization(&accounts, current_slot);
accounts
.verify_bank_hash_and_lamports(current_slot, &no_ancestors, 22300)
.unwrap();
let accounts = reconstruct_accounts_db_via_serialization(&accounts, current_slot);
accounts
.verify_bank_hash_and_lamports(current_slot, &no_ancestors, 22300)
.unwrap();

// repeating should be no-op
accounts.shrink_all_slots();
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_append_vec(shrink_slot)
);
// repeating should be no-op
accounts.shrink_all_slots(*startup);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_append_vec(shrink_slot)
);
}
}

#[test]
Expand Down Expand Up @@ -8652,7 +8666,7 @@ pub mod tests {
);

// Now, do full-shrink.
accounts.shrink_all_slots();
accounts.shrink_all_slots(false);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_append_vec(shrink_slot)
Expand Down Expand Up @@ -8712,7 +8726,7 @@ pub mod tests {
);

// Now, do full-shrink.
accounts.shrink_all_slots();
accounts.shrink_all_slots(false);
assert_eq!(
pubkey_count_after_shrink,
accounts.all_account_count_in_append_vec(shrink_slot)
Expand Down Expand Up @@ -8914,7 +8928,7 @@ pub mod tests {
}
accounts.add_root(1);
accounts.clean_accounts(None);
accounts.shrink_all_slots();
accounts.shrink_all_slots(false);
accounts.print_accounts_stats("post-shrink");
let num_stores = accounts.recycle_stores.read().unwrap().entry_count();
assert!(num_stores > 0);
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ impl Bank {
clean.stop();

let mut shrink = Measure::start("shrink");
self.shrink_all_slots();
self.shrink_all_slots(false);
shrink.stop();

info!(
Expand Down Expand Up @@ -4526,7 +4526,7 @@ impl Bank {

let mut shrink_all_slots_time = Measure::start("shrink_all_slots");
if self.slot() > 0 {
self.shrink_all_slots();
self.shrink_all_slots(true);
}
shrink_all_slots_time.stop();

Expand Down Expand Up @@ -4815,8 +4815,8 @@ impl Bank {
self.rc.accounts.accounts_db.clean_accounts(max_clean_slot);
}

pub fn shrink_all_slots(&self) {
self.rc.accounts.accounts_db.shrink_all_slots();
pub fn shrink_all_slots(&self, is_startup: bool) {
self.rc.accounts.accounts_db.shrink_all_slots(is_startup);
}

pub fn print_accounts_stats(&self) {
Expand Down