Skip to content

Commit

Permalink
introduce AccountForStorage for StorableAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Apr 13, 2024
1 parent c9e3c90 commit ee2240a
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 169 deletions.
25 changes: 7 additions & 18 deletions accounts-db/src/account_storage/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
account_info::AccountInfo,
accounts_hash::AccountHash,
append_vec::AppendVecStoredAccountMeta,
storable_accounts::StorableAccounts,
storable_accounts::{AccountForStorage, StorableAccounts},
tiered_storage::hot::{HotAccount, HotAccountMeta},
},
solana_sdk::{account::ReadableAccount, hash::Hash, pubkey::Pubkey, stake_history::Epoch},
Expand All @@ -26,29 +26,18 @@ lazy_static! {
/// This struct contains what is needed to store accounts to a storage
/// 1. account & pubkey (StorableAccounts)
/// 2. hash per account (Maybe in StorableAccounts, otherwise has to be passed in separately)
pub struct StorableAccountsWithHashes<
'a: 'b,
'b,
T: ReadableAccount + Sync + 'b,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
> {
pub struct StorableAccountsWithHashes<'a: 'b, 'b, U: StorableAccounts<'a>, V: Borrow<AccountHash>> {
/// accounts to store
/// always has pubkey and account
/// may also have hash per account
pub(crate) accounts: &'b U,
/// if accounts does not have hash, this has a hash per account
hashes: Option<Vec<V>>,
_phantom: PhantomData<&'a T>,
_phantom: PhantomData<&'a ()>,
}

impl<
'a: 'b,
'b,
T: ReadableAccount + Sync + 'b,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
> StorableAccountsWithHashes<'a, 'b, T, U, V>
impl<'a: 'b, 'b, U: StorableAccounts<'a>, V: Borrow<AccountHash>>
StorableAccountsWithHashes<'a, 'b, U, V>
{
/// used when accounts contains hash already
pub fn new(accounts: &'b U) -> Self {
Expand All @@ -72,7 +61,7 @@ impl<
}

/// get all account fields at 'index'
pub fn get(&self, index: usize) -> (Option<&T>, &Pubkey, &AccountHash) {
pub fn get(&self, index: usize) -> (Option<AccountForStorage>, &Pubkey, &AccountHash) {
let account = self.accounts.account_default_if_zero_lamport(index);
let pubkey = self.accounts.pubkey(index);
let hash = if self.accounts.has_hash() {
Expand All @@ -87,7 +76,7 @@ impl<
/// None if account at index has lamports == 0
/// Otherwise, Some(account)
/// This is the only way to access the account.
pub fn account(&self, index: usize) -> Option<&T> {
pub fn account(&self, index: usize) -> Option<AccountForStorage<'a>> {
self.accounts.account_default_if_zero_lamport(index)
}

Expand Down
7 changes: 2 additions & 5 deletions accounts-db/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
AccountsAddRootTiming, AccountsDb, LoadHint, LoadedAccount, ScanAccountStorageData,
ScanStorageResult, VerifyAccountsHashAndLamportsConfig,
},
accounts_index::{IndexKey, ScanConfig, ScanError, ScanResult, ZeroLamport},
accounts_index::{IndexKey, ScanConfig, ScanError, ScanResult},
ancestors::Ancestors,
storable_accounts::StorableAccounts,
},
Expand Down Expand Up @@ -677,10 +677,7 @@ impl Accounts {
.store_cached_inline_update_index((slot, &accounts_to_store[..]), Some(&transactions));
}

pub fn store_accounts_cached<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
) {
pub fn store_accounts_cached<'a>(&self, accounts: impl StorableAccounts<'a>) {
self.accounts_db.store_cached(accounts, None)
}

Expand Down
67 changes: 30 additions & 37 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5969,17 +5969,11 @@ impl AccountsDb {
AccountHash(Hash::new_from_array(hasher.finalize().into()))
}

fn write_accounts_to_storage<
'a,
'b,
T: ReadableAccount + Sync,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
>(
fn write_accounts_to_storage<'a, 'b, U: StorableAccounts<'a>, V: Borrow<AccountHash>>(
&self,
slot: Slot,
storage: &AccountStorageEntry,
accounts_and_meta_to_store: &StorableAccountsWithHashes<'a, 'b, T, U, V>,
accounts_and_meta_to_store: &StorableAccountsWithHashes<'a, 'b, U, V>,
) -> Vec<AccountInfo> {
let mut infos: Vec<AccountInfo> = Vec::with_capacity(accounts_and_meta_to_store.len());
let mut total_append_accounts_us = 0;
Expand Down Expand Up @@ -6378,10 +6372,10 @@ impl AccountsDb {
}
}

fn write_accounts_to_cache<'a, 'b, T: ReadableAccount + Sync>(
fn write_accounts_to_cache<'a, 'b>(
&self,
slot: Slot,
accounts_and_meta_to_store: &impl StorableAccounts<'b, T>,
accounts_and_meta_to_store: &impl StorableAccounts<'b>,
txn_iter: Box<dyn std::iter::Iterator<Item = &Option<&SanitizedTransaction>> + 'a>,
) -> Vec<AccountInfo> {
let mut write_version_producer: Box<dyn Iterator<Item = u64>> =
Expand Down Expand Up @@ -6429,9 +6423,9 @@ impl AccountsDb {
.collect()
}

fn store_accounts_to<'a: 'c, 'b, 'c, T: ReadableAccount + Sync + ZeroLamport + 'b>(
fn store_accounts_to<'a: 'c, 'b, 'c>(
&self,
accounts: &'c impl StorableAccounts<'b, T>,
accounts: &'c impl StorableAccounts<'b>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
store_to: &StoreTo,
transactions: Option<&[Option<&'a SanitizedTransaction>]>,
Expand Down Expand Up @@ -6473,7 +6467,7 @@ impl AccountsDb {
self.write_accounts_to_storage(
slot,
storage,
&StorableAccountsWithHashes::<'_, '_, _, _, &AccountHash>::new(accounts),
&StorableAccountsWithHashes::<'_, '_, _, &AccountHash>::new(accounts),
)
} else {
match hashes {
Expand All @@ -6490,7 +6484,7 @@ impl AccountsDb {
for index in 0..accounts.len() {
let (pubkey, account) =
(accounts.pubkey(index), accounts.account(index));
let hash = Self::hash_account(account, pubkey);
let hash = Self::hash_account(&account, pubkey);
hashes.push(hash);
}
hash_time.stop();
Expand Down Expand Up @@ -7772,10 +7766,10 @@ impl AccountsDb {
self.bank_hash_stats.lock().unwrap().get(&slot).cloned()
}

fn update_index<'a, T: ReadableAccount + Sync>(
fn update_index<'a>(
&self,
infos: Vec<AccountInfo>,
accounts: &impl StorableAccounts<'a, T>,
accounts: &impl StorableAccounts<'a>,
reclaim: UpsertReclaim,
update_index_thread_selection: UpdateIndexThreadSelection,
) -> SlotList<AccountInfo> {
Expand All @@ -7796,7 +7790,7 @@ impl AccountsDb {
target_slot,
old_slot,
pubkey,
pubkey_account.1,
&pubkey_account.1,
&self.account_indexes,
info,
&mut reclaims,
Expand Down Expand Up @@ -8159,9 +8153,9 @@ impl AccountsDb {
.fetch_add(measure.as_us(), Ordering::Relaxed);
}

pub fn store_cached<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
pub fn store_cached<'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
accounts: impl StorableAccounts<'a>,
transactions: Option<&'a [Option<&'a SanitizedTransaction>]>,
) {
self.store(
Expand All @@ -8173,12 +8167,9 @@ impl AccountsDb {
);
}

pub(crate) fn store_cached_inline_update_index<
'a,
T: ReadableAccount + Sync + ZeroLamport + 'a,
>(
pub(crate) fn store_cached_inline_update_index<'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
accounts: impl StorableAccounts<'a>,
transactions: Option<&'a [Option<&'a SanitizedTransaction>]>,
) {
self.store(
Expand All @@ -8203,9 +8194,9 @@ impl AccountsDb {
);
}

fn store<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
fn store<'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
accounts: impl StorableAccounts<'a>,
store_to: &StoreTo,
transactions: Option<&'a [Option<&'a SanitizedTransaction>]>,
reclaim: StoreReclaims,
Expand All @@ -8222,7 +8213,7 @@ impl AccountsDb {
(0..accounts.len()).for_each(|index| {
let account = accounts.account(index);
total_data += account.data().len();
stats.update(account);
stats.update(&account);
});

self.stats
Expand Down Expand Up @@ -8389,9 +8380,9 @@ impl AccountsDb {
}
}

fn store_accounts_unfrozen<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
fn store_accounts_unfrozen<'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
accounts: impl StorableAccounts<'a>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
store_to: &StoreTo,
transactions: Option<&'a [Option<&'a SanitizedTransaction>]>,
Expand All @@ -8417,9 +8408,9 @@ impl AccountsDb {
);
}

pub fn store_accounts_frozen<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
pub fn store_accounts_frozen<'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
accounts: impl StorableAccounts<'a>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
storage: &Arc<AccountStorageEntry>,
) -> StoreAccountsTiming {
Expand All @@ -8438,9 +8429,9 @@ impl AccountsDb {
)
}

fn store_accounts_custom<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
fn store_accounts_custom<'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
accounts: impl StorableAccounts<'a>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
store_to: &StoreTo,
reset_accounts: bool,
Expand Down Expand Up @@ -9564,6 +9555,7 @@ pub mod tests {
ancient_append_vecs,
append_vec::{test_utils::TempFile, AppendVec, AppendVecStoredAccountMeta},
cache_hash_data::CacheHashDataFile,
storable_accounts::AccountForStorage,
},
assert_matches::assert_matches,
itertools::Itertools,
Expand Down Expand Up @@ -9632,14 +9624,15 @@ pub mod tests {
}

/// this tuple contains slot info PER account
impl<'a, T: ReadableAccount + Sync> StorableAccounts<'a, T>
for (Slot, &'a [(&'a Pubkey, &'a T, Slot)])
impl<'a, T: ReadableAccount + Sync> StorableAccounts<'a> for (Slot, &'a [(&'a Pubkey, &'a T, Slot)])
where
AccountForStorage<'a>: From<&'a T>,
{
fn pubkey(&self, index: usize) -> &Pubkey {
self.1[index].0
}
fn account(&self, index: usize) -> &T {
self.1[index].1
fn account(&self, index: usize) -> AccountForStorage<'a> {
self.1[index].1.into()
}
fn slot(&self, index: usize) -> Slot {
// note that this could be different than 'target_slot()' PER account
Expand Down
16 changes: 3 additions & 13 deletions accounts-db/src/accounts_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use {
error::TieredStorageError, hot::HOT_FORMAT, index::IndexOffset, TieredStorage,
},
},
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
clock::Slot,
pubkey::Pubkey,
},
solana_sdk::{account::AccountSharedData, clock::Slot, pubkey::Pubkey},
std::{
borrow::Borrow,
io::Read,
Expand Down Expand Up @@ -249,15 +245,9 @@ impl AccountsFile {
/// So, return.len() is 1 + (number of accounts written)
/// After each account is appended, the internal `current_len` is updated
/// and will be available to other threads.
pub fn append_accounts<
'a,
'b,
T: ReadableAccount + Sync,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
>(
pub fn append_accounts<'a, 'b, U: StorableAccounts<'a>, V: Borrow<AccountHash>>(
&self,
accounts: &StorableAccountsWithHashes<'a, 'b, T, U, V>,
accounts: &StorableAccountsWithHashes<'a, 'b, U, V>,
skip: usize,
) -> Option<Vec<StoredAccountInfo>> {
match self {
Expand Down
8 changes: 4 additions & 4 deletions accounts-db/src/ancient_append_vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use {
},
accounts_file::AccountsFile,
accounts_hash::AccountHash,
accounts_index::{AccountsIndexScanResult, ZeroLamport},
accounts_index::AccountsIndexScanResult,
active_stats::ActiveStatItem,
storable_accounts::{StorableAccounts, StorableAccountsBySlot},
},
rand::{thread_rng, Rng},
rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator},
solana_measure::measure_us,
solana_sdk::{account::ReadableAccount, clock::Slot},
solana_sdk::clock::Slot,
std::{
collections::HashMap,
num::{NonZeroU64, Saturating},
Expand Down Expand Up @@ -440,10 +440,10 @@ impl AccountsDb {
/// create append vec of size 'bytes'
/// write 'accounts_to_write' into it
/// return shrink_in_progress and some metrics
fn write_ancient_accounts<'a, 'b: 'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
fn write_ancient_accounts<'a, 'b: 'a>(
&'b self,
bytes: u64,
accounts_to_write: impl StorableAccounts<'a, T>,
accounts_to_write: impl StorableAccounts<'a>,
write_ancient_accounts: &mut WriteAncientAccounts<'b>,
) {
let target_slot = accounts_to_write.target_slot();
Expand Down
15 changes: 5 additions & 10 deletions accounts-db/src/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,15 +733,9 @@ impl AppendVec {
/// So, return.len() is 1 + (number of accounts written)
/// After each account is appended, the internal `current_len` is updated
/// and will be available to other threads.
pub fn append_accounts<
'a,
'b,
T: ReadableAccount + Sync,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
>(
pub fn append_accounts<'a, 'b, U: StorableAccounts<'a>, V: Borrow<AccountHash>>(
&self,
accounts: &StorableAccountsWithHashes<'a, 'b, T, U, V>,
accounts: &StorableAccountsWithHashes<'a, 'b, U, V>,
skip: usize,
) -> Option<Vec<StoredAccountInfo>> {
let _lock = self.append_lock.lock().unwrap();
Expand Down Expand Up @@ -775,6 +769,7 @@ impl AppendVec {
let account_meta_ptr = &account_meta as *const AccountMeta;
let data_len = stored_meta.data_len as usize;
let data_ptr = account
.as_ref()
.map(|account| account.data())
.unwrap_or_default()
.as_ptr();
Expand Down Expand Up @@ -902,7 +897,7 @@ pub mod tests {
// for (Slot, &'a [(&'a Pubkey, &'a T)])
let slot = 0 as Slot;
let pubkey = Pubkey::default();
StorableAccountsWithHashes::<'_, '_, _, _, &AccountHash>::new(&(
StorableAccountsWithHashes::<'_, '_, _, &AccountHash>::new(&(
slot,
&[(&pubkey, &account)][..],
));
Expand Down Expand Up @@ -996,7 +991,7 @@ pub mod tests {
let accounts2 = (slot, &accounts[..]);
let storable = StorableAccountsWithHashes::new_with_hashes(&accounts2, hashes);
let get_account = storable.account(0);
assert!(accounts_equal(&account, get_account.unwrap()));
assert!(accounts_equal(&account, &get_account.unwrap()));
}

#[test]
Expand Down
Loading

0 comments on commit ee2240a

Please sign in to comment.