From 7563360bee418f9cc7d9a0783a2993fb136b74d1 Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Mon, 30 Sep 2024 20:37:50 -0700 Subject: [PATCH] move pdu_cache to service --- src/config.rs | 4 +-- src/database.rs | 9 +----- src/database/key_value/rooms/timeline.rs | 25 ++------------- src/service.rs | 5 +-- src/service/rooms/timeline.rs | 40 +++++++++++++++++++++--- src/service/rooms/timeline/data.rs | 1 - 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index c980c26b..c53f1dd5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -49,7 +49,7 @@ pub(crate) struct Config { #[serde(default = "default_cache_capacity_modifier")] pub(crate) cache_capacity_modifier: f64, #[serde(default = "default_pdu_cache_capacity")] - pub(crate) pdu_cache_capacity: u32, + pub(crate) pdu_cache_capacity: usize, #[serde(default = "default_cleanup_second_interval")] pub(crate) cleanup_second_interval: u32, #[serde(default = "default_max_request_size")] @@ -390,7 +390,7 @@ fn default_rocksdb_max_open_files() -> i32 { 1000 } -fn default_pdu_cache_capacity() -> u32 { +fn default_pdu_cache_capacity() -> usize { 150_000 } diff --git a/src/database.rs b/src/database.rs index d3704497..ea1fe1c3 100644 --- a/src/database.rs +++ b/src/database.rs @@ -27,7 +27,7 @@ use crate::{ timeline::PduCount, }, }, - services, utils, Config, Error, PduEvent, Result, + services, utils, Config, Error, Result, }; pub(crate) mod abstraction; @@ -236,7 +236,6 @@ pub(crate) struct KeyValueDatabase { pub(super) senderkey_pusher: Arc, // Uncategorized trees - pub(super) pdu_cache: Mutex>>, pub(super) shorteventid_cache: Mutex>>, pub(super) auth_chain_cache: Mutex, Arc>>>, @@ -468,12 +467,6 @@ impl KeyValueDatabase { global: builder.open_tree("global")?, server_signingkeys: builder.open_tree("server_signingkeys")?, - pdu_cache: Mutex::new(LruCache::new( - config - .pdu_cache_capacity - .try_into() - .expect("pdu cache capacity fits into usize"), - )), #[allow( clippy::as_conversions, clippy::cast_sign_loss, diff --git a/src/database/key_value/rooms/timeline.rs b/src/database/key_value/rooms/timeline.rs index 656598db..71b75652 100644 --- a/src/database/key_value/rooms/timeline.rs +++ b/src/database/key_value/rooms/timeline.rs @@ -132,14 +132,7 @@ impl service::rooms::timeline::Data for KeyValueDatabase { /// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[tracing::instrument(skip(self))] fn get_pdu(&self, event_id: &EventId) -> Result>> { - let lookup = Lookup::Pdu; - - if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(Some(Arc::clone(p))); - } - - if let Some(pdu) = self + Ok(self .get_non_outlier_pdu(event_id)? .map_or_else( || { @@ -154,18 +147,7 @@ impl service::rooms::timeline::Data for KeyValueDatabase { }, |x| Ok(Some(x)), )? - .map(Arc::new) - { - METRICS.record_lookup(lookup, FoundIn::Database); - self.pdu_cache - .lock() - .unwrap() - .insert(event_id.to_owned(), Arc::clone(&pdu)); - Ok(Some(pdu)) - } else { - METRICS.record_lookup(lookup, FoundIn::Nothing); - Ok(None) - } + .map(Arc::new)) } /// Returns the pdu. @@ -241,7 +223,6 @@ impl service::rooms::timeline::Data for KeyValueDatabase { &self, pdu_id: &PduId, pdu_json: &CanonicalJsonObject, - pdu: &PduEvent, ) -> Result<()> { if self.pduid_pdu.get(pdu_id.as_bytes())?.is_some() { self.pduid_pdu.insert( @@ -256,8 +237,6 @@ impl service::rooms::timeline::Data for KeyValueDatabase { )); } - self.pdu_cache.lock().unwrap().remove(&(*pdu.event_id).to_owned()); - Ok(()) } diff --git a/src/service.rs b/src/service.rs index df7735aa..73633685 100644 --- a/src/service.rs +++ b/src/service.rs @@ -130,9 +130,10 @@ impl Services { (100.0 * config.cache_capacity_modifier) as usize, )), }, - timeline: rooms::timeline::Service { + timeline: rooms::timeline::Service::new( db, - }, + config.pdu_cache_capacity, + ), threads: rooms::threads::Service { db, }, diff --git a/src/service/rooms/timeline.rs b/src/service/rooms/timeline.rs index 933524e2..341fb1c4 100644 --- a/src/service/rooms/timeline.rs +++ b/src/service/rooms/timeline.rs @@ -1,9 +1,10 @@ use std::{ cmp::Ordering, collections::{BTreeMap, HashSet}, - sync::Arc, + sync::{Arc, Mutex}, }; +use lru_cache::LruCache; use ruma::{ api::{client::error::ErrorKind, federation}, canonical_json::to_canonical_value, @@ -30,6 +31,7 @@ use tracing::{error, info, warn}; use super::{short::ShortRoomId, state_compressor::CompressedStateEvent}; use crate::{ api::server_server, + observability::{FoundIn, Lookup, METRICS}, service::{ appservice::NamespaceRegex, globals::{marker, SigningKeys}, @@ -111,10 +113,21 @@ impl Ord for PduCount { } pub(crate) struct Service { - pub(crate) db: &'static dyn Data, + db: &'static dyn Data, + pdu_cache: Mutex>>, } impl Service { + pub(crate) fn new( + db: &'static dyn Data, + pdu_cache_capacity: usize, + ) -> Self { + Self { + db, + pdu_cache: Mutex::new(LruCache::new(pdu_cache_capacity)), + } + } + #[tracing::instrument(skip(self))] pub(crate) fn first_pdu_in_room( &self, @@ -174,7 +187,24 @@ impl Service { &self, event_id: &EventId, ) -> Result>> { - self.db.get_pdu(event_id) + let lookup = Lookup::Pdu; + + if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(Some(Arc::clone(p))); + } + + if let Some(pdu) = self.db.get_pdu(event_id)? { + METRICS.record_lookup(lookup, FoundIn::Database); + self.pdu_cache + .lock() + .unwrap() + .insert(event_id.to_owned(), Arc::clone(&pdu)); + Ok(Some(pdu)) + } else { + METRICS.record_lookup(lookup, FoundIn::Nothing); + Ok(None) + } } /// Returns the pdu. @@ -203,7 +233,9 @@ impl Service { pdu_json: &CanonicalJsonObject, pdu: &PduEvent, ) -> Result<()> { - self.db.replace_pdu(pdu_id, pdu_json, pdu) + self.db.replace_pdu(pdu_id, pdu_json)?; + self.pdu_cache.lock().unwrap().remove(&(*pdu.event_id).to_owned()); + Ok(()) } /// Creates a new persisted data unit and adds it to a room. diff --git a/src/service/rooms/timeline/data.rs b/src/service/rooms/timeline/data.rs index acd0b33c..a9a85760 100644 --- a/src/service/rooms/timeline/data.rs +++ b/src/service/rooms/timeline/data.rs @@ -76,7 +76,6 @@ pub(crate) trait Data: Send + Sync { &self, pdu_id: &PduId, pdu_json: &CanonicalJsonObject, - pdu: &PduEvent, ) -> Result<()>; /// Returns an iterator over all events and their tokens in a room that