add test variations for shrink_collect (#28640)

add test variations
This commit is contained in:
Jeff Washington (jwash) 2022-10-29 14:24:08 -07:00 committed by GitHub
parent 5889ee30b0
commit fad6c99e9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 156 additions and 81 deletions

View File

@ -17345,8 +17345,10 @@ pub mod tests {
SHRINK_COLLECT_CHUNK_SIZE + 1, SHRINK_COLLECT_CHUNK_SIZE + 1,
SHRINK_COLLECT_CHUNK_SIZE * 2, SHRINK_COLLECT_CHUNK_SIZE * 2,
]; ];
// 2 = append_opposite_alive_account + append_opposite_zero_lamport_account
let max_appended_accounts = 2;
let max_num_accounts = *account_counts.iter().max().unwrap(); let max_num_accounts = *account_counts.iter().max().unwrap();
let pubkeys = (0..max_num_accounts) let pubkeys = (0..(max_num_accounts + max_appended_accounts))
.map(|_| solana_sdk::pubkey::new_rand()) .map(|_| solana_sdk::pubkey::new_rand())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// write accounts, maybe remove from index // write accounts, maybe remove from index
@ -17357,89 +17359,162 @@ pub mod tests {
// illegal - zero lamport accounts are written with 0 space // illegal - zero lamport accounts are written with 0 space
continue; continue;
} }
for account_count in account_counts { for alive in [false, true] {
for alive in [false, true] { for append_opposite_alive_account in [false, true] {
debug!("space: {space}, lamports: {lamports}, alive: {alive}, account_count: {account_count}"); for append_opposite_zero_lamport_account in [true, false] {
let size = 100000; for mut account_count in account_counts {
let db = AccountsDb::new_single_for_tests(); let mut normal_account_count = account_count;
let slot5 = 5; let mut pubkey_opposite_zero_lamports = None;
let inserted_store = db.create_and_insert_store(slot5, size, "test"); if append_opposite_zero_lamport_account {
let account = AccountSharedData::new( pubkey_opposite_zero_lamports = Some(&pubkeys[account_count]);
lamports, normal_account_count += 1;
space, account_count += 1;
AccountSharedData::default().owner(), }
); let mut pubkey_opposite_alive = None;
for pubkey in pubkeys.iter().take(account_count) { if append_opposite_alive_account {
// store in append vec and index // this needs to happen AFTER append_opposite_zero_lamport_account
db.store_uncached(slot5, &[(pubkey, &account)]); pubkey_opposite_alive = Some(&pubkeys[account_count]);
if !alive { account_count += 1;
// remove from index so pubkey is 'dead' }
db.accounts_index.purge_exact( debug!("space: {space}, lamports: {lamports}, alive: {alive}, account_count: {account_count}, append_opposite_alive_account: {append_opposite_alive_account}, append_opposite_zero_lamport_account: {append_opposite_zero_lamport_account}, normal_account_count: {normal_account_count}");
pubkey, let size = 100000;
&([slot5].into_iter().collect::<HashSet<_>>()), let db = AccountsDb::new_single_for_tests();
&mut Vec::default(), let slot5 = 5;
let inserted_store =
db.create_and_insert_store(slot5, size, "test");
let mut account = AccountSharedData::new(
lamports,
space,
AccountSharedData::default().owner(),
);
for pubkey in pubkeys.iter().take(account_count) {
// store in append vec and index
let old_lamports = account.lamports();
if Some(pubkey) == pubkey_opposite_zero_lamports {
account.set_lamports(u64::from(old_lamports == 0));
}
db.store_uncached(slot5, &[(pubkey, &account)]);
account.set_lamports(old_lamports);
let mut alive = alive;
if append_opposite_alive_account
&& Some(pubkey) == pubkey_opposite_alive
{
// invert this for one special pubkey
alive = !alive;
}
if !alive {
// remove from index so pubkey is 'dead'
db.accounts_index.purge_exact(
pubkey,
&([slot5].into_iter().collect::<HashSet<_>>()),
&mut Vec::default(),
);
}
}
let storages = vec![inserted_store];
let mut stored_accounts = Vec::default();
assert_eq!(storages.len(), 1);
let shrink_collect = db.shrink_collect(
storages.iter(),
&mut stored_accounts,
&ShrinkStats::default(),
);
let expect_single_opposite_alive_account =
if append_opposite_alive_account {
vec![*pubkey_opposite_alive.unwrap()]
} else {
vec![]
};
let expected_alive_accounts = if alive {
pubkeys[..normal_account_count]
.iter()
.filter(|p| Some(p) != pubkey_opposite_alive.as_ref())
.sorted()
.cloned()
.collect::<Vec<_>>()
} else {
expect_single_opposite_alive_account.clone()
};
let expected_unrefed = if alive {
expect_single_opposite_alive_account.clone()
} else {
pubkeys[..normal_account_count]
.iter()
.sorted()
.cloned()
.collect::<Vec<_>>()
};
assert_eq!(
shrink_collect
.alive_accounts
.iter()
.map(|(pubkey, _)| *pubkey)
.sorted()
.collect::<Vec<_>>(),
expected_alive_accounts
);
assert_eq!(
shrink_collect
.unrefed_pubkeys
.iter()
.sorted()
.cloned()
.cloned()
.collect::<Vec<_>>(),
expected_unrefed
);
let alive_total_one_account = 136 + space;
if alive {
assert_eq!(
shrink_collect.aligned_total,
PAGE_SIZE
* if account_count >= 100 {
4
} else if account_count >= 50 {
2
} else {
1
}
);
let mut expected_alive_total =
alive_total_one_account * normal_account_count;
if append_opposite_zero_lamport_account {
// zero lamport accounts store size=0 data
expected_alive_total -= space;
}
assert_eq!(shrink_collect.alive_total, expected_alive_total);
} else if append_opposite_alive_account {
assert_eq!(shrink_collect.aligned_total, 4096);
assert_eq!(shrink_collect.alive_total, alive_total_one_account);
} else {
assert_eq!(shrink_collect.aligned_total, 0);
assert_eq!(shrink_collect.alive_total, 0);
}
assert_eq!(shrink_collect.original_bytes, 102400);
assert_eq!(
shrink_collect.store_ids,
vec![storages[0].append_vec_id()]
);
assert_eq!(shrink_collect.total_starting_accounts, account_count);
let mut expected_all_are_zero_lamports = lamports == 0;
if !append_opposite_alive_account {
expected_all_are_zero_lamports |= !alive;
}
if append_opposite_zero_lamport_account && lamports == 0 && alive {
expected_all_are_zero_lamports =
!expected_all_are_zero_lamports;
}
assert_eq!(
shrink_collect.all_are_zero_lamports,
expected_all_are_zero_lamports
); );
} }
} }
let storages = vec![inserted_store];
let mut stored_accounts = Vec::default();
assert_eq!(storages.len(), 1);
let shrink_collect = db.shrink_collect(
storages.iter(),
&mut stored_accounts,
&ShrinkStats::default(),
);
if alive {
assert!(shrink_collect.unrefed_pubkeys.is_empty());
assert_eq!(shrink_collect.alive_accounts.len(), account_count);
assert_eq!(
shrink_collect
.alive_accounts
.iter()
.map(|(pubkey, _)| *pubkey)
.sorted()
.collect::<Vec<_>>(),
pubkeys[..account_count]
.iter()
.sorted()
.cloned()
.collect::<Vec<_>>()
);
assert_eq!(
shrink_collect.aligned_total,
PAGE_SIZE
* if account_count >= 100 {
4
} else if account_count >= 50 {
2
} else {
1
}
);
// 136 is size of serialized account, with zero length data, and with padding to next aligned entry
assert_eq!(shrink_collect.alive_total, (136 + space) * account_count);
} else {
assert!(shrink_collect.alive_accounts.is_empty());
assert_eq!(shrink_collect.unrefed_pubkeys.len(), account_count);
assert_eq!(
shrink_collect
.unrefed_pubkeys
.iter()
.sorted()
.cloned()
.collect::<Vec<_>>(),
pubkeys[..account_count].iter().sorted().collect::<Vec<_>>()
);
assert_eq!(shrink_collect.aligned_total, 0);
assert_eq!(shrink_collect.alive_total, 0);
}
assert_eq!(shrink_collect.original_bytes, 102400);
assert_eq!(shrink_collect.store_ids, vec![storages[0].append_vec_id()]);
assert_eq!(shrink_collect.total_starting_accounts, account_count);
assert_eq!(
shrink_collect.all_are_zero_lamports,
lamports == 0 || !alive
);
} }
} }
} }