Skip to content

Commit

Permalink
Shares accounts hash cache data between full and incremental (#33164)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6298c6c)

# Conflicts:
#	runtime/src/accounts_db.rs
#	runtime/src/cache_hash_data.rs
  • Loading branch information
brooksprumo authored and mergify[bot] committed Sep 11, 2023
1 parent 2cbbf4b commit f2e100c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
65 changes: 58 additions & 7 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,9 +1396,13 @@ pub struct AccountsDb {
/// Set of storage paths to pick from
pub(crate) paths: Vec<PathBuf>,

<<<<<<< HEAD:runtime/src/accounts_db.rs
/// Directories for account hash calculations, within base_working_path
full_accounts_hash_cache_path: PathBuf,
incremental_accounts_hash_cache_path: PathBuf,
=======
accounts_hash_cache_path: PathBuf,
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs
transient_accounts_hash_cache_path: PathBuf,

// used by tests
Expand Down Expand Up @@ -2374,6 +2378,19 @@ impl AccountsDb {
(cache_path, Some(temp_dir))
};

<<<<<<< HEAD:runtime/src/accounts_db.rs
=======
let accounts_hash_cache_path = accounts_hash_cache_path.unwrap_or_else(|| {
let accounts_hash_cache_path =
base_working_path.join(Self::DEFAULT_ACCOUNTS_HASH_CACHE_DIR);
if !accounts_hash_cache_path.exists() {
fs_err::create_dir(&accounts_hash_cache_path)
.expect("create accounts hash cache dir");
}
accounts_hash_cache_path
});

>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs
let mut bank_hash_stats = HashMap::new();
bank_hash_stats.insert(0, BankHashStats::default());

Expand Down Expand Up @@ -2402,10 +2419,17 @@ impl AccountsDb {
write_cache_limit_bytes: None,
write_version: AtomicU64::new(0),
paths: vec![],
<<<<<<< HEAD:runtime/src/accounts_db.rs
full_accounts_hash_cache_path: accounts_hash_cache_path.join("full"),
incremental_accounts_hash_cache_path: accounts_hash_cache_path.join("incremental"),
transient_accounts_hash_cache_path: accounts_hash_cache_path.join("transient"),
temp_accounts_hash_cache_path,
=======
base_working_path,
base_working_temp_dir,
transient_accounts_hash_cache_path: accounts_hash_cache_path.join("transient"),
accounts_hash_cache_path,
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs
shrink_paths: RwLock::new(None),
temp_paths: None,
file_size: DEFAULT_FILE_SIZE,
Expand Down Expand Up @@ -7517,18 +7541,20 @@ impl AccountsDb {
fn get_cache_hash_data(
accounts_hash_cache_path: PathBuf,
config: &CalcAccountsHashConfig<'_>,
kind: CalcAccountsHashKind,
slot: Slot,
) -> CacheHashData {
if !config.store_detailed_debug_info_on_failure {
CacheHashData::new(accounts_hash_cache_path)
let accounts_hash_cache_path = if !config.store_detailed_debug_info_on_failure {
accounts_hash_cache_path
} else {
// this path executes when we are failing with a hash mismatch
let failed_dir = accounts_hash_cache_path
.join("failed_calculate_accounts_hash_cache")
.join(slot.to_string());
let _ = std::fs::remove_dir_all(&failed_dir);
CacheHashData::new(failed_dir)
}
_ = std::fs::remove_dir_all(&failed_dir);
failed_dir
};
CacheHashData::new(accounts_hash_cache_path, kind == CalcAccountsHashKind::Full)
}

// modeled after calculate_accounts_delta_hash
Expand All @@ -7543,8 +7569,12 @@ impl AccountsDb {
config,
storages,
stats,
<<<<<<< HEAD:runtime/src/accounts_db.rs
CalcAccountsHashFlavor::Full,
self.full_accounts_hash_cache_path.clone(),
=======
CalcAccountsHashKind::Full,
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs
)?;
let AccountsHashEnum::Full(accounts_hash) = accounts_hash else {
panic!("calculate_accounts_hash_from_storages must return a FullAccountsHash");
Expand All @@ -7571,8 +7601,12 @@ impl AccountsDb {
config,
storages,
stats,
<<<<<<< HEAD:runtime/src/accounts_db.rs
CalcAccountsHashFlavor::Incremental,
self.incremental_accounts_hash_cache_path.clone(),
=======
CalcAccountsHashKind::Incremental,
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs
)?;
let AccountsHashEnum::Incremental(incremental_accounts_hash) = accounts_hash else {
panic!("calculate_incremental_accounts_hash must return an IncrementalAccountsHash");
Expand All @@ -7585,18 +7619,35 @@ impl AccountsDb {
config: &CalcAccountsHashConfig<'_>,
storages: &SortedStorages<'_>,
mut stats: HashStats,
<<<<<<< HEAD:runtime/src/accounts_db.rs
flavor: CalcAccountsHashFlavor,
accounts_hash_cache_path: PathBuf,
) -> Result<(AccountsHashEnum, u64), AccountsHashVerificationError> {
=======
kind: CalcAccountsHashKind,
) -> Result<(AccountsHashKind, u64), AccountsHashVerificationError> {
let total_time = Measure::start("");
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs
let _guard = self.active_stats.activate(ActiveStatItem::Hash);
stats.oldest_root = storages.range().start;

self.mark_old_slots_as_dirty(storages, config.epoch_schedule.slots_per_epoch, &mut stats);

let slot = storages.max_slot_inclusive();
let use_bg_thread_pool = config.use_bg_thread_pool;
let accounts_hash_cache_path = self.accounts_hash_cache_path.clone();
let scan_and_hash = || {
<<<<<<< HEAD:runtime/src/accounts_db.rs
let cache_hash_data = Self::get_cache_hash_data(accounts_hash_cache_path, config, slot);
=======
let (cache_hash_data, cache_hash_data_us) = measure_us!(Self::get_cache_hash_data(
accounts_hash_cache_path,
config,
kind,
slot
));
stats.cache_hash_data_us += cache_hash_data_us;
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/accounts_db.rs

let bounds = Range {
start: 0,
Expand Down Expand Up @@ -9592,7 +9643,7 @@ pub mod tests {
let temp_dir = TempDir::new().unwrap();
let accounts_hash_cache_path = temp_dir.path().to_path_buf();
self.scan_snapshot_stores_with_cache(
&CacheHashData::new(accounts_hash_cache_path),
&CacheHashData::new(accounts_hash_cache_path, true),
storage,
stats,
bins,
Expand Down Expand Up @@ -10691,7 +10742,7 @@ pub mod tests {
};

let result = accounts_db.scan_account_storage_no_bank(
&CacheHashData::new(accounts_hash_cache_path),
&CacheHashData::new(accounts_hash_cache_path, true),
&CalcAccountsHashConfig::default(),
&get_storage_refs(&[storage]),
test_scan,
Expand Down
28 changes: 25 additions & 3 deletions runtime/src/cache_hash_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,48 @@ impl CacheHashDataFile {
}
}

pub type PreExistingCacheFiles = HashSet<PathBuf>;
pub struct CacheHashData {
cache_dir: PathBuf,
<<<<<<< HEAD:runtime/src/cache_hash_data.rs
pre_existing_cache_files: Arc<Mutex<PreExistingCacheFiles>>,
pub stats: Arc<Mutex<CacheHashDataStats>>,
=======
pre_existing_cache_files: Arc<Mutex<HashSet<PathBuf>>>,
should_delete_old_cache_files_on_drop: bool,
pub stats: Arc<CacheHashDataStats>,
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/cache_hash_data.rs
}

impl Drop for CacheHashData {
fn drop(&mut self) {
<<<<<<< HEAD:runtime/src/cache_hash_data.rs
self.delete_old_cache_files();
self.stats.lock().unwrap().report();
=======
if self.should_delete_old_cache_files_on_drop {
self.delete_old_cache_files();
}
self.stats.report();
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/cache_hash_data.rs
}
}

impl CacheHashData {
pub fn new(cache_dir: PathBuf) -> CacheHashData {
pub fn new(cache_dir: PathBuf, should_delete_old_cache_files_on_drop: bool) -> CacheHashData {
std::fs::create_dir_all(&cache_dir).unwrap_or_else(|err| {
panic!("error creating cache dir {}: {err}", cache_dir.display())
});

let result = CacheHashData {
cache_dir,
<<<<<<< HEAD:runtime/src/cache_hash_data.rs
pre_existing_cache_files: Arc::new(Mutex::new(PreExistingCacheFiles::default())),
stats: Arc::new(Mutex::new(CacheHashDataStats::default())),
=======
pre_existing_cache_files: Arc::new(Mutex::new(HashSet::default())),
should_delete_old_cache_files_on_drop,
stats: Arc::default(),
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/cache_hash_data.rs
};

result.get_cache_files();
Expand Down Expand Up @@ -270,6 +288,10 @@ impl CacheHashData {
stats.total_entries = entries;
stats.cache_file_size += capacity as usize;

<<<<<<< HEAD:runtime/src/cache_hash_data.rs
=======
fn pre_existing_cache_file_will_be_used(&self, file_name: impl AsRef<Path>) {
>>>>>>> 6298c6c31e (Shares accounts hash cache data between full and incremental (#33164)):accounts-db/src/cache_hash_data.rs
self.pre_existing_cache_files
.lock()
.unwrap()
Expand Down Expand Up @@ -386,7 +408,7 @@ pub mod tests {
data_this_pass.push(this_bin_data);
}
}
let cache = CacheHashData::new(cache_dir.clone());
let cache = CacheHashData::new(cache_dir.clone(), true);
let file_name = PathBuf::from("test");
cache.save(&file_name, &data_this_pass).unwrap();
cache.get_cache_files();
Expand Down

0 comments on commit f2e100c

Please sign in to comment.