From 0ad47571594316dfc801c6b75e0c094c0fb4a6d2 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Fri, 25 Feb 2022 12:58:08 -0600 Subject: [PATCH] plumbing for 'other_slot' in 'update_index' (#23330) --- runtime/benches/accounts_index.rs | 2 ++ runtime/src/accounts_db.rs | 7 +++++ runtime/src/accounts_index.rs | 47 +++++++++++++++++++++++++--- runtime/src/in_mem_accounts_index.rs | 10 ++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/runtime/benches/accounts_index.rs b/runtime/benches/accounts_index.rs index 6f81ac49ff..9a076d4dfc 100644 --- a/runtime/benches/accounts_index.rs +++ b/runtime/benches/accounts_index.rs @@ -26,6 +26,7 @@ fn bench_accounts_index(bencher: &mut Bencher) { for f in 0..NUM_FORKS { for pubkey in pubkeys.iter().take(NUM_PUBKEYS) { index.upsert( + f, f, pubkey, &AccountSharedData::default(), @@ -43,6 +44,7 @@ fn bench_accounts_index(bencher: &mut Bencher) { for _p in 0..NUM_PUBKEYS { let pubkey = thread_rng().gen_range(0, NUM_PUBKEYS); index.upsert( + fork, fork, &pubkeys[pubkey], &AccountSharedData::default(), diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index fcfc5394e0..735ebf410f 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -5968,6 +5968,7 @@ impl AccountsDb { let pubkey_account = (accounts.pubkey(i), accounts.account(i)); let pubkey = pubkey_account.0; self.accounts_index.upsert( + slot, slot, pubkey, pubkey_account.1, @@ -11059,6 +11060,7 @@ pub mod tests { let info3 = AccountInfo::new(StorageLocation::AppendVec(3, 0), 0, 0); let mut reclaims = vec![]; accounts_index.upsert( + 0, 0, &key0, &AccountSharedData::default(), @@ -11068,6 +11070,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); accounts_index.upsert( + 1, 1, &key0, &AccountSharedData::default(), @@ -11077,6 +11080,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); accounts_index.upsert( + 1, 1, &key1, &AccountSharedData::default(), @@ -11086,6 +11090,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); accounts_index.upsert( + 2, 2, &key1, &AccountSharedData::default(), @@ -11095,6 +11100,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); accounts_index.upsert( + 2, 2, &key2, &AccountSharedData::default(), @@ -11104,6 +11110,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); accounts_index.upsert( + 3, 3, &key2, &AccountSharedData::default(), diff --git a/runtime/src/accounts_index.rs b/runtime/src/accounts_index.rs index c98434082a..3a6f24a962 100644 --- a/runtime/src/accounts_index.rs +++ b/runtime/src/accounts_index.rs @@ -1747,7 +1747,8 @@ impl AccountsIndex { /// on return, the index's previous account info may be returned in 'reclaims' depending on 'previous_slot_entry_was_cached' pub fn upsert( &self, - slot: Slot, + new_slot: Slot, + _old_slot: Slot, pubkey: &Pubkey, account: &impl ReadableAccount, account_indexes: &AccountSecondaryIndexes, @@ -1769,13 +1770,23 @@ impl AccountsIndex { // - The secondary index is never consulted as primary source of truth for gets/stores. // So, what the accounts_index sees alone is sufficient as a source of truth for other non-scan // account operations. - let new_item = - PreAllocatedAccountMapEntry::new(slot, account_info, &self.storage.storage, store_raw); + let new_item = PreAllocatedAccountMapEntry::new( + new_slot, + account_info, + &self.storage.storage, + store_raw, + ); let map = &self.account_maps[self.bin_calculator.bin_from_pubkey(pubkey)]; { let r_account_maps = map.read().unwrap(); - r_account_maps.upsert(pubkey, new_item, reclaims, previous_slot_entry_was_cached); + r_account_maps.upsert( + pubkey, + new_item, + None, + reclaims, + previous_slot_entry_was_cached, + ); } self.update_secondary_indexes(pubkey, account, account_indexes); } @@ -2864,6 +2875,7 @@ pub mod tests { let index = AccountsIndex::::default_for_tests(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3075,6 +3087,7 @@ pub mod tests { if upsert { // insert first entry for pubkey. This will use new_entry_after_update and not call update. index.upsert( + slot0, slot0, &key, &AccountSharedData::default(), @@ -3111,6 +3124,7 @@ pub mod tests { // insert second entry for pubkey. This will use update and NOT use new_entry_after_update. if upsert { index.upsert( + slot1, slot1, &key, &AccountSharedData::default(), @@ -3184,6 +3198,7 @@ pub mod tests { w_account_maps.upsert( &key.pubkey(), new_entry, + None, &mut SlotList::default(), UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); @@ -3223,6 +3238,7 @@ pub mod tests { let index = AccountsIndex::::default_for_tests(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3254,6 +3270,7 @@ pub mod tests { let index = AccountsIndex::::default_for_tests(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3294,6 +3311,7 @@ pub mod tests { let mut pubkeys: Vec = std::iter::repeat_with(|| { let new_pubkey = solana_sdk::pubkey::new_rand(); index.upsert( + root_slot, root_slot, &new_pubkey, &AccountSharedData::default(), @@ -3310,6 +3328,7 @@ pub mod tests { if num_pubkeys != 0 { pubkeys.push(Pubkey::default()); index.upsert( + root_slot, root_slot, &Pubkey::default(), &AccountSharedData::default(), @@ -3452,6 +3471,7 @@ pub mod tests { assert!(iter.next().is_none()); let mut gc = vec![]; index.upsert( + 0, 0, &solana_sdk::pubkey::new_rand(), &AccountSharedData::default(), @@ -3477,6 +3497,7 @@ pub mod tests { let index = AccountsIndex::::default_for_tests(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3591,6 +3612,7 @@ pub mod tests { let ancestors = vec![(0, 0)].into_iter().collect(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3608,6 +3630,7 @@ pub mod tests { let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3631,6 +3654,7 @@ pub mod tests { let ancestors = vec![(0, 0)].into_iter().collect(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3641,6 +3665,7 @@ pub mod tests { ); assert!(gc.is_empty()); index.upsert( + 1, 1, &key.pubkey(), &AccountSharedData::default(), @@ -3667,6 +3692,7 @@ pub mod tests { let index = AccountsIndex::::default_for_tests(); let mut gc = Vec::new(); index.upsert( + 0, 0, &key.pubkey(), &AccountSharedData::default(), @@ -3677,6 +3703,7 @@ pub mod tests { ); assert!(gc.is_empty()); index.upsert( + 1, 1, &key.pubkey(), &AccountSharedData::default(), @@ -3686,6 +3713,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); index.upsert( + 2, 2, &key.pubkey(), &AccountSharedData::default(), @@ -3695,6 +3723,7 @@ pub mod tests { UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, ); index.upsert( + 3, 3, &key.pubkey(), &AccountSharedData::default(), @@ -3707,6 +3736,7 @@ pub mod tests { index.add_root(1, false); index.add_root(3, false); index.upsert( + 4, 4, &key.pubkey(), &AccountSharedData::default(), @@ -3751,6 +3781,7 @@ pub mod tests { let mut gc = Vec::new(); assert_eq!(0, account_maps_stats_len(&index)); index.upsert( + 1, 1, &key.pubkey(), &AccountSharedData::default(), @@ -3762,6 +3793,7 @@ pub mod tests { assert_eq!(1, account_maps_stats_len(&index)); index.upsert( + 1, 1, &key.pubkey(), &AccountSharedData::default(), @@ -3781,6 +3813,7 @@ pub mod tests { assert_eq!(1, account_maps_stats_len(&index)); index.upsert( + 1, 1, &key.pubkey(), &AccountSharedData::default(), @@ -3855,6 +3888,7 @@ pub mod tests { // Insert slots into secondary index for slot in &slots { index.upsert( + *slot, *slot, &account_key, // Make sure these accounts are added to secondary index @@ -4035,6 +4069,7 @@ pub mod tests { // Wrong program id index.upsert( + 0, 0, &account_key, &AccountSharedData::create(0, account_data.to_vec(), Pubkey::default(), false, 0), @@ -4048,6 +4083,7 @@ pub mod tests { // Wrong account data size index.upsert( + 0, 0, &account_key, &AccountSharedData::create(0, account_data[1..].to_vec(), *token_id, false, 0), @@ -4171,6 +4207,7 @@ pub mod tests { // First write one mint index index.upsert( + slot, slot, &account_key, &AccountSharedData::create(0, account_data1.to_vec(), *token_id, false, 0), @@ -4182,6 +4219,7 @@ pub mod tests { // Now write a different mint index for the same account index.upsert( + slot, slot, &account_key, &AccountSharedData::create(0, account_data2.to_vec(), *token_id, false, 0), @@ -4201,6 +4239,7 @@ pub mod tests { // If a later slot also introduces secondary_key1, then it should still exist in the index let later_slot = slot + 1; index.upsert( + later_slot, later_slot, &account_key, &AccountSharedData::create(0, account_data1.to_vec(), *token_id, false, 0), diff --git a/runtime/src/in_mem_accounts_index.rs b/runtime/src/in_mem_accounts_index.rs index 7be77c9309..1b70dfc8fd 100644 --- a/runtime/src/in_mem_accounts_index.rs +++ b/runtime/src/in_mem_accounts_index.rs @@ -343,6 +343,7 @@ impl InMemAccountsIndex { &self, pubkey: &Pubkey, new_value: PreAllocatedAccountMapEntry, + other_slot: Option, reclaims: &mut SlotList, previous_slot_entry_was_cached: bool, ) { @@ -352,6 +353,7 @@ impl InMemAccountsIndex { Self::lock_and_update_slot_list( entry, new_value.into(), + other_slot, reclaims, previous_slot_entry_was_cached, ); @@ -368,6 +370,7 @@ impl InMemAccountsIndex { Self::lock_and_update_slot_list( current, new_value.into(), + other_slot, reclaims, previous_slot_entry_was_cached, ); @@ -401,6 +404,7 @@ impl InMemAccountsIndex { Self::lock_and_update_slot_list( &disk_entry, new_value.into(), + other_slot, reclaims, previous_slot_entry_was_cached, ); @@ -440,6 +444,7 @@ impl InMemAccountsIndex { pub fn lock_and_update_slot_list( current: &AccountMapEntryInner, new_value: (Slot, T), + other_slot: Option, reclaims: &mut SlotList, previous_slot_entry_was_cached: bool, ) { @@ -449,6 +454,7 @@ impl InMemAccountsIndex { &mut slot_list, slot, new_entry, + other_slot, reclaims, previous_slot_entry_was_cached, ); @@ -466,6 +472,7 @@ impl InMemAccountsIndex { list: &mut SlotList, slot: Slot, account_info: T, + _other_slot: Option, reclaims: &mut SlotList, previous_slot_entry_was_cached: bool, ) -> bool { @@ -531,6 +538,7 @@ impl InMemAccountsIndex { InMemAccountsIndex::lock_and_update_slot_list( occupied.get(), (slot, account_info), + None, &mut Vec::default(), false, ); @@ -557,6 +565,7 @@ impl InMemAccountsIndex { InMemAccountsIndex::lock_and_update_slot_list( &disk_entry, (slot, account_info), + None, &mut Vec::default(), false, ); @@ -612,6 +621,7 @@ impl InMemAccountsIndex { &mut slot_list, slot, account_info, + None, reclaims, previous_slot_entry_was_cached, );