forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 261
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
bank: add lightweight bank hash cache to reduce bank forks usage #3061
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ use { | |
crate::{ | ||
accounts_background_service::{AbsRequestSender, SnapshotRequest, SnapshotRequestKind}, | ||
bank::{epoch_accounts_hash_utils, Bank, SquashTiming}, | ||
bank_hash_cache::BankHashCache, | ||
installed_scheduler_pool::{ | ||
BankWithScheduler, InstalledSchedulerPoolArc, SchedulingContext, | ||
}, | ||
|
@@ -78,6 +79,9 @@ pub struct BankForks { | |
in_vote_only_mode: Arc<AtomicBool>, | ||
highest_slot_at_startup: Slot, | ||
scheduler_pool: Option<InstalledSchedulerPoolArc>, | ||
|
||
/// Lightweight cache for just the latest bank hash | ||
bank_hash_cache: Arc<RwLock<BankHashCache>>, | ||
} | ||
|
||
impl Index<u64> for BankForks { | ||
|
@@ -88,7 +92,9 @@ impl Index<u64> for BankForks { | |
} | ||
|
||
impl BankForks { | ||
pub fn new_rw_arc(root_bank: Bank) -> Arc<RwLock<Self>> { | ||
pub fn new_rw_arc(mut root_bank: Bank) -> Arc<RwLock<Self>> { | ||
let bank_hash_cache = Arc::new(RwLock::new(BankHashCache::default())); | ||
root_bank.set_bank_hash_cache(bank_hash_cache.clone()); | ||
let root_bank = Arc::new(root_bank); | ||
let root_slot = root_bank.slot(); | ||
|
||
|
@@ -100,16 +106,14 @@ impl BankForks { | |
|
||
let parents = root_bank.parents(); | ||
for parent in parents { | ||
if banks | ||
.insert( | ||
parent.slot(), | ||
BankWithScheduler::new_without_scheduler(parent.clone()), | ||
) | ||
.is_some() | ||
{ | ||
// All ancestors have already been inserted by another fork | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is leftover from when |
||
break; | ||
} | ||
banks.insert( | ||
parent.slot(), | ||
BankWithScheduler::new_without_scheduler(parent.clone()), | ||
); | ||
bank_hash_cache | ||
.write() | ||
.unwrap() | ||
.freeze(parent.slot(), parent.hash()); | ||
} | ||
|
||
let mut descendants = HashMap::<_, HashSet<_>>::new(); | ||
|
@@ -128,6 +132,7 @@ impl BankForks { | |
in_vote_only_mode: Arc::new(AtomicBool::new(false)), | ||
highest_slot_at_startup: 0, | ||
scheduler_pool: None, | ||
bank_hash_cache, | ||
})); | ||
|
||
root_bank.set_fork_graph_in_program_cache(Arc::downgrade(&bank_forks)); | ||
|
@@ -207,6 +212,10 @@ impl BankForks { | |
self.get(slot).map(|bank| bank.hash()) | ||
} | ||
|
||
pub fn bank_hash_cache(&self) -> Arc<RwLock<BankHashCache>> { | ||
self.bank_hash_cache.clone() | ||
} | ||
|
||
pub fn root_bank(&self) -> Arc<Bank> { | ||
self[self.root()].clone() | ||
} | ||
|
@@ -223,6 +232,7 @@ impl BankForks { | |
if self.root.load(Ordering::Relaxed) < self.highest_slot_at_startup { | ||
bank.set_check_program_modification_slot(true); | ||
} | ||
bank.set_bank_hash_cache(self.bank_hash_cache()); | ||
|
||
let bank = Arc::new(bank); | ||
let bank = if let Some(scheduler_pool) = &self.scheduler_pool { | ||
|
@@ -445,6 +455,10 @@ impl BankForks { | |
let dropped_banks_len = removed_banks.len(); | ||
|
||
let mut drop_parent_banks_time = Measure::start("set_root::drop_banks"); | ||
self.bank_hash_cache | ||
.write() | ||
.unwrap() | ||
.prune(removed_banks.iter().map(|bank| bank.slot())); | ||
drop(parents); | ||
drop_parent_banks_time.stop(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Lightweight cache tracking the bank hashes of replayed banks. | ||
//! This can be useful to avoid read-locking bank forks just to query the bank hash. | ||
|
||
use { | ||
solana_sdk::{clock::Slot, hash::Hash}, | ||
std::collections::BTreeMap, | ||
}; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct BankHashCache { | ||
hashes: BTreeMap<Slot, Hash>, | ||
} | ||
|
||
impl BankHashCache { | ||
/// Insert new frozen bank, returning the hash of the previously dumped bank if it exists | ||
pub fn freeze(&mut self, slot: Slot, hash: Hash) -> Option<Hash> { | ||
self.hashes.insert(slot, hash) | ||
} | ||
|
||
/// Returns the replayed bank hash of `slot`. | ||
/// If `slot` has been dumped, returns the previously replayed hash. | ||
pub fn bank_hash(&self, slot: Slot) -> Option<Hash> { | ||
self.hashes.get(&slot).copied() | ||
} | ||
|
||
/// Removes `slots` from the cache. Intended to be used with `BankForks::prune_non_rooted` | ||
pub fn prune<I>(&mut self, slots: I) | ||
where | ||
I: Iterator<Item = Slot>, | ||
{ | ||
for slot in slots { | ||
self.hashes.remove(&slot); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use { | ||
crate::{ | ||
bank::Bank, | ||
bank_forks::BankForks, | ||
genesis_utils::{create_genesis_config, GenesisConfigInfo}, | ||
}, | ||
solana_sdk::pubkey::Pubkey, | ||
}; | ||
|
||
#[test] | ||
fn test_bank_hash_cache() { | ||
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000); | ||
let bank0 = Bank::new_for_tests(&genesis_config); | ||
let slot = bank0.slot(); | ||
let bank_forks = BankForks::new_rw_arc(bank0); | ||
let bank_hash_cache = bank_forks.read().unwrap().bank_hash_cache(); | ||
bank_forks.read().unwrap()[slot].freeze(); | ||
assert_eq!( | ||
bank_hash_cache.read().unwrap().bank_hash(slot).unwrap(), | ||
bank_forks.read().unwrap()[slot].hash() | ||
); | ||
|
||
let bank0 = bank_forks.read().unwrap().get(slot).unwrap(); | ||
let slot = 10; | ||
let bank10 = Bank::new_from_parent(bank0, &Pubkey::new_unique(), slot); | ||
bank_forks.write().unwrap().insert(bank10); | ||
bank_forks.read().unwrap()[slot].freeze(); | ||
assert_eq!( | ||
bank_hash_cache.read().unwrap().bank_hash(slot).unwrap(), | ||
bank_forks.read().unwrap()[slot].hash() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
root_bank
is not frozen until afterBankForks
is created, so this is necessary.