move shortstatekey_cache to service

This commit is contained in:
Charles Hall 2024-10-08 13:38:12 -07:00
parent 190b788683
commit d3b62e598d
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
4 changed files with 35 additions and 35 deletions

View file

@ -7,11 +7,8 @@ use std::{
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
}; };
use lru_cache::LruCache;
use ruma::{ use ruma::{
events::{ events::{push_rules::PushRulesEvent, GlobalAccountDataEventType},
push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType,
},
push::Ruleset, push::Ruleset,
EventId, OwnedRoomId, OwnedUserId, RoomId, UserId, EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
}; };
@ -236,8 +233,6 @@ pub(crate) struct KeyValueDatabase {
pub(super) senderkey_pusher: Arc<dyn KvTree>, pub(super) senderkey_pusher: Arc<dyn KvTree>,
// Uncategorized trees // Uncategorized trees
pub(super) shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
pub(super) our_real_users_cache: pub(super) our_real_users_cache:
RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>, RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>,
pub(super) appservice_in_room_cache: pub(super) appservice_in_room_cache:
@ -461,14 +456,6 @@ impl KeyValueDatabase {
global: builder.open_tree("global")?, global: builder.open_tree("global")?,
server_signingkeys: builder.open_tree("server_signingkeys")?, server_signingkeys: builder.open_tree("server_signingkeys")?,
#[allow(
clippy::as_conversions,
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
shortstatekey_cache: Mutex::new(LruCache::new(
(100_000.0 * config.cache_capacity_modifier) as usize,
)),
our_real_users_cache: RwLock::new(HashMap::new()), our_real_users_cache: RwLock::new(HashMap::new()),
appservice_in_room_cache: RwLock::new(HashMap::new()), appservice_in_room_cache: RwLock::new(HashMap::new()),
lasttimelinecount_cache: Mutex::new(HashMap::new()), lasttimelinecount_cache: Mutex::new(HashMap::new()),

View file

@ -4,7 +4,6 @@ use ruma::{events::StateEventType, EventId, RoomId};
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS},
service::{ service::{
self, self,
rooms::short::{ rooms::short::{
@ -130,15 +129,6 @@ impl service::rooms::short::Data for KeyValueDatabase {
&self, &self,
shortstatekey: ShortStateKey, shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> { ) -> Result<(StateEventType, String)> {
let lookup = Lookup::ShortToStateKey;
if let Some(id) =
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(id.clone());
}
let bytes = self let bytes = self
.shortstatekey_statekey .shortstatekey_statekey
.get(&shortstatekey.get().to_be_bytes())? .get(&shortstatekey.get().to_be_bytes())?
@ -168,16 +158,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
) )
})?; })?;
let result = (event_type, state_key); Ok((event_type, state_key))
METRICS.record_lookup(lookup, FoundIn::Database);
self.shortstatekey_cache
.lock()
.unwrap()
.insert(shortstatekey, result.clone());
Ok(result)
} }
/// Returns `(shortstatehash, already_existed)` /// Returns `(shortstatehash, already_existed)`

View file

@ -128,6 +128,14 @@ impl Services {
{ {
(100_000.0 * config.cache_capacity_modifier) as usize (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 { state: rooms::state::Service {
db, db,

View file

@ -41,6 +41,8 @@ pub(crate) struct Service {
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>, eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
statekeyshort_cache: statekeyshort_cache:
Mutex<LruCache<(StateEventType, String), ShortStateKey>>, Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
} }
impl Service { impl Service {
@ -49,6 +51,7 @@ impl Service {
shorteventid_cache_size: usize, shorteventid_cache_size: usize,
eventidshort_cache_size: usize, eventidshort_cache_size: usize,
statekeyshort_cache_size: usize, statekeyshort_cache_size: usize,
shortstatekey_cache_size: usize,
) -> Self { ) -> Self {
Self { Self {
db, db,
@ -61,6 +64,9 @@ impl Service {
statekeyshort_cache: Mutex::new(LruCache::new( statekeyshort_cache: Mutex::new(LruCache::new(
statekeyshort_cache_size, statekeyshort_cache_size,
)), )),
shortstatekey_cache: Mutex::new(LruCache::new(
shortstatekey_cache_size,
)),
} }
} }
@ -189,7 +195,25 @@ impl Service {
&self, &self,
shortstatekey: ShortStateKey, shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> { ) -> Result<(StateEventType, String)> {
self.db.get_statekey_from_short(shortstatekey) let lookup = Lookup::ShortToStateKey;
if let Some(id) =
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(id.clone());
}
let x = self.db.get_statekey_from_short(shortstatekey)?;
METRICS.record_lookup(lookup, FoundIn::Database);
self.shortstatekey_cache
.lock()
.unwrap()
.insert(shortstatekey, x.clone());
Ok(x)
} }
/// Returns `(shortstatehash, already_existed)` /// Returns `(shortstatehash, already_existed)`