Skip to content

Commit

Permalink
[TieredStorage] Use mmap.len() in TieredStorage::file_size() for HotS…
Browse files Browse the repository at this point in the history
…torage (anza-xyz#381)

#### Problem
The current implementation of TieredStorage::file_size() requires
a sys-call to provide the file size.

#### Summary of Changes
Add len() API to TieredStorageReader, and have HotStorageReader()
implement the API using Mmap::len().

#### Test Plan
Update existing unit-test to also verify HotStorageReader::len().
  • Loading branch information
yhchiang-sol authored Mar 22, 2024
1 parent fe16e84 commit f1a82cb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
9 changes: 2 additions & 7 deletions accounts-db/src/tiered_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use {
solana_sdk::account::ReadableAccount,
std::{
borrow::Borrow,
fs::{self, OpenOptions},
fs,
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -156,12 +156,7 @@ impl TieredStorage {

/// Returns the size of the underlying accounts file.
pub fn file_size(&self) -> TieredStorageResult<u64> {
let file = OpenOptions::new().read(true).open(&self.path);

Ok(file
.and_then(|file| file.metadata())
.map(|metadata| metadata.len())
.unwrap_or(0))
Ok(self.reader().map_or(0, |reader| reader.len()))
}
}

Expand Down
21 changes: 20 additions & 1 deletion accounts-db/src/tiered_storage/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ impl HotStorageReader {
Ok(Self { mmap, footer })
}

/// Returns the size of the underlying storage.
pub fn len(&self) -> u64 {
self.mmap.len() as u64
}

/// Returns whether the nderlying storage is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the footer of the underlying tiered-storage accounts file.
pub fn footer(&self) -> &TieredStorageFooter {
&self.footer
Expand Down Expand Up @@ -709,7 +719,7 @@ pub mod tests {
super::*,
crate::tiered_storage::{
byte_block::ByteBlockWriter,
file::TieredWritableFile,
file::{TieredStorageMagicNumber, TieredWritableFile},
footer::{AccountBlockFormat, AccountMetaFormat, TieredStorageFooter, FOOTER_SIZE},
hot::{HotAccountMeta, HotStorageReader},
index::{AccountIndexWriterEntry, IndexBlockFormat, IndexOffset},
Expand Down Expand Up @@ -1420,5 +1430,14 @@ pub mod tests {
let partial_accounts = hot_storage.accounts(IndexOffset(i as u32)).unwrap();
assert_eq!(&partial_accounts, &accounts[i..]);
}
let footer = hot_storage.footer();

let expected_size: u64 = footer.owners_block_offset
+ std::mem::size_of::<Pubkey>() as u64 * footer.owner_count as u64
+ std::mem::size_of::<TieredStorageFooter>() as u64
+ std::mem::size_of::<TieredStorageMagicNumber>() as u64;

assert!(!hot_storage.is_empty());
assert_eq!(expected_size, hot_storage.len());
}
}
14 changes: 14 additions & 0 deletions accounts-db/src/tiered_storage/readable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ impl TieredStorageReader {
}
}

/// Returns the size of the underlying storage.
pub fn len(&self) -> u64 {
match self {
Self::Hot(hot) => hot.len(),
}
}

/// Returns whether the nderlying storage is empty.
pub fn is_empty(&self) -> bool {
match self {
Self::Hot(hot) => hot.is_empty(),
}
}

/// Returns the footer of the associated HotAccountsFile.
pub fn footer(&self) -> &TieredStorageFooter {
match self {
Expand Down

0 comments on commit f1a82cb

Please sign in to comment.