Skip to content

Commit

Permalink
plumbing for 'other_slot' in 'update_index' (solana-labs#23330)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Mar 4, 2022
1 parent 2ae0790 commit a7e4dd2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions runtime/benches/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
7 changes: 7 additions & 0 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5976,6 +5976,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,
Expand Down Expand Up @@ -11067,6 +11068,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(),
Expand All @@ -11076,6 +11078,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
accounts_index.upsert(
1,
1,
&key0,
&AccountSharedData::default(),
Expand All @@ -11085,6 +11088,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
accounts_index.upsert(
1,
1,
&key1,
&AccountSharedData::default(),
Expand All @@ -11094,6 +11098,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
accounts_index.upsert(
2,
2,
&key1,
&AccountSharedData::default(),
Expand All @@ -11103,6 +11108,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
accounts_index.upsert(
2,
2,
&key2,
&AccountSharedData::default(),
Expand All @@ -11112,6 +11118,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
accounts_index.upsert(
3,
3,
&key2,
&AccountSharedData::default(),
Expand Down
47 changes: 43 additions & 4 deletions runtime/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,8 @@ impl<T: IndexValue> AccountsIndex<T> {
/// 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,
Expand All @@ -1773,13 +1774,23 @@ impl<T: IndexValue> AccountsIndex<T> {
// - 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);
}
Expand Down Expand Up @@ -2868,6 +2879,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default_for_tests();
let mut gc = Vec::new();
index.upsert(
0,
0,
&key.pubkey(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3079,6 +3091,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(),
Expand Down Expand Up @@ -3115,6 +3128,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(),
Expand Down Expand Up @@ -3188,6 +3202,7 @@ pub mod tests {
w_account_maps.upsert(
&key.pubkey(),
new_entry,
None,
&mut SlotList::default(),
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
Expand Down Expand Up @@ -3227,6 +3242,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default_for_tests();
let mut gc = Vec::new();
index.upsert(
0,
0,
&key.pubkey(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3258,6 +3274,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default_for_tests();
let mut gc = Vec::new();
index.upsert(
0,
0,
&key.pubkey(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3298,6 +3315,7 @@ pub mod tests {
let mut pubkeys: Vec<Pubkey> = std::iter::repeat_with(|| {
let new_pubkey = solana_sdk::pubkey::new_rand();
index.upsert(
root_slot,
root_slot,
&new_pubkey,
&AccountSharedData::default(),
Expand All @@ -3314,6 +3332,7 @@ pub mod tests {
if num_pubkeys != 0 {
pubkeys.push(Pubkey::default());
index.upsert(
root_slot,
root_slot,
&Pubkey::default(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3456,6 +3475,7 @@ pub mod tests {
assert!(iter.next().is_none());
let mut gc = vec![];
index.upsert(
0,
0,
&solana_sdk::pubkey::new_rand(),
&AccountSharedData::default(),
Expand All @@ -3481,6 +3501,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default_for_tests();
let mut gc = Vec::new();
index.upsert(
0,
0,
&key.pubkey(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3595,6 +3616,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(),
Expand All @@ -3612,6 +3634,7 @@ pub mod tests {

let mut gc = Vec::new();
index.upsert(
0,
0,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3635,6 +3658,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(),
Expand All @@ -3645,6 +3669,7 @@ pub mod tests {
);
assert!(gc.is_empty());
index.upsert(
1,
1,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3671,6 +3696,7 @@ pub mod tests {
let index = AccountsIndex::<bool>::default_for_tests();
let mut gc = Vec::new();
index.upsert(
0,
0,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3681,6 +3707,7 @@ pub mod tests {
);
assert!(gc.is_empty());
index.upsert(
1,
1,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3690,6 +3717,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
index.upsert(
2,
2,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3699,6 +3727,7 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
index.upsert(
3,
3,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3711,6 +3740,7 @@ pub mod tests {
index.add_root(1, false);
index.add_root(3, false);
index.upsert(
4,
4,
&key.pubkey(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3755,6 +3785,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(),
Expand All @@ -3766,6 +3797,7 @@ pub mod tests {
assert_eq!(1, account_maps_stats_len(&index));

index.upsert(
1,
1,
&key.pubkey(),
&AccountSharedData::default(),
Expand All @@ -3785,6 +3817,7 @@ pub mod tests {

assert_eq!(1, account_maps_stats_len(&index));
index.upsert(
1,
1,
&key.pubkey(),
&AccountSharedData::default(),
Expand Down Expand Up @@ -3859,6 +3892,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
Expand Down Expand Up @@ -4039,6 +4073,7 @@ pub mod tests {

// Wrong program id
index.upsert(
0,
0,
&account_key,
&AccountSharedData::create(0, account_data.to_vec(), Pubkey::default(), false, 0),
Expand All @@ -4052,6 +4087,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),
Expand Down Expand Up @@ -4175,6 +4211,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),
Expand All @@ -4186,6 +4223,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),
Expand All @@ -4205,6 +4243,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),
Expand Down
Loading

0 comments on commit a7e4dd2

Please sign in to comment.