use core::hash::{BuildHasher, Hash};
use std::{
any::{Any, TypeId},
collections::HashMap,
sync::{Arc, Mutex, OnceLock, RwLock, RwLockReadGuard, RwLockWriteGuard},
};
#[cfg(feature = "async-graphql")]
use smol_bytes::Utf8Bytes;
#[cfg(feature = "async-graphql")]
use super::dynamic::DynRegistry;
use crate::Timestamp;
use super::{
RegistryOptions, RegistryOverride,
state::{Entry, Flip, RetireOrigin, Retirement, RootAction, RootAdvance, RootState, Stamp},
};
#[cfg(test)]
mod tests;
const SHARDS: usize = 16;
pub trait RootKey: Clone + Eq + Hash + Send + Sync + 'static {}
impl<T> RootKey for T where T: Clone + Eq + Hash + Send + Sync + 'static {}
pub trait RetiredLedger<Root>: Send + Sync {
fn owed(&self, key: &Root, act: Retirement);
}
pub trait GenerationLedger<Root>: Send + Sync {
fn flipped(&self, key: &Root, act: Flip);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RegistryCounts {
pub analyzing: usize,
pub retiring: usize,
pub retired: usize,
pub restoring: usize,
}
pub struct Registry<Root> {
options: RwLock<RegistryOptions>,
shards: Box<[Mutex<HashMap<Root, Entry>>]>,
stripe: std::collections::hash_map::RandomState,
ledger: OnceLock<Arc<dyn RetiredLedger<Root>>>,
curator: OnceLock<Arc<dyn GenerationLedger<Root>>>,
}
impl<Root> core::fmt::Debug for Registry<Root> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Registry")
.field("options", &self.options())
.field("shards", &self.shards.len())
.field("held", &self.held())
.field("ledger", &self.ledger.get().is_some())
.field("curator", &self.curator.get().is_some())
.finish()
}
}
impl<Root> Registry<Root> {
#[must_use]
pub fn new(options: RegistryOptions) -> Self {
Self {
options: RwLock::new(options),
shards: core::iter::repeat_with(|| Mutex::new(HashMap::new()))
.take(SHARDS)
.collect(),
stripe: std::collections::hash_map::RandomState::new(),
ledger: OnceLock::new(),
curator: OnceLock::new(),
}
}
#[inline]
#[must_use]
pub fn options(&self) -> RegistryOptions {
self
.options
.read()
.unwrap_or_else(|held| held.into_inner())
.clone()
}
pub fn apply_knobs(&self, over: &RegistryOverride) {
let mut current = self
.options
.write()
.unwrap_or_else(|held| held.into_inner());
if let Some(debounce) = over.debounce() {
current.set_debounce(debounce);
}
if let Some(batch_max_items) = over.batch_max_items() {
current.set_batch_max_items(batch_max_items);
}
if let Some(batch_max_wait) = over.batch_max_wait() {
current.set_batch_max_wait(batch_max_wait);
}
if let Some(retire_drain) = over.retire_drain() {
current.set_retire_drain(retire_drain);
}
if let Some(settle) = over.settle() {
current.set_settle(settle);
}
}
#[must_use = "a refused install leaves the acts going to whoever was already seated"]
pub fn installing(&self, ledger: Arc<dyn RetiredLedger<Root>>) -> bool {
self.ledger.set(ledger).is_ok()
}
#[inline]
#[must_use]
pub fn is_recorded(&self) -> bool {
self.ledger.get().is_some()
}
#[must_use = "a refused install leaves the flips going to whoever was already seated"]
pub fn curating(&self, curator: Arc<dyn GenerationLedger<Root>>) -> bool {
self.curator.set(curator).is_ok()
}
#[inline]
#[must_use]
pub fn is_curated(&self) -> bool {
self.curator.get().is_some()
}
#[must_use]
pub fn held(&self) -> usize {
self
.shards
.iter()
.map(|shard| shard.lock().unwrap_or_else(|held| held.into_inner()).len())
.sum()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.held() == 0
}
}
impl<Root: RootKey> Registry<Root> {
#[must_use]
pub fn state(&self, key: &Root) -> RootState {
self
.shard(key)
.get(key)
.map_or(RootState::Idle, Entry::state)
}
#[inline]
#[must_use]
pub fn inflight(&self, key: &Root) -> u32 {
self.state(key).inflight()
}
pub fn intake(&self, key: &Root, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Intake, stamp)
}
pub fn dispatched(&self, key: &Root, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Dispatched, stamp)
}
pub fn observed(&self, key: &Root, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Observed, stamp)
}
pub fn retire(&self, key: &Root, origin: RetireOrigin, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Retire(origin), stamp)
}
pub fn restore(&self, key: &Root, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Restore, stamp)
}
pub fn reappear(&self, key: &Root, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Reappear, stamp)
}
pub fn restored(&self, key: &Root, stamp: Stamp) -> RootAdvance {
self.act(key, RootAction::Restored, stamp)
}
pub fn rebuilt(&self, key: &Root, at: Timestamp, origin: RetireOrigin, late_tails: bool) {
self
.shard(key)
.insert(key.clone(), Entry::rebuilt(at, origin, late_tails));
}
#[must_use]
pub fn counts(&self) -> RegistryCounts {
let mut counts = RegistryCounts::default();
for stripe in &self.shards {
let shard = stripe.lock().unwrap_or_else(|held| held.into_inner());
for entry in shard.values() {
match entry.state_ref() {
RootState::Idle => {}
RootState::Analyzing { .. } => counts.analyzing += 1,
RootState::Retiring { .. } => counts.retiring += 1,
RootState::Retired { .. } => counts.retired += 1,
RootState::Restoring => counts.restoring += 1,
}
}
}
counts
}
#[must_use]
pub fn snapshot(&self, limit: usize) -> Vec<(Root, RootState)> {
let mut out = Vec::with_capacity(limit.min(SHARDS * 4));
'walk: for stripe in &self.shards {
let shard = stripe.lock().unwrap_or_else(|held| held.into_inner());
for (key, entry) in shard.iter() {
if out.len() >= limit {
break 'walk;
}
out.push((key.clone(), entry.state()));
}
}
out
}
pub fn lapse(&self, stamp: Stamp) -> Vec<Root> {
let ceiling = self
.options
.read()
.unwrap_or_else(|held| held.into_inner())
.retire_drain();
let mut lapsed: Vec<Root> = Vec::new();
let mut owed: Vec<(Root, Retirement)> = Vec::new();
let mut turned: Vec<(Root, Flip)> = Vec::new();
for stripe in &self.shards {
let mut shard = stripe.lock().unwrap_or_else(|held| held.into_inner());
shard.retain(|key, entry| {
let RootState::Retiring { since, .. } = entry.state_ref() else {
return true;
};
let since = *since;
if stamp.monotonic().saturating_duration_since(since) < ceiling {
return true;
}
let _spent = entry.advance(
RootAction::Lapsed,
stamp,
&mut |act| owed.push((key.clone(), act)),
&mut |act| turned.push((key.clone(), act)),
);
lapsed.push(key.clone());
!entry.is_vacant()
});
}
if let Some(ledger) = self.ledger.get() {
for (key, act) in owed {
ledger.owed(&key, act);
}
}
if let Some(curator) = self.curator.get() {
for (key, act) in turned {
curator.flipped(&key, act);
}
}
lapsed
}
fn act(&self, key: &Root, action: RootAction, stamp: Stamp) -> RootAdvance {
let mut owed: Vec<Retirement> = Vec::new();
let mut turned: Vec<Flip> = Vec::new();
let advance = {
let mut shard = self.shard(key);
let entry = shard.entry(key.clone()).or_insert_with(Entry::idle);
let advance = entry.advance(action, stamp, &mut |act| owed.push(act), &mut |act| {
turned.push(act)
});
if entry.is_vacant() {
shard.remove(key);
}
advance
};
if let Some(ledger) = self.ledger.get() {
for act in owed {
ledger.owed(key, act);
}
}
if let Some(curator) = self.curator.get() {
for act in turned {
curator.flipped(key, act);
}
}
advance
}
fn shard(&self, key: &Root) -> std::sync::MutexGuard<'_, HashMap<Root, Entry>> {
let stripe = (self.stripe.hash_one(key) as usize) % SHARDS;
self.shards[stripe]
.lock()
.unwrap_or_else(|held| held.into_inner())
}
}
pub(crate) struct Counting<Root> {
table: Arc<Registry<Root>>,
stamp: Stamp,
}
impl<Root: RootKey> Counting<Root> {
pub(crate) fn open(registries: Option<&Arc<Registries>>, family: &str) -> Option<Self> {
let table = registries?.counting::<Root>(family)?;
Some(Self {
table,
stamp: Stamp::now(),
})
}
#[inline]
pub(crate) fn dispatched(&self, key: &Root) {
let _late = self.table.dispatched(key, self.stamp);
}
#[inline]
pub(crate) fn observed(&self, key: &Root) {
let _late = self.table.observed(key, self.stamp);
}
}
pub struct Registries {
options: RegistryOptions,
by_family: RwLock<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>,
#[cfg(feature = "async-graphql")]
by_name: RwLock<HashMap<Utf8Bytes, Arc<dyn DynRegistry>>>,
}
impl core::fmt::Debug for Registries {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut debug = f.debug_struct("Registries");
debug.field("options", &self.options);
debug.field("families", &self.families());
#[cfg(feature = "async-graphql")]
debug.field("named", &self.read_names().len());
debug.finish()
}
}
impl Registries {
#[must_use]
pub fn new(options: RegistryOptions) -> Self {
Self {
options,
by_family: RwLock::new(HashMap::new()),
#[cfg(feature = "async-graphql")]
by_name: RwLock::new(HashMap::new()),
}
}
#[inline]
#[must_use]
pub const fn options(&self) -> &RegistryOptions {
&self.options
}
pub fn open<Root: RootKey>(&self, family: &str) -> Arc<Registry<Root>> {
if let Some(open) = self.of::<Root>() {
return open;
}
let mut families = self.write();
let entry = families
.entry(TypeId::of::<Root>())
.or_insert_with(|| Arc::new(Registry::<Root>::new(self.options.for_family(family))));
Arc::clone(entry)
.downcast::<Registry<Root>>()
.unwrap_or_else(|_| unreachable!("a family's table is keyed by its own root type"))
}
#[must_use]
pub fn of<Root: RootKey>(&self) -> Option<Arc<Registry<Root>>> {
let families = self.read();
let table = families.get(&TypeId::of::<Root>())?;
Arc::clone(table).downcast::<Registry<Root>>().ok()
}
#[must_use]
pub fn counting<Root: RootKey>(&self, family: &str) -> Option<Arc<Registry<Root>>> {
(TypeId::of::<Root>() != TypeId::of::<()>()).then(|| self.open::<Root>(family))
}
#[must_use]
pub fn families(&self) -> usize {
self.read().len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.families() == 0
}
#[cfg(feature = "async-graphql")]
#[must_use]
pub fn expose<Root>(&self, family: &str) -> Arc<Registry<Root>>
where
Root: RootKey + core::fmt::Display + core::str::FromStr,
{
let table = self.open::<Root>(family);
let mut named = self.write_names();
named.insert(
Utf8Bytes::from(family),
Arc::clone(&table) as Arc<dyn DynRegistry>,
);
table
}
#[cfg(feature = "async-graphql")]
#[must_use]
pub(crate) fn dyn_for(&self, family: &str) -> Option<Arc<dyn DynRegistry>> {
self.read_names().get(family).map(Arc::clone)
}
fn read(&self) -> RwLockReadGuard<'_, HashMap<TypeId, Arc<dyn Any + Send + Sync>>> {
self
.by_family
.read()
.unwrap_or_else(|held| held.into_inner())
}
fn write(&self) -> RwLockWriteGuard<'_, HashMap<TypeId, Arc<dyn Any + Send + Sync>>> {
self
.by_family
.write()
.unwrap_or_else(|held| held.into_inner())
}
#[cfg(feature = "async-graphql")]
fn read_names(&self) -> RwLockReadGuard<'_, HashMap<Utf8Bytes, Arc<dyn DynRegistry>>> {
self.by_name.read().unwrap_or_else(|held| held.into_inner())
}
#[cfg(feature = "async-graphql")]
fn write_names(&self) -> RwLockWriteGuard<'_, HashMap<Utf8Bytes, Arc<dyn DynRegistry>>> {
self
.by_name
.write()
.unwrap_or_else(|held| held.into_inner())
}
}
impl Default for Registries {
#[inline]
fn default() -> Self {
Self::new(RegistryOptions::new())
}
}