mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 16:21:24 +01:00
record FoundIn with metrics instead of traces
This is much more efficient in terms of network use and data storage, and also easier to visualize.
This commit is contained in:
parent
9364d44ce2
commit
0c2094a56f
8 changed files with 142 additions and 68 deletions
|
|
@ -1,19 +1,23 @@
|
|||
use std::{collections::HashSet, mem::size_of, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
database::KeyValueDatabase, observability::FoundIn, service, utils, Result,
|
||||
database::KeyValueDatabase,
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
service, utils, Result,
|
||||
};
|
||||
|
||||
impl service::rooms::auth_chain::Data for KeyValueDatabase {
|
||||
#[tracing::instrument(skip(self, key), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self, key))]
|
||||
fn get_cached_eventid_authchain(
|
||||
&self,
|
||||
key: &[u64],
|
||||
) -> Result<Option<Arc<HashSet<u64>>>> {
|
||||
let lookup = Lookup::AuthChain;
|
||||
|
||||
// Check RAM cache
|
||||
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key)
|
||||
{
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Some(Arc::clone(result)));
|
||||
}
|
||||
|
||||
|
|
@ -34,7 +38,7 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
|
|||
});
|
||||
|
||||
if let Some(chain) = chain {
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
let chain = Arc::new(chain);
|
||||
|
||||
// Cache in RAM
|
||||
|
|
@ -47,7 +51,7 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
|
|||
}
|
||||
}
|
||||
|
||||
FoundIn::Nothing.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,30 +3,33 @@ use std::sync::Arc;
|
|||
use ruma::{events::StateEventType, EventId, RoomId};
|
||||
|
||||
use crate::{
|
||||
database::KeyValueDatabase, observability::FoundIn, service, services,
|
||||
utils, Error, Result,
|
||||
database::KeyValueDatabase,
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
service, services, utils, Error, Result,
|
||||
};
|
||||
|
||||
impl service::rooms::short::Data for KeyValueDatabase {
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64> {
|
||||
let lookup = Lookup::CreateEventIdToShort;
|
||||
|
||||
if let Some(short) =
|
||||
self.eventidshort_cache.lock().unwrap().get_mut(event_id)
|
||||
{
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
let short = if let Some(shorteventid) =
|
||||
self.eventid_shorteventid.get(event_id.as_bytes())?
|
||||
{
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
utils::u64_from_bytes(&shorteventid).map_err(|_| {
|
||||
Error::bad_database("Invalid shorteventid in db.")
|
||||
})?
|
||||
} else {
|
||||
FoundIn::Nothing.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
|
||||
let shorteventid = services().globals.next_count()?;
|
||||
self.eventid_shorteventid
|
||||
|
|
@ -50,13 +53,15 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<Option<u64>> {
|
||||
let lookup = Lookup::StateKeyToShort;
|
||||
|
||||
if let Some(short) = self
|
||||
.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||
{
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Some(*short));
|
||||
}
|
||||
|
||||
|
|
@ -75,32 +80,34 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
.transpose()?;
|
||||
|
||||
if let Some(s) = short {
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
self.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((event_type.clone(), state_key.to_owned()), s);
|
||||
} else {
|
||||
FoundIn::Nothing.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
}
|
||||
|
||||
Ok(short)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn get_or_create_shortstatekey(
|
||||
&self,
|
||||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<u64> {
|
||||
let lookup = Lookup::CreateStateKeyToShort;
|
||||
|
||||
if let Some(short) = self
|
||||
.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||
{
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
|
|
@ -111,13 +118,13 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
let short = if let Some(shortstatekey) =
|
||||
self.statekey_shortstatekey.get(&db_key)?
|
||||
{
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
utils::u64_from_bytes(&shortstatekey).map_err(|_| {
|
||||
Error::bad_database("Invalid shortstatekey in db.")
|
||||
})?
|
||||
} else {
|
||||
FoundIn::Nothing.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
|
||||
let shortstatekey = services().globals.next_count()?;
|
||||
self.statekey_shortstatekey
|
||||
|
|
@ -135,15 +142,17 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
Ok(short)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn get_eventid_from_short(
|
||||
&self,
|
||||
shorteventid: u64,
|
||||
) -> Result<Arc<EventId>> {
|
||||
let lookup = Lookup::ShortToEventId;
|
||||
|
||||
if let Some(id) =
|
||||
self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid)
|
||||
{
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Arc::clone(id));
|
||||
}
|
||||
|
||||
|
|
@ -165,7 +174,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
Error::bad_database("EventId in shorteventid_eventid is invalid.")
|
||||
})?;
|
||||
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
self.shorteventid_cache
|
||||
.lock()
|
||||
|
|
@ -175,15 +184,17 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
Ok(event_id)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn get_statekey_from_short(
|
||||
&self,
|
||||
shortstatekey: u64,
|
||||
) -> Result<(StateEventType, String)> {
|
||||
let lookup = Lookup::ShortToStateKey;
|
||||
|
||||
if let Some(id) =
|
||||
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey)
|
||||
{
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(id.clone());
|
||||
}
|
||||
|
||||
|
|
@ -218,7 +229,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
|
||||
let result = (event_type, state_key);
|
||||
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
self.shortstatekey_cache
|
||||
.lock()
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use ruma::{
|
|||
|
||||
use crate::{
|
||||
database::KeyValueDatabase,
|
||||
observability::FoundIn,
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
service::{self, appservice::RegistrationInfo},
|
||||
services, utils, Error, Result,
|
||||
};
|
||||
|
|
@ -171,19 +171,21 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn get_our_real_users(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
) -> Result<Arc<HashSet<OwnedUserId>>> {
|
||||
let lookup = Lookup::OurRealUsers;
|
||||
|
||||
let maybe =
|
||||
self.our_real_users_cache.read().unwrap().get(room_id).cloned();
|
||||
if let Some(users) = maybe {
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
Ok(users)
|
||||
} else {
|
||||
self.update_joined_count(room_id)?;
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
Ok(Arc::clone(
|
||||
self.our_real_users_cache.read().unwrap().get(room_id).unwrap(),
|
||||
))
|
||||
|
|
@ -192,13 +194,15 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
|
|||
|
||||
#[tracing::instrument(
|
||||
skip(self, appservice),
|
||||
fields(cache_result, appservice_id = appservice.registration.id),
|
||||
fields(appservice_id = appservice.registration.id),
|
||||
)]
|
||||
fn appservice_in_room(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
appservice: &RegistrationInfo,
|
||||
) -> Result<bool> {
|
||||
let lookup = Lookup::AppserviceInRoom;
|
||||
|
||||
let maybe = self
|
||||
.appservice_in_room_cache
|
||||
.read()
|
||||
|
|
@ -208,7 +212,7 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
|
|||
.copied();
|
||||
|
||||
if let Some(b) = maybe {
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
Ok(b)
|
||||
} else {
|
||||
let bridge_user_id = UserId::parse_with_server_name(
|
||||
|
|
@ -225,7 +229,7 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
|
|||
})
|
||||
});
|
||||
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
self.appservice_in_room_cache
|
||||
.write()
|
||||
.unwrap()
|
||||
|
|
|
|||
|
|
@ -8,17 +8,20 @@ use service::rooms::timeline::PduCount;
|
|||
use tracing::error;
|
||||
|
||||
use crate::{
|
||||
database::KeyValueDatabase, observability::FoundIn, service, services,
|
||||
utils, Error, PduEvent, Result,
|
||||
database::KeyValueDatabase,
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
service, services, utils, Error, PduEvent, Result,
|
||||
};
|
||||
|
||||
impl service::rooms::timeline::Data for KeyValueDatabase {
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn last_timeline_count(
|
||||
&self,
|
||||
sender_user: &UserId,
|
||||
room_id: &RoomId,
|
||||
) -> Result<PduCount> {
|
||||
let lookup = Lookup::LastTimelineCount;
|
||||
|
||||
match self
|
||||
.lasttimelinecount_cache
|
||||
.lock()
|
||||
|
|
@ -35,15 +38,15 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
|
|||
r.ok()
|
||||
})
|
||||
{
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
Ok(*v.insert(last_count.0))
|
||||
} else {
|
||||
FoundIn::Nothing.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
Ok(PduCount::Normal(0))
|
||||
}
|
||||
}
|
||||
hash_map::Entry::Occupied(o) => {
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
Ok(*o.get())
|
||||
}
|
||||
}
|
||||
|
|
@ -125,10 +128,12 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
|
|||
/// Returns the pdu.
|
||||
///
|
||||
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
#[tracing::instrument(skip(self))]
|
||||
fn get_pdu(&self, event_id: &EventId) -> Result<Option<Arc<PduEvent>>> {
|
||||
let lookup = Lookup::Pdu;
|
||||
|
||||
if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) {
|
||||
FoundIn::Cache.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Some(Arc::clone(p)));
|
||||
}
|
||||
|
||||
|
|
@ -149,14 +154,14 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
|
|||
)?
|
||||
.map(Arc::new)
|
||||
{
|
||||
FoundIn::Database.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
self.pdu_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(event_id.to_owned(), Arc::clone(&pdu));
|
||||
Ok(Some(pdu))
|
||||
} else {
|
||||
FoundIn::Nothing.record("cache_result");
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue