Skip to content

Commit

Permalink
[TieredStorage] Handles reduced-offset and offset conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
yhchiang-sol committed Mar 11, 2024
1 parent f4f04f9 commit 1590a84
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions accounts-db/src/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl AccountInfo {
}
}

fn get_reduced_offset(offset: usize) -> OffsetReduced {
pub fn get_reduced_offset(offset: usize) -> OffsetReduced {
(offset / ALIGN_BOUNDARY_OFFSET) as OffsetReduced
}

Expand All @@ -174,7 +174,7 @@ impl AccountInfo {
)
}

fn reduced_offset_to_offset(reduced_offset: OffsetReduced) -> Offset {
pub fn reduced_offset_to_offset(reduced_offset: OffsetReduced) -> Offset {
(reduced_offset as Offset) * ALIGN_BOUNDARY_OFFSET
}

Expand Down
45 changes: 38 additions & 7 deletions accounts-db/src/accounts_file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{
account_info::AccountInfo,
account_storage::meta::{
StorableAccountsWithHashesAndWriteVersions, StoredAccountInfo, StoredAccountMeta,
},
Expand Down Expand Up @@ -160,15 +161,24 @@ impl AccountsFile {
/// Return (account metadata, next_index) pair for the account at the
/// specified `index` if any. Otherwise return None. Also return the
/// index of the next entry.
pub fn get_account(&self, index: usize) -> Option<(StoredAccountMeta<'_>, usize)> {
pub fn get_account(&self, offset: usize) -> Option<(StoredAccountMeta<'_>, usize)> {
match self {
Self::AppendVec(av) => av.get_account(index),
Self::AppendVec(av) => av.get_account(offset),
Self::TieredHot(ts) => {
// The get_account API here uses usize "offset", which AccountsDB
// assumes it is an multiple of 8.
//
// On the other hand, TieredHot uses IndexOffset in get_account(),
// which is equivalent to the reduced-offset concept in AccountsDB.
// As a result, we need to convert it to reduced_offset, but returns
// usize offset in the return value.
if let Some(reader) = ts.reader() {
return reader
.get_account(IndexOffset(index as u32))
.get_account(IndexOffset(AccountInfo::get_reduced_offset(offset)))
.unwrap()
.map(|(metas, index_offset)| (metas, index_offset.0 as usize));
.map(|(metas, index_offset)| {
(metas, AccountInfo::reduced_offset_to_offset(index_offset.0))
});
}
None
}
Expand All @@ -184,7 +194,10 @@ impl AccountsFile {
Self::AppendVec(av) => av.account_matches_owners(offset, owners),
Self::TieredHot(ts) => {
if let Some(reader) = ts.reader() {
return reader.account_matches_owners(IndexOffset(offset as u32), owners);
return reader.account_matches_owners(
IndexOffset(AccountInfo::get_reduced_offset(offset)),
owners,
);
}
Err(MatchAccountOwnerError::UnableToLoad)
}
Expand All @@ -210,7 +223,13 @@ impl AccountsFile {
Self::AppendVec(av) => av.accounts(offset),
Self::TieredHot(ts) => {
if let Some(reader) = ts.reader() {
return reader.accounts(IndexOffset(offset as u32)).unwrap();
// A conversion is needed here as TieredStorage uses
// IndexOffset (which is equivalent to reduced-offset)
// while AccountsDb uses usize non-reduced offset that
// assumes the offset must be a multiple of 8.
return reader
.accounts(IndexOffset(AccountInfo::get_reduced_offset(offset)))
.unwrap();
}
vec![]
}
Expand All @@ -237,7 +256,19 @@ impl AccountsFile {
) -> Option<Vec<StoredAccountInfo>> {
match self {
Self::AppendVec(av) => av.append_accounts(accounts, skip),
Self::TieredHot(ts) => ts.write_accounts(accounts, skip, &HOT_FORMAT).ok(),
Self::TieredHot(ts) => ts
.write_accounts(accounts, skip, &HOT_FORMAT)
.map(|mut infos| {
infos.iter_mut().for_each(|info| {
// A conversion is needed here as TieredStorage uses
// IndexOffset (which is equivalent to reduced-offset)
// while AccountsDb uses usize non-reduced offset that
// assumes the offset must be a multiple of 8.
info.offset = AccountInfo::reduced_offset_to_offset(info.offset as u32);
});
infos
})
.ok(),
}
}
}
Expand Down

0 comments on commit 1590a84

Please sign in to comment.