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] Test for creating HotStorageWriter on an existing file #34677

Closed
Closed
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
117 changes: 115 additions & 2 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 {
accounts_hash::AccountHash,
tiered_storage::{
byte_block,
file::TieredStorageFile,
footer::{
AccountBlockFormat, AccountMetaFormat, OwnersBlockFormat, TieredStorageFooter,
},
Expand All @@ -15,7 +16,8 @@ use {
mmap_utils::{get_pod, get_slice},
owners::{OwnerOffset, OwnersBlock},
readable::TieredReadableAccount,
TieredStorageError, TieredStorageFormat, TieredStorageResult,
Borrow, ReadableAccount, StorableAccounts, StorableAccountsWithHashesAndWriteVersions,
StoredAccountInfo, TieredStorageError, TieredStorageFormat, TieredStorageResult,
},
},
bytemuck::{Pod, Zeroable},
Expand All @@ -33,6 +35,19 @@ pub const HOT_FORMAT: TieredStorageFormat = TieredStorageFormat {
account_block_format: AccountBlockFormat::AlignedRaw,
};

/// An helper function that creates a new default footer for hot
/// accounts storage.
fn new_hot_footer() -> TieredStorageFooter {
TieredStorageFooter {
account_meta_format: HOT_FORMAT.account_meta_format,
account_meta_entry_size: HOT_FORMAT.meta_entry_size as u32,
account_block_format: HOT_FORMAT.account_block_format,
index_block_format: HOT_FORMAT.index_block_format,
owners_block_format: HOT_FORMAT.owners_block_format,
..TieredStorageFooter::default()
}
}

/// The maximum number of padding bytes used in a hot account entry.
const MAX_HOT_PADDING: u8 = 7;

Expand Down Expand Up @@ -430,6 +445,42 @@ impl HotStorageReader {
}
}

/// The writer that creates a hot accounts file.
#[derive(Debug)]
pub struct HotStorageWriter {
storage: TieredStorageFile,
}

impl HotStorageWriter {
/// Create a new HotStorageWriter with the specified path.
pub fn new(file_path: impl AsRef<Path>) -> TieredStorageResult<Self> {
Ok(Self {
storage: TieredStorageFile::new_writable(file_path)?,
})
}

/// An underdevelopment function that will implement append_accounts().
///
/// The function is currently private and does not write any accounts.
fn write_accounts<
'a,
'b,
T: ReadableAccount + Sync,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
>(
&self,
_storable_accounts: &StorableAccountsWithHashesAndWriteVersions<'a, 'b, T, U, V>,
_skip: usize,
) -> TieredStorageResult<Vec<StoredAccountInfo>> {
let footer = new_hot_footer();

footer.write_footer_block(&self.storage)?;

Ok(vec![])
}
}

#[cfg(test)]
pub mod tests {
use {
Expand All @@ -448,7 +499,13 @@ pub mod tests {
assert_matches::assert_matches,
memoffset::offset_of,
rand::{seq::SliceRandom, Rng},
solana_sdk::{account::ReadableAccount, hash::Hash, pubkey::Pubkey, stake_history::Epoch},
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
hash::Hash,
pubkey::Pubkey,
slot_history::Slot,
stake_history::Epoch,
},
tempfile::TempDir,
};

Expand Down Expand Up @@ -1026,4 +1083,60 @@ pub mod tests {
Ok(None)
);
}

#[test]
fn test_hot_storage_writer_creation() {
// 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_writer_creation");

// Write a zero-account hot accounts file using the HotStorageWriter
{
let account_refs: Vec<(&Pubkey, &AccountSharedData)> = vec![];
let account_data = (Slot::MAX, &account_refs[..]);
let hashes: Vec<AccountHash> = vec![];
let storable_accounts =
StorableAccountsWithHashesAndWriteVersions::new_with_hashes_and_write_versions(
&account_data,
hashes,
vec![],
);
let hot_writer = HotStorageWriter::new(&path).unwrap();
hot_writer.write_accounts(&storable_accounts, 0).unwrap();
}

// verifies the HotStorageReader can successfully open the created file
// with the expected format.
let hot_storage = HotStorageReader::new_from_path(&path).unwrap();
assert_eq!(
hot_storage.footer().account_meta_format,
HOT_FORMAT.account_meta_format
);
assert_eq!(
hot_storage.footer().account_block_format,
HOT_FORMAT.account_block_format
);
assert_eq!(
hot_storage.footer().owners_block_format,
HOT_FORMAT.owners_block_format
);
assert_eq!(
hot_storage.footer().index_block_format,
HOT_FORMAT.index_block_format
);
assert_eq!(
hot_storage.footer().account_meta_entry_size,
HOT_FORMAT.meta_entry_size as u32
);
}

#[test]
fn test_hot_storage_writer_twice_on_same_path() {
// 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_writer_twice_on_same_path");

assert_matches!(HotStorageWriter::new(&path), Ok(_));
assert_matches!(HotStorageWriter::new(&path), Err(_));
}
}