Skip to content

Commit

Permalink
[TieredStorage] Make TieredStorage's Offset compatible with AccountsD…
Browse files Browse the repository at this point in the history
…B's offset (solana-labs#74)

* [TieredStorage] Handles reduced-offset and offset conversion

* fix comment

---------

Co-authored-by: jeff washington <jeff.washington@solana.com>
  • Loading branch information
yhchiang-sol and jeffwashington authored Apr 1, 2024
1 parent 8153c52 commit b47a4ec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 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
50 changes: 38 additions & 12 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 @@ -114,16 +115,21 @@ impl AccountsFile {
}

/// Return (account metadata, next_index) pair for the account at the
/// specified `index` if any. Otherwise return None. Also return the
/// specified `offset` 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),
// Note: The conversion here is needed as the AccountsDB currently
// assumes all offsets are multiple of 8 while TieredStorage uses
// IndexOffset that is equivalent to AccountInfo::reduced_offset.
Self::TieredStorage(ts) => ts
.reader()?
.get_account(IndexOffset(index as u32))
.get_account(IndexOffset(AccountInfo::get_reduced_offset(offset)))
.ok()?
.map(|(metas, index_offset)| (metas, index_offset.0 as usize)),
.map(|(metas, index_offset)| {
(metas, AccountInfo::reduced_offset_to_offset(index_offset.0))
}),
}
}

Expand All @@ -134,11 +140,17 @@ impl AccountsFile {
) -> std::result::Result<usize, MatchAccountOwnerError> {
match self {
Self::AppendVec(av) => av.account_matches_owners(offset, owners),
// Note: The conversion here is needed as the AccountsDB currently
// assumes all offsets are multiple of 8 while TieredStorage uses
// IndexOffset that is equivalent to AccountInfo::reduced_offset.
Self::TieredStorage(ts) => {
let Some(reader) = ts.reader() else {
return Err(MatchAccountOwnerError::UnableToLoad);
};
reader.account_matches_owners(IndexOffset(offset as u32), owners)
reader.account_matches_owners(
IndexOffset(AccountInfo::get_reduced_offset(offset)),
owners,
)
}
}
}
Expand Down Expand Up @@ -168,9 +180,16 @@ impl AccountsFile {
pub fn accounts(&self, offset: usize) -> Vec<StoredAccountMeta> {
match self {
Self::AppendVec(av) => av.accounts(offset),
// Note: The conversion here is needed as the AccountsDB currently
// assumes all offsets are multiple of 8 while TieredStorage uses
// IndexOffset that is equivalent to AccountInfo::reduced_offset.
Self::TieredStorage(ts) => ts
.reader()
.and_then(|reader| reader.accounts(IndexOffset(offset as u32)).ok())
.and_then(|reader| {
reader
.accounts(IndexOffset(AccountInfo::get_reduced_offset(offset)))
.ok()
})
.unwrap_or_default(),
}
}
Expand All @@ -195,11 +214,18 @@ impl AccountsFile {
) -> Option<Vec<StoredAccountInfo>> {
match self {
Self::AppendVec(av) => av.append_accounts(accounts, skip),
// Currently we only support HOT_FORMAT. If we later want to use
// a different format, then we will need a way to pass-in it.
// TODO: consider adding function like write_accounts_to_hot_storage() or something
// to hide implementation detail.
Self::TieredStorage(ts) => ts.write_accounts(accounts, skip, &HOT_FORMAT).ok(),
// Note: The conversion here is needed as the AccountsDB currently
// assumes all offsets are multiple of 8 while TieredStorage uses
// IndexOffset that is equivalent to AccountInfo::reduced_offset.
Self::TieredStorage(ts) => ts
.write_accounts(accounts, skip, &HOT_FORMAT)
.map(|mut infos| {
infos.iter_mut().for_each(|info| {
info.offset = AccountInfo::reduced_offset_to_offset(info.offset as u32);
});
infos
})
.ok(),
}
}
}
Expand Down

0 comments on commit b47a4ec

Please sign in to comment.