From aef8692c8f245012fdc7f5e4838466653a8557b4 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Wed, 18 Jan 2023 09:10:56 -0600 Subject: [PATCH] remove SnapshotStorageOne (#29747) --- runtime/src/account_storage.rs | 8 +++---- runtime/src/accounts_db.rs | 25 ++++++++++----------- runtime/src/serde_snapshot.rs | 12 +++++----- runtime/src/serde_snapshot/tests.rs | 2 +- runtime/src/snapshot_minimizer.rs | 10 ++++----- runtime/src/snapshot_utils.rs | 12 +++++----- runtime/src/sorted_storages.rs | 35 ++++++++++++++++------------- 7 files changed, 54 insertions(+), 50 deletions(-) diff --git a/runtime/src/account_storage.rs b/runtime/src/account_storage.rs index de552f121a..35d19a004a 100644 --- a/runtime/src/account_storage.rs +++ b/runtime/src/account_storage.rs @@ -1,7 +1,7 @@ //! Manage the map of slot -> append vec use { - crate::accounts_db::{AccountStorageEntry, AppendVecId, SnapshotStorageOne}, + crate::accounts_db::{AccountStorageEntry, AppendVecId}, dashmap::DashMap, solana_sdk::clock::Slot, std::sync::Arc, @@ -10,7 +10,7 @@ use { #[derive(Clone, Debug)] pub struct AccountStorageReference { /// the single storage for a given slot - pub(crate) storage: SnapshotStorageOne, + pub(crate) storage: Arc, /// id can be read from 'storage', but it is an atomic read. /// id will never change while a storage is held, so we store it separately here for faster runtime lookup in 'get_account_storage_entry' pub(crate) id: AppendVecId, @@ -92,7 +92,7 @@ impl AccountStorage { /// remove the append vec at 'slot' /// returns the current contents - pub(crate) fn remove(&self, slot: &Slot) -> Option { + pub(crate) fn remove(&self, slot: &Slot) -> Option> { assert!(self.shrink_in_progress_map.is_empty()); self.map.remove(slot).map(|(_, entry)| entry.storage) } @@ -192,7 +192,7 @@ impl<'a> AccountStorageIter<'a> { } impl<'a> Iterator for AccountStorageIter<'a> { - type Item = (Slot, SnapshotStorageOne); + type Item = (Slot, Arc); fn next(&mut self) -> Option { if let Some(entry) = self.iter.next() { diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index b1df5c2051..5c838a87b5 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -600,10 +600,9 @@ impl<'a> MultiThreadProgress<'a> { /// An offset into the AccountsDb::storage vector pub type AtomicAppendVecId = AtomicU32; pub type AppendVecId = u32; -pub type SnapshotStorageOne = Arc; -pub type SnapshotStorages = Vec>; +pub type SnapshotStorages = Vec>>; /// exactly 1 append vec per slot -pub type SnapshotStoragesOne = Vec; +pub type SnapshotStoragesOne = Vec>; type AccountSlots = HashMap>; type AppendVecOffsets = HashMap>; @@ -1121,7 +1120,7 @@ impl RecycleStores { self.entries.iter() } - fn add_entries(&mut self, new_entries: Vec) { + fn add_entries(&mut self, new_entries: Vec>) { let now = Instant::now(); for new_entry in new_entries { self.total_bytes += new_entry.total_bytes(); @@ -1129,7 +1128,7 @@ impl RecycleStores { } } - fn expire_old_entries(&mut self) -> Vec { + fn expire_old_entries(&mut self) -> Vec> { let mut expired = vec![]; let now = Instant::now(); let mut expired_bytes = 0; @@ -3888,7 +3887,7 @@ impl AccountsDb { slot: Slot, add_dirty_stores: bool, shrink_in_progress: Option, - ) -> Vec { + ) -> Vec> { let mut dead_storages = Vec::default(); let mut not_retaining_store = |store: &Arc| { @@ -3913,7 +3912,7 @@ impl AccountsDb { pub(crate) fn drop_or_recycle_stores( &self, - dead_storages: Vec, + dead_storages: Vec>, stats: &ShrinkStats, ) { let mut recycle_stores_write_elapsed = Measure::start("recycle_stores_write_time"); @@ -5457,7 +5456,7 @@ impl AccountsDb { fn recycle_slot_stores( &self, total_removed_storage_entries: usize, - slot_stores: &[SnapshotStorageOne], + slot_stores: &[Arc], ) -> u64 { let mut recycle_stores_write_elapsed = Measure::start("recycle_stores_write_elapsed"); let mut recycle_stores = self.recycle_stores.write().unwrap(); @@ -6735,7 +6734,7 @@ impl AccountsDb { } /// iterate over a single storage, calling scanner on each item - fn scan_single_account_storage(storage: &SnapshotStorageOne, scanner: &mut S) + fn scan_single_account_storage(storage: &Arc, scanner: &mut S) where S: AppendVecScan, { @@ -6746,7 +6745,7 @@ impl AccountsDb { }); } - fn update_old_slot_stats(&self, stats: &HashStats, storage: Option<&SnapshotStorageOne>) { + fn update_old_slot_stats(&self, stats: &HashStats, storage: Option<&Arc>) { if let Some(storage) = storage { stats.roots_older_than_epoch.fetch_add(1, Ordering::Relaxed); let mut ancients = 0; @@ -6802,7 +6801,7 @@ impl AccountsDb { /// return true iff storage is valid for loading from cache fn hash_storage_info( hasher: &mut impl StdHasher, - storage: Option<&SnapshotStorageOne>, + storage: Option<&Arc>, slot: Slot, ) -> bool { if let Some(append_vec) = storage { @@ -8289,7 +8288,7 @@ impl AccountsDb { let store = std::mem::take(store).unwrap(); store.has_accounts().then_some((store, *slot)) }) - .collect::>() + .collect::, Slot)>>() }) .collect::>() }); @@ -9623,7 +9622,7 @@ pub mod tests { sample_storages_and_account_in_slot(1, accounts) } - fn get_storage_refs(input: &[SnapshotStorageOne]) -> SortedStorages { + fn get_storage_refs(input: &[Arc]) -> SortedStorages { SortedStorages::new(input) } diff --git a/runtime/src/serde_snapshot.rs b/runtime/src/serde_snapshot.rs index 2c75790bc1..8df27dd47c 100644 --- a/runtime/src/serde_snapshot.rs +++ b/runtime/src/serde_snapshot.rs @@ -3,7 +3,7 @@ use { accounts::Accounts, accounts_db::{ AccountShrinkThreshold, AccountStorageEntry, AccountsDb, AccountsDbConfig, AppendVecId, - AtomicAppendVecId, BankHashInfo, IndexGenerationInfo, SnapshotStorageOne, + AtomicAppendVecId, BankHashInfo, IndexGenerationInfo, }, accounts_hash::AccountsHash, accounts_index::AccountSecondaryIndexes, @@ -345,7 +345,7 @@ pub(crate) fn bank_to_stream( serde_style: SerdeStyle, stream: &mut BufWriter, bank: &Bank, - snapshot_storages: &[Vec], + snapshot_storages: &[Vec>], ) -> Result<(), Error> where W: Write, @@ -367,7 +367,7 @@ pub(crate) fn bank_to_stream_no_extra_fields( serde_style: SerdeStyle, stream: &mut BufWriter, bank: &Bank, - snapshot_storages: &[Vec], + snapshot_storages: &[Vec>], ) -> Result<(), Error> where W: Write, @@ -445,7 +445,7 @@ pub fn reserialize_bank_with_new_accounts_hash( struct SerializableBankAndStorage<'a, C> { bank: &'a Bank, - snapshot_storages: &'a [Vec], + snapshot_storages: &'a [Vec>], phantom: std::marker::PhantomData, } @@ -461,7 +461,7 @@ impl<'a, C: TypeContext<'a>> Serialize for SerializableBankAndStorage<'a, C> { #[cfg(test)] struct SerializableBankAndStorageNoExtra<'a, C> { bank: &'a Bank, - snapshot_storages: &'a [Vec], + snapshot_storages: &'a [Vec>], phantom: std::marker::PhantomData, } @@ -494,7 +494,7 @@ impl<'a, C> From> for SerializableBankA struct SerializableAccountsDb<'a, C> { accounts_db: &'a AccountsDb, slot: Slot, - account_storage_entries: &'a [Vec], + account_storage_entries: &'a [Vec>], phantom: std::marker::PhantomData, } diff --git a/runtime/src/serde_snapshot/tests.rs b/runtime/src/serde_snapshot/tests.rs index d91d2607ac..19a635aea5 100644 --- a/runtime/src/serde_snapshot/tests.rs +++ b/runtime/src/serde_snapshot/tests.rs @@ -146,7 +146,7 @@ fn accountsdb_to_stream( stream: &mut W, accounts_db: &AccountsDb, slot: Slot, - account_storage_entries: &[Vec], + account_storage_entries: &[Vec>], ) -> Result<(), Error> where W: Write, diff --git a/runtime/src/snapshot_minimizer.rs b/runtime/src/snapshot_minimizer.rs index 2cd0a20dd7..9b7726f94d 100644 --- a/runtime/src/snapshot_minimizer.rs +++ b/runtime/src/snapshot_minimizer.rs @@ -3,7 +3,7 @@ use { crate::{ accounts_db::{ - AccountsDb, GetUniqueAccountsResult, PurgeStats, SnapshotStorageOne, StoreReclaims, + AccountStorageEntry, AccountsDb, GetUniqueAccountsResult, PurgeStats, StoreReclaims, }, bank::Bank, builtins, static_ids, @@ -27,7 +27,7 @@ use { collections::HashSet, sync::{ atomic::{AtomicUsize, Ordering}, - Mutex, + Arc, Mutex, }, }, }; @@ -273,7 +273,7 @@ impl<'a> SnapshotMinimizer<'a> { fn process_snapshot_storages( &self, minimized_slot_set: DashSet, - ) -> (Vec, Vec) { + ) -> (Vec, Vec>) { let snapshot_storages = self .accounts_db() .get_snapshot_storages(..=self.starting_slot, None) @@ -301,8 +301,8 @@ impl<'a> SnapshotMinimizer<'a> { /// Creates new storage replacing `storages` that contains only accounts in `minimized_account_set`. fn filter_storage( &self, - storage: &SnapshotStorageOne, - dead_storages: &Mutex>, + storage: &Arc, + dead_storages: &Mutex>>, ) { let slot = storage.slot(); let GetUniqueAccountsResult { diff --git a/runtime/src/snapshot_utils.rs b/runtime/src/snapshot_utils.rs index 0ee9fa0caa..17e9f53cef 100644 --- a/runtime/src/snapshot_utils.rs +++ b/runtime/src/snapshot_utils.rs @@ -2,8 +2,8 @@ use { crate::{ account_storage::AccountStorageMap, accounts_db::{ - AccountShrinkThreshold, AccountsDbConfig, AtomicAppendVecId, - CalcAccountsHashDataSource, SnapshotStorageOne, SnapshotStoragesOne, + AccountShrinkThreshold, AccountStorageEntry, AccountsDbConfig, AtomicAppendVecId, + CalcAccountsHashDataSource, SnapshotStoragesOne, }, accounts_index::AccountSecondaryIndexes, accounts_update_notifier_interface::AccountsUpdateNotifier, @@ -847,7 +847,7 @@ where pub fn add_bank_snapshot( bank_snapshots_dir: impl AsRef, bank: &Bank, - snapshot_storages: &[SnapshotStorageOne], + snapshot_storages: &[Arc], snapshot_version: SnapshotVersion, ) -> Result { let mut add_snapshot_time = Measure::start("add-snapshot-ms"); @@ -908,11 +908,11 @@ pub fn add_bank_snapshot( }) } -/// serializing needs Vec>, but data structure at runtime is Vec +/// serializing needs Vec>>, but data structure at runtime is Vec> /// translates to what we need pub(crate) fn get_storages_to_serialize( - snapshot_storages: &[SnapshotStorageOne], -) -> Vec> { + snapshot_storages: &[Arc], +) -> Vec>> { snapshot_storages .iter() .map(|storage| vec![Arc::clone(storage)]) diff --git a/runtime/src/sorted_storages.rs b/runtime/src/sorted_storages.rs index 2529591f4c..927624431f 100644 --- a/runtime/src/sorted_storages.rs +++ b/runtime/src/sorted_storages.rs @@ -1,11 +1,12 @@ use { - crate::accounts_db::SnapshotStorageOne, + crate::accounts_db::AccountStorageEntry, log::*, solana_measure::measure::Measure, solana_sdk::clock::Slot, std::{ collections::HashMap, ops::{Bound, Range, RangeBounds}, + sync::Arc, }, }; @@ -16,7 +17,7 @@ pub struct SortedStorages<'a> { /// the actual storages /// A HashMap allows sparse storage and fast lookup of Slot -> Storage. /// We expect ~432k slots. - storages: HashMap, + storages: HashMap>, } impl<'a> SortedStorages<'a> { @@ -28,7 +29,7 @@ impl<'a> SortedStorages<'a> { } } - /// primary method of retrieving (Slot, SnapshotStorageOne) + /// primary method of retrieving (Slot, Arc) pub fn iter_range(&'a self, range: &R) -> SortedStoragesIter<'a> where R: RangeBounds, @@ -36,7 +37,7 @@ impl<'a> SortedStorages<'a> { SortedStoragesIter::new(self, range) } - fn get(&self, slot: Slot) -> Option<&SnapshotStorageOne> { + fn get(&self, slot: Slot) -> Option<&Arc> { self.storages.get(&slot).copied() } @@ -62,7 +63,7 @@ impl<'a> SortedStorages<'a> { // assumption: // source.slot() is unique from all other items in 'source' - pub fn new(source: &'a [SnapshotStorageOne]) -> Self { + pub fn new(source: &'a [Arc]) -> Self { let slots = source.iter().map(|storage| { storage.slot() // this must be unique. Will be enforced in new_with_slots }); @@ -70,10 +71,10 @@ impl<'a> SortedStorages<'a> { } /// create `SortedStorages` from 'source' iterator. - /// 'source' contains a SnapshotStorageOne and its associated slot + /// 'source' contains a Arc and its associated slot /// 'source' does not have to be sorted in any way, but is assumed to not have duplicate slot #s pub fn new_with_slots( - source: impl Iterator + Clone, + source: impl Iterator, Slot)> + Clone, // A slot used as a lower bound, but potentially smaller than the smallest slot in the given 'source' iterator min_slot: Option, // highest valid slot. Only matters if source array does not contain a slot >= max_slot_inclusive. @@ -142,7 +143,7 @@ pub struct SortedStoragesIter<'a> { } impl<'a> Iterator for SortedStoragesIter<'a> { - type Item = (Slot, Option<&'a SnapshotStorageOne>); + type Item = (Slot, Option<&'a Arc>); fn next(&mut self) -> Option { let slot = self.next_slot; @@ -194,13 +195,17 @@ pub mod tests { use { super::*, crate::{ - accounts_db::{AccountStorageEntry, AppendVecId, SnapshotStorageOne}, + accounts_db::{AccountStorageEntry, AppendVecId}, append_vec::AppendVec, }, std::sync::Arc, }; impl<'a> SortedStorages<'a> { - pub fn new_debug(source: &[(&'a SnapshotStorageOne, Slot)], min: Slot, len: usize) -> Self { + pub fn new_debug( + source: &[(&'a Arc, Slot)], + min: Slot, + len: usize, + ) -> Self { let mut storages = HashMap::default(); let range = Range { start: min, @@ -213,7 +218,7 @@ pub mod tests { Self { range, storages } } - pub fn new_for_tests(storages: &[&'a SnapshotStorageOne], slots: &[Slot]) -> Self { + pub fn new_for_tests(storages: &[&'a Arc], slots: &[Slot]) -> Self { assert_eq!(storages.len(), slots.len()); SortedStorages::new_with_slots( storages.iter().cloned().zip(slots.iter().cloned()), @@ -226,7 +231,7 @@ pub mod tests { #[test] fn test_sorted_storages_range_iter() { let storages = SortedStorages::empty(); - let check = |(slot, storages): (Slot, Option<&SnapshotStorageOne>)| { + let check = |(slot, storages): (Slot, Option<&Arc>)| { assert!(storages.is_none()); slot }; @@ -250,7 +255,7 @@ pub mod tests { // only item is slot 3 let s1 = create_sample_store(1); let storages = SortedStorages::new_for_tests(&[&s1], &[3]); - let check = |(slot, storages): (Slot, Option<&SnapshotStorageOne>)| { + let check = |(slot, storages): (Slot, Option<&Arc>)| { assert!( (slot != 3) ^ storages.is_some(), "slot: {slot}, storages: {storages:?}" @@ -286,7 +291,7 @@ pub mod tests { let store4 = create_sample_store(4); let storages = SortedStorages::new_for_tests(&[&store2, &store4], &[2, 4]); - let check = |(slot, storage): (Slot, Option<&SnapshotStorageOne>)| { + let check = |(slot, storage): (Slot, Option<&Arc>)| { assert!( (slot != 2 && slot != 4) ^ storage @@ -358,7 +363,7 @@ pub mod tests { ); } - fn create_sample_store(id: AppendVecId) -> SnapshotStorageOne { + fn create_sample_store(id: AppendVecId) -> Arc { let tf = crate::append_vec::test_utils::get_append_vec_path("create_sample_store"); let (_temp_dirs, paths) = crate::accounts_db::get_temp_accounts_paths(1).unwrap(); let size: usize = 123;