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] Use mmap.len() in TieredStorage::file_size() for HotStorage #381

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
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
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()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR directly, but why does this method return a Result? Seems like returning a u64 should be fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I also noticed that. Will have a PR that have it return u64 and rename it to len() for consistency.

}
}

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
Loading