2021-09-17 09:39:41 -07:00
|
|
|
use crate::accounts_index::{AccountsIndexConfig, IndexValue};
|
2021-09-22 10:40:30 -07:00
|
|
|
use crate::bucket_map_holder::BucketMapHolder;
|
2021-09-16 14:12:22 -07:00
|
|
|
use crate::in_mem_accounts_index::InMemAccountsIndex;
|
2021-09-15 07:54:16 -07:00
|
|
|
use std::fmt::Debug;
|
2021-09-12 15:14:59 -07:00
|
|
|
use std::{
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
thread::{Builder, JoinHandle},
|
|
|
|
};
|
|
|
|
|
|
|
|
// eventually hold the bucket map
|
|
|
|
// Also manages the lifetime of the background processing threads.
|
|
|
|
// When this instance is dropped, it will drop the bucket map and cleanup
|
|
|
|
// and it will stop all the background threads and join them.
|
2021-09-14 15:51:07 -07:00
|
|
|
pub struct AccountsIndexStorage<T: IndexValue> {
|
2021-09-12 15:14:59 -07:00
|
|
|
// for managing the bg threads
|
|
|
|
exit: Arc<AtomicBool>,
|
2021-09-17 09:53:25 -07:00
|
|
|
handles: Option<Vec<JoinHandle<()>>>,
|
2021-09-12 15:14:59 -07:00
|
|
|
|
|
|
|
// eventually the backing storage
|
2021-09-18 20:08:58 -07:00
|
|
|
pub storage: Arc<BucketMapHolder<T>>,
|
2021-09-16 14:12:22 -07:00
|
|
|
pub in_mem: Vec<Arc<InMemAccountsIndex<T>>>,
|
2021-09-12 15:14:59 -07:00
|
|
|
}
|
|
|
|
|
2021-09-15 07:54:16 -07:00
|
|
|
impl<T: IndexValue> Debug for AccountsIndexStorage<T> {
|
|
|
|
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:51:07 -07:00
|
|
|
impl<T: IndexValue> Drop for AccountsIndexStorage<T> {
|
2021-09-12 15:14:59 -07:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.exit.store(true, Ordering::Relaxed);
|
2021-09-20 07:58:20 -07:00
|
|
|
self.storage.wait_dirty_or_aged.notify_all();
|
2021-09-17 09:53:25 -07:00
|
|
|
if let Some(handles) = self.handles.take() {
|
|
|
|
handles
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|handle| handle.join().unwrap());
|
2021-09-12 15:14:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:51:07 -07:00
|
|
|
impl<T: IndexValue> AccountsIndexStorage<T> {
|
2021-09-17 15:02:43 -07:00
|
|
|
pub fn new(bins: usize, config: &Option<AccountsIndexConfig>) -> AccountsIndexStorage<T> {
|
2021-09-27 09:25:06 -07:00
|
|
|
let num_threads = std::cmp::max(2, num_cpus::get() / 4);
|
2021-09-17 15:02:43 -07:00
|
|
|
let threads = config
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|config| config.flush_threads)
|
2021-09-27 09:25:06 -07:00
|
|
|
.unwrap_or(num_threads);
|
2021-09-17 15:02:43 -07:00
|
|
|
|
2021-09-23 17:56:44 -07:00
|
|
|
let storage = Arc::new(BucketMapHolder::new(bins, config, threads));
|
|
|
|
|
|
|
|
let in_mem = (0..bins)
|
|
|
|
.into_iter()
|
|
|
|
.map(|bin| Arc::new(InMemAccountsIndex::new(&storage, bin)))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-09-12 15:14:59 -07:00
|
|
|
let exit = Arc::new(AtomicBool::default());
|
2021-09-17 09:53:25 -07:00
|
|
|
let handles = Some(
|
|
|
|
(0..threads)
|
|
|
|
.into_iter()
|
|
|
|
.map(|_| {
|
|
|
|
let storage_ = Arc::clone(&storage);
|
|
|
|
let exit_ = Arc::clone(&exit);
|
2021-09-18 07:56:08 -07:00
|
|
|
let in_mem_ = in_mem.clone();
|
|
|
|
|
2021-09-17 09:53:25 -07:00
|
|
|
// note that rayon use here causes us to exhaust # rayon threads and many tests running in parallel deadlock
|
|
|
|
Builder::new()
|
|
|
|
.name("solana-idx-flusher".to_string())
|
|
|
|
.spawn(move || {
|
2021-09-22 10:40:30 -07:00
|
|
|
storage_.background(exit_, in_mem_);
|
2021-09-17 09:53:25 -07:00
|
|
|
})
|
|
|
|
.unwrap()
|
2021-09-12 15:14:59 -07:00
|
|
|
})
|
2021-09-17 09:53:25 -07:00
|
|
|
.collect(),
|
2021-09-12 15:14:59 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
exit,
|
2021-09-17 09:53:25 -07:00
|
|
|
handles,
|
2021-09-12 15:14:59 -07:00
|
|
|
storage,
|
2021-09-16 14:12:22 -07:00
|
|
|
in_mem,
|
2021-09-12 15:14:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|