use crate::rules::{BucketRulesSnapshot, BucketSnapshotRef, DynRulesContainer};
use arc_swap::ArcSwap;
use rustfs_s3_types::EventName;
use starshard::{DEFAULT_SHARDS, ShardedHashMap};
use std::fmt;
use std::sync::Arc;
pub struct SubscriberIndex {
inner: ShardedHashMap<String, Arc<ArcSwap<BucketRulesSnapshot<DynRulesContainer>>>>,
empty_rules: Arc<DynRulesContainer>,
}
impl fmt::Debug for SubscriberIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SubscriberIndex").finish_non_exhaustive()
}
}
impl SubscriberIndex {
pub fn new(empty_rules: Arc<DynRulesContainer>) -> Self {
Self {
inner: ShardedHashMap::new(DEFAULT_SHARDS),
empty_rules,
}
}
pub fn load_snapshot(&self, bucket: &str) -> BucketSnapshotRef {
match self.inner.get(&bucket.to_string()) {
Some(cell) => cell.load_full(),
None => Arc::new(BucketRulesSnapshot::empty(self.empty_rules.clone())),
}
}
#[inline]
pub fn has_subscriber(&self, bucket: &str, event: &EventName) -> bool {
let snap = self.load_snapshot(bucket);
if snap.event_mask == 0 {
return false;
}
snap.has_event(event)
}
pub fn store_snapshot(&self, bucket: &str, new_snapshot: BucketRulesSnapshot<DynRulesContainer>) {
let key = bucket.to_string();
let empty_rules = self.empty_rules.clone();
let cell = self
.inner
.compute_if_absent(key, || Arc::new(ArcSwap::from_pointee(BucketRulesSnapshot::empty(empty_rules.clone()))));
cell.store(Arc::new(new_snapshot));
}
pub fn clear_bucket(&self, bucket: &str) {
if let Some(cell) = self.inner.get(&bucket.to_string()) {
cell.store(Arc::new(BucketRulesSnapshot::empty(self.empty_rules.clone())));
}
}
}
impl Default for SubscriberIndex {
fn default() -> Self {
Self::new(Arc::new(EmptyRules) as Arc<DynRulesContainer>)
}
}
#[derive(Debug)]
struct EmptyRules;
impl crate::rules::subscriber_snapshot::RulesContainer for EmptyRules {
type Rule = dyn crate::rules::subscriber_snapshot::RuleEvents;
fn iter_rules<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Self::Rule> + 'a> {
Box::new(std::iter::empty())
}
}
#[cfg(test)]
mod tests {
use super::*;
use rustfs_s3_types::EventName;
fn snapshot_with_mask(mask: u64) -> BucketRulesSnapshot<DynRulesContainer> {
BucketRulesSnapshot {
event_mask: mask,
rules: Arc::new(EmptyRules) as Arc<DynRulesContainer>,
}
}
#[test]
fn store_then_load_roundtrips_snapshot() {
let index = SubscriberIndex::default();
assert!(!index.has_subscriber("bucket", &EventName::ObjectCreatedPut));
index.store_snapshot("bucket", snapshot_with_mask(EventName::ObjectCreatedPut.mask()));
assert!(index.has_subscriber("bucket", &EventName::ObjectCreatedPut));
index.clear_bucket("bucket");
assert!(!index.has_subscriber("bucket", &EventName::ObjectCreatedPut));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn concurrent_first_writes_share_one_cell() {
let mask = EventName::ObjectCreatedPut.mask();
for _round in 0..64 {
let index = Arc::new(SubscriberIndex::default());
const WRITERS: usize = 16;
let mut handles = Vec::with_capacity(WRITERS);
for _ in 0..WRITERS {
let index = index.clone();
handles.push(tokio::spawn(async move {
index.store_snapshot("shared-bucket", snapshot_with_mask(mask));
}));
}
for handle in handles {
handle.await.expect("writer task must not panic");
}
assert!(
index.has_subscriber("shared-bucket", &EventName::ObjectCreatedPut),
"a concurrently-stored snapshot must survive; none was lost to a clobbered cell"
);
}
}
}