From 8153c529389ed6d11d692f2021a45e1323d6cbd4 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Mon, 1 Apr 2024 13:54:45 -0700 Subject: [PATCH] [TieredStorage] enum-based AccountsFileProvider (#457) * [TieredStorage] enum-based AccountsFileProvider * derive traits * remove refs --------- Co-authored-by: jeff washington --- accounts-db/src/account_storage.rs | 5 +- accounts-db/src/accounts_db.rs | 80 ++++++++++++++++++++++++------ accounts-db/src/accounts_file.rs | 18 +++++++ accounts-db/src/sorted_storages.rs | 10 +++- 4 files changed, 96 insertions(+), 17 deletions(-) diff --git a/accounts-db/src/account_storage.rs b/accounts-db/src/account_storage.rs index e1fb4f092ad623..185a3324094ee4 100644 --- a/accounts-db/src/account_storage.rs +++ b/accounts-db/src/account_storage.rs @@ -276,7 +276,7 @@ impl Default for AccountStorageStatus { #[cfg(test)] pub(crate) mod tests { - use {super::*, std::path::Path}; + use {super::*, crate::accounts_file::AccountsFileProvider, std::path::Path}; #[test] fn test_shrink_in_progress() { @@ -297,12 +297,14 @@ pub(crate) mod tests { slot, id, store_file_size, + AccountsFileProvider::AppendVec, )); let entry2 = Arc::new(AccountStorageEntry::new( common_store_path, slot, id, store_file_size2, + AccountsFileProvider::AppendVec, )); storage .map @@ -353,6 +355,7 @@ pub(crate) mod tests { slot, id, store_file_size, + AccountsFileProvider::AppendVec, )) } fn get_test_storage(&self) -> Arc { diff --git a/accounts-db/src/accounts_db.rs b/accounts-db/src/accounts_db.rs index 9360d93df57553..4b5f027bf38fcf 100644 --- a/accounts-db/src/accounts_db.rs +++ b/accounts-db/src/accounts_db.rs @@ -34,7 +34,8 @@ use { }, accounts_cache::{AccountsCache, CachedAccount, SlotCache}, accounts_file::{ - AccountsFile, AccountsFileError, MatchAccountOwnerError, ALIGN_BOUNDARY_OFFSET, + AccountsFile, AccountsFileError, AccountsFileProvider, MatchAccountOwnerError, + ALIGN_BOUNDARY_OFFSET, }, accounts_hash::{ AccountHash, AccountsDeltaHash, AccountsHash, AccountsHashKind, AccountsHasher, @@ -57,9 +58,7 @@ use { ancient_append_vecs::{ get_ancient_append_vec_capacity, is_ancient, AccountsToStore, StorageSelector, }, - append_vec::{ - aligned_stored_size, AppendVec, APPEND_VEC_MMAPPED_FILES_OPEN, STORE_META_OVERHEAD, - }, + append_vec::{aligned_stored_size, APPEND_VEC_MMAPPED_FILES_OPEN, STORE_META_OVERHEAD}, cache_hash_data::{ CacheHashData, CacheHashDataFileReference, DeletionPolicy as CacheHashDeletionPolicy, }, @@ -1053,10 +1052,16 @@ pub struct AccountStorageEntry { } impl AccountStorageEntry { - pub fn new(path: &Path, slot: Slot, id: AccountsFileId, file_size: u64) -> Self { + pub fn new( + path: &Path, + slot: Slot, + id: AccountsFileId, + file_size: u64, + provider: AccountsFileProvider, + ) -> Self { let tail = AccountsFile::file_name(slot, id); let path = Path::new(path).join(tail); - let accounts = AccountsFile::AppendVec(AppendVec::new(path, true, file_size as usize)); + let accounts = provider.new_writable(path, file_size); Self { id, @@ -2536,7 +2541,13 @@ impl AccountsDb { } fn new_storage_entry(&self, slot: Slot, path: &Path, size: u64) -> AccountStorageEntry { - AccountStorageEntry::new(path, slot, self.next_id(), size) + AccountStorageEntry::new( + path, + slot, + self.next_id(), + size, + AccountsFileProvider::AppendVec, + ) } pub fn expected_cluster_type(&self) -> ClusterType { @@ -9496,7 +9507,7 @@ pub mod tests { accounts_hash::MERKLE_FANOUT, accounts_index::{tests::*, AccountSecondaryIndexesIncludeExclude}, ancient_append_vecs, - append_vec::{test_utils::TempFile, AppendVecStoredAccountMeta}, + append_vec::{test_utils::TempFile, AppendVec, AppendVecStoredAccountMeta}, cache_hash_data::CacheHashDataFile, }, assert_matches::assert_matches, @@ -10507,7 +10518,13 @@ pub mod tests { let (_temp_dirs, paths) = get_temp_accounts_paths(1).unwrap(); let slot_expected: Slot = 0; let size: usize = 123; - let data = AccountStorageEntry::new(&paths[0], slot_expected, 0, size as u64); + let data = AccountStorageEntry::new( + &paths[0], + slot_expected, + 0, + size as u64, + AccountsFileProvider::AppendVec, + ); let arc = Arc::new(data); let storages = vec![arc]; @@ -10558,7 +10575,13 @@ pub mod tests { let (_temp_dirs, paths) = get_temp_accounts_paths(1).unwrap(); let slot_expected: Slot = 0; let size: usize = 123; - let mut data = AccountStorageEntry::new(&paths[0], slot_expected, 0, size as u64); + let mut data = AccountStorageEntry::new( + &paths[0], + slot_expected, + 0, + size as u64, + AccountsFileProvider::AppendVec, + ); let av = AccountsFile::AppendVec(AppendVec::new(&tf.path, true, 1024 * 1024)); data.accounts = av; @@ -10674,7 +10697,13 @@ pub mod tests { let (_temp_dirs, paths) = get_temp_accounts_paths(1).unwrap(); let slot_expected: Slot = 0; let size: usize = 123; - let mut data = AccountStorageEntry::new(&paths[0], slot_expected, 0, size as u64); + let mut data = AccountStorageEntry::new( + &paths[0], + slot_expected, + 0, + size as u64, + AccountsFileProvider::AppendVec, + ); let av = AccountsFile::AppendVec(AppendVec::new(&tf.path, true, 1024 * 1024)); data.accounts = av; @@ -10753,7 +10782,13 @@ pub mod tests { let (_temp_dirs, paths) = get_temp_accounts_paths(1).unwrap(); let file_size = account_data_size.unwrap_or(123) * 100 / fill_percentage; let size_aligned: usize = aligned_stored_size(file_size as usize); - let mut data = AccountStorageEntry::new(&paths[0], slot, id, size_aligned as u64); + let mut data = AccountStorageEntry::new( + &paths[0], + slot, + id, + size_aligned as u64, + AccountsFileProvider::AppendVec, + ); let av = AccountsFile::AppendVec(AppendVec::new( &tf.path, true, @@ -12863,6 +12898,7 @@ pub mod tests { slot_id_1, store1_id, store_file_size, + AccountsFileProvider::AppendVec, )); store1.alive_bytes.store(0, Ordering::Release); @@ -12876,6 +12912,7 @@ pub mod tests { slot_id_2, store2_id, store_file_size, + AccountsFileProvider::AppendVec, )); // The store2's alive_ratio is 0.5: as its page aligned alive size is 1 page. @@ -12892,6 +12929,7 @@ pub mod tests { slot_id_3, store3_id, store_file_size, + AccountsFileProvider::AppendVec, )); db.storage.insert(slot_id_1, Arc::clone(&store1)); @@ -12936,6 +12974,7 @@ pub mod tests { slot_id_1, store1_id, store_file_size, + AccountsFileProvider::AppendVec, )); store1.alive_bytes.store(0, Ordering::Release); db.storage.insert(slot_id_1, Arc::clone(&store1)); @@ -12948,6 +12987,7 @@ pub mod tests { slot_id_2, store2_id, store_file_size, + AccountsFileProvider::AppendVec, )); db.storage.insert(slot_id_2, Arc::clone(&store2)); @@ -12965,6 +13005,7 @@ pub mod tests { slot_id_3, store3_id, store_file_size, + AccountsFileProvider::AppendVec, )); // The store3's alive ratio is 1.0 as its page-aligned alive size is 2 pages @@ -13002,6 +13043,7 @@ pub mod tests { slot1, store1_id, store_file_size, + AccountsFileProvider::AppendVec, )); // store1 has 1 page-aligned alive bytes, its alive ratio is 1/4: 0.25 @@ -13020,6 +13062,7 @@ pub mod tests { slot2, store2_id, store_file_size, + AccountsFileProvider::AppendVec, )); // store2 has 2 page-aligned bytes, its alive ratio is 2/4: 0.5 @@ -15027,11 +15070,18 @@ pub mod tests { #[test] fn test_shrink_productive() { solana_logger::setup(); - let s1 = AccountStorageEntry::new(Path::new("."), 0, 0, 1024); + let s1 = + AccountStorageEntry::new(Path::new("."), 0, 0, 1024, AccountsFileProvider::AppendVec); let store = Arc::new(s1); assert!(!AccountsDb::is_shrinking_productive(0, &store)); - let s1 = AccountStorageEntry::new(Path::new("."), 0, 0, PAGE_SIZE * 4); + let s1 = AccountStorageEntry::new( + Path::new("."), + 0, + 0, + PAGE_SIZE * 4, + AccountsFileProvider::AppendVec, + ); let store = Arc::new(s1); store.add_account((3 * PAGE_SIZE as usize) - 1); store.add_account(10); @@ -15054,6 +15104,7 @@ pub mod tests { 0, 1, store_file_size, + AccountsFileProvider::AppendVec, )); match accounts.shrink_ratio { AccountShrinkThreshold::TotalSpace { shrink_ratio } => { @@ -17400,6 +17451,7 @@ pub mod tests { 0, 1, store_file_size, + AccountsFileProvider::AppendVec, )); let db = AccountsDb::new_single_for_tests(); let slot0 = 0; diff --git a/accounts-db/src/accounts_file.rs b/accounts-db/src/accounts_file.rs index 0cd11296ce50b3..251aedcd1de562 100644 --- a/accounts-db/src/accounts_file.rs +++ b/accounts-db/src/accounts_file.rs @@ -231,6 +231,24 @@ impl<'a> Iterator for AccountsFileIter<'a> { } } +/// An enum that creates AccountsFile instance with the specified format. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum AccountsFileProvider { + AppendVec, + HotStorage, +} + +impl AccountsFileProvider { + pub fn new_writable(&self, path: impl Into, file_size: u64) -> AccountsFile { + match self { + Self::AppendVec => { + AccountsFile::AppendVec(AppendVec::new(path, true, file_size as usize)) + } + Self::HotStorage => AccountsFile::TieredStorage(TieredStorage::new_writable(path)), + } + } +} + #[cfg(test)] pub mod tests { use crate::accounts_file::AccountsFile; diff --git a/accounts-db/src/sorted_storages.rs b/accounts-db/src/sorted_storages.rs index 72cf084a0896f7..f47dadb249a077 100644 --- a/accounts-db/src/sorted_storages.rs +++ b/accounts-db/src/sorted_storages.rs @@ -196,7 +196,7 @@ mod tests { super::*, crate::{ accounts_db::{AccountStorageEntry, AccountsFileId}, - accounts_file::AccountsFile, + accounts_file::{AccountsFile, AccountsFileProvider}, append_vec::AppendVec, }, std::sync::Arc, @@ -445,7 +445,13 @@ mod tests { let (_temp_dirs, paths) = crate::accounts_db::get_temp_accounts_paths(1).unwrap(); let size: usize = 123; let slot = 0; - let mut data = AccountStorageEntry::new(&paths[0], slot, id, size as u64); + let mut data = AccountStorageEntry::new( + &paths[0], + slot, + id, + size as u64, + AccountsFileProvider::AppendVec, + ); let av = AccountsFile::AppendVec(AppendVec::new(&tf.path, true, 1024 * 1024)); data.accounts = av;