Instrument caches

This commit is contained in:
Lambda 2024-05-20 10:03:53 +00:00
parent 62bff27d50
commit 67cb6f817d
8 changed files with 132 additions and 31 deletions

View file

@ -27,7 +27,9 @@ use serde_json::value::to_raw_value;
use tokio::sync::MutexGuard;
use tracing::{error, warn};
use crate::{service::pdu::PduBuilder, services, Error, PduEvent, Result};
use crate::{
service::pdu::PduBuilder, services, utils::FoundIn, Error, PduEvent, Result,
};
pub(crate) struct Service {
pub(crate) db: &'static dyn Data,
@ -121,7 +123,7 @@ impl Service {
/// Whether a server is allowed to see an event through federation, based on
/// the room's history_visibility at that event's state.
#[tracing::instrument(skip(self, origin, room_id, event_id))]
#[tracing::instrument(skip(self), fields(cache_result))]
pub(crate) fn server_can_see_event(
&self,
origin: &ServerName,
@ -138,6 +140,7 @@ impl Service {
.unwrap()
.get_mut(&(origin.to_owned(), shortstatehash))
{
FoundIn::Cache.record("cache_result");
return Ok(*visibility);
}
@ -188,6 +191,7 @@ impl Service {
}
};
FoundIn::Database.record("cache_result");
self.server_visibility_cache
.lock()
.unwrap()
@ -198,7 +202,7 @@ impl Service {
/// Whether a user is allowed to see an event, based on
/// the room's history_visibility at that event's state.
#[tracing::instrument(skip(self, user_id, room_id, event_id))]
#[tracing::instrument(skip(self), fields(cache_result))]
pub(crate) fn user_can_see_event(
&self,
user_id: &UserId,
@ -215,6 +219,7 @@ impl Service {
.unwrap()
.get_mut(&(user_id.to_owned(), shortstatehash))
{
FoundIn::Cache.record("cache_result");
return Ok(*visibility);
}
@ -257,6 +262,7 @@ impl Service {
}
};
FoundIn::Database.record("cache_result");
self.user_visibility_cache
.lock()
.unwrap()

View file

@ -10,7 +10,11 @@ use lru_cache::LruCache;
use ruma::{EventId, RoomId};
use self::data::StateDiff;
use crate::{services, utils, Result};
use crate::{
services,
utils::{self, FoundIn},
Result,
};
#[derive(Clone)]
pub(crate) struct CompressedStateLayer {
@ -33,7 +37,7 @@ impl Service {
/// Returns a stack with info on shortstatehash, full state, added diff and
/// removed diff for the selected shortstatehash and each parent layer.
#[allow(clippy::type_complexity)]
#[tracing::instrument(skip(self))]
#[tracing::instrument(skip(self), fields(cache_result))]
pub(crate) fn load_shortstatehash_info(
&self,
shortstatehash: u64,
@ -41,6 +45,7 @@ impl Service {
if let Some(r) =
self.stateinfo_cache.lock().unwrap().get_mut(&shortstatehash)
{
FoundIn::Cache.record("cache_result");
return Ok(r.clone());
}
@ -50,7 +55,7 @@ impl Service {
removed,
} = self.db.get_statediff(shortstatehash)?;
if let Some(parent) = parent {
let response = if let Some(parent) = parent {
let mut response = self.load_shortstatehash_info(parent)?;
let mut state = (*response.last().unwrap().full_state).clone();
state.extend(added.iter().copied());
@ -65,26 +70,23 @@ impl Service {
added,
removed: Arc::new(removed),
});
self.stateinfo_cache
.lock()
.unwrap()
.insert(shortstatehash, response.clone());
Ok(response)
response
} else {
let response = vec![CompressedStateLayer {
vec![CompressedStateLayer {
shortstatehash,
full_state: added.clone(),
added,
removed,
}];
self.stateinfo_cache
.lock()
.unwrap()
.insert(shortstatehash, response.clone());
Ok(response)
}
}]
};
FoundIn::Database.record("cache_result");
self.stateinfo_cache
.lock()
.unwrap()
.insert(shortstatehash, response.clone());
Ok(response)
}
// Allowed because this function uses `services()`