From 6ea5cde02e980eade9c17f99932b894d432bdf56 Mon Sep 17 00:00:00 2001 From: Yuchen Wu Date: Thu, 20 Jun 2024 15:32:37 -0700 Subject: [PATCH] Report evction/invalidtion types This helps the storage to log/track different type of cache deletions. --- .bleep | 2 +- pingora-cache/src/lib.rs | 12 +++++++++--- pingora-cache/src/memory.rs | 15 ++++++++++++--- pingora-cache/src/put.rs | 2 +- pingora-cache/src/storage.rs | 16 +++++++++++++++- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.bleep b/.bleep index 990b361..123ae30 100644 --- a/.bleep +++ b/.bleep @@ -1 +1 @@ -b8ac54b482f643f04533dcc04061b1575482c92d \ No newline at end of file +27bf4df19b465281ddc2d348ed6f3f0402fa60cc \ No newline at end of file diff --git a/pingora-cache/src/lib.rs b/pingora-cache/src/lib.rs index 563727c..6ba057e 100644 --- a/pingora-cache/src/lib.rs +++ b/pingora-cache/src/lib.rs @@ -44,7 +44,7 @@ pub use key::CacheKey; use lock::{CacheLock, LockStatus, Locked}; pub use memory::MemCache; pub use meta::{CacheMeta, CacheMetaDefaults}; -pub use storage::{HitHandler, MissHandler, Storage}; +pub use storage::{HitHandler, MissHandler, PurgeType, Storage}; pub use variance::VarianceBuilder; pub mod prelude {} @@ -658,7 +658,10 @@ impl HttpCache { let handle = span.handle(); for item in evicted { // 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(); @@ -1063,7 +1066,10 @@ impl HttpCache { let inner = self.inner_mut(); let mut span = inner.traces.child("purge"); 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 span.set_tag(|| trace::Tag::new("purged", matches!(result, Ok(true)))); result diff --git a/pingora-cache/src/memory.rs b/pingora-cache/src/memory.rs index 3863b2b..dec8f1a 100644 --- a/pingora-cache/src/memory.rs +++ b/pingora-cache/src/memory.rs @@ -306,7 +306,12 @@ impl Storage for MemCache { Ok(Box::new(miss_handler)) } - async fn purge(&'static self, key: &CompactCacheKey, _trace: &SpanHandle) -> Result { + async fn purge( + &'static self, + key: &CompactCacheKey, + _type: PurgeType, + _trace: &SpanHandle, + ) -> Result { // This usually purges the primary key because, without a lookup, the variance key is usually // empty let hash = key.combined(); @@ -525,7 +530,9 @@ mod test { 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!(!cache.temp.read().contains_key(&hash)); @@ -551,7 +558,9 @@ mod test { 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!(!cache.cached.read().contains_key(&hash)); diff --git a/pingora-cache/src/put.rs b/pingora-cache/src/put.rs index e29cfad..e1041e0 100644 --- a/pingora-cache/src/put.rs +++ b/pingora-cache/src/put.rs @@ -119,7 +119,7 @@ impl CachePutCtx { .handle(); for item in evicted { // TODO: warn/log the error - let _ = self.storage.purge(&item, &trace).await; + let _ = self.storage.purge(&item, PurgeType::Eviction, &trace).await; } } diff --git a/pingora-cache/src/storage.rs b/pingora-cache/src/storage.rs index c6365c7..0d4e104 100644 --- a/pingora-cache/src/storage.rs +++ b/pingora-cache/src/storage.rs @@ -22,6 +22,15 @@ use async_trait::async_trait; use pingora_error::Result; 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 #[async_trait] pub trait Storage { @@ -45,7 +54,12 @@ pub trait Storage { /// Delete the cached asset for the given key /// /// [CompactCacheKey] is used here because it is how eviction managers store the keys - async fn purge(&'static self, key: &CompactCacheKey, trace: &SpanHandle) -> Result; + async fn purge( + &'static self, + key: &CompactCacheKey, + purge_type: PurgeType, + trace: &SpanHandle, + ) -> Result; /// Update cache header and metadata for the already stored asset. async fn update_meta(