AcctIdx: Put InMemAcctIndex behind arc (#19904)

This commit is contained in:
Jeff Washington (jwash) 2021-09-15 12:36:08 -05:00 committed by GitHub
parent b467e7fb3b
commit 99f2c746d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 10 deletions

View File

@ -44,7 +44,7 @@ pub type ScanResult<T> = Result<T, ScanError>;
pub type SlotList<T> = Vec<(Slot, T)>; pub type SlotList<T> = Vec<(Slot, T)>;
pub type SlotSlice<'s, T> = &'s [(Slot, T)]; pub type SlotSlice<'s, T> = &'s [(Slot, T)];
pub type RefCount = u64; pub type RefCount = u64;
pub type AccountMap<V> = InMemAccountsIndex<V>; pub type AccountMap<V> = Arc<InMemAccountsIndex<V>>;
pub(crate) type AccountMapEntry<T> = Arc<AccountMapEntryInner<T>>; pub(crate) type AccountMapEntry<T> = Arc<AccountMapEntryInner<T>>;
@ -745,7 +745,7 @@ impl<T: IndexValue> AccountsIndex<T> {
let storage = AccountsIndexStorage::new(); let storage = AccountsIndexStorage::new();
let account_maps = (0..bins) let account_maps = (0..bins)
.into_iter() .into_iter()
.map(|bin| RwLock::new(AccountMap::new(&storage, bin))) .map(|bin| RwLock::new(Arc::new(InMemAccountsIndex::new(&storage, bin))))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
(account_maps, bin_calculator, storage) (account_maps, bin_calculator, storage)
} }
@ -1119,7 +1119,7 @@ impl<T: IndexValue> AccountsIndex<T> {
) { ) {
if !dead_keys.is_empty() { if !dead_keys.is_empty() {
for key in dead_keys.iter() { for key in dead_keys.iter() {
let mut w_index = self.get_account_maps_write_lock(key); let w_index = self.get_account_maps_write_lock(key);
if w_index.remove_if_slot_list_empty(**key) { if w_index.remove_if_slot_list_empty(**key) {
// Note it's only safe to remove all the entries for this key // Note it's only safe to remove all the entries for this key
// because we have the lock for this key's entry in the AccountsIndex, // because we have the lock for this key's entry in the AccountsIndex,
@ -1476,7 +1476,7 @@ impl<T: IndexValue> AccountsIndex<T> {
// big enough so not likely to re-allocate, small enough to not over-allocate by too much // big enough so not likely to re-allocate, small enough to not over-allocate by too much
// this assumes 10% of keys are duplicates. This vector will be flattened below. // this assumes 10% of keys are duplicates. This vector will be flattened below.
let mut w_account_maps = self.account_maps[pubkey_bin].write().unwrap(); let w_account_maps = self.account_maps[pubkey_bin].write().unwrap();
let mut insert_time = Measure::start("insert_into_primary_index"); let mut insert_time = Measure::start("insert_into_primary_index");
items.into_iter().for_each(|(pubkey, new_item)| { items.into_iter().for_each(|(pubkey, new_item)| {
let already_exists = let already_exists =
@ -1533,7 +1533,7 @@ impl<T: IndexValue> AccountsIndex<T> {
previous_slot_entry_was_cached, previous_slot_entry_was_cached,
) { ) {
drop(r_account_maps); drop(r_account_maps);
let mut 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, 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);
@ -1612,7 +1612,7 @@ impl<T: IndexValue> AccountsIndex<T> {
// locked and inserted the pubkey inbetween when `is_slot_list_empty=true` and the call to // locked and inserted the pubkey inbetween when `is_slot_list_empty=true` and the call to
// remove() below. // remove() below.
if is_slot_list_empty { if is_slot_list_empty {
let mut w_maps = self.get_account_maps_write_lock(pubkey); let w_maps = self.get_account_maps_write_lock(pubkey);
w_maps.remove_if_slot_list_empty(*pubkey); w_maps.remove_if_slot_list_empty(*pubkey);
} }
} }
@ -2863,7 +2863,7 @@ pub mod tests {
); );
assert_eq!(0, account_maps_len_expensive(&index)); assert_eq!(0, account_maps_len_expensive(&index));
let mut w_account_maps = index.get_account_maps_write_lock(&key.pubkey()); let w_account_maps = index.get_account_maps_write_lock(&key.pubkey());
w_account_maps.upsert( w_account_maps.upsert(
&key.pubkey(), &key.pubkey(),
new_entry, new_entry,

View File

@ -81,7 +81,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
// If the slot list for pubkey exists in the index and is empty, remove the index entry for pubkey and return true. // If the slot list for pubkey exists in the index and is empty, remove the index entry for pubkey and return true.
// Return false otherwise. // Return false otherwise.
pub fn remove_if_slot_list_empty(&mut self, pubkey: Pubkey) -> bool { pub fn remove_if_slot_list_empty(&self, pubkey: Pubkey) -> bool {
let m = Measure::start("entry"); let m = Measure::start("entry");
let mut map = self.map().write().unwrap(); let mut map = self.map().write().unwrap();
let entry = map.entry(pubkey); let entry = map.entry(pubkey);
@ -105,7 +105,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
} }
pub fn upsert( pub fn upsert(
&mut self, &self,
pubkey: &Pubkey, pubkey: &Pubkey,
new_value: AccountMapEntry<T>, new_value: AccountMapEntry<T>,
reclaims: &mut SlotList<T>, reclaims: &mut SlotList<T>,
@ -230,7 +230,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
// return None if item was created new // return None if item was created new
// if entry for pubkey already existed, return Some(entry). Caller needs to call entry.update. // if entry for pubkey already existed, return Some(entry). Caller needs to call entry.update.
pub fn insert_new_entry_if_missing_with_lock( pub fn insert_new_entry_if_missing_with_lock(
&mut self, &self,
pubkey: Pubkey, pubkey: Pubkey,
new_entry: AccountMapEntry<T>, new_entry: AccountMapEntry<T>,
) -> Option<(WriteAccountMapEntry<T>, T, Pubkey)> { ) -> Option<(WriteAccountMapEntry<T>, T, Pubkey)> {