Make accounts file clean faster (#11071)

This commit is contained in:
sakridge
2020-07-15 09:37:40 -07:00
committed by GitHub
parent 8548c3a7eb
commit 7fe870ba48

View File

@ -32,6 +32,7 @@ use solana_ledger::{
leader_schedule::FixedSchedule, leader_schedule::FixedSchedule,
leader_schedule_cache::LeaderScheduleCache, leader_schedule_cache::LeaderScheduleCache,
}; };
use solana_measure::measure::Measure;
use solana_metrics::datapoint_info; use solana_metrics::datapoint_info;
use solana_runtime::{ use solana_runtime::{
bank::Bank, bank::Bank,
@ -208,9 +209,13 @@ impl Validator {
} }
} }
info!("Cleaning accounts paths..");
let mut start = Measure::start("clean_accounts_paths");
for accounts_path in &config.account_paths { for accounts_path in &config.account_paths {
cleanup_accounts_path(accounts_path); cleanup_accounts_path(accounts_path);
} }
start.stop();
info!("done. {}", start);
let mut validator_exit = ValidatorExit::default(); let mut validator_exit = ValidatorExit::default();
let exit = Arc::new(AtomicBool::new(false)); let exit = Arc::new(AtomicBool::new(false));
@ -991,30 +996,13 @@ fn get_stake_percent_in_gossip(bank: &Bank, cluster_info: &ClusterInfo, log: boo
online_stake * 100 / total_activated_stake online_stake * 100 / total_activated_stake
} }
fn cleanup_accounts_dir_entry(path: std::fs::DirEntry, accounts_file_regex: &regex::Regex) {
if let Ok(file_type) = path.file_type() {
if file_type.is_file() {
if let Ok(file_name) = path.file_name().into_string() {
if accounts_file_regex.is_match(&file_name) {
if let Err(e) = std::fs::remove_file(path.path()) {
info!("Couldn't delete file: {:?} error: {:?}", path, e);
}
}
}
}
}
}
// Cleanup anything that looks like an accounts append-vec // Cleanup anything that looks like an accounts append-vec
fn cleanup_accounts_path(account_path: &std::path::Path) { fn cleanup_accounts_path(account_path: &std::path::Path) {
use regex::Regex; if std::fs::remove_dir_all(account_path).is_err() {
let accounts_file_regex = Regex::new(r"(\d+).(\d+)").unwrap(); warn!(
if let Ok(dir_entries) = std::fs::read_dir(&account_path) { "encountered error removing accounts path: {:?}",
for entry in dir_entries { account_path
if let Ok(path) = entry { );
cleanup_accounts_dir_entry(path, &accounts_file_regex);
}
}
} }
} }
@ -1196,20 +1184,4 @@ mod tests {
rpc_override_health_check rpc_override_health_check
)); ));
} }
#[test]
fn accounts_clean() {
use std::fs::File;
let temp_dir = tempfile::tempdir_in("farf").unwrap();
let temp_path = temp_dir.path();
{
let _file1 = File::create(temp_path.join("foo.txt")).unwrap();
let _file2 = File::create(temp_path.join("123.2222")).unwrap();
}
std::fs::create_dir(temp_path.join("12.088")).unwrap();
cleanup_accounts_path(temp_dir.path());
assert!(File::open(temp_path.join("foo.txt")).is_ok());
assert!(File::open(temp_path.join("123.2222")).is_err());
assert!(std::fs::read_dir(temp_path.join("12.088")).is_ok());
}
} }