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

Add StatusCache::root_slot_deltas() and use it #26170

Merged
merged 2 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions core/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,17 @@ mod tests {
let last_bank_snapshot_info =
snapshot_utils::get_highest_bank_snapshot_pre(bank_snapshots_dir)
.expect("no bank snapshots found in path");
let slot_deltas = last_bank
.src
.status_cache
.read()
.unwrap()
.root_slot_deltas();
let accounts_package = AccountsPackage::new(
&last_bank,
&last_bank_snapshot_info,
bank_snapshots_dir,
last_bank.src.slot_deltas(&last_bank.src.roots()),
slot_deltas,
&snapshot_config.full_snapshot_archives_dir,
&snapshot_config.incremental_snapshot_archives_dir,
last_bank.get_snapshot_storages(None),
Expand Down Expand Up @@ -626,7 +632,13 @@ mod tests {
.get(snapshot_test_config.bank_forks.root())
.unwrap()
.src
.roots();
.status_cache
.read()
.unwrap()
.roots()
.iter()
.cloned()
.sorted();
assert!(slots_to_snapshot.into_iter().eq(expected_slots_to_snapshot));
}
}
Expand Down
16 changes: 16 additions & 0 deletions runtime/benches/status_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ fn bench_status_cache_slot_deltas(bencher: &mut Bencher) {

bencher.iter(|| test::black_box(status_cache.slot_deltas(&slots)));
}

#[bench]
fn bench_status_cache_root_slot_deltas(bencher: &mut Bencher) {
let mut status_cache = BankStatusCache::default();

// fill the status cache
let slots: Vec<_> = (42..).take(MAX_CACHE_ENTRIES).collect();
for slot in &slots {
for _ in 0..5 {
status_cache.insert(&Hash::new_unique(), Hash::new_unique(), *slot, Ok(()));
}
status_cache.add_root(*slot);
}

bencher.iter(|| test::black_box(status_cache.root_slot_deltas()));
}
18 changes: 0 additions & 18 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,24 +595,6 @@ pub struct StatusCacheRc {
pub status_cache: Arc<RwLock<BankStatusCache>>,
}

impl StatusCacheRc {
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
pub fn slot_deltas(&self, slots: &[Slot]) -> Vec<BankSlotDelta> {
let sc = self.status_cache.read().unwrap();
sc.slot_deltas(slots)
}

pub fn roots(&self) -> Vec<Slot> {
self.status_cache
.read()
.unwrap()
.roots()
.iter()
.cloned()
.sorted()
.collect()
}
}

pub type TransactionCheckResult = (Result<()>, Option<NoncePartial>);

pub struct TransactionResults {
Expand Down
13 changes: 9 additions & 4 deletions runtime/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,18 @@ impl BankForks {
{
let snapshot_root_bank = self.root_bank();
let root_slot = snapshot_root_bank.slot();
// Save off the status cache because these may get pruned if another
// `set_root()` is called before the snapshots package can be generated
let status_cache_slot_deltas = snapshot_root_bank
.src
.status_cache
.read()
.unwrap()
.root_slot_deltas();
if let Err(e) =
accounts_background_request_sender.send_snapshot_request(SnapshotRequest {
snapshot_root_bank,
// Save off the status cache because these may get pruned
// if another `set_root()` is called before the snapshots package
// can be generated
status_cache_slot_deltas: bank.src.slot_deltas(&bank.src.roots()),
status_cache_slot_deltas,
})
{
warn!(
Expand Down
6 changes: 4 additions & 2 deletions runtime/src/snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1949,11 +1949,12 @@ pub fn package_and_archive_full_snapshot(
maximum_full_snapshot_archives_to_retain: usize,
maximum_incremental_snapshot_archives_to_retain: usize,
) -> Result<FullSnapshotArchiveInfo> {
let slot_deltas = bank.src.status_cache.read().unwrap().root_slot_deltas();
let accounts_package = AccountsPackage::new(
bank,
bank_snapshot_info,
bank_snapshots_dir,
bank.src.slot_deltas(&bank.src.roots()),
slot_deltas,
&full_snapshot_archives_dir,
&incremental_snapshot_archives_dir,
snapshot_storages,
Expand Down Expand Up @@ -1998,11 +1999,12 @@ pub fn package_and_archive_incremental_snapshot(
maximum_full_snapshot_archives_to_retain: usize,
maximum_incremental_snapshot_archives_to_retain: usize,
) -> Result<IncrementalSnapshotArchiveInfo> {
let slot_deltas = bank.src.status_cache.read().unwrap().root_slot_deltas();
let accounts_package = AccountsPackage::new(
bank,
bank_snapshot_info,
bank_snapshots_dir,
bank.src.slot_deltas(&bank.src.roots()),
slot_deltas,
&full_snapshot_archives_dir,
&incremental_snapshot_archives_dir,
snapshot_storages,
Expand Down
20 changes: 17 additions & 3 deletions runtime/src/status_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<T: Serialize + Clone> Default for StatusCache<T> {
Self {
cache: HashMap::default(),
// 0 is always a root
roots: [0].iter().cloned().collect(),
roots: HashSet::from([0]),
slot_deltas: HashMap::default(),
}
}
Expand Down Expand Up @@ -230,7 +230,21 @@ impl<T: Serialize + Clone> StatusCache<T> {
(
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
*slot,
self.roots.contains(slot),
self.slot_deltas.get(slot).unwrap_or(&empty).clone(),
Arc::clone(self.slot_deltas.get(slot).unwrap_or(&empty)),
)
})
.collect()
}

/// Get the statuses for all the root slots
pub fn root_slot_deltas(&self) -> Vec<SlotDelta<T>> {
self.roots
.iter()
.map(|slot| {
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
(
*slot,
true,
self.slot_deltas.get(slot).cloned().unwrap_or_default(),
brooksprumo marked this conversation as resolved.
Show resolved Hide resolved
)
})
.collect()
Expand Down Expand Up @@ -450,7 +464,7 @@ mod tests {
for i in 0..(MAX_CACHE_ENTRIES + 1) {
status_cache.add_root(i as u64);
}
let slots: Vec<_> = (0_u64..MAX_CACHE_ENTRIES as u64 + 1).collect();
let slots: Vec<_> = (0..MAX_CACHE_ENTRIES as u64 + 1).collect();
assert_eq!(status_cache.slot_deltas.len(), 1);
assert!(status_cache.slot_deltas.get(&1).is_some());
let slot_deltas = status_cache.slot_deltas(&slots);
Expand Down