AcctIdx: PreAllocatedAccountMapEntry (#20117)

This commit is contained in:
Jeff Washington (jwash)
2021-09-23 08:11:32 -05:00
committed by GitHub
parent 61a3df6173
commit 4d3e32803f
2 changed files with 109 additions and 78 deletions

View File

@ -237,6 +237,51 @@ impl<T: IndexValue> ReadAccountMapEntry<T> {
} }
} }
pub struct PreAllocatedAccountMapEntry<T: IndexValue> {
entry: AccountMapEntry<T>,
}
impl<T: IndexValue> From<PreAllocatedAccountMapEntry<T>> for AccountMapEntry<T> {
fn from(source: PreAllocatedAccountMapEntry<T>) -> AccountMapEntry<T> {
source.entry
}
}
impl<T: IndexValue> From<PreAllocatedAccountMapEntry<T>> for (Slot, T) {
fn from(source: PreAllocatedAccountMapEntry<T>) -> (Slot, T) {
source.entry.slot_list.write().unwrap().remove(0)
}
}
impl<T: IndexValue> PreAllocatedAccountMapEntry<T> {
/// create an entry that is equivalent to this process:
/// 1. new empty (refcount=0, slot_list={})
/// 2. update(slot, account_info)
/// This code is called when the first entry [ie. (slot,account_info)] for a pubkey is inserted into the index.
pub fn new(
slot: Slot,
account_info: T,
storage: &Arc<BucketMapHolder<T>>,
) -> PreAllocatedAccountMapEntry<T> {
Self::new_internal(slot, account_info, AccountMapEntryMeta::new_dirty(storage))
}
fn new_internal(
slot: Slot,
account_info: T,
meta: AccountMapEntryMeta,
) -> PreAllocatedAccountMapEntry<T> {
let ref_count = if account_info.is_cached() { 0 } else { 1 };
PreAllocatedAccountMapEntry {
entry: Arc::new(AccountMapEntryInner::new(
vec![(slot, account_info)],
ref_count,
meta,
)),
}
}
}
#[self_referencing] #[self_referencing]
pub struct WriteAccountMapEntry<T: IndexValue> { pub struct WriteAccountMapEntry<T: IndexValue> {
owned_entry: AccountMapEntry<T>, owned_entry: AccountMapEntry<T>,
@ -266,23 +311,6 @@ impl<T: IndexValue> WriteAccountMapEntry<T> {
self.borrow_owned_entry().set_dirty(true); self.borrow_owned_entry().set_dirty(true);
result result
} }
// create an entry that is equivalent to this process:
// 1. new empty (refcount=0, slot_list={})
// 2. update(slot, account_info)
// This code is called when the first entry [ie. (slot,account_info)] for a pubkey is inserted into the index.
pub fn new_entry_after_update(
slot: Slot,
account_info: T,
storage: &Arc<BucketMapHolder<T>>,
) -> AccountMapEntry<T> {
let ref_count = if account_info.is_cached() { 0 } else { 1 };
Arc::new(AccountMapEntryInner::new(
vec![(slot, account_info)],
ref_count,
AccountMapEntryMeta::new_dirty(storage),
))
}
} }
#[derive(Debug, Default, AbiExample, Clone)] #[derive(Debug, Default, AbiExample, Clone)]
@ -1539,11 +1567,8 @@ impl<T: IndexValue> AccountsIndex<T> {
let is_zero_lamport = account_info.is_zero_lamport(); let is_zero_lamport = account_info.is_zero_lamport();
let result = if is_zero_lamport { Some(pubkey) } else { None }; let result = if is_zero_lamport { Some(pubkey) } else { None };
let info = WriteAccountMapEntry::new_entry_after_update( let info =
slot, PreAllocatedAccountMapEntry::new(slot, account_info, &self.storage.storage);
account_info,
&self.storage.storage,
);
binned[bin].1.push((pubkey, info)); binned[bin].1.push((pubkey, info));
result result
}) })
@ -1609,20 +1634,25 @@ impl<T: IndexValue> AccountsIndex<T> {
// - The secondary index is never consulted as primary source of truth for gets/stores. // - 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 // So, what the accounts_index sees alone is sufficient as a source of truth for other non-scan
// account operations. // account operations.
let new_item = let new_item = PreAllocatedAccountMapEntry::new(slot, account_info, &self.storage.storage);
WriteAccountMapEntry::new_entry_after_update(slot, account_info, &self.storage.storage);
let map = &self.account_maps[self.bin_calculator.bin_from_pubkey(pubkey)]; let map = &self.account_maps[self.bin_calculator.bin_from_pubkey(pubkey)];
let r_account_maps = map.read().unwrap(); let r_account_maps = map.read().unwrap();
if !r_account_maps.update_key_if_exists( let (updated, new_item) = r_account_maps.update_key_if_exists(
pubkey, pubkey,
&new_item, new_item,
reclaims, reclaims,
previous_slot_entry_was_cached, previous_slot_entry_was_cached,
) { );
if !updated {
drop(r_account_maps); drop(r_account_maps);
let w_account_maps = map.write().unwrap(); let w_account_maps = map.write().unwrap();
w_account_maps.upsert(pubkey, new_item, reclaims, previous_slot_entry_was_cached); w_account_maps.upsert(
pubkey,
new_item.unwrap(),
reclaims,
previous_slot_entry_was_cached,
);
} }
self.update_secondary_indexes(pubkey, account_owner, account_data, account_indexes); self.update_secondary_indexes(pubkey, account_owner, account_data, account_indexes);
} }
@ -1967,6 +1997,18 @@ pub mod tests {
) )
} }
impl<T: IndexValue> Clone for PreAllocatedAccountMapEntry<T> {
fn clone(&self) -> Self {
// clone the AccountMapEntryInner into a new Arc
let (slot, info) = self.entry.slot_list.read().unwrap()[0];
let meta = AccountMapEntryMeta {
dirty: AtomicBool::new(self.entry.dirty()),
age: AtomicU8::new(self.entry.age()),
};
PreAllocatedAccountMapEntry::new_internal(slot, info, meta)
}
}
#[test] #[test]
fn test_bitfield_delete_non_excess() { fn test_bitfield_delete_non_excess() {
solana_logger::setup(); solana_logger::setup();
@ -2790,11 +2832,8 @@ pub mod tests {
let account_info = AccountInfoTest::default(); let account_info = AccountInfoTest::default();
let index = AccountsIndex::default_for_tests(); let index = AccountsIndex::default_for_tests();
let new_entry = WriteAccountMapEntry::new_entry_after_update( let new_entry: AccountMapEntry<_> =
slot, PreAllocatedAccountMapEntry::new(slot, account_info, &index.storage.storage).into();
account_info,
&index.storage.storage,
);
assert_eq!(new_entry.ref_count.load(Ordering::Relaxed), 0); assert_eq!(new_entry.ref_count.load(Ordering::Relaxed), 0);
assert_eq!(new_entry.slot_list.read().unwrap().capacity(), 1); assert_eq!(new_entry.slot_list.read().unwrap().capacity(), 1);
assert_eq!( assert_eq!(
@ -2806,11 +2845,8 @@ pub mod tests {
let account_info = true; let account_info = true;
let index = AccountsIndex::default_for_tests(); let index = AccountsIndex::default_for_tests();
let new_entry = WriteAccountMapEntry::new_entry_after_update( let new_entry: AccountMapEntry<_> =
slot, PreAllocatedAccountMapEntry::new(slot, account_info, &index.storage.storage).into();
account_info,
&index.storage.storage,
);
assert_eq!(new_entry.ref_count.load(Ordering::Relaxed), 1); assert_eq!(new_entry.ref_count.load(Ordering::Relaxed), 1);
assert_eq!(new_entry.slot_list.read().unwrap().capacity(), 1); assert_eq!(new_entry.slot_list.read().unwrap().capacity(), 1);
assert_eq!( assert_eq!(
@ -2874,11 +2910,9 @@ pub mod tests {
assert_eq!(entry.ref_count(), if is_cached { 0 } else { 1 }); assert_eq!(entry.ref_count(), if is_cached { 0 } else { 1 });
let expected = vec![(slot0, account_infos[0])]; let expected = vec![(slot0, account_infos[0])];
assert_eq!(entry.slot_list().to_vec(), expected); assert_eq!(entry.slot_list().to_vec(), expected);
let new_entry = WriteAccountMapEntry::new_entry_after_update( let new_entry: AccountMapEntry<_> =
slot0, PreAllocatedAccountMapEntry::new(slot0, account_infos[0], &index.storage.storage)
account_infos[0], .into();
&index.storage.storage,
);
assert_eq!( assert_eq!(
entry.slot_list().to_vec(), entry.slot_list().to_vec(),
new_entry.slot_list.read().unwrap().to_vec(), new_entry.slot_list.read().unwrap().to_vec(),
@ -2924,12 +2958,9 @@ pub mod tests {
vec![(slot0, account_infos[0]), (slot1, account_infos[1])] vec![(slot0, account_infos[0]), (slot1, account_infos[1])]
); );
let new_entry = WriteAccountMapEntry::new_entry_after_update( let new_entry =
slot1, PreAllocatedAccountMapEntry::new(slot1, account_infos[1], &index.storage.storage);
account_infos[1], assert_eq!(entry.slot_list()[1], new_entry.into());
&index.storage.storage,
);
assert_eq!(entry.slot_list()[1], new_entry.slot_list.read().unwrap()[0],);
} }
} }
@ -2951,26 +2982,24 @@ pub mod tests {
let slot = 0; let slot = 0;
let account_info = true; let account_info = true;
let new_entry = WriteAccountMapEntry::new_entry_after_update( let new_entry =
slot, PreAllocatedAccountMapEntry::new(slot, account_info, &index.storage.storage);
account_info,
&index.storage.storage,
);
assert_eq!(0, account_maps_len_expensive(&index)); assert_eq!(0, account_maps_len_expensive(&index));
// will fail because key doesn't exist // will fail because key doesn't exist
let r_account_maps = index.get_account_maps_read_lock(&key.pubkey()); let r_account_maps = index.get_account_maps_read_lock(&key.pubkey());
assert!(!r_account_maps.update_key_if_exists( assert!(
&key.pubkey(), !r_account_maps
&new_entry, .update_key_if_exists(
&mut SlotList::default(), &key.pubkey(),
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE, new_entry.clone(),
)); &mut SlotList::default(),
drop(r_account_maps); UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
assert_eq!( )
(slot, account_info), .0
new_entry.slot_list.read().as_ref().unwrap()[0]
); );
drop(r_account_maps);
assert_eq!((slot, account_info), new_entry.clone().into());
assert_eq!(0, account_maps_len_expensive(&index)); assert_eq!(0, account_maps_len_expensive(&index));
let w_account_maps = index.get_account_maps_write_lock(&key.pubkey()); let w_account_maps = index.get_account_maps_write_lock(&key.pubkey());

View File

@ -1,6 +1,6 @@
use crate::accounts_index::{ use crate::accounts_index::{
AccountMapEntry, AccountMapEntryInner, AccountMapEntryMeta, IndexValue, RefCount, SlotList, AccountMapEntry, AccountMapEntryInner, AccountMapEntryMeta, IndexValue,
SlotSlice, PreAllocatedAccountMapEntry, RefCount, SlotList, SlotSlice,
}; };
use crate::bucket_map_holder::{Age, BucketMapHolder}; use crate::bucket_map_holder::{Age, BucketMapHolder};
use crate::bucket_map_holder_stats::BucketMapHolderStats; use crate::bucket_map_holder_stats::BucketMapHolderStats;
@ -219,7 +219,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
pub fn upsert( pub fn upsert(
&self, &self,
pubkey: &Pubkey, pubkey: &Pubkey,
new_value: AccountMapEntry<T>, new_value: PreAllocatedAccountMapEntry<T>,
reclaims: &mut SlotList<T>, reclaims: &mut SlotList<T>,
previous_slot_entry_was_cached: bool, previous_slot_entry_was_cached: bool,
) { ) {
@ -239,7 +239,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
let current = occupied.get_mut(); let current = occupied.get_mut();
Self::lock_and_update_slot_list( Self::lock_and_update_slot_list(
current, current,
new_value.slot_list.write().unwrap().remove(0), new_value.into(),
reclaims, reclaims,
previous_slot_entry_was_cached, previous_slot_entry_was_cached,
); );
@ -252,14 +252,14 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
// on disk, so merge new_value with what was on disk // on disk, so merge new_value with what was on disk
Self::lock_and_update_slot_list( Self::lock_and_update_slot_list(
&disk_entry, &disk_entry,
new_value.slot_list.write().unwrap().remove(0), new_value.into(),
reclaims, reclaims,
previous_slot_entry_was_cached, previous_slot_entry_was_cached,
); );
disk_entry disk_entry
} else { } else {
// not on disk, so insert new thing // not on disk, so insert new thing
new_value new_value.into()
}; };
assert!(new_value.dirty()); assert!(new_value.dirty());
vacant.insert(new_value); vacant.insert(new_value);
@ -347,20 +347,20 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
pub fn update_key_if_exists( pub fn update_key_if_exists(
&self, &self,
pubkey: &Pubkey, pubkey: &Pubkey,
new_value: &AccountMapEntry<T>, new_value: PreAllocatedAccountMapEntry<T>,
reclaims: &mut SlotList<T>, reclaims: &mut SlotList<T>,
previous_slot_entry_was_cached: bool, previous_slot_entry_was_cached: bool,
) -> bool { ) -> (bool, Option<PreAllocatedAccountMapEntry<T>>) {
if let Some(current) = self.map().read().unwrap().get(pubkey) { if let Some(current) = self.map().read().unwrap().get(pubkey) {
Self::lock_and_update_slot_list( Self::lock_and_update_slot_list(
current, current,
new_value.slot_list.write().unwrap().remove(0), new_value.into(),
reclaims, reclaims,
previous_slot_entry_was_cached, previous_slot_entry_was_cached,
); );
true (true, None)
} else { } else {
false (false, Some(new_value))
} }
} }
@ -375,12 +375,13 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
fn insert_returner( fn insert_returner(
existing: &AccountMapEntry<T>, existing: &AccountMapEntry<T>,
pubkey: &Pubkey, pubkey: &Pubkey,
new_entry: AccountMapEntry<T>, new_entry: PreAllocatedAccountMapEntry<T>,
) -> (AccountMapEntry<T>, T, Pubkey) { ) -> (AccountMapEntry<T>, T, Pubkey) {
let (_slot, info): (Slot, T) = new_entry.into();
( (
Arc::clone(existing), Arc::clone(existing),
// extract the new account_info from the unused 'new_entry' // extract the new account_info from the unused 'new_entry'
new_entry.slot_list.write().unwrap().remove(0).1, info,
*pubkey, *pubkey,
) )
} }
@ -390,7 +391,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
pub fn insert_new_entry_if_missing_with_lock( pub fn insert_new_entry_if_missing_with_lock(
&self, &self,
pubkey: Pubkey, pubkey: Pubkey,
new_entry: AccountMapEntry<T>, new_entry: PreAllocatedAccountMapEntry<T>,
) -> Option<(AccountMapEntry<T>, T, Pubkey)> { ) -> Option<(AccountMapEntry<T>, T, Pubkey)> {
let m = Measure::start("entry"); let m = Measure::start("entry");
let mut map = self.map().write().unwrap(); let mut map = self.map().write().unwrap();
@ -420,6 +421,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
result result
} else { } else {
// not on disk, so insert new thing and we're done // not on disk, so insert new thing and we're done
let new_entry: AccountMapEntry<T> = new_entry.into();
assert!(new_entry.dirty()); assert!(new_entry.dirty());
vacant.insert(new_entry); vacant.insert(new_entry);
None // returns None if item was created new None // returns None if item was created new