Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit b1e3780

Browse files
v1.18: Adds cache hash data deletion policy enum (backport of #34956) (#34962)
Adds cache hash data deletion policy enum (#34956) (cherry picked from commit e155d9c) Co-authored-by: Brooks <[email protected]>
1 parent 7903866 commit b1e3780

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

accounts-db/src/accounts_db.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ use {
5858
append_vec::{
5959
aligned_stored_size, AppendVec, APPEND_VEC_MMAPPED_FILES_OPEN, STORE_META_OVERHEAD,
6060
},
61-
cache_hash_data::{CacheHashData, CacheHashDataFileReference},
61+
cache_hash_data::{
62+
CacheHashData, CacheHashDataFileReference, DeletionPolicy as CacheHashDeletionPolicy,
63+
},
6264
contains::Contains,
6365
epoch_accounts_hash::EpochAccountsHashManager,
6466
in_mem_accounts_index::StartupStats,
@@ -7572,10 +7574,13 @@ impl AccountsDb {
75727574
_ = std::fs::remove_dir_all(&failed_dir);
75737575
failed_dir
75747576
};
7575-
CacheHashData::new(
7576-
accounts_hash_cache_path,
7577-
(kind == CalcAccountsHashKind::Incremental).then_some(storages_start_slot),
7578-
)
7577+
let deletion_policy = match kind {
7578+
CalcAccountsHashKind::Full => CacheHashDeletionPolicy::AllUnused,
7579+
CalcAccountsHashKind::Incremental => {
7580+
CacheHashDeletionPolicy::UnusedAtLeast(storages_start_slot)
7581+
}
7582+
};
7583+
CacheHashData::new(accounts_hash_cache_path, deletion_policy)
75797584
}
75807585

75817586
// modeled after calculate_accounts_delta_hash
@@ -9798,7 +9803,7 @@ pub mod tests {
97989803
let temp_dir = TempDir::new().unwrap();
97999804
let accounts_hash_cache_path = temp_dir.path().to_path_buf();
98009805
self.scan_snapshot_stores_with_cache(
9801-
&CacheHashData::new(accounts_hash_cache_path, None),
9806+
&CacheHashData::new(accounts_hash_cache_path, CacheHashDeletionPolicy::AllUnused),
98029807
storage,
98039808
stats,
98049809
bins,
@@ -10866,7 +10871,7 @@ pub mod tests {
1086610871
};
1086710872

1086810873
let result = accounts_db.scan_account_storage_no_bank(
10869-
&CacheHashData::new(accounts_hash_cache_path, None),
10874+
&CacheHashData::new(accounts_hash_cache_path, CacheHashDeletionPolicy::AllUnused),
1087010875
&CalcAccountsHashConfig::default(),
1087110876
&get_storage_refs(&[storage]),
1087210877
test_scan,

accounts-db/src/cache_hash_data.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ impl CacheHashDataFile {
193193
pub(crate) struct CacheHashData {
194194
cache_dir: PathBuf,
195195
pre_existing_cache_files: Arc<Mutex<HashSet<PathBuf>>>,
196-
/// Decides which old cache files to delete. See `delete_old_cache_files()` for more info.
197-
storages_start_slot: Option<Slot>,
196+
deletion_policy: DeletionPolicy,
198197
pub stats: Arc<CacheHashDataStats>,
199198
}
200199

@@ -206,15 +205,15 @@ impl Drop for CacheHashData {
206205
}
207206

208207
impl CacheHashData {
209-
pub(crate) fn new(cache_dir: PathBuf, storages_start_slot: Option<Slot>) -> CacheHashData {
208+
pub(crate) fn new(cache_dir: PathBuf, deletion_policy: DeletionPolicy) -> CacheHashData {
210209
std::fs::create_dir_all(&cache_dir).unwrap_or_else(|err| {
211210
panic!("error creating cache dir {}: {err}", cache_dir.display())
212211
});
213212

214213
let result = CacheHashData {
215214
cache_dir,
216215
pre_existing_cache_files: Arc::new(Mutex::new(HashSet::default())),
217-
storages_start_slot,
216+
deletion_policy,
218217
stats: Arc::default(),
219218
};
220219

@@ -229,21 +228,24 @@ impl CacheHashData {
229228
let mut old_cache_files =
230229
std::mem::take(&mut *self.pre_existing_cache_files.lock().unwrap());
231230

232-
// If `storages_start_slot` is None, we're doing a full accounts hash calculation, and thus
233-
// all unused cache files can be deleted.
234-
// If `storages_start_slot` is Some, we're doing an incremental accounts hash calculation,
235-
// and we only want to delete the unused cache files *that IAH considered*.
236-
if let Some(storages_start_slot) = self.storages_start_slot {
237-
old_cache_files.retain(|old_cache_file| {
238-
let Some(parsed_filename) = parse_filename(old_cache_file) else {
239-
// if parsing the cache filename fails, we *do* want to delete it
240-
return true;
241-
};
242-
243-
// if the old cache file is in the incremental accounts hash calculation range,
244-
// then delete it
245-
parsed_filename.slot_range_start >= storages_start_slot
246-
});
231+
match self.deletion_policy {
232+
DeletionPolicy::AllUnused => {
233+
// no additional work to do here; we will delete everything in `old_cache_files`
234+
}
235+
DeletionPolicy::UnusedAtLeast(storages_start_slot) => {
236+
// when calculating an incremental accounts hash, we only want to delete the unused
237+
// cache files *that IAH considered*
238+
old_cache_files.retain(|old_cache_file| {
239+
let Some(parsed_filename) = parse_filename(old_cache_file) else {
240+
// if parsing the cache filename fails, we *do* want to delete it
241+
return true;
242+
};
243+
244+
// if the old cache file is in the incremental accounts hash calculation range,
245+
// then delete it
246+
parsed_filename.slot_range_start >= storages_start_slot
247+
});
248+
}
247249
}
248250

249251
if !old_cache_files.is_empty() {
@@ -410,6 +412,19 @@ fn parse_filename(cache_filename: impl AsRef<Path>) -> Option<ParsedFilename> {
410412
})
411413
}
412414

415+
/// Decides which old cache files to delete
416+
///
417+
/// See `delete_old_cache_files()` for more info.
418+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
419+
pub enum DeletionPolicy {
420+
/// Delete *all* the unused cache files
421+
/// Should be used when calculating full accounts hash
422+
AllUnused,
423+
/// Delete *only* the unused cache files with starting slot range *at least* this slot
424+
/// Should be used when calculating incremental accounts hash
425+
UnusedAtLeast(Slot),
426+
}
427+
413428
#[cfg(test)]
414429
mod tests {
415430
use {super::*, crate::accounts_hash::AccountHash, rand::Rng};
@@ -477,7 +492,8 @@ mod tests {
477492
data_this_pass.push(this_bin_data);
478493
}
479494
}
480-
let cache = CacheHashData::new(cache_dir.clone(), None);
495+
let cache =
496+
CacheHashData::new(cache_dir.clone(), DeletionPolicy::AllUnused);
481497
let file_name = PathBuf::from("test");
482498
cache.save(&file_name, &data_this_pass).unwrap();
483499
cache.get_cache_files();

0 commit comments

Comments
 (0)