LoadedAccountAccessor::Cached does not store Pubkey (#21030)

This commit is contained in:
Jeff Washington (jwash)
2021-11-04 10:28:04 -05:00
committed by GitHub
parent aaccba8377
commit 76e533be46
3 changed files with 22 additions and 29 deletions

View File

@ -73,7 +73,7 @@ impl ReplicaAccountsServer for ReplicaAccountsServerImpl {
LoadedAccount::Stored(stored_account_meta) => Some( LoadedAccount::Stored(stored_account_meta) => Some(
ReplicaAccountInfo::from_stored_account_meta(&stored_account_meta), ReplicaAccountInfo::from_stored_account_meta(&stored_account_meta),
), ),
LoadedAccount::Cached((_pubkey, cached_account)) => { LoadedAccount::Cached(cached_account) => {
Some(ReplicaAccountInfo::from_cached_account(&cached_account)) Some(ReplicaAccountInfo::from_cached_account(&cached_account))
} }
}); });

View File

@ -134,8 +134,8 @@ impl CachedAccountInner {
} }
} }
} }
pub fn pubkey(&self) -> Pubkey { pub fn pubkey(&self) -> &Pubkey {
self.pubkey &self.pubkey
} }
} }

View File

@ -410,7 +410,7 @@ pub enum LoadedAccountAccessor<'a> {
// AccountStorageEntry // AccountStorageEntry
Stored(Option<(Arc<AccountStorageEntry>, usize)>), Stored(Option<(Arc<AccountStorageEntry>, usize)>),
// None value in Cached variant means the cache was flushed // None value in Cached variant means the cache was flushed
Cached(Option<(Pubkey, Cow<'a, CachedAccount>)>), Cached(Option<Cow<'a, CachedAccount>>),
} }
mod accountsdb_plugin_utils; mod accountsdb_plugin_utils;
@ -444,10 +444,9 @@ impl<'a> LoadedAccountAccessor<'a> {
fn get_loaded_account(&mut self) -> Option<LoadedAccount> { fn get_loaded_account(&mut self) -> Option<LoadedAccount> {
match self { match self {
LoadedAccountAccessor::Cached(cached_account) => { LoadedAccountAccessor::Cached(cached_account) => {
let cached_account: (Pubkey, Cow<'a, CachedAccount>) = let cached_account: Cow<'a, CachedAccount> = cached_account.take().expect(
cached_account.take().expect( "Cache flushed/purged should be handled before trying to fetch account",
"Cache flushed/purged should be handled before trying to fetch account", );
);
Some(LoadedAccount::Cached(cached_account)) Some(LoadedAccount::Cached(cached_account))
} }
LoadedAccountAccessor::Stored(maybe_storage_entry) => { LoadedAccountAccessor::Stored(maybe_storage_entry) => {
@ -468,14 +467,14 @@ impl<'a> LoadedAccountAccessor<'a> {
pub enum LoadedAccount<'a> { pub enum LoadedAccount<'a> {
Stored(StoredAccountMeta<'a>), Stored(StoredAccountMeta<'a>),
Cached((Pubkey, Cow<'a, CachedAccount>)), Cached(Cow<'a, CachedAccount>),
} }
impl<'a> LoadedAccount<'a> { impl<'a> LoadedAccount<'a> {
pub fn owner(&self) -> &Pubkey { pub fn owner(&self) -> &Pubkey {
match self { match self {
LoadedAccount::Stored(stored_account_meta) => &stored_account_meta.account_meta.owner, LoadedAccount::Stored(stored_account_meta) => &stored_account_meta.account_meta.owner,
LoadedAccount::Cached((_, cached_account)) => cached_account.account.owner(), LoadedAccount::Cached(cached_account) => cached_account.account.owner(),
} }
} }
@ -484,21 +483,21 @@ impl<'a> LoadedAccount<'a> {
LoadedAccount::Stored(stored_account_meta) => { LoadedAccount::Stored(stored_account_meta) => {
stored_account_meta.account_meta.executable stored_account_meta.account_meta.executable
} }
LoadedAccount::Cached((_, cached_account)) => cached_account.account.executable(), LoadedAccount::Cached(cached_account) => cached_account.account.executable(),
} }
} }
pub fn loaded_hash(&self) -> Hash { pub fn loaded_hash(&self) -> Hash {
match self { match self {
LoadedAccount::Stored(stored_account_meta) => *stored_account_meta.hash, LoadedAccount::Stored(stored_account_meta) => *stored_account_meta.hash,
LoadedAccount::Cached((_, cached_account)) => cached_account.hash(), LoadedAccount::Cached(cached_account) => cached_account.hash(),
} }
} }
pub fn pubkey(&self) -> &Pubkey { pub fn pubkey(&self) -> &Pubkey {
match self { match self {
LoadedAccount::Stored(stored_account_meta) => &stored_account_meta.meta.pubkey, LoadedAccount::Stored(stored_account_meta) => &stored_account_meta.meta.pubkey,
LoadedAccount::Cached((pubkey, _)) => pubkey, LoadedAccount::Cached(cached_account) => cached_account.pubkey(),
} }
} }
@ -514,7 +513,7 @@ impl<'a> LoadedAccount<'a> {
LoadedAccount::Stored(stored_account_meta) => { LoadedAccount::Stored(stored_account_meta) => {
AccountsDb::hash_stored_account(slot, stored_account_meta) AccountsDb::hash_stored_account(slot, stored_account_meta)
} }
LoadedAccount::Cached((_, cached_account)) => { LoadedAccount::Cached(cached_account) => {
AccountsDb::hash_account(slot, &cached_account.account, pubkey) AccountsDb::hash_account(slot, &cached_account.account, pubkey)
} }
} }
@ -530,14 +529,14 @@ impl<'a> LoadedAccount<'a> {
pub fn lamports(&self) -> u64 { pub fn lamports(&self) -> u64 {
match self { match self {
LoadedAccount::Stored(stored_account_meta) => stored_account_meta.account_meta.lamports, LoadedAccount::Stored(stored_account_meta) => stored_account_meta.account_meta.lamports,
LoadedAccount::Cached((_, cached_account)) => cached_account.account.lamports(), LoadedAccount::Cached(cached_account) => cached_account.account.lamports(),
} }
} }
pub fn take_account(self) -> AccountSharedData { pub fn take_account(self) -> AccountSharedData {
match self { match self {
LoadedAccount::Stored(stored_account_meta) => stored_account_meta.clone_account(), LoadedAccount::Stored(stored_account_meta) => stored_account_meta.clone_account(),
LoadedAccount::Cached((_, cached_account)) => match cached_account { LoadedAccount::Cached(cached_account) => match cached_account {
Cow::Owned(cached_account) => cached_account.account.clone(), Cow::Owned(cached_account) => cached_account.account.clone(),
Cow::Borrowed(cached_account) => cached_account.account.clone(), Cow::Borrowed(cached_account) => cached_account.account.clone(),
}, },
@ -3208,9 +3207,8 @@ impl AccountsDb {
slot_cache slot_cache
.par_iter() .par_iter()
.filter_map(|cached_account| { .filter_map(|cached_account| {
cache_map_func(LoadedAccount::Cached(( cache_map_func(LoadedAccount::Cached(Cow::Borrowed(
*cached_account.key(), cached_account.value(),
Cow::Borrowed(cached_account.value()),
))) )))
}) })
.collect() .collect()
@ -3220,9 +3218,8 @@ impl AccountsDb {
slot_cache slot_cache
.iter() .iter()
.filter_map(|cached_account| { .filter_map(|cached_account| {
cache_map_func(LoadedAccount::Cached(( cache_map_func(LoadedAccount::Cached(Cow::Borrowed(
*cached_account.key(), cached_account.value(),
Cow::Borrowed(cached_account.value()),
))) )))
}) })
.collect(), .collect(),
@ -3683,10 +3680,7 @@ impl AccountsDb {
offset: usize, offset: usize,
) -> LoadedAccountAccessor<'a> { ) -> LoadedAccountAccessor<'a> {
if store_id == CACHE_VIRTUAL_STORAGE_ID { if store_id == CACHE_VIRTUAL_STORAGE_ID {
let maybe_cached_account = self let maybe_cached_account = self.accounts_cache.load(slot, pubkey).map(Cow::Owned);
.accounts_cache
.load(slot, pubkey)
.map(|cached_account| (*pubkey, Cow::Owned(cached_account)));
LoadedAccountAccessor::Cached(maybe_cached_account) LoadedAccountAccessor::Cached(maybe_cached_account)
} else { } else {
let maybe_storage_entry = self let maybe_storage_entry = self
@ -5365,10 +5359,9 @@ impl AccountsDb {
let keys = slot_cache.get_all_pubkeys(); let keys = slot_cache.get_all_pubkeys();
for key in keys { for key in keys {
if let Some(cached_account) = slot_cache.get_cloned(&key) { if let Some(cached_account) = slot_cache.get_cloned(&key) {
let mut accessor = LoadedAccountAccessor::Cached(Some(( let mut accessor = LoadedAccountAccessor::Cached(Some(
key,
Cow::Owned(cached_account), Cow::Owned(cached_account),
))); ));
let account = accessor.get_loaded_account().unwrap(); let account = accessor.get_loaded_account().unwrap();
scan_func(account, &mut retval, slot); scan_func(account, &mut retval, slot);
}; };