Skip to content

Commit f58daaf

Browse files
brooksprumomircea-c
authored andcommitted
Refactors AccountsBackgroundService if a snapshot request was not handled (anza-xyz#6418)
1 parent 2f195b2 commit f58daaf

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

runtime/src/accounts_background_service.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub struct SnapshotRequestHandler {
143143
}
144144

145145
impl SnapshotRequestHandler {
146-
// Returns the latest requested snapshot block height and storages
146+
// Returns the latest requested snapshot slot and storages
147147
#[allow(clippy::type_complexity)]
148148
pub fn handle_snapshot_requests(
149149
&self,
@@ -492,7 +492,7 @@ pub struct AbsRequestHandlers {
492492
}
493493

494494
impl AbsRequestHandlers {
495-
// Returns the latest requested snapshot block height, if one exists
495+
// Returns the latest requested snapshot slot, if one exists
496496
#[allow(clippy::type_complexity)]
497497
pub fn handle_snapshot_requests(
498498
&self,
@@ -629,26 +629,38 @@ impl AccountsBackgroundService {
629629
break;
630630
}
631631
}
632-
} else if previous_clean_time.elapsed() > CLEAN_INTERVAL {
633-
// Note that the flush will do an internal clean of the
634-
// cache up to bank.slot(), so should be safe as long
635-
// as any later snapshots that are taken are of
636-
// slots >= bank.slot()
637-
bank.force_flush_accounts_cache();
638-
bank.clean_accounts();
639-
last_cleaned_slot = bank.slot();
640-
previous_clean_time = Instant::now();
641-
bank.shrink_ancient_slots();
642-
bank.shrink_candidate_slots();
643-
previous_shrink_time = Instant::now();
644632
} else {
645-
// Note that the flush will do an internal clean of the
646-
// cache up to bank.slot(), so should be safe as long
647-
// as any later snapshots that are taken are of
648-
// slots >= bank.slot()
649-
bank.flush_accounts_cache_if_needed();
633+
// we didn't handle a snapshot request, so do flush/clean/shrink
634+
635+
// see the comments in Bank::clean_accounts() for why we sub 1 here
636+
let max_clean_slot_inclusive = bank.slot().saturating_sub(1);
637+
638+
let duration_since_previous_clean = previous_clean_time.elapsed();
639+
let should_clean = duration_since_previous_clean > CLEAN_INTERVAL;
640+
641+
// if we're cleaning, then force flush, otherwise be lazy
642+
let force_flush = should_clean;
643+
bank.rc
644+
.accounts
645+
.accounts_db
646+
.flush_accounts_cache(force_flush, Some(max_clean_slot_inclusive));
647+
648+
if should_clean {
649+
bank.clean_accounts();
650+
last_cleaned_slot = max_clean_slot_inclusive;
651+
previous_clean_time = Instant::now();
652+
}
653+
650654
let duration_since_previous_shrink = previous_shrink_time.elapsed();
651-
if duration_since_previous_shrink > SHRINK_INTERVAL {
655+
let should_shrink = duration_since_previous_shrink > SHRINK_INTERVAL;
656+
// To avoid pathological interactions between the clean and shrink
657+
// timers, call shrink for either should_shrink or should_clean.
658+
if should_shrink || should_clean {
659+
if should_clean {
660+
// We used to only squash (aka shrink ancients) when we also
661+
// cleaned, so keep that same behavior here for now.
662+
bank.shrink_ancient_slots();
663+
}
652664
bank.shrink_candidate_slots();
653665
previous_shrink_time = Instant::now();
654666
}

0 commit comments

Comments
 (0)