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

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

547 lines
14 KiB
Rust
Raw Normal View History

2022-10-05 20:34:31 +02:00
use std::{
collections::{BTreeSet, HashMap},
fmt::{Debug, Write},
2022-10-05 20:34:31 +02:00
mem::size_of,
2025-07-19 22:05:43 +01:00
sync::Arc,
2022-10-05 20:34:31 +02:00
};
2022-09-06 23:15:09 +02:00
use async_trait::async_trait;
2024-12-14 21:58:01 -05:00
use conduwuit::{
2025-07-19 22:05:43 +01:00
Result, SyncMutex,
arrayvec::ArrayVec,
2025-04-29 06:55:54 +00:00
at, checked, err, expected, implement, utils,
utils::{bytes, math::usize_from_f64, stream::IterStream},
};
use database::Map;
use futures::{Stream, StreamExt};
2022-10-05 20:33:55 +02:00
use lru_cache::LruCache;
2022-09-06 23:15:09 +02:00
use ruma::{EventId, RoomId};
2024-10-31 08:41:43 +00:00
use crate::{
Dep, rooms,
2024-11-17 03:57:21 +00:00
rooms::short::{ShortEventId, ShortId, ShortStateHash, ShortStateKey},
2024-10-31 08:41:43 +00:00
};
2024-05-09 15:59:08 -07:00
pub struct Service {
2025-07-19 22:05:43 +01:00
pub stateinfo_cache: SyncMutex<StateInfoLruCache>,
2024-06-28 22:51:39 +00:00
db: Data,
2024-07-18 06:37:47 +00:00
services: Services,
}
2024-07-18 06:37:47 +00:00
struct Services {
short: Dep<rooms::short::Service>,
state: Dep<rooms::state::Service>,
}
struct Data {
shortstatehash_statediff: Arc<Map>,
}
#[derive(Clone)]
struct StateDiff {
2024-11-14 23:35:53 +00:00
parent: Option<ShortStateHash>,
2024-11-02 06:12:54 +00:00
added: Arc<CompressedState>,
removed: Arc<CompressedState>,
}
#[derive(Clone, Default)]
pub struct ShortStateInfo {
pub shortstatehash: ShortStateHash,
2024-11-02 06:12:54 +00:00
pub full_state: Arc<CompressedState>,
pub added: Arc<CompressedState>,
pub removed: Arc<CompressedState>,
}
#[derive(Clone, Default)]
pub struct HashSetCompressStateEvent {
pub shortstatehash: ShortStateHash,
2024-11-02 06:12:54 +00:00
pub added: Arc<CompressedState>,
pub removed: Arc<CompressedState>,
}
2024-10-31 08:41:43 +00:00
type StateInfoLruCache = LruCache<ShortStateHash, ShortStateInfoVec>;
type ShortStateInfoVec = Vec<ShortStateInfo>;
type ParentStatesVec = Vec<ShortStateInfo>;
pub type CompressedState = BTreeSet<CompressedStateEvent>;
pub type CompressedStateEvent = [u8; 2 * size_of::<ShortId>()];
#[async_trait]
2024-07-04 03:26:19 +00:00
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
let config = &args.server.config;
let cache_capacity =
f64::from(config.stateinfo_cache_capacity) * config.cache_capacity_modifier;
2024-07-04 03:26:19 +00:00
Ok(Arc::new(Self {
stateinfo_cache: LruCache::new(usize_from_f64(cache_capacity)?).into(),
db: Data {
shortstatehash_statediff: args.db["shortstatehash_statediff"].clone(),
},
2024-07-18 06:37:47 +00:00
services: Services {
short: args.depend::<rooms::short::Service>("rooms::short"),
state: args.depend::<rooms::state::Service>("rooms::state"),
},
2024-07-04 03:26:19 +00:00
}))
}
async fn memory_usage(&self, out: &mut (dyn Write + Send)) -> Result {
let (cache_len, ents) = {
2025-07-19 22:05:43 +01:00
let cache = self.stateinfo_cache.lock();
let ents = cache.iter().map(at!(1)).flat_map(|vec| vec.iter()).fold(
HashMap::new(),
|mut ents, ssi| {
2024-11-15 22:23:42 +00:00
for cs in &[&ssi.added, &ssi.removed, &ssi.full_state] {
ents.insert(Arc::as_ptr(cs), compressed_state_size(cs));
}
ents
},
);
(cache.len(), ents)
};
let ents_len = ents.len();
let bytes = ents.values().copied().fold(0_usize, usize::saturating_add);
let bytes = bytes::pretty(bytes);
writeln!(out, "stateinfo_cache: {cache_len} {ents_len} ({bytes})")?;
2024-07-04 03:26:19 +00:00
Ok(())
2024-05-27 03:17:20 +00:00
}
2025-07-19 22:05:43 +01:00
async fn clear_cache(&self) { self.stateinfo_cache.lock().clear(); }
2024-07-04 03:26:19 +00:00
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
2025-04-29 06:55:54 +00:00
/// Returns a stack with info on shortstatehash, full state, added diff and
/// removed diff for the selected shortstatehash and each parent layer.
#[implement(Service)]
#[tracing::instrument(name = "load", level = "debug", skip(self))]
pub async fn load_shortstatehash_info(
&self,
shortstatehash: ShortStateHash,
) -> Result<ShortStateInfoVec> {
2025-07-19 22:05:43 +01:00
if let Some(r) = self.stateinfo_cache.lock().get_mut(&shortstatehash) {
2025-04-29 06:55:54 +00:00
return Ok(r.clone());
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
let stack = self.new_shortstatehash_info(shortstatehash).await?;
2024-11-15 22:23:42 +00:00
2025-04-29 06:55:54 +00:00
self.cache_shortstatehash_info(shortstatehash, stack.clone())
.await?;
2024-11-15 22:23:42 +00:00
2025-04-29 06:55:54 +00:00
Ok(stack)
}
2024-11-15 22:23:42 +00:00
2025-04-29 06:55:54 +00:00
/// Returns a stack with info on shortstatehash, full state, added diff and
/// removed diff for the selected shortstatehash and each parent layer.
#[implement(Service)]
#[tracing::instrument(
2025-01-14 05:20:42 +00:00
name = "cache",
level = "debug",
skip_all,
fields(
?shortstatehash,
stack = stack.len(),
),
)]
2025-04-29 06:55:54 +00:00
async fn cache_shortstatehash_info(
&self,
shortstatehash: ShortStateHash,
stack: ShortStateInfoVec,
) -> Result {
2025-07-19 22:05:43 +01:00
self.stateinfo_cache.lock().insert(shortstatehash, stack);
2025-04-29 06:55:54 +00:00
Ok(())
}
2025-04-29 06:55:54 +00:00
#[implement(Service)]
async fn new_shortstatehash_info(
&self,
shortstatehash: ShortStateHash,
) -> Result<ShortStateInfoVec> {
let StateDiff { parent, added, removed } = self.get_statediff(shortstatehash).await?;
2024-11-15 22:23:42 +00:00
2025-04-29 06:55:54 +00:00
let Some(parent) = parent else {
return Ok(vec![ShortStateInfo {
2024-11-15 22:23:42 +00:00
shortstatehash,
2025-04-29 06:55:54 +00:00
full_state: added.clone(),
2024-11-15 22:23:42 +00:00
added,
2025-04-29 06:55:54 +00:00
removed,
}]);
};
2024-08-08 17:18:30 +00:00
2025-04-29 06:55:54 +00:00
let mut stack = Box::pin(self.load_shortstatehash_info(parent)).await?;
let top = stack.last().expect("at least one frame");
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
let mut full_state = (*top.full_state).clone();
full_state.extend(added.iter().copied());
2024-11-14 23:35:53 +00:00
2025-04-29 06:55:54 +00:00
let removed = (*removed).clone();
for r in &removed {
full_state.remove(r);
2024-03-05 19:48:54 -05:00
}
2025-04-29 06:55:54 +00:00
stack.push(ShortStateInfo {
shortstatehash,
added,
removed: Arc::new(removed),
full_state: Arc::new(full_state),
});
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
Ok(stack)
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
#[implement(Service)]
pub fn compress_state_events<'a, I>(
&'a self,
state: I,
) -> impl Stream<Item = CompressedStateEvent> + Send + 'a
where
I: Iterator<Item = (&'a ShortStateKey, &'a EventId)> + Clone + Debug + Send + 'a,
{
let event_ids = state.clone().map(at!(1));
let short_event_ids = self
.services
.short
.multi_get_or_create_shorteventid(event_ids);
state
.stream()
.map(at!(0))
.zip(short_event_ids)
.map(|(shortstatekey, shorteventid)| compress_state_event(*shortstatekey, shorteventid))
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
#[implement(Service)]
pub async fn compress_state_event(
&self,
shortstatekey: ShortStateKey,
event_id: &EventId,
) -> CompressedStateEvent {
let shorteventid = self
.services
.short
.get_or_create_shorteventid(event_id)
.await;
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
compress_state_event(shortstatekey, shorteventid)
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
/// Creates a new shortstatehash that often is just a diff to an already
/// existing shortstatehash and therefore very efficient.
///
/// There are multiple layers of diffs. The bottom layer 0 always contains
/// the full state. Layer 1 contains diffs to states of layer 0, layer 2
/// diffs to layer 1 and so on. If layer n > 0 grows too big, it will be
/// combined with layer n-1 to create a new diff on layer n-1 that's
/// based on layer n-2. If that layer is also too big, it will recursively
/// fix above layers too.
///
/// * `shortstatehash` - Shortstatehash of this state
/// * `statediffnew` - Added to base. Each vec is shortstatekey+shorteventid
/// * `statediffremoved` - Removed from base. Each vec is
/// shortstatekey+shorteventid
/// * `diff_to_sibling` - Approximately how much the diff grows each time for
/// this layer
/// * `parent_states` - A stack with info on shortstatehash, full state, added
/// diff and removed diff for each parent layer
#[implement(Service)]
pub fn save_state_from_diff(
&self,
shortstatehash: ShortStateHash,
statediffnew: Arc<CompressedState>,
statediffremoved: Arc<CompressedState>,
diff_to_sibling: usize,
mut parent_states: ParentStatesVec,
) -> Result {
let statediffnew_len = statediffnew.len();
let statediffremoved_len = statediffremoved.len();
let diffsum = checked!(statediffnew_len + statediffremoved_len)?;
if parent_states.len() > 3 {
// Number of layers
// To many layers, we have to go deeper
let parent = parent_states.pop().expect("parent must have a state");
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
let mut parent_new = (*parent.added).clone();
let mut parent_removed = (*parent.removed).clone();
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
for removed in statediffremoved.iter() {
if !parent_new.remove(removed) {
// It was not added in the parent and we removed it
parent_removed.insert(*removed);
2021-08-14 19:07:50 +02:00
}
2025-04-29 06:55:54 +00:00
// Else it was added in the parent and we removed it again. We
// can forget this change
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
for new in statediffnew.iter() {
if !parent_removed.remove(new) {
// It was not touched in the parent and we added it
parent_new.insert(*new);
2021-08-12 23:04:00 +02:00
}
2025-04-29 06:55:54 +00:00
// Else it was removed in the parent and we added it again. We
// can forget this change
2021-08-12 23:04:00 +02:00
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
self.save_state_from_diff(
shortstatehash,
Arc::new(parent_new),
Arc::new(parent_removed),
diffsum,
parent_states,
)?;
return Ok(());
2020-09-13 22:24:36 +02:00
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
if parent_states.is_empty() {
// There is no parent layer, create a new state
self.save_statediff(shortstatehash, &StateDiff {
parent: None,
added: statediffnew,
removed: statediffremoved,
});
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
return Ok(());
}
// Else we have two options.
// 1. We add the current diff on top of the parent layer.
// 2. We replace a layer above
let parent = parent_states.pop().expect("parent must have a state");
let parent_added_len = parent.added.len();
let parent_removed_len = parent.removed.len();
let parent_diff = checked!(parent_added_len + parent_removed_len)?;
if checked!(diffsum * diffsum)? >= checked!(2 * diff_to_sibling * parent_diff)? {
// Diff too big, we replace above layer(s)
let mut parent_new = (*parent.added).clone();
let mut parent_removed = (*parent.removed).clone();
for removed in statediffremoved.iter() {
if !parent_new.remove(removed) {
// It was not added in the parent and we removed it
parent_removed.insert(*removed);
}
// Else it was added in the parent and we removed it again. We
// can forget this change
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
for new in statediffnew.iter() {
if !parent_removed.remove(new) {
// It was not touched in the parent and we added it
parent_new.insert(*new);
}
// Else it was removed in the parent and we added it again. We
// can forget this change
2025-02-25 18:38:12 +00:00
}
2024-03-05 19:48:54 -05:00
2025-04-29 06:55:54 +00:00
self.save_state_from_diff(
shortstatehash,
Arc::new(parent_new),
Arc::new(parent_removed),
diffsum,
parent_states,
)?;
} else {
// Diff small enough, we add diff as layer on top of parent
self.save_statediff(shortstatehash, &StateDiff {
parent: Some(parent.shortstatehash),
added: statediffnew,
removed: statediffremoved,
2025-04-29 06:55:54 +00:00
});
2022-06-20 12:08:58 +02:00
}
2025-04-29 06:55:54 +00:00
Ok(())
}
2025-04-29 06:55:54 +00:00
/// Returns the new shortstatehash, and the state diff from the previous
/// room state
#[implement(Service)]
#[tracing::instrument(skip(self, new_state_ids_compressed), level = "debug")]
pub async fn save_state(
&self,
room_id: &RoomId,
new_state_ids_compressed: Arc<CompressedState>,
) -> Result<HashSetCompressStateEvent> {
let previous_shortstatehash = self
.services
.state
.get_room_shortstatehash(room_id)
.await
.ok();
let state_hash =
utils::calculate_hash(new_state_ids_compressed.iter().map(|bytes| &bytes[..]));
let (new_shortstatehash, already_existed) = self
.services
.short
.get_or_create_shortstatehash(&state_hash)
.await;
if Some(new_shortstatehash) == previous_shortstatehash {
return Ok(HashSetCompressStateEvent {
shortstatehash: new_shortstatehash,
..Default::default()
});
}
2025-04-29 06:55:54 +00:00
let states_parents = if let Some(p) = previous_shortstatehash {
self.load_shortstatehash_info(p).await.unwrap_or_default()
} else {
ShortStateInfoVec::new()
};
let (statediffnew, statediffremoved) = if let Some(parent_stateinfo) = states_parents.last() {
let statediffnew: CompressedState = new_state_ids_compressed
.difference(&parent_stateinfo.full_state)
.copied()
.collect();
let statediffremoved: CompressedState = parent_stateinfo
.full_state
.difference(&new_state_ids_compressed)
.copied()
.collect();
(Arc::new(statediffnew), Arc::new(statediffremoved))
} else {
(new_state_ids_compressed, Arc::new(CompressedState::new()))
};
if !already_existed {
self.save_state_from_diff(
new_shortstatehash,
statediffnew.clone(),
statediffremoved.clone(),
2, // every state change is 2 event changes on average
states_parents,
)?;
}
2024-11-14 23:35:53 +00:00
2025-04-29 06:55:54 +00:00
Ok(HashSetCompressStateEvent {
shortstatehash: new_shortstatehash,
added: statediffnew,
removed: statediffremoved,
})
}
2024-11-14 23:35:53 +00:00
2025-04-29 06:55:54 +00:00
#[implement(Service)]
#[tracing::instrument(skip(self), level = "debug", name = "get")]
async fn get_statediff(&self, shortstatehash: ShortStateHash) -> Result<StateDiff> {
const BUFSIZE: usize = size_of::<ShortStateHash>();
const STRIDE: usize = size_of::<ShortStateHash>();
let value = self
.db
.shortstatehash_statediff
.aqry::<BUFSIZE, _>(&shortstatehash)
.await
.map_err(|e| {
err!(Database("Failed to find StateDiff from short {shortstatehash:?}: {e}"))
})?;
let parent = utils::u64_from_bytes(&value[0..size_of::<u64>()])
.ok()
.take_if(|parent| *parent != 0);
debug_assert!(value.len() % STRIDE == 0, "value not aligned to stride");
let _num_values = value.len() / STRIDE;
let mut add_mode = true;
let mut added = CompressedState::new();
let mut removed = CompressedState::new();
let mut i = STRIDE;
while let Some(v) = value.get(i..expected!(i + 2 * STRIDE)) {
if add_mode && v.starts_with(&0_u64.to_be_bytes()) {
add_mode = false;
i = expected!(i + STRIDE);
continue;
}
2025-04-29 06:55:54 +00:00
if add_mode {
added.insert(v.try_into()?);
} else {
removed.insert(v.try_into()?);
}
2025-04-29 06:55:54 +00:00
i = expected!(i + 2 * STRIDE);
}
2025-04-29 06:55:54 +00:00
Ok(StateDiff {
parent,
added: Arc::new(added),
removed: Arc::new(removed),
})
}
#[implement(Service)]
fn save_statediff(&self, shortstatehash: ShortStateHash, diff: &StateDiff) {
let mut value = Vec::<u8>::with_capacity(
2_usize
.saturating_add(diff.added.len())
.saturating_add(diff.removed.len()),
);
let parent = diff.parent.unwrap_or(0_u64);
value.extend_from_slice(&parent.to_be_bytes());
for new in diff.added.iter() {
value.extend_from_slice(&new[..]);
}
2025-04-29 06:55:54 +00:00
if !diff.removed.is_empty() {
value.extend_from_slice(&0_u64.to_be_bytes());
for removed in diff.removed.iter() {
value.extend_from_slice(&removed[..]);
}
}
self.db
.shortstatehash_statediff
.insert(&shortstatehash.to_be_bytes(), &value);
}
#[inline]
#[must_use]
pub(crate) fn compress_state_event(
shortstatekey: ShortStateKey,
shorteventid: ShortEventId,
) -> CompressedStateEvent {
const SIZE: usize = size_of::<CompressedStateEvent>();
let mut v = ArrayVec::<u8, SIZE>::new();
v.extend(shortstatekey.to_be_bytes());
v.extend(shorteventid.to_be_bytes());
v.as_ref()
.try_into()
.expect("failed to create CompressedStateEvent")
}
2024-11-17 03:57:21 +00:00
#[inline]
#[must_use]
pub(crate) fn parse_compressed_state_event(
compressed_event: CompressedStateEvent,
) -> (ShortStateKey, ShortEventId) {
2024-11-17 03:57:21 +00:00
use utils::u64_from_u8;
let shortstatekey = u64_from_u8(&compressed_event[0..size_of::<ShortStateKey>()]);
let shorteventid = u64_from_u8(&compressed_event[size_of::<ShortStateKey>()..]);
(shortstatekey, shorteventid)
}
#[inline]
fn compressed_state_size(compressed_state: &CompressedState) -> usize {
compressed_state
.len()
.checked_mul(size_of::<CompressedStateEvent>())
.expect("CompressedState size overflow")
}