Skip to content

Commit

Permalink
[TieredStorage] Avoid sys-call in TieredStorage::file_size()
Browse files Browse the repository at this point in the history
  • Loading branch information
yhchiang-sol committed Mar 22, 2024
1 parent 0e932c7 commit f107708
Show file tree
Hide file tree
Showing 3 changed files with 22 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
15 changes: 14 additions & 1 deletion accounts-db/src/tiered_storage/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ impl HotStorageReader {
Ok(Self { mmap, footer })
}

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

/// Returns the footer of the underlying tiered-storage accounts file.
pub fn footer(&self) -> &TieredStorageFooter {
&self.footer
Expand Down Expand Up @@ -704,7 +709,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 @@ -1415,5 +1420,13 @@ 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_eq!(expected_size, hot_storage.len());
}
}
6 changes: 6 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,12 @@ impl TieredStorageReader {
}
}

pub fn len(&self) -> u64 {
match self {
Self::Hot(hot) => hot.len(),
}
}

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

0 comments on commit f107708

Please sign in to comment.