Files
continuwuity/src/service/rooms/auth_chain/mod.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

258 lines
6.3 KiB
Rust
Raw Normal View History

mod data;
2024-06-28 22:51:39 +00:00
2022-10-05 20:34:31 +02:00
use std::{
2024-12-28 02:51:30 +00:00
collections::{BTreeSet, HashSet, VecDeque},
fmt::Debug,
2022-10-05 20:34:31 +02:00
sync::Arc,
2025-01-29 08:39:44 +00:00
time::Instant,
2022-10-05 20:34:31 +02:00
};
2022-09-06 23:15:09 +02:00
2024-12-14 21:58:01 -05:00
use conduwuit::{
Err, Result, at, debug, debug_error, implement, trace,
2025-01-20 09:02:50 +00:00
utils::{
IterStream,
stream::{ReadyExt, TryBroadbandExt},
2025-01-20 09:02:50 +00:00
},
validated, warn,
};
2025-01-29 08:39:44 +00:00
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use ruma::{EventId, OwnedEventId, RoomId};
2021-08-12 23:04:00 +02:00
2024-07-18 06:37:47 +00:00
use self::data::Data;
use crate::{Dep, rooms, rooms::short::ShortEventId};
2021-08-14 19:07:50 +02:00
2024-05-09 15:59:08 -07:00
pub struct Service {
2024-07-18 06:37:47 +00:00
services: Services,
2024-05-27 03:17:20 +00:00
db: Data,
}
2022-06-20 12:08:58 +02:00
2024-07-18 06:37:47 +00:00
struct Services {
short: Dep<rooms::short::Service>,
timeline: Dep<rooms::timeline::Service>,
}
2025-01-29 08:39:44 +00:00
type Bucket<'a> = BTreeSet<(u64, &'a EventId)>;
2024-07-04 03:26:19 +00:00
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
2024-07-18 06:37:47 +00:00
services: Services {
short: args.depend::<rooms::short::Service>("rooms::short"),
timeline: args.depend::<rooms::timeline::Service>("rooms::timeline"),
},
db: Data::new(&args),
2024-07-04 03:26:19 +00:00
}))
2024-05-27 03:17:20 +00:00
}
2024-07-04 03:26:19 +00:00
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
2025-01-20 09:05:49 +00:00
#[implement(Service)]
2025-01-29 08:39:44 +00:00
pub fn event_ids_iter<'a, I>(
2025-01-20 09:05:49 +00:00
&'a self,
2025-01-29 08:39:44 +00:00
room_id: &'a RoomId,
2025-01-20 09:05:49 +00:00
starting_events: I,
2025-01-29 08:39:44 +00:00
) -> impl Stream<Item = Result<OwnedEventId>> + Send + 'a
2025-01-20 09:05:49 +00:00
where
I: Iterator<Item = &'a EventId> + Clone + Debug + ExactSizeIterator + Send + 'a,
{
2025-01-29 08:39:44 +00:00
self.get_auth_chain(room_id, starting_events)
.map_ok(|chain| {
self.services
.short
.multi_get_eventid_from_short(chain.into_iter().stream())
.ready_filter(Result::is_ok)
})
.try_flatten_stream()
2025-01-20 09:05:49 +00:00
}
2025-01-20 09:05:49 +00:00
#[implement(Service)]
#[tracing::instrument(name = "auth_chain", level = "debug", skip_all)]
pub async fn get_auth_chain<'a, I>(
&'a self,
room_id: &RoomId,
starting_events: I,
) -> Result<Vec<ShortEventId>>
where
I: Iterator<Item = &'a EventId> + Clone + Debug + ExactSizeIterator + Send + 'a,
{
const NUM_BUCKETS: usize = 50; //TODO: change possible w/o disrupting db?
2025-01-29 08:39:44 +00:00
const BUCKET: Bucket<'_> = BTreeSet::new();
2025-01-20 09:05:49 +00:00
2025-01-29 08:39:44 +00:00
let started = Instant::now();
2025-01-20 09:05:49 +00:00
let mut starting_ids = self
.services
.short
.multi_get_or_create_shorteventid(starting_events.clone())
.zip(starting_events.clone().stream())
.boxed();
let mut buckets = [BUCKET; NUM_BUCKETS];
while let Some((short, starting_event)) = starting_ids.next().await {
let bucket: usize = short.try_into()?;
let bucket: usize = validated!(bucket % NUM_BUCKETS);
buckets[bucket].insert((short, starting_event));
2024-04-10 13:55:09 -07:00
}
2025-01-20 09:05:49 +00:00
debug!(
starting_events = ?starting_events.count(),
elapsed = ?started.elapsed(),
"start",
);
2024-03-05 19:48:54 -05:00
2025-01-20 09:05:49 +00:00
let full_auth_chain: Vec<ShortEventId> = buckets
.into_iter()
.try_stream()
2025-01-29 08:39:44 +00:00
.broad_and_then(|chunk| self.get_auth_chain_outer(room_id, started, chunk))
.try_collect()
.map_ok(|auth_chain: Vec<_>| auth_chain.into_iter().flatten().collect())
.map_ok(|mut full_auth_chain: Vec<_>| {
full_auth_chain.sort_unstable();
full_auth_chain.dedup();
full_auth_chain
})
.boxed()
.await?;
2024-03-05 19:48:54 -05:00
2025-01-29 08:39:44 +00:00
debug!(
chain_length = ?full_auth_chain.len(),
elapsed = ?started.elapsed(),
"done",
);
Ok(full_auth_chain)
}
#[implement(Service)]
async fn get_auth_chain_outer(
&self,
room_id: &RoomId,
started: Instant,
chunk: Bucket<'_>,
) -> Result<Vec<ShortEventId>> {
let chunk_key: Vec<ShortEventId> = chunk.iter().map(at!(0)).collect();
if chunk_key.is_empty() {
return Ok(Vec::new());
}
2025-01-20 09:02:50 +00:00
2025-01-29 08:39:44 +00:00
if let Ok(cached) = self.get_cached_eventid_authchain(&chunk_key).await {
return Ok(cached.to_vec());
}
let chunk_cache: Vec<_> = chunk
.into_iter()
.try_stream()
.broad_and_then(|(shortid, event_id)| async move {
if let Ok(cached) = self.get_cached_eventid_authchain(&[shortid]).await {
2025-01-20 09:05:49 +00:00
return Ok(cached.to_vec());
}
2025-01-29 08:39:44 +00:00
let auth_chain = self.get_auth_chain_inner(room_id, event_id).await?;
self.cache_auth_chain_vec(vec![shortid], auth_chain.as_slice());
2025-01-20 09:05:49 +00:00
debug!(
2025-01-29 08:39:44 +00:00
?event_id,
2025-01-20 09:05:49 +00:00
elapsed = ?started.elapsed(),
2025-01-29 08:39:44 +00:00
"Cache missed event"
2025-01-20 09:05:49 +00:00
);
2025-01-29 08:39:44 +00:00
Ok(auth_chain)
2025-01-20 09:05:49 +00:00
})
.try_collect()
2025-01-29 08:39:44 +00:00
.map_ok(|chunk_cache: Vec<_>| chunk_cache.into_iter().flatten().collect())
.map_ok(|mut chunk_cache: Vec<_>| {
chunk_cache.sort_unstable();
chunk_cache.dedup();
chunk_cache
2025-01-20 09:05:49 +00:00
})
.await?;
2025-01-29 08:39:44 +00:00
self.cache_auth_chain_vec(chunk_key, chunk_cache.as_slice());
2025-01-20 09:05:49 +00:00
debug!(
2025-01-29 08:39:44 +00:00
chunk_cache_length = ?chunk_cache.len(),
2025-01-20 09:05:49 +00:00
elapsed = ?started.elapsed(),
2025-01-29 08:39:44 +00:00
"Cache missed chunk",
2025-01-20 09:05:49 +00:00
);
2025-01-29 08:39:44 +00:00
Ok(chunk_cache)
2025-01-20 09:05:49 +00:00
}
#[implement(Service)]
#[tracing::instrument(name = "inner", level = "trace", skip(self, room_id))]
async fn get_auth_chain_inner(
&self,
room_id: &RoomId,
event_id: &EventId,
) -> Result<Vec<ShortEventId>> {
let mut todo: VecDeque<_> = [event_id.to_owned()].into();
let mut found = HashSet::new();
while let Some(event_id) = todo.pop_front() {
trace!(?event_id, "processing auth event");
match self.services.timeline.get_pdu(&event_id).await {
| Err(e) => {
debug_error!(?event_id, ?e, "Could not find pdu mentioned in auth events");
},
| Ok(pdu) => {
if pdu.room_id.as_ref().is_some_and(|r| r == room_id) {
2025-01-20 09:05:49 +00:00
return Err!(Request(Forbidden(error!(
?event_id,
?room_id,
wrong_room_id = ?pdu.room_id.unwrap(),
2025-01-20 09:05:49 +00:00
"auth event for incorrect room"
))));
2025-01-20 09:02:50 +00:00
}
2025-01-20 09:05:49 +00:00
for auth_event in &pdu.auth_events {
let sauthevent = self
.services
.short
.get_or_create_shorteventid(auth_event)
.await;
2024-03-05 19:48:54 -05:00
2025-01-20 09:05:49 +00:00
if found.insert(sauthevent) {
trace!(?event_id, ?auth_event, "adding auth event to processing queue");
2024-09-25 03:52:28 +00:00
2025-01-20 09:05:49 +00:00
todo.push_back(auth_event.clone());
2022-10-05 18:36:12 +02:00
}
2025-01-20 09:05:49 +00:00
}
},
2022-10-05 18:36:12 +02:00
}
}
2025-01-20 09:05:49 +00:00
Ok(found.into_iter().collect())
}
2025-01-20 09:05:49 +00:00
#[implement(Service)]
#[inline]
pub async fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Arc<[ShortEventId]>> {
self.db.get_cached_eventid_authchain(key).await
}
2024-11-27 04:48:40 +00:00
2025-01-20 09:05:49 +00:00
#[implement(Service)]
#[tracing::instrument(skip_all, level = "debug")]
pub fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: &HashSet<ShortEventId>) {
let val: Arc<[ShortEventId]> = auth_chain.iter().copied().collect();
2024-04-10 13:55:09 -07:00
2025-01-20 09:05:49 +00:00
self.db.cache_auth_chain(key, val);
}
2024-11-27 04:48:40 +00:00
2025-01-20 09:05:49 +00:00
#[implement(Service)]
#[tracing::instrument(skip_all, level = "debug")]
pub fn cache_auth_chain_vec(&self, key: Vec<u64>, auth_chain: &[ShortEventId]) {
let val: Arc<[ShortEventId]> = auth_chain.iter().copied().collect();
2025-01-20 09:05:49 +00:00
self.db.cache_auth_chain(key, val);
}
2024-11-27 04:48:40 +00:00
2025-01-20 09:05:49 +00:00
#[implement(Service)]
pub fn get_cache_usage(&self) -> (usize, usize) {
2025-07-19 22:05:43 +01:00
let cache = self.db.auth_chain_cache.lock();
2025-01-20 09:05:49 +00:00
(cache.len(), cache.capacity())
}
2025-01-20 09:05:49 +00:00
#[implement(Service)]
2025-07-19 22:05:43 +01:00
pub fn clear_cache(&self) { self.db.auth_chain_cache.lock().clear(); }