Skip to content

Commit 13d2884

Browse files
committed
Introduce eviction count.
1 parent 67171df commit 13d2884

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

nativelink-store/src/default_store_factory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use nativelink_error::Error;
2424
use nativelink_util::health_utils::HealthRegistryBuilder;
2525
use nativelink_util::metrics::StoreType;
2626
use nativelink_util::store_trait::{Store, StoreDriver};
27-
2827
use crate::completeness_checking_store::CompletenessCheckingStore;
2928
use crate::compression_store::CompressionStore;
3029
use crate::dedup_store::DedupStore;
@@ -147,6 +146,7 @@ fn should_wrap_in_metrics_store(spec: &StoreSpec) -> bool {
147146
matches!(
148147
spec,
149148
StoreSpec::Memory(_)
149+
| StoreSpec::Grpc(_)
150150
| StoreSpec::ExperimentalCloudObjectStore(_)
151151
| StoreSpec::ExperimentalMongo(_)
152152
| StoreSpec::Filesystem(_)

nativelink-store/src/metrics_store.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use crate::callback_utils::RemoveItemCallbackHolder;
2+
use crate::filesystem_store::FilesystemStore;
3+
use crate::memory_store::MemoryStore;
14
use async_trait::async_trait;
25
use nativelink_error::Error;
36
use nativelink_metric::MetricsComponent;
47
use nativelink_util::buf_channel::{DropCloserReadHalf, DropCloserWriteHalf};
58
use nativelink_util::health_utils::{HealthStatus, HealthStatusIndicator};
6-
use nativelink_util::metrics::{StoreMetricAttrs, StoreType, STORE_METRICS};
9+
use nativelink_util::metrics::{STORE_METRICS, StoreMetricAttrs, StoreType};
710
use nativelink_util::store_trait::{
811
RemoveItemCallback, Store, StoreDriver, StoreKey, StoreLike, UploadSizeInfo,
912
};
@@ -15,15 +18,31 @@ use std::time::Instant;
1518
#[derive(MetricsComponent, Debug)]
1619
pub struct MetricsStore {
1720
inner: Arc<Store>,
18-
attrs: StoreMetricAttrs,
21+
attrs: Arc<StoreMetricAttrs>,
1922
}
2023

2124
impl MetricsStore {
2225
#[must_use]
2326
pub fn new(inner: Arc<Store>, name: &str, store_type: StoreType) -> Arc<Self> {
27+
let attrs = Arc::new(StoreMetricAttrs::new_with_name(store_type, name));
28+
if should_add_remove_callback(inner.clone()) {
29+
#[derive(Debug)]
30+
struct EvictionCallback {
31+
attrs: Arc<StoreMetricAttrs>,
32+
}
33+
impl RemoveItemCallback for EvictionCallback {
34+
fn callback<'a>(&'a self, store_key: StoreKey<'a>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> {
35+
Box::pin(async { STORE_METRICS.eviction_count.add(1, self.attrs.eviction()) })
36+
}
37+
}
38+
if let Err(e) = inner.register_remove_callback(Arc::new(EvictionCallback { attrs: attrs.clone() })) {
39+
tracing::error!("Failed to register remove callback: {:?}", e);
40+
}
41+
}
42+
2443
Arc::new(Self {
2544
inner: inner.clone(),
26-
attrs: StoreMetricAttrs::new_with_name(store_type, name),
45+
attrs: attrs.clone(),
2746
})
2847
}
2948
}
@@ -76,7 +95,9 @@ impl StoreDriver for MetricsStore {
7695
.store_operation_duration
7796
.record(duration_ms as f64, &self.attrs.write_success());
7897
} else {
79-
STORE_METRICS.store_operations.add(1, &self.attrs.write_error());
98+
STORE_METRICS
99+
.store_operations
100+
.add(1, &self.attrs.write_error());
80101
STORE_METRICS
81102
.store_operation_duration
82103
.record(duration_ms as f64, &self.attrs.write_error());
@@ -144,3 +165,8 @@ impl HealthStatusIndicator for MetricsStore {
144165
self.inner.check_health(_namespace).await
145166
}
146167
}
168+
169+
fn should_add_remove_callback(store: Arc<Store>) -> bool {
170+
store.downcast_ref::<FilesystemStore>(None).is_some()
171+
|| store.downcast_ref::<MemoryStore>(None).is_some()
172+
}

nativelink-util/src/metrics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,11 @@ pub static STORE_METRICS: LazyLock<StoreMetrics> = LazyLock::new(|| {
16561656
5000.0, // 5 seconds
16571657
])
16581658
.build(),
1659+
1660+
eviction_count: meter
1661+
.u64_counter("eviction_count")
1662+
.with_description("Number of evictions")
1663+
.build(),
16591664
}
16601665
});
16611666

@@ -1665,6 +1670,8 @@ pub struct StoreMetrics {
16651670
pub store_operation_duration: metrics::Histogram<f64>,
16661671
/// Counter of store operations by type and result
16671672
pub store_operations: metrics::Counter<u64>,
1673+
/// Counter of evictions
1674+
pub eviction_count: metrics::Counter<u64>,
16681675
}
16691676

16701677
#[derive(Debug, Clone)]
@@ -1676,6 +1683,7 @@ pub struct StoreMetricAttrs {
16761683
read_error: Vec<KeyValue>,
16771684
write_success: Vec<KeyValue>,
16781685
write_error: Vec<KeyValue>,
1686+
eviction: Vec<KeyValue>,
16791687
}
16801688

16811689
impl StoreMetricAttrs {
@@ -1703,6 +1711,7 @@ impl StoreMetricAttrs {
17031711
read_error: make_attrs(CacheOperationName::Read, CacheOperationResult::Error),
17041712
write_success: make_attrs(CacheOperationName::Write, CacheOperationResult::Success),
17051713
write_error: make_attrs(CacheOperationName::Write, CacheOperationResult::Error),
1714+
eviction: make_attrs(CacheOperationName::Evict, CacheOperationResult::Success),
17061715
}
17071716
}
17081717

@@ -1731,4 +1740,8 @@ impl StoreMetricAttrs {
17311740
pub fn write_error(&self) -> &[KeyValue] {
17321741
&self.write_error
17331742
}
1743+
#[must_use]
1744+
pub fn eviction(&self) -> &[KeyValue] {
1745+
&self.eviction
1746+
}
17341747
}

0 commit comments

Comments
 (0)