From 47502d1f361ef0efe8915c494e3f626b9c6f2f09 Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Mon, 30 Sep 2024 21:06:37 -0700 Subject: [PATCH] move shorteventid_cache to service --- src/database.rs | 9 ------- src/database/key_value/rooms/short.rs | 16 ------------ src/service.rs | 13 +++++++++- src/service/rooms/short.rs | 37 ++++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/database.rs b/src/database.rs index ea1fe1c3..ba3709cd 100644 --- a/src/database.rs +++ b/src/database.rs @@ -236,7 +236,6 @@ pub(crate) struct KeyValueDatabase { pub(super) senderkey_pusher: Arc, // Uncategorized trees - pub(super) shorteventid_cache: Mutex>>, pub(super) auth_chain_cache: Mutex, Arc>>>, pub(super) eventidshort_cache: Mutex>, @@ -480,14 +479,6 @@ impl KeyValueDatabase { clippy::cast_sign_loss, clippy::cast_possible_truncation )] - shorteventid_cache: Mutex::new(LruCache::new( - (100_000.0 * config.cache_capacity_modifier) as usize, - )), - #[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, )), diff --git a/src/database/key_value/rooms/short.rs b/src/database/key_value/rooms/short.rs index ca499ed2..15840e6e 100644 --- a/src/database/key_value/rooms/short.rs +++ b/src/database/key_value/rooms/short.rs @@ -162,15 +162,6 @@ impl service::rooms::short::Data for KeyValueDatabase { &self, shorteventid: ShortEventId, ) -> Result> { - let lookup = Lookup::ShortToEventId; - - if let Some(id) = - self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(Arc::clone(id)); - } - let bytes = self .shorteventid_eventid .get(&shorteventid.get().to_be_bytes())? @@ -189,13 +180,6 @@ impl service::rooms::short::Data for KeyValueDatabase { Error::bad_database("EventId in shorteventid_eventid is invalid.") })?; - METRICS.record_lookup(lookup, FoundIn::Database); - - self.shorteventid_cache - .lock() - .unwrap() - .insert(shorteventid, Arc::clone(&event_id)); - Ok(event_id) } diff --git a/src/service.rs b/src/service.rs index 73633685..6f038ecb 100644 --- a/src/service.rs +++ b/src/service.rs @@ -45,6 +45,7 @@ pub(crate) struct Services { } impl Services { + #[allow(clippy::too_many_lines)] pub(crate) fn build< D: appservice::Data + pusher::Data @@ -93,7 +94,17 @@ impl Services { db, }, search: db, - short: rooms::short::Service::new(db), + short: rooms::short::Service::new( + db, + #[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 d165ef9e..87554692 100644 --- a/src/service/rooms/short.rs +++ b/src/service/rooms/short.rs @@ -1,8 +1,12 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; +use lru_cache::LruCache; use ruma::{events::StateEventType, EventId, RoomId}; -use crate::utils::error::Result; +use crate::{ + observability::{FoundIn, Lookup, METRICS}, + utils::error::Result, +}; macro_rules! short_id_type { ($name:ident) => { @@ -33,12 +37,19 @@ pub(crate) use data::Data; pub(crate) struct Service { db: &'static dyn Data, + shorteventid_cache: Mutex>>, } impl Service { - pub(crate) fn new(db: &'static dyn Data) -> Self { + pub(crate) fn new( + db: &'static dyn Data, + shorteventid_cache_size: usize, + ) -> Self { Self { db, + shorteventid_cache: Mutex::new(LruCache::new( + shorteventid_cache_size, + )), } } @@ -69,7 +80,25 @@ impl Service { &self, shorteventid: ShortEventId, ) -> Result> { - self.db.get_eventid_from_short(shorteventid) + let lookup = Lookup::ShortToEventId; + + if let Some(id) = + self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid) + { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(Arc::clone(id)); + } + + let event_id = self.db.get_eventid_from_short(shorteventid)?; + + METRICS.record_lookup(lookup, FoundIn::Database); + + self.shorteventid_cache + .lock() + .unwrap() + .insert(shorteventid, Arc::clone(&event_id)); + + Ok(event_id) } pub(crate) fn get_statekey_from_short(