Skip to content
Merged
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
25 changes: 15 additions & 10 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,11 +1176,16 @@ impl AccountStorageEntry {
self.accounts.accounts(0)
}

fn remove_account(&self, num_bytes: usize, reset_accounts: bool) -> usize {
fn remove_accounts(
&self,
num_bytes: usize,
reset_accounts: bool,
num_accounts: usize,
) -> usize {
let mut count_and_status = self.count_and_status.lock_write();
let (mut count, mut status) = *count_and_status;

if count == 1 && status == AccountStorageStatus::Full && reset_accounts {
if count == num_accounts && status == AccountStorageStatus::Full && reset_accounts {
// this case arises when we remove the last account from the
// storage, but we've learned from previous write attempts that
// the storage is full
Expand All @@ -1199,14 +1204,14 @@ impl AccountStorageEntry {
// Some code path is removing accounts too many; this may result in an
// unintended reveal of old state for unrelated accounts.
assert!(
count > 0,
count >= num_accounts,
"double remove of account in slot: {}/store: {}!!",
self.slot(),
self.append_vec_id(),
);

self.alive_bytes.fetch_sub(num_bytes, Ordering::SeqCst);
count -= 1;
count = count.saturating_sub(num_accounts);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

With the assert above, doesn't that make the sub-assign here safe?

I like safe math when the semantics match the usage. Here, I think if we ever subtracted count enough to go negative, that would be a programmer bug. And IMO a .checked_sub().expect() would document that invariant. A saturating-sub that kept us at zero might hide some bug elsewhere.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I figured the assert may go away at some point in the future. Yes, this is redundant. It is not a perf issue. I know we spent much time converting subtracts to saturating or safe math.

*count_and_status = (count, status);
count
}
Expand Down Expand Up @@ -7897,7 +7902,7 @@ impl AccountsDb {
offsets.iter().for_each(|offset| {
let account = store.accounts.get_account(*offset).unwrap();
let stored_size = account.0.stored_size();
let count = store.remove_account(stored_size, reset_accounts);
let count = store.remove_accounts(stored_size, reset_accounts, 1);
if count == 0 {
self.dirty_stores.insert(*slot, store.clone());
dead_slots.insert(*slot);
Expand Down Expand Up @@ -12610,7 +12615,7 @@ pub mod tests {
db.storage
.get_slot_storage_entry(0)
.unwrap()
.remove_account(0, true);
.remove_accounts(0, true, 1);
assert!(db.get_snapshot_storages(..=after_slot).0.is_empty());
}

Expand All @@ -12637,8 +12642,8 @@ pub mod tests {
accounts.store_for_tests(0, &[(&pubkey, &account)]);
accounts.add_root_and_flush_write_cache(0);
let storage_entry = accounts.storage.get_slot_storage_entry(0).unwrap();
storage_entry.remove_account(0, true);
storage_entry.remove_account(0, true);
storage_entry.remove_accounts(0, true, 1);
storage_entry.remove_accounts(0, true, 1);
}

fn do_full_clean_refcount(store1_first: bool, store_size: u64) {
Expand Down Expand Up @@ -15068,7 +15073,7 @@ pub mod tests {
let store = Arc::new(s1);
store.add_account((3 * PAGE_SIZE as usize) - 1);
store.add_account(10);
store.remove_account(10, false);
store.remove_accounts(10, false, 1);
assert!(AccountsDb::is_shrinking_productive(0, &store));

store.add_account(PAGE_SIZE as usize);
Expand Down Expand Up @@ -17204,7 +17209,7 @@ pub mod tests {
num_bytes: usize,
reset_accounts: bool,
) {
storage.remove_account(num_bytes, reset_accounts);
storage.remove_accounts(num_bytes, reset_accounts, 1);
}

pub(crate) fn create_storages_and_update_index_with_customized_account_size_per_slot(
Expand Down