From 2b2b4169df0e0d62d9863dee706ece80e4abc33c Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Mon, 30 Sep 2024 21:45:04 -0700 Subject: [PATCH] move eventidshort_cache to service --- src/database.rs | 11 +-------- src/database/key_value/rooms/short.rs | 35 +++++++-------------------- src/service.rs | 8 ++++++ src/service/rooms/short.rs | 31 ++++++++++++++++++++++-- src/service/rooms/short/data.rs | 3 ++- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/database.rs b/src/database.rs index e506f4f1..d625a10f 100644 --- a/src/database.rs +++ b/src/database.rs @@ -13,7 +13,7 @@ use ruma::{ push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType, }, push::Ruleset, - EventId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId, + EventId, OwnedRoomId, OwnedUserId, RoomId, UserId, }; use tracing::{debug, error, info, info_span, warn, Instrument}; @@ -236,7 +236,6 @@ pub(crate) struct KeyValueDatabase { pub(super) senderkey_pusher: Arc, // Uncategorized trees - pub(super) eventidshort_cache: Mutex>, pub(super) statekeyshort_cache: Mutex>, pub(super) shortstatekey_cache: @@ -464,14 +463,6 @@ impl KeyValueDatabase { global: builder.open_tree("global")?, server_signingkeys: builder.open_tree("server_signingkeys")?, - #[allow( - clippy::as_conversions, - clippy::cast_sign_loss, - clippy::cast_possible_truncation - )] - eventidshort_cache: Mutex::new(LruCache::new( - (100_000.0 * config.cache_capacity_modifier) as usize, - )), #[allow( clippy::as_conversions, clippy::cast_sign_loss, diff --git a/src/database/key_value/rooms/short.rs b/src/database/key_value/rooms/short.rs index 15840e6e..c60f1bb8 100644 --- a/src/database/key_value/rooms/short.rs +++ b/src/database/key_value/rooms/short.rs @@ -19,43 +19,26 @@ impl service::rooms::short::Data for KeyValueDatabase { fn get_or_create_shorteventid( &self, event_id: &EventId, - ) -> Result { - let lookup = Lookup::CreateEventIdToShort; - - if let Some(short) = - self.eventidshort_cache.lock().unwrap().get_mut(event_id) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(*short); - } - - let short = if let Some(shorteventid) = + ) -> Result<(ShortEventId, bool)> { + let (short, created) = if let Some(shorteventid) = self.eventid_shorteventid.get(event_id.as_bytes())? { - METRICS.record_lookup(lookup, FoundIn::Database); + let shorteventid = + utils::u64_from_bytes(&shorteventid).map_err(|_| { + Error::bad_database("Invalid shorteventid in db.") + })?; - utils::u64_from_bytes(&shorteventid).map_err(|_| { - Error::bad_database("Invalid shorteventid in db.") - })? + (shorteventid, false) } else { - METRICS.record_lookup(lookup, FoundIn::Nothing); - let shorteventid = services().globals.next_count()?; self.eventid_shorteventid .insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?; self.shorteventid_eventid .insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?; - shorteventid + (shorteventid, true) }; - let short = ShortEventId::new(short); - - self.eventidshort_cache - .lock() - .unwrap() - .insert(event_id.to_owned(), short); - - Ok(short) + Ok((ShortEventId::new(short), created)) } #[tracing::instrument(skip(self), fields(cache_result))] diff --git a/src/service.rs b/src/service.rs index c5725f83..d25dfe7c 100644 --- a/src/service.rs +++ b/src/service.rs @@ -112,6 +112,14 @@ impl Services { { (100_000.0 * config.cache_capacity_modifier) as usize }, + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] + { + (100_000.0 * config.cache_capacity_modifier) as usize + }, ), state: rooms::state::Service { db, diff --git a/src/service/rooms/short.rs b/src/service/rooms/short.rs index 87554692..2c18e668 100644 --- a/src/service/rooms/short.rs +++ b/src/service/rooms/short.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Mutex}; use lru_cache::LruCache; -use ruma::{events::StateEventType, EventId, RoomId}; +use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId}; use crate::{ observability::{FoundIn, Lookup, METRICS}, @@ -38,18 +38,23 @@ pub(crate) use data::Data; pub(crate) struct Service { db: &'static dyn Data, shorteventid_cache: Mutex>>, + eventidshort_cache: Mutex>, } impl Service { pub(crate) fn new( db: &'static dyn Data, shorteventid_cache_size: usize, + eventidshort_cache_size: usize, ) -> Self { Self { db, shorteventid_cache: Mutex::new(LruCache::new( shorteventid_cache_size, )), + eventidshort_cache: Mutex::new(LruCache::new( + eventidshort_cache_size, + )), } } @@ -57,7 +62,29 @@ impl Service { &self, event_id: &EventId, ) -> Result { - self.db.get_or_create_shorteventid(event_id) + let lookup = Lookup::CreateEventIdToShort; + + if let Some(short) = + self.eventidshort_cache.lock().unwrap().get_mut(event_id) + { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(*short); + } + + let (short, created) = self.db.get_or_create_shorteventid(event_id)?; + + if created { + METRICS.record_lookup(lookup, FoundIn::Nothing); + } else { + METRICS.record_lookup(lookup, FoundIn::Database); + } + + self.eventidshort_cache + .lock() + .unwrap() + .insert(event_id.to_owned(), short); + + Ok(short) } pub(crate) fn get_shortstatekey( diff --git a/src/service/rooms/short/data.rs b/src/service/rooms/short/data.rs index 3650a0b1..7b3fe19e 100644 --- a/src/service/rooms/short/data.rs +++ b/src/service/rooms/short/data.rs @@ -6,10 +6,11 @@ use super::{ShortEventId, ShortRoomId, ShortStateHash, ShortStateKey}; use crate::Result; pub(crate) trait Data: Send + Sync { + /// The returned bool indicates whether it was created fn get_or_create_shorteventid( &self, event_id: &EventId, - ) -> Result; + ) -> Result<(ShortEventId, bool)>; fn get_shortstatekey( &self,