Skip to content

Commit 99d852f

Browse files
committed
Introduce store_size metric.
1 parent 6e638f8 commit 99d852f

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

nativelink-store/src/filesystem_store.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ impl<Fe: FileEntry> FilesystemStore<Fe> {
725725
.ok_or_else(|| make_err!(Code::NotFound, "{digest} not found in filesystem store. This may indicate the file was evicted due to cache pressure. Consider increasing 'max_bytes' in your filesystem store's eviction_policy configuration."))
726726
}
727727

728+
pub fn get_len(&self) -> u64 {
729+
self.evicting_map.len()
730+
}
731+
728732
async fn update_file(
729733
self: Pin<&Self>,
730734
mut entry: Fe,

nativelink-store/src/metrics_store.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::memory_store::MemoryStore;
44
use async_trait::async_trait;
55
use nativelink_error::Error;
66
use nativelink_metric::MetricsComponent;
7+
use nativelink_proto::build_event_stream::File;
78
use nativelink_util::buf_channel::{DropCloserReadHalf, DropCloserWriteHalf};
89
use nativelink_util::health_utils::{HealthStatus, HealthStatusIndicator};
9-
use nativelink_util::metrics::{STORE_METRICS, StoreMetricAttrs, StoreType};
10+
use nativelink_util::metrics::{StoreMetricAttrs, StoreType, STORE_METRICS};
1011
use nativelink_util::store_trait::{
1112
RemoveItemCallback, Store, StoreDriver, StoreKey, StoreLike, UploadSizeInfo,
1213
};
@@ -25,19 +26,26 @@ impl MetricsStore {
2526
#[must_use]
2627
pub fn new(inner: Arc<Store>, name: &str, store_type: StoreType) -> Arc<Self> {
2728
let attrs = Arc::new(StoreMetricAttrs::new_with_name(store_type, name));
28-
if should_add_remove_callback(inner.clone()) {
29+
if let Some(fs_store) = inner.downcast_ref::<FilesystemStore>(None) {
2930
#[derive(Debug)]
3031
struct EvictionCallback {
3132
attrs: Arc<StoreMetricAttrs>,
3233
}
3334
impl RemoveItemCallback for EvictionCallback {
34-
fn callback<'a>(&'a self, store_key: StoreKey<'a>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> {
35+
fn callback<'a>(
36+
&'a self,
37+
_store_key: StoreKey<'a>,
38+
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
3539
Box::pin(async { STORE_METRICS.eviction_count.add(1, self.attrs.eviction()) })
3640
}
3741
}
38-
if let Err(e) = inner.register_remove_callback(Arc::new(EvictionCallback { attrs: attrs.clone() })) {
42+
if let Err(e) = inner.register_remove_callback(Arc::new(EvictionCallback {
43+
attrs: attrs.clone(),
44+
})) {
3945
tracing::error!("Failed to register remove callback: {:?}", e);
4046
}
47+
48+
STORE_METRICS.store_size.record(fs_store.get_len(), &attrs.store_size());
4149
}
4250

4351
Arc::new(Self {
@@ -103,6 +111,10 @@ impl StoreDriver for MetricsStore {
103111
.record(duration_ms as f64, &self.attrs.write_error());
104112
}
105113

114+
if let Some(fs_store) = self.inner.downcast_ref::<FilesystemStore>(None) {
115+
STORE_METRICS.store_size.record(fs_store.get_len(), &self.attrs.store_size());
116+
}
117+
106118
result
107119
}
108120

@@ -168,5 +180,4 @@ impl HealthStatusIndicator for MetricsStore {
168180

169181
fn should_add_remove_callback(store: Arc<Store>) -> bool {
170182
store.downcast_ref::<FilesystemStore>(None).is_some()
171-
|| store.downcast_ref::<MemoryStore>(None).is_some()
172183
}

nativelink-util/src/evicting_map.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ struct State<
108108
#[metric(help = "Total size of all items in the store")]
109109
sum_store_size: u64,
110110

111+
store_len: u64,
112+
111113
#[metric(help = "Number of bytes evicted from the store")]
112114
evicted_bytes: Counter,
113115
#[metric(help = "Number of items evicted from the store")]
@@ -148,6 +150,7 @@ impl<
148150
btree.remove(key);
149151
}
150152
self.sum_store_size -= eviction_item.data.len();
153+
self.store_len -= 1;
151154
if replaced {
152155
self.replaced_items.inc();
153156
self.replaced_bytes.add(eviction_item.data.len());
@@ -234,6 +237,7 @@ where
234237
lru: LruCache::unbounded(),
235238
btree: None,
236239
sum_store_size: 0,
240+
store_len: 0,
237241
evicted_bytes: Counter::default(),
238242
evicted_items: CounterWithTime::default(),
239243
replaced_bytes: Counter::default(),
@@ -546,6 +550,10 @@ where
546550
.await
547551
}
548552

553+
pub fn len(&self) -> u64 {
554+
self.state.lock().store_len
555+
}
556+
549557
fn inner_insert_many<It>(
550558
&self,
551559
state: &mut State<K, Q, T, C>,
@@ -572,6 +580,7 @@ where
572580
replaced_items.push(old_item);
573581
}
574582
state.sum_store_size += new_item_size;
583+
state.store_len += 1;
575584
state.lifetime_inserted_bytes.add(new_item_size);
576585
}
577586

nativelink-util/src/metrics.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,11 @@ pub static STORE_METRICS: LazyLock<StoreMetrics> = LazyLock::new(|| {
16431643
.u64_counter("eviction_count")
16441644
.with_description("Number of evictions")
16451645
.build(),
1646+
1647+
store_size: meter
1648+
.u64_gauge("store_size")
1649+
.with_description("Number of items in the store")
1650+
.build(),
16461651
}
16471652
});
16481653

@@ -1654,6 +1659,8 @@ pub struct StoreMetrics {
16541659
pub store_operations: metrics::Counter<u64>,
16551660
/// Counter of evictions
16561661
pub eviction_count: metrics::Counter<u64>,
1662+
/// Counter of items in the store
1663+
pub store_size: metrics::Gauge<u64>,
16571664
}
16581665

16591666
#[derive(Debug, Clone)]
@@ -1666,6 +1673,8 @@ pub struct StoreMetricAttrs {
16661673
write_success: Vec<KeyValue>,
16671674
write_error: Vec<KeyValue>,
16681675
eviction: Vec<KeyValue>,
1676+
store_size: Vec<KeyValue>,
1677+
16691678
}
16701679

16711680
impl StoreMetricAttrs {
@@ -1675,11 +1684,12 @@ impl StoreMetricAttrs {
16751684
/// type, instance ID).
16761685
#[must_use]
16771686
pub fn new_with_name(store_type: StoreType, name: &str) -> Self {
1687+
let base_attrs = vec![
1688+
KeyValue::new(STORE_TYPE, store_type.to_string()),
1689+
KeyValue::new(STORE_NAME, name.to_string()),
1690+
];
16781691
let make_attrs = |op: CacheOperationName, result: CacheOperationResult| {
1679-
let mut attrs = vec![
1680-
KeyValue::new(STORE_TYPE, store_type.to_string()),
1681-
KeyValue::new(STORE_NAME, name.to_string()),
1682-
];
1692+
let mut attrs = base_attrs.clone();
16831693
attrs.push(KeyValue::new(CACHE_OPERATION, op));
16841694
attrs.push(KeyValue::new(CACHE_RESULT, result));
16851695
attrs
@@ -1694,6 +1704,8 @@ impl StoreMetricAttrs {
16941704
write_success: make_attrs(CacheOperationName::Write, CacheOperationResult::Success),
16951705
write_error: make_attrs(CacheOperationName::Write, CacheOperationResult::Error),
16961706
eviction: make_attrs(CacheOperationName::Evict, CacheOperationResult::Success),
1707+
store_size: base_attrs.clone(),
1708+
16971709
}
16981710
}
16991711

@@ -1726,4 +1738,8 @@ impl StoreMetricAttrs {
17261738
pub fn eviction(&self) -> &[KeyValue] {
17271739
&self.eviction
17281740
}
1741+
#[must_use]
1742+
pub fn store_size(&self) -> &[KeyValue] {
1743+
&self.store_size
1744+
}
17291745
}

0 commit comments

Comments
 (0)