mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
move statekeyshort_cache to service
This commit is contained in:
parent
2b2b4169df
commit
190b788683
5 changed files with 79 additions and 64 deletions
|
|
@ -236,8 +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) statekeyshort_cache:
|
|
||||||
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
|
|
||||||
pub(super) shortstatekey_cache:
|
pub(super) shortstatekey_cache:
|
||||||
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
|
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
|
||||||
pub(super) our_real_users_cache:
|
pub(super) our_real_users_cache:
|
||||||
|
|
@ -471,14 +469,6 @@ impl KeyValueDatabase {
|
||||||
shortstatekey_cache: Mutex::new(LruCache::new(
|
shortstatekey_cache: Mutex::new(LruCache::new(
|
||||||
(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
|
|
||||||
)]
|
|
||||||
statekeyshort_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()),
|
||||||
|
|
|
||||||
|
|
@ -47,18 +47,6 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<ShortStateKey>> {
|
) -> Result<Option<ShortStateKey>> {
|
||||||
let lookup = Lookup::StateKeyToShort;
|
|
||||||
|
|
||||||
if let Some(short) = self
|
|
||||||
.statekeyshort_cache
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
|
||||||
{
|
|
||||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
|
||||||
return Ok(Some(*short));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut db_key = event_type.to_string().as_bytes().to_vec();
|
let mut db_key = event_type.to_string().as_bytes().to_vec();
|
||||||
db_key.push(0xFF);
|
db_key.push(0xFF);
|
||||||
db_key.extend_from_slice(state_key.as_bytes());
|
db_key.extend_from_slice(state_key.as_bytes());
|
||||||
|
|
@ -75,17 +63,6 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
||||||
})
|
})
|
||||||
.transpose()?;
|
.transpose()?;
|
||||||
|
|
||||||
if let Some(s) = short {
|
|
||||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
|
||||||
|
|
||||||
self.statekeyshort_cache
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.insert((event_type.clone(), state_key.to_owned()), s);
|
|
||||||
} else {
|
|
||||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(short)
|
Ok(short)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -94,50 +71,32 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
||||||
&self,
|
&self,
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<ShortStateKey> {
|
) -> Result<(ShortStateKey, bool)> {
|
||||||
let lookup = Lookup::CreateStateKeyToShort;
|
|
||||||
|
|
||||||
if let Some(short) = self
|
|
||||||
.statekeyshort_cache
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
|
||||||
{
|
|
||||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
|
||||||
return Ok(*short);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut db_key = event_type.to_string().as_bytes().to_vec();
|
let mut db_key = event_type.to_string().as_bytes().to_vec();
|
||||||
db_key.push(0xFF);
|
db_key.push(0xFF);
|
||||||
db_key.extend_from_slice(state_key.as_bytes());
|
db_key.extend_from_slice(state_key.as_bytes());
|
||||||
|
|
||||||
let short = if let Some(shortstatekey) =
|
let (short, created) = if let Some(shortstatekey) =
|
||||||
self.statekey_shortstatekey.get(&db_key)?
|
self.statekey_shortstatekey.get(&db_key)?
|
||||||
{
|
{
|
||||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
(
|
||||||
|
utils::u64_from_bytes(&shortstatekey).map_err(|_| {
|
||||||
utils::u64_from_bytes(&shortstatekey).map_err(|_| {
|
Error::bad_database("Invalid shortstatekey in db.")
|
||||||
Error::bad_database("Invalid shortstatekey in db.")
|
})?,
|
||||||
})?
|
false,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
|
||||||
|
|
||||||
let shortstatekey = services().globals.next_count()?;
|
let shortstatekey = services().globals.next_count()?;
|
||||||
self.statekey_shortstatekey
|
self.statekey_shortstatekey
|
||||||
.insert(&db_key, &shortstatekey.to_be_bytes())?;
|
.insert(&db_key, &shortstatekey.to_be_bytes())?;
|
||||||
self.shortstatekey_statekey
|
self.shortstatekey_statekey
|
||||||
.insert(&shortstatekey.to_be_bytes(), &db_key)?;
|
.insert(&shortstatekey.to_be_bytes(), &db_key)?;
|
||||||
shortstatekey
|
(shortstatekey, true)
|
||||||
};
|
};
|
||||||
|
|
||||||
let short = ShortStateKey::new(short);
|
let short = ShortStateKey::new(short);
|
||||||
|
|
||||||
self.statekeyshort_cache
|
Ok((short, created))
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.insert((event_type.clone(), state_key.to_owned()), short);
|
|
||||||
|
|
||||||
Ok(short)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
#[tracing::instrument(skip(self))]
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,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,
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ 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>>,
|
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
||||||
|
statekeyshort_cache:
|
||||||
|
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl Service {
|
||||||
|
|
@ -46,6 +48,7 @@ impl Service {
|
||||||
db: &'static dyn Data,
|
db: &'static dyn Data,
|
||||||
shorteventid_cache_size: usize,
|
shorteventid_cache_size: usize,
|
||||||
eventidshort_cache_size: usize,
|
eventidshort_cache_size: usize,
|
||||||
|
statekeyshort_cache_size: usize,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
db,
|
db,
|
||||||
|
|
@ -55,6 +58,9 @@ impl Service {
|
||||||
eventidshort_cache: Mutex::new(LruCache::new(
|
eventidshort_cache: Mutex::new(LruCache::new(
|
||||||
eventidshort_cache_size,
|
eventidshort_cache_size,
|
||||||
)),
|
)),
|
||||||
|
statekeyshort_cache: Mutex::new(LruCache::new(
|
||||||
|
statekeyshort_cache_size,
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,7 +98,32 @@ impl Service {
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<ShortStateKey>> {
|
) -> Result<Option<ShortStateKey>> {
|
||||||
self.db.get_shortstatekey(event_type, state_key)
|
let lookup = Lookup::StateKeyToShort;
|
||||||
|
|
||||||
|
if let Some(short) = self
|
||||||
|
.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||||
|
{
|
||||||
|
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||||
|
return Ok(Some(*short));
|
||||||
|
}
|
||||||
|
|
||||||
|
let short = self.db.get_shortstatekey(event_type, state_key)?;
|
||||||
|
|
||||||
|
if let Some(short) = short {
|
||||||
|
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||||
|
|
||||||
|
self.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert((event_type.clone(), state_key.to_owned()), short);
|
||||||
|
} else {
|
||||||
|
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(short)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_or_create_shortstatekey(
|
pub(crate) fn get_or_create_shortstatekey(
|
||||||
|
|
@ -100,7 +131,33 @@ impl Service {
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<ShortStateKey> {
|
) -> Result<ShortStateKey> {
|
||||||
self.db.get_or_create_shortstatekey(event_type, state_key)
|
let lookup = Lookup::CreateStateKeyToShort;
|
||||||
|
|
||||||
|
if let Some(short) = self
|
||||||
|
.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||||
|
{
|
||||||
|
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||||
|
return Ok(*short);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (short, created) =
|
||||||
|
self.db.get_or_create_shortstatekey(event_type, state_key)?;
|
||||||
|
|
||||||
|
if created {
|
||||||
|
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||||
|
} else {
|
||||||
|
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.statekeyshort_cache
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert((event_type.clone(), state_key.to_owned()), short);
|
||||||
|
|
||||||
|
Ok(short)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_eventid_from_short(
|
pub(crate) fn get_eventid_from_short(
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,12 @@ pub(crate) trait Data: Send + Sync {
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<ShortStateKey>>;
|
) -> Result<Option<ShortStateKey>>;
|
||||||
|
|
||||||
|
/// The returned bool indicates whether it was created
|
||||||
fn get_or_create_shortstatekey(
|
fn get_or_create_shortstatekey(
|
||||||
&self,
|
&self,
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<ShortStateKey>;
|
) -> Result<(ShortStateKey, bool)>;
|
||||||
|
|
||||||
fn get_eventid_from_short(
|
fn get_eventid_from_short(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue