mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 07:41:23 +01:00
move eventidshort_cache to service
This commit is contained in:
parent
095ee483ac
commit
2b2b4169df
5 changed files with 49 additions and 39 deletions
|
|
@ -13,7 +13,7 @@ use ruma::{
|
||||||
push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType,
|
push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType,
|
||||||
},
|
},
|
||||||
push::Ruleset,
|
push::Ruleset,
|
||||||
EventId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
|
EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
|
||||||
};
|
};
|
||||||
use tracing::{debug, error, info, info_span, warn, Instrument};
|
use tracing::{debug, error, info, info_span, warn, Instrument};
|
||||||
|
|
||||||
|
|
@ -236,7 +236,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) eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
|
||||||
pub(super) statekeyshort_cache:
|
pub(super) statekeyshort_cache:
|
||||||
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
|
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
|
||||||
pub(super) shortstatekey_cache:
|
pub(super) shortstatekey_cache:
|
||||||
|
|
@ -464,14 +463,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
|
|
||||||
)]
|
|
||||||
eventidshort_cache: Mutex::new(LruCache::new(
|
|
||||||
(100_000.0 * config.cache_capacity_modifier) as usize,
|
|
||||||
)),
|
|
||||||
#[allow(
|
#[allow(
|
||||||
clippy::as_conversions,
|
clippy::as_conversions,
|
||||||
clippy::cast_sign_loss,
|
clippy::cast_sign_loss,
|
||||||
|
|
|
||||||
|
|
@ -19,43 +19,26 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
||||||
fn get_or_create_shorteventid(
|
fn get_or_create_shorteventid(
|
||||||
&self,
|
&self,
|
||||||
event_id: &EventId,
|
event_id: &EventId,
|
||||||
) -> Result<ShortEventId> {
|
) -> Result<(ShortEventId, bool)> {
|
||||||
let lookup = Lookup::CreateEventIdToShort;
|
let (short, created) = if let Some(shorteventid) =
|
||||||
|
|
||||||
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) =
|
|
||||||
self.eventid_shorteventid.get(event_id.as_bytes())?
|
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(|_| {
|
(shorteventid, false)
|
||||||
Error::bad_database("Invalid shorteventid in db.")
|
|
||||||
})?
|
|
||||||
} else {
|
} else {
|
||||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
|
||||||
|
|
||||||
let shorteventid = services().globals.next_count()?;
|
let shorteventid = services().globals.next_count()?;
|
||||||
self.eventid_shorteventid
|
self.eventid_shorteventid
|
||||||
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
|
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
|
||||||
self.shorteventid_eventid
|
self.shorteventid_eventid
|
||||||
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
|
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
|
||||||
shorteventid
|
(shorteventid, true)
|
||||||
};
|
};
|
||||||
|
|
||||||
let short = ShortEventId::new(short);
|
Ok((ShortEventId::new(short), created))
|
||||||
|
|
||||||
self.eventidshort_cache
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.insert(event_id.to_owned(), short);
|
|
||||||
|
|
||||||
Ok(short)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,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,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use lru_cache::LruCache;
|
use lru_cache::LruCache;
|
||||||
use ruma::{events::StateEventType, EventId, RoomId};
|
use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
observability::{FoundIn, Lookup, METRICS},
|
observability::{FoundIn, Lookup, METRICS},
|
||||||
|
|
@ -38,18 +38,23 @@ pub(crate) use data::Data;
|
||||||
pub(crate) struct Service {
|
pub(crate) struct Service {
|
||||||
db: &'static dyn Data,
|
db: &'static dyn Data,
|
||||||
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
|
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
|
||||||
|
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl Service {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
db: &'static dyn Data,
|
db: &'static dyn Data,
|
||||||
shorteventid_cache_size: usize,
|
shorteventid_cache_size: usize,
|
||||||
|
eventidshort_cache_size: usize,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
db,
|
db,
|
||||||
shorteventid_cache: Mutex::new(LruCache::new(
|
shorteventid_cache: Mutex::new(LruCache::new(
|
||||||
shorteventid_cache_size,
|
shorteventid_cache_size,
|
||||||
)),
|
)),
|
||||||
|
eventidshort_cache: Mutex::new(LruCache::new(
|
||||||
|
eventidshort_cache_size,
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,7 +62,29 @@ impl Service {
|
||||||
&self,
|
&self,
|
||||||
event_id: &EventId,
|
event_id: &EventId,
|
||||||
) -> Result<ShortEventId> {
|
) -> Result<ShortEventId> {
|
||||||
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(
|
pub(crate) fn get_shortstatekey(
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,11 @@ use super::{ShortEventId, ShortRoomId, ShortStateHash, ShortStateKey};
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
pub(crate) trait Data: Send + Sync {
|
pub(crate) trait Data: Send + Sync {
|
||||||
|
/// The returned bool indicates whether it was created
|
||||||
fn get_or_create_shorteventid(
|
fn get_or_create_shorteventid(
|
||||||
&self,
|
&self,
|
||||||
event_id: &EventId,
|
event_id: &EventId,
|
||||||
) -> Result<ShortEventId>;
|
) -> Result<(ShortEventId, bool)>;
|
||||||
|
|
||||||
fn get_shortstatekey(
|
fn get_shortstatekey(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue