2020-03-23 08:50:23 -07:00
|
|
|
// Service to clean up dead slots in accounts_db
|
|
|
|
//
|
|
|
|
// This can be expensive since we have to walk the append vecs being cleaned up.
|
|
|
|
|
2020-06-17 08:27:03 -07:00
|
|
|
use solana_runtime::bank_forks::BankForks;
|
2020-03-23 08:50:23 -07:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc, RwLock,
|
|
|
|
};
|
|
|
|
use std::thread::{self, sleep, Builder, JoinHandle};
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-04-06 01:30:23 -07:00
|
|
|
pub struct AccountsBackgroundService {
|
|
|
|
t_background: JoinHandle<()>,
|
2020-03-23 08:50:23 -07:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:30:23 -07:00
|
|
|
const INTERVAL_MS: u64 = 100;
|
2020-06-11 22:51:43 -07:00
|
|
|
const SHRUNKEN_ACCOUNT_PER_SEC: usize = 250;
|
|
|
|
const SHRUNKEN_ACCOUNT_PER_INTERVAL: usize =
|
|
|
|
SHRUNKEN_ACCOUNT_PER_SEC / (1000 / INTERVAL_MS as usize);
|
2020-07-02 22:25:17 -07:00
|
|
|
const CLEAN_INTERVAL_SLOTS: u64 = 100;
|
2020-04-06 01:30:23 -07:00
|
|
|
|
|
|
|
impl AccountsBackgroundService {
|
2020-03-23 08:50:23 -07:00
|
|
|
pub fn new(bank_forks: Arc<RwLock<BankForks>>, exit: &Arc<AtomicBool>) -> Self {
|
2020-04-06 01:30:23 -07:00
|
|
|
info!("AccountsBackgroundService active");
|
2020-03-23 08:50:23 -07:00
|
|
|
let exit = exit.clone();
|
2020-06-11 22:51:43 -07:00
|
|
|
let mut consumed_budget = 0;
|
2020-07-02 22:25:17 -07:00
|
|
|
let mut last_cleaned_slot = 0;
|
2020-04-06 01:30:23 -07:00
|
|
|
let t_background = Builder::new()
|
|
|
|
.name("solana-accounts-background".to_string())
|
2020-03-23 08:50:23 -07:00
|
|
|
.spawn(move || loop {
|
|
|
|
if exit.load(Ordering::Relaxed) {
|
|
|
|
break;
|
|
|
|
}
|
2020-07-02 22:25:17 -07:00
|
|
|
let bank = bank_forks.read().unwrap().root_bank().clone();
|
2020-04-06 01:30:23 -07:00
|
|
|
|
|
|
|
bank.process_dead_slots();
|
|
|
|
|
2020-06-11 22:51:43 -07:00
|
|
|
consumed_budget = bank
|
|
|
|
.process_stale_slot_with_budget(consumed_budget, SHRUNKEN_ACCOUNT_PER_INTERVAL);
|
2020-04-06 01:30:23 -07:00
|
|
|
|
2020-07-02 22:25:17 -07:00
|
|
|
if bank.block_height() - last_cleaned_slot > CLEAN_INTERVAL_SLOTS {
|
|
|
|
bank.clean_accounts();
|
|
|
|
last_cleaned_slot = bank.block_height();
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:30:23 -07:00
|
|
|
sleep(Duration::from_millis(INTERVAL_MS));
|
2020-03-23 08:50:23 -07:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-04-06 01:30:23 -07:00
|
|
|
Self { t_background }
|
2020-03-23 08:50:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join(self) -> thread::Result<()> {
|
2020-04-06 01:30:23 -07:00
|
|
|
self.t_background.join()
|
2020-03-23 08:50:23 -07:00
|
|
|
}
|
|
|
|
}
|