Skip to content

Commit

Permalink
[TieredStorage] enum-based AccountsFileProvider (solana-labs#457)
Browse files Browse the repository at this point in the history
* [TieredStorage] enum-based AccountsFileProvider

* derive traits

* remove refs

---------

Co-authored-by: jeff washington <jeff.washington@solana.com>
  • Loading branch information
yhchiang-sol and jeffwashington authored Apr 1, 2024
1 parent a5aee48 commit 8153c52
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
5 changes: 4 additions & 1 deletion accounts-db/src/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -353,6 +355,7 @@ pub(crate) mod tests {
slot,
id,
store_file_size,
AccountsFileProvider::AppendVec,
))
}
fn get_test_storage(&self) -> Arc<AccountStorageEntry> {
Expand Down
80 changes: 66 additions & 14 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -12863,6 +12898,7 @@ pub mod tests {
slot_id_1,
store1_id,
store_file_size,
AccountsFileProvider::AppendVec,
));
store1.alive_bytes.store(0, Ordering::Release);

Expand All @@ -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.
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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));

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -15054,6 +15104,7 @@ pub mod tests {
0,
1,
store_file_size,
AccountsFileProvider::AppendVec,
));
match accounts.shrink_ratio {
AccountShrinkThreshold::TotalSpace { shrink_ratio } => {
Expand Down Expand Up @@ -17400,6 +17451,7 @@ pub mod tests {
0,
1,
store_file_size,
AccountsFileProvider::AppendVec,
));
let db = AccountsDb::new_single_for_tests();
let slot0 = 0;
Expand Down
18 changes: 18 additions & 0 deletions accounts-db/src/accounts_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>, 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;
Expand Down
10 changes: 8 additions & 2 deletions accounts-db/src/sorted_storages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 8153c52

Please sign in to comment.