Skip to content

Commit

Permalink
refactor shrink candidate result struct
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Oct 19, 2023
1 parent 2d4a96a commit 30c2567
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,11 @@ pub struct PubkeyHashAccount {
pub account: AccountSharedData,
}

struct SelectShrinkCandidateResult {
pub to_shrink: HashMap<Slot, Arc<AccountStorageEntry>>,
pub next_batch: ShrinkCandidates,
}

impl AccountsDb {
pub const DEFAULT_ACCOUNTS_HASH_CACHE_DIR: &'static str = "accounts_hash_cache";

Expand Down Expand Up @@ -4287,19 +4292,17 @@ impl AccountsDb {
self.storage.all_slots()
}

/// Given the input `ShrinkCandidates`, this function sorts the stores by their alive ratio
/// in increasing order with the most sparse entries in the front. It will then simulate the
/// shrinking by working on the most sparse entries first and if the overall alive ratio is
/// achieved, it will stop and return:
/// first tuple element: the filtered-down candidates and
/// second duple element: the candidates which
/// are skipped in this round and might be eligible for the future shrink.
/// Given the input `ShrinkCandidates`, this function selects the stores by their shrinking byte-savings
/// in decreasing order to raise overall alive ratio to meet with the desired `shrink_ratio`.
/// Returns SelectShrinkCandidateResult:
/// - the candidates to shrink in this pass
/// - the remaining candidates to retry in next pass
fn select_candidates_by_total_usage(
&self,
shrink_slots: &ShrinkCandidates,
shrink_ratio: f64,
oldest_non_ancient_slot: Option<Slot>,
) -> (HashMap<Slot, Arc<AccountStorageEntry>>, ShrinkCandidates) {
) -> SelectShrinkCandidateResult {
struct StoreUsageInfo {
slot: Slot,
alive_ratio: f64,
Expand Down Expand Up @@ -4385,7 +4388,10 @@ impl AccountsDb {
.map(|s| s.slot)
.collect();

(shrink_slots, shrink_slots_next_batch)
SelectShrinkCandidateResult {
to_shrink: shrink_slots,
next_batch: shrink_slots_next_batch,
}
}

fn get_roots_less_than(&self, slot: Slot) -> Vec<Slot> {
Expand Down Expand Up @@ -4778,13 +4784,15 @@ impl AccountsDb {

let (shrink_slots, shrink_slots_next_batch) = {
if let AccountShrinkThreshold::TotalSpace { shrink_ratio } = self.shrink_ratio {
let (shrink_slots, shrink_slots_next_batch) = self
.select_candidates_by_total_usage(
&shrink_candidates_slots,
shrink_ratio,
self.ancient_append_vec_offset
.map(|_| oldest_non_ancient_slot),
);
let SelectShrinkCandidateResult {
to_shrink: shrink_slots,
next_batch: shrink_slots_next_batch,
} = self.select_candidates_by_total_usage(
&shrink_candidates_slots,
shrink_ratio,
self.ancient_append_vec_offset
.map(|_| oldest_non_ancient_slot),
);
(shrink_slots, Some(shrink_slots_next_batch))
} else {
(
Expand Down Expand Up @@ -13382,8 +13390,10 @@ pub mod tests {
let candidates = ShrinkCandidates::default();
let db = AccountsDb::new_single_for_tests();

let (selected_candidates, next_candidates) =
db.select_candidates_by_total_usage(&candidates, DEFAULT_ACCOUNTS_SHRINK_RATIO, None);
let SelectShrinkCandidateResult {
to_shrink: selected_candidates,
next_batch: next_candidates,
} = db.select_candidates_by_total_usage(&candidates, DEFAULT_ACCOUNTS_SHRINK_RATIO, None);

assert_eq!(0, selected_candidates.len());
assert_eq!(0, next_candidates.len());
Expand Down Expand Up @@ -13454,8 +13464,10 @@ pub mod tests {
// The target ratio is also set to larger than store2's alive ratio: 0.5 so that it would be added
// to the candidates list for next round.
let target_alive_ratio = 0.6;
let (selected_candidates, next_candidates) =
db.select_candidates_by_total_usage(&candidates, target_alive_ratio, None);
let SelectShrinkCandidateResult {
to_shrink: selected_candidates,
next_batch: next_candidates,
} = db.select_candidates_by_total_usage(&candidates, target_alive_ratio, None);
assert_eq!(1, selected_candidates.len());
assert!(selected_candidates.contains(&slot_id_1));
assert_eq!(1, next_candidates.len());
Expand Down Expand Up @@ -13520,8 +13532,10 @@ pub mod tests {

// Set the target ratio to default (0.8), both store1 and store2 must be selected and store3 is ignored.
let target_alive_ratio = DEFAULT_ACCOUNTS_SHRINK_RATIO;
let (selected_candidates, next_candidates) =
db.select_candidates_by_total_usage(&candidates, target_alive_ratio, None);
let SelectShrinkCandidateResult {
to_shrink: selected_candidates,
next_batch: next_candidates,
} = db.select_candidates_by_total_usage(&candidates, target_alive_ratio, None);
assert_eq!(2, selected_candidates.len());
assert!(selected_candidates.contains(&slot_id_1));
assert!(selected_candidates.contains(&slot_id_2));
Expand Down Expand Up @@ -13577,7 +13591,10 @@ pub mod tests {
for newest_ancient_slot in [None, Some(slot1), Some(slot2)] {
// Set the target ratio to default (0.8), both stores from the two different slots must be selected.
let target_alive_ratio = DEFAULT_ACCOUNTS_SHRINK_RATIO;
let (selected_candidates, next_candidates) = db.select_candidates_by_total_usage(
let SelectShrinkCandidateResult {
to_shrink: selected_candidates,
next_batch: next_candidates,
} = db.select_candidates_by_total_usage(
&candidates,
target_alive_ratio,
newest_ancient_slot.map(|newest_ancient_slot| newest_ancient_slot + 1),
Expand Down

0 comments on commit 30c2567

Please sign in to comment.