Skip to content

Commit

Permalink
feat: add metric for total size of cached partial state witnesses (#1…
Browse files Browse the repository at this point in the history
…1320)

`CacheEntry` tracks its own size in `total_parts_size`, that is pretty
straightforward since we can only increment it as we add more parts. The
total size across all cached entries in
`PartialEncodedStateWitnessTracker` is calculated by simply iterating
across all entries. We can afford that performance-wise since LRU cache
size is currently limited to 200 entries and we plan to reduce that
before the mainnet release.
  • Loading branch information
pugachAG committed May 15, 2024
1 parent 7befdbd commit 1479c90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions chain/client/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,11 @@ pub(crate) static PARTIAL_WITNESS_PARTS_RECEIVED_RATIO: Lazy<HistogramVec> = Laz
)
.unwrap()
});

pub(crate) static PARTIAL_WITNESS_CACHE_SIZE: Lazy<Gauge> = Lazy::new(|| {
try_create_gauge(
"near_partial_witness_cache_size",
"Total size in bytes of all currently cached witness parts",
)
.unwrap()
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct CacheEntry {
pub data_parts_required: usize,
pub parts: Vec<Option<Box<[u8]>>>,
pub rs: Arc<Option<ReedSolomon>>,
pub total_parts_size: usize,
}

impl CacheEntry {
Expand All @@ -76,6 +77,7 @@ impl CacheEntry {
data_parts_present: 0,
data_parts_required: data_parts,
parts: vec![None; total_parts],
total_parts_size: 0,
rs,
}
}
Expand Down Expand Up @@ -105,6 +107,7 @@ impl CacheEntry {
// Increment the count of data parts present even if the part has been decoded before.
// We use this in metrics to track the number of parts received. Insert the part into the cache entry.
self.data_parts_present += 1;
self.total_parts_size += part.len();
self.parts[part_ord] = Some(part);
self.shard_id = shard_id;
self.duration_to_last_part = self.timer.elapsed();
Expand Down Expand Up @@ -196,6 +199,7 @@ impl PartialEncodedStateWitnessTracker {

self.client_sender.send(ProcessChunkStateWitnessMessage(encoded_witness));
}
self.record_total_parts_cache_size_metric();
Ok(())
}

Expand Down Expand Up @@ -252,4 +256,10 @@ impl PartialEncodedStateWitnessTracker {
}
Ok(())
}

fn record_total_parts_cache_size_metric(&self) {
let total_size: usize =
self.parts_cache.iter().map(|(_, entry)| entry.total_parts_size).sum();
metrics::PARTIAL_WITNESS_CACHE_SIZE.set(total_size as f64);
}
}

0 comments on commit 1479c90

Please sign in to comment.