2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
dashmap::{mapref::entry::Entry::Occupied, DashMap},
|
2022-04-15 11:30:03 -07:00
|
|
|
log::*,
|
2021-12-03 09:00:31 -08:00
|
|
|
solana_sdk::{pubkey::Pubkey, timing::AtomicInterval},
|
|
|
|
std::{
|
|
|
|
collections::HashSet,
|
|
|
|
fmt::Debug,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicU64, Ordering},
|
|
|
|
RwLock,
|
|
|
|
},
|
2021-05-12 15:29:30 -07:00
|
|
|
},
|
2020-12-31 18:06:03 -08:00
|
|
|
};
|
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
// The only cases where an inner key should map to a different outer key is
|
|
|
|
// if the key had different account data for the indexed key across different
|
|
|
|
// slots. As this is rare, it should be ok to use a Vec here over a HashSet, even
|
|
|
|
// though we are running some key existence checks.
|
|
|
|
pub type SecondaryReverseIndexEntry = RwLock<Vec<Pubkey>>;
|
2020-12-31 18:06:03 -08:00
|
|
|
|
|
|
|
pub trait SecondaryIndexEntry: Debug {
|
2021-05-12 15:29:30 -07:00
|
|
|
fn insert_if_not_exists(&self, key: &Pubkey, inner_keys_count: &AtomicU64);
|
|
|
|
// Removes a value from the set. Returns whether the value was present in the set.
|
|
|
|
fn remove_inner_key(&self, key: &Pubkey) -> bool;
|
2020-12-31 18:06:03 -08:00
|
|
|
fn is_empty(&self) -> bool;
|
|
|
|
fn keys(&self) -> Vec<Pubkey>;
|
|
|
|
fn len(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct SecondaryIndexStats {
|
2021-07-28 18:16:36 -07:00
|
|
|
last_report: AtomicInterval,
|
2021-05-12 15:29:30 -07:00
|
|
|
num_inner_keys: AtomicU64,
|
|
|
|
}
|
|
|
|
|
2020-12-31 18:06:03 -08:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct DashMapSecondaryIndexEntry {
|
2021-05-12 15:29:30 -07:00
|
|
|
account_keys: DashMap<Pubkey, ()>,
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SecondaryIndexEntry for DashMapSecondaryIndexEntry {
|
2021-05-12 15:29:30 -07:00
|
|
|
fn insert_if_not_exists(&self, key: &Pubkey, inner_keys_count: &AtomicU64) {
|
|
|
|
if self.account_keys.get(key).is_none() {
|
|
|
|
self.account_keys.entry(*key).or_insert_with(|| {
|
|
|
|
inner_keys_count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
});
|
|
|
|
}
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
fn remove_inner_key(&self, key: &Pubkey) -> bool {
|
|
|
|
self.account_keys.remove(key).is_some()
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2021-05-12 15:29:30 -07:00
|
|
|
self.account_keys.is_empty()
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn keys(&self) -> Vec<Pubkey> {
|
2021-05-12 15:29:30 -07:00
|
|
|
self.account_keys
|
2020-12-31 18:06:03 -08:00
|
|
|
.iter()
|
|
|
|
.map(|entry_ref| *entry_ref.key())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
2021-05-12 15:29:30 -07:00
|
|
|
self.account_keys.len()
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct RwLockSecondaryIndexEntry {
|
2021-05-12 15:29:30 -07:00
|
|
|
account_keys: RwLock<HashSet<Pubkey>>,
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SecondaryIndexEntry for RwLockSecondaryIndexEntry {
|
2021-05-12 15:29:30 -07:00
|
|
|
fn insert_if_not_exists(&self, key: &Pubkey, inner_keys_count: &AtomicU64) {
|
|
|
|
let exists = self.account_keys.read().unwrap().contains(key);
|
|
|
|
if !exists {
|
|
|
|
let mut w_account_keys = self.account_keys.write().unwrap();
|
|
|
|
w_account_keys.insert(*key);
|
|
|
|
inner_keys_count.fetch_add(1, Ordering::Relaxed);
|
2020-12-31 18:06:03 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
fn remove_inner_key(&self, key: &Pubkey) -> bool {
|
|
|
|
self.account_keys.write().unwrap().remove(key)
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2021-05-12 15:29:30 -07:00
|
|
|
self.account_keys.read().unwrap().is_empty()
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn keys(&self) -> Vec<Pubkey> {
|
2021-05-12 15:29:30 -07:00
|
|
|
self.account_keys.read().unwrap().iter().cloned().collect()
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
2021-05-12 15:29:30 -07:00
|
|
|
self.account_keys.read().unwrap().len()
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct SecondaryIndex<SecondaryIndexEntryType: SecondaryIndexEntry + Default + Sync + Send> {
|
2021-05-12 15:29:30 -07:00
|
|
|
metrics_name: &'static str,
|
2020-12-31 18:06:03 -08:00
|
|
|
// Map from index keys to index values
|
|
|
|
pub index: DashMap<Pubkey, SecondaryIndexEntryType>,
|
|
|
|
pub reverse_index: DashMap<Pubkey, SecondaryReverseIndexEntry>,
|
2021-05-12 15:29:30 -07:00
|
|
|
stats: SecondaryIndexStats,
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<SecondaryIndexEntryType: SecondaryIndexEntry + Default + Sync + Send>
|
|
|
|
SecondaryIndex<SecondaryIndexEntryType>
|
|
|
|
{
|
2021-05-12 15:29:30 -07:00
|
|
|
pub fn new(metrics_name: &'static str) -> Self {
|
|
|
|
Self {
|
|
|
|
metrics_name,
|
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&self, key: &Pubkey, inner_key: &Pubkey) {
|
2020-12-31 18:06:03 -08:00
|
|
|
{
|
2022-09-22 15:23:03 -07:00
|
|
|
let pubkeys_map = self
|
|
|
|
.index
|
|
|
|
.get(key)
|
|
|
|
.unwrap_or_else(|| self.index.entry(*key).or_default().downgrade());
|
2020-12-31 18:06:03 -08:00
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
pubkeys_map.insert_if_not_exists(inner_key, &self.stats.num_inner_keys);
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
2022-03-20 18:27:24 -07:00
|
|
|
{
|
|
|
|
let outer_keys = self.reverse_index.get(inner_key).unwrap_or_else(|| {
|
|
|
|
self.reverse_index
|
|
|
|
.entry(*inner_key)
|
|
|
|
.or_insert(RwLock::new(Vec::with_capacity(1)))
|
|
|
|
.downgrade()
|
|
|
|
});
|
|
|
|
|
|
|
|
let should_insert = !outer_keys.read().unwrap().contains(key);
|
|
|
|
if should_insert {
|
|
|
|
let mut w_outer_keys = outer_keys.write().unwrap();
|
|
|
|
if !w_outer_keys.contains(key) {
|
|
|
|
w_outer_keys.push(*key);
|
|
|
|
}
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:16:36 -07:00
|
|
|
if self.stats.last_report.should_update(1000) {
|
2021-05-12 15:29:30 -07:00
|
|
|
datapoint_info!(
|
|
|
|
self.metrics_name,
|
|
|
|
("num_secondary_keys", self.index.len() as i64, i64),
|
|
|
|
(
|
|
|
|
"num_inner_keys",
|
|
|
|
self.stats.num_inner_keys.load(Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"num_reverse_index_keys",
|
|
|
|
self.reverse_index.len() as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 18:06:03 -08:00
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
// Only safe to call from `remove_by_inner_key()` due to asserts
|
|
|
|
fn remove_index_entries(&self, outer_key: &Pubkey, removed_inner_key: &Pubkey) {
|
|
|
|
let is_outer_key_empty = {
|
|
|
|
let inner_key_map = self
|
|
|
|
.index
|
2021-06-18 06:34:46 -07:00
|
|
|
.get_mut(outer_key)
|
2021-05-12 15:29:30 -07:00
|
|
|
.expect("If we're removing a key, then it must have an entry in the map");
|
|
|
|
// If we deleted a pubkey from the reverse_index, then the corresponding entry
|
|
|
|
// better exist in this index as well or the two indexes are out of sync!
|
2021-06-18 06:34:46 -07:00
|
|
|
assert!(inner_key_map.value().remove_inner_key(removed_inner_key));
|
2021-05-12 15:29:30 -07:00
|
|
|
inner_key_map.is_empty()
|
2020-12-31 18:06:03 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Delete the `key` if the set of inner keys is empty
|
2021-05-12 15:29:30 -07:00
|
|
|
if is_outer_key_empty {
|
2020-12-31 18:06:03 -08:00
|
|
|
// Other threads may have interleaved writes to this `key`,
|
|
|
|
// so double-check again for its emptiness
|
2021-05-12 15:29:30 -07:00
|
|
|
if let Occupied(key_entry) = self.index.entry(*outer_key) {
|
2020-12-31 18:06:03 -08:00
|
|
|
if key_entry.get().is_empty() {
|
|
|
|
key_entry.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:29:30 -07:00
|
|
|
pub fn remove_by_inner_key(&self, inner_key: &Pubkey) {
|
2020-12-31 18:06:03 -08:00
|
|
|
// Save off which keys in `self.index` had slots removed so we can remove them
|
|
|
|
// after we purge the reverse index
|
2021-05-12 15:29:30 -07:00
|
|
|
let mut removed_outer_keys: HashSet<Pubkey> = HashSet::new();
|
2020-12-31 18:06:03 -08:00
|
|
|
|
|
|
|
// Check if the entry for `inner_key` in the reverse index is empty
|
|
|
|
// and can be removed
|
2021-05-12 15:29:30 -07:00
|
|
|
if let Some((_, outer_keys_set)) = self.reverse_index.remove(inner_key) {
|
|
|
|
for removed_outer_key in outer_keys_set.into_inner().unwrap().into_iter() {
|
|
|
|
removed_outer_keys.insert(removed_outer_key);
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove this value from those keys
|
2021-05-12 15:29:30 -07:00
|
|
|
for outer_key in &removed_outer_keys {
|
|
|
|
self.remove_index_entries(outer_key, inner_key);
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
2021-05-12 15:29:30 -07:00
|
|
|
|
|
|
|
// Safe to `fetch_sub()` here because a dead key cannot be removed more than once,
|
|
|
|
// and the `num_inner_keys` must have been incremented by exactly removed_outer_keys.len()
|
|
|
|
// in previous unique insertions of `inner_key` into `self.index` for each key
|
|
|
|
// in `removed_outer_keys`
|
|
|
|
self.stats
|
|
|
|
.num_inner_keys
|
|
|
|
.fetch_sub(removed_outer_keys.len() as u64, Ordering::Relaxed);
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, key: &Pubkey) -> Vec<Pubkey> {
|
|
|
|
if let Some(inner_keys_map) = self.index.get(key) {
|
|
|
|
inner_keys_map.keys()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 11:30:03 -07:00
|
|
|
|
|
|
|
/// log top 20 (owner, # accounts) in descending order of # accounts
|
|
|
|
pub fn log_contents(&self) {
|
|
|
|
let mut entries = self
|
|
|
|
.index
|
|
|
|
.iter()
|
|
|
|
.map(|entry| (entry.value().len(), *entry.key()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
entries.sort_unstable();
|
|
|
|
entries
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.take(20)
|
|
|
|
.for_each(|(v, k)| info!("owner: {}, accounts: {}", k, v));
|
|
|
|
}
|
2020-12-31 18:06:03 -08:00
|
|
|
}
|