Report evction/invalidtion types

This helps the storage to log/track different type of cache deletions.
This commit is contained in:
Yuchen Wu 2024-06-20 15:32:37 -07:00
parent 872d4e0295
commit 6ea5cde02e
5 changed files with 38 additions and 9 deletions

2
.bleep
View file

@ -1 +1 @@
b8ac54b482f643f04533dcc04061b1575482c92d 27bf4df19b465281ddc2d348ed6f3f0402fa60cc

View file

@ -44,7 +44,7 @@ pub use key::CacheKey;
use lock::{CacheLock, LockStatus, Locked}; use lock::{CacheLock, LockStatus, Locked};
pub use memory::MemCache; pub use memory::MemCache;
pub use meta::{CacheMeta, CacheMetaDefaults}; pub use meta::{CacheMeta, CacheMetaDefaults};
pub use storage::{HitHandler, MissHandler, Storage}; pub use storage::{HitHandler, MissHandler, PurgeType, Storage};
pub use variance::VarianceBuilder; pub use variance::VarianceBuilder;
pub mod prelude {} pub mod prelude {}
@ -658,7 +658,10 @@ impl HttpCache {
let handle = span.handle(); let handle = span.handle();
for item in evicted { for item in evicted {
// TODO: warn/log the error // TODO: warn/log the error
let _ = inner.storage.purge(&item, &handle).await; let _ = inner
.storage
.purge(&item, PurgeType::Eviction, &handle)
.await;
} }
} }
inner.traces.finish_miss_span(); inner.traces.finish_miss_span();
@ -1063,7 +1066,10 @@ impl HttpCache {
let inner = self.inner_mut(); let inner = self.inner_mut();
let mut span = inner.traces.child("purge"); let mut span = inner.traces.child("purge");
let key = inner.key.as_ref().unwrap().to_compact(); let key = inner.key.as_ref().unwrap().to_compact();
let result = inner.storage.purge(&key, &span.handle()).await; let result = inner
.storage
.purge(&key, PurgeType::Invalidation, &span.handle())
.await;
// FIXME: also need to remove from eviction manager // FIXME: also need to remove from eviction manager
span.set_tag(|| trace::Tag::new("purged", matches!(result, Ok(true)))); span.set_tag(|| trace::Tag::new("purged", matches!(result, Ok(true))));
result result

View file

@ -306,7 +306,12 @@ impl Storage for MemCache {
Ok(Box::new(miss_handler)) Ok(Box::new(miss_handler))
} }
async fn purge(&'static self, key: &CompactCacheKey, _trace: &SpanHandle) -> Result<bool> { async fn purge(
&'static self,
key: &CompactCacheKey,
_type: PurgeType,
_trace: &SpanHandle,
) -> Result<bool> {
// This usually purges the primary key because, without a lookup, the variance key is usually // This usually purges the primary key because, without a lookup, the variance key is usually
// empty // empty
let hash = key.combined(); let hash = key.combined();
@ -525,7 +530,9 @@ mod test {
assert!(cache.temp.read().contains_key(&hash)); assert!(cache.temp.read().contains_key(&hash));
let result = cache.purge(&key, &Span::inactive().handle()).await; let result = cache
.purge(&key, PurgeType::Invalidation, &Span::inactive().handle())
.await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(!cache.temp.read().contains_key(&hash)); assert!(!cache.temp.read().contains_key(&hash));
@ -551,7 +558,9 @@ mod test {
assert!(cache.cached.read().contains_key(&hash)); assert!(cache.cached.read().contains_key(&hash));
let result = cache.purge(&key, &Span::inactive().handle()).await; let result = cache
.purge(&key, PurgeType::Invalidation, &Span::inactive().handle())
.await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(!cache.cached.read().contains_key(&hash)); assert!(!cache.cached.read().contains_key(&hash));

View file

@ -119,7 +119,7 @@ impl<C: CachePut> CachePutCtx<C> {
.handle(); .handle();
for item in evicted { for item in evicted {
// TODO: warn/log the error // TODO: warn/log the error
let _ = self.storage.purge(&item, &trace).await; let _ = self.storage.purge(&item, PurgeType::Eviction, &trace).await;
} }
} }

View file

@ -22,6 +22,15 @@ use async_trait::async_trait;
use pingora_error::Result; use pingora_error::Result;
use std::any::Any; use std::any::Any;
/// The reason a purge() is called
#[derive(Debug, Clone, Copy)]
pub enum PurgeType {
// For eviction because the cache storage is full
Eviction,
// For cache invalidation
Invalidation,
}
/// Cache storage interface /// Cache storage interface
#[async_trait] #[async_trait]
pub trait Storage { pub trait Storage {
@ -45,7 +54,12 @@ pub trait Storage {
/// Delete the cached asset for the given key /// Delete the cached asset for the given key
/// ///
/// [CompactCacheKey] is used here because it is how eviction managers store the keys /// [CompactCacheKey] is used here because it is how eviction managers store the keys
async fn purge(&'static self, key: &CompactCacheKey, trace: &SpanHandle) -> Result<bool>; async fn purge(
&'static self,
key: &CompactCacheKey,
purge_type: PurgeType,
trace: &SpanHandle,
) -> Result<bool>;
/// Update cache header and metadata for the already stored asset. /// Update cache header and metadata for the already stored asset.
async fn update_meta( async fn update_meta(