Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TieredStorage] HotStorageReader::get_account_meta_from_offset() #33724

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions accounts-db/src/tiered_storage/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
footer::{AccountBlockFormat, AccountMetaFormat, OwnersBlockFormat, TieredStorageFooter},
index::AccountIndexFormat,
meta::{AccountMetaFlags, AccountMetaOptionalFields, TieredAccountMeta},
mmap_utils::get_type,
TieredStorageFormat, TieredStorageResult,
},
memmap2::{Mmap, MmapOptions},
Expand Down Expand Up @@ -215,6 +216,12 @@ impl HotStorageReader {
pub fn num_accounts(&self) -> usize {
self.footer.account_entry_count as usize
}

/// Returns the account meta located at the specified offset.
fn get_account_meta_from_offset(&self, offset: usize) -> TieredStorageResult<&HotAccountMeta> {
let (meta, _) = get_type::<HotAccountMeta>(&self.mmap, offset)?;
Ok(meta)
}
}

#[cfg(test)]
Expand All @@ -234,6 +241,7 @@ pub mod tests {
},
::solana_sdk::{hash::Hash, pubkey::Pubkey, stake_history::Epoch},
memoffset::offset_of,
rand::Rng,
tempfile::TempDir,
};

Expand Down Expand Up @@ -398,4 +406,53 @@ pub mod tests {
assert_eq!(expected_footer, *hot_storage.footer());
}
}

#[test]
fn test_hot_storage_get_account_meta_from_offset() {
// Generate a new temp path that is guaranteed to NOT already have a file.
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("test_hot_storage_footer");

const NUM_ACCOUNTS: u32 = 10;
let mut rng = rand::thread_rng();

let hot_account_metas: Vec<_> = (0..NUM_ACCOUNTS)
.map(|_| {
HotAccountMeta::new()
.with_lamports(rng.gen_range(0..u64::MAX))
.with_owner_index(rng.gen_range(0..NUM_ACCOUNTS))
})
.collect();

let account_offsets: Vec<_>;
let footer = TieredStorageFooter {
account_meta_format: AccountMetaFormat::Hot,
account_entry_count: NUM_ACCOUNTS,
..TieredStorageFooter::default()
};
{
let file = TieredStorageFile::new_writable(&path).unwrap();
let mut current_offset = 0;

account_offsets = hot_account_metas
.iter()
.map(|meta| {
let prev_offset = current_offset;
current_offset += file.write_type(meta).unwrap();
prev_offset
})
.collect();
// while the test only focuses on account metas, writing a footer
// here is necessary to make it a valid tiered-storage file.
footer.write_footer_block(&file).unwrap();
}

let hot_storage = HotStorageReader::new_from_path(&path).unwrap();

for (offset, expected_meta) in account_offsets.iter().zip(hot_account_metas.iter()) {
let meta = hot_storage.get_account_meta_from_offset(*offset).unwrap();
assert_eq!(meta, expected_meta);
}
assert_eq!(&footer, hot_storage.footer());
}
}