implement get_account_sizes for hot storages (#676)

This commit is contained in:
Jeff Washington (jwash) 2024-04-09 15:21:09 -05:00 committed by GitHub
parent 4753a8aefa
commit 374705293c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 7 deletions

View File

@ -1,5 +1,6 @@
use {
crate::{
account_info::AccountInfo,
accounts_hash::AccountHash,
append_vec::AppendVecStoredAccountMeta,
storable_accounts::StorableAccounts,
@ -134,7 +135,7 @@ impl<'storage> StoredAccountMeta<'storage> {
pub fn offset(&self) -> usize {
match self {
Self::AppendVec(av) => av.offset(),
Self::Hot(hot) => hot.index().0 as usize,
Self::Hot(hot) => AccountInfo::reduced_offset_to_offset(hot.index().0),
}
}

View File

@ -13992,9 +13992,10 @@ pub mod tests {
}
}
#[test]
fn test_alive_bytes() {
let accounts_db = AccountsDb::new_single_for_tests();
#[test_case(AccountsFileProvider::AppendVec)]
#[test_case(AccountsFileProvider::HotStorage)]
fn test_alive_bytes(accounts_file_provider: AccountsFileProvider) {
let accounts_db = AccountsDb::new_single_for_tests_with_provider(accounts_file_provider);
let slot: Slot = 0;
let num_keys = 10;
@ -14025,7 +14026,12 @@ pub mod tests {
let reclaims = [account_info];
accounts_db.remove_dead_accounts(reclaims.iter(), None, true);
let after_size = storage0.alive_bytes.load(Ordering::Acquire);
assert_eq!(before_size, after_size + account.stored_size());
if storage0.count() == 0 && AccountsFileProvider::HotStorage == accounts_file_provider {
// when `remove_dead_accounts` reaches 0 accounts, all bytes are marked as dead
assert_eq!(after_size, 0);
} else {
assert_eq!(before_size, after_size + account.stored_size());
}
}
}

View File

@ -178,10 +178,14 @@ impl AccountsFile {
AccountsFileIter::new(self)
}
/// for each offset in `sorted_offsets`, return the account size
pub(crate) fn get_account_sizes(&self, sorted_offsets: &[usize]) -> Vec<usize> {
match self {
Self::AppendVec(av) => av.get_account_sizes(sorted_offsets),
Self::TieredStorage(_) => unimplemented!(),
Self::TieredStorage(ts) => ts
.reader()
.and_then(|reader| reader.get_account_sizes(sorted_offsets).ok())
.unwrap_or_default(),
}
}

View File

@ -674,7 +674,7 @@ impl AppendVec {
/// for each offset in `sorted_offsets`, get the size of the account. No other information is needed for the account.
pub(crate) fn get_account_sizes(&self, sorted_offsets: &[usize]) -> Vec<usize> {
let mut result = Vec::with_capacity(sorted_offsets.len());
for offset in sorted_offsets.iter().cloned() {
for &offset in sorted_offsets {
let Some((stored_meta, _)) = self.get_type::<StoredMeta>(offset) else {
break;
};

View File

@ -579,6 +579,23 @@ impl HotStorageReader {
Ok(())
}
/// for each offset in `sorted_offsets`, return the account size
pub(crate) fn get_account_sizes(
&self,
sorted_offsets: &[usize],
) -> TieredStorageResult<Vec<usize>> {
let mut result = Vec::with_capacity(sorted_offsets.len());
for &offset in sorted_offsets {
let index_offset = IndexOffset(AccountInfo::get_reduced_offset(offset));
let account_offset = self.get_account_offset(index_offset)?;
let meta = self.get_account_meta_from_offset(account_offset)?;
let account_block = self.get_account_block(account_offset, index_offset)?;
let data_len = meta.account_data_size(account_block);
result.push(stored_size(data_len));
}
Ok(result)
}
/// iterate over all entries to put in index
pub(crate) fn scan_index(
&self,

View File

@ -124,6 +124,16 @@ impl TieredStorageReader {
}
}
/// for each offset in `sorted_offsets`, return the account size
pub(crate) fn get_account_sizes(
&self,
sorted_offsets: &[usize],
) -> TieredStorageResult<Vec<usize>> {
match self {
Self::Hot(hot) => hot.get_account_sizes(sorted_offsets),
}
}
/// Returns a slice suitable for use when archiving tiered storages
pub fn data_for_archive(&self) -> &[u8] {
match self {