Skip to content

feat(cache): hit/miss + bytes counters (#418) - #481

Merged
thiras merged 12 commits into
mainfrom
feat/cache-hit-miss-metrics-418
May 8, 2026
Merged

thiras merged 12 commits into
mainfrom
feat/cache-hit-miss-metrics-418

Conversation

@thiras

@thiras thiras commented May 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #418. Adds four decdn_cache_* counters so operators (and #275 decdn node top) can reason about cache behaviour without log scraping:

  • decdn_cache_hits_total — get() calls served from the local store (first-attempt + waiter-retry hits).
  • decdn_cache_misses_total — bumped once at every pull_through entry and once on every operator-evicted NotFound. Storage I/O failures (CacheError::Store) propagate without bumping either counter so hit-rate dashboards aren't polluted by infrastructure failures (see module-level doc in crates/cache/src/metrics.rs).
  • decdn_cache_bytes_returned_total — bytes returned from get() on success (cache-hit + pull-through-success paths).
  • decdn_cache_pull_through_bytes_total — bytes received from origin during a miss, counted before BLAKE3 verification / size-cap re-check (origin egress is paid the moment bytes arrive).

ADR appendix-observability.md §2.3 already listed hits_total / misses_total as Mandatory; this PR also registers the two new byte counters there. Wiring is purely additive — the cache crate already owned CacheMetrics, the engine already accepted Option<Arc<CacheMetrics>> via open_full, and runtime::build_cache already passes node_metrics.cache_metrics() through. No new deps; decdn-cache → decdn-node direction preserved.

Test plan

  • Unit tests in crates/cache/src/engine.rs::tests cover hit, evicted, no-origin, pull-through-bytes, pull-through-bytes-returned, and the hits/misses accounting invariant.
  • concurrent_gets_coalesce_into_single_origin_fetch extended to assert 1 miss + 4 hits under 5 concurrent gets — locks the waiter-retry-hit increment.
  • crates/node/src/metrics.rs integration test drives a real CacheEngine and asserts the rendered OpenMetrics output, with a line-anchored matcher (has_metric_line) so substring collisions can't pass silently.
  • cargo nextest run — 672/672 tests pass.
  • cargo clippy --workspace --all-targets -- -D warnings clean.
  • cargo fmt -- --check clean.

Notes

The misses counter docstring deliberately doesn't claim a strict hits-XOR-misses invariant: storage I/O errors propagate via ? without bumping either counter, by design. The contract and reasoning are documented at the module level in crates/cache/src/metrics.rs.

🤖 Generated with Claude Code

thiras and others added 11 commits May 8, 2026 17:16
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Critical: relax the `misses` docstring's absolute hits-XOR-misses claim.
Store I/O errors (`CacheError::Store`) propagate from
`CacheEngine::get` via `?` on `has`/`read_local` without bumping
either counter, which is the right call (storage failures are
infrastructure events, not cache outcomes — conflating them would
pollute hit-rate dashboards). Hoist the contract to the module-level
doc and weaken per-field claims to match.

Coverage: extend `concurrent_gets_coalesce_into_single_origin_fetch`
to assert `1 miss + 4 hits` under coalescing — locks the waiter-retry
hit increment that was previously asserted only by inspection.

Test robustness: introduce `has_metric_line` line-anchored matcher
for OpenMetrics output so `decdn_cache_hits_total 1` cannot
accidentally substring-match a future `..._foo` series or `_created`
companion. Tighten `hits_plus_misses_equals_total_gets` with a
`pull_through_bytes` assertion so a stray bump in either error arm
fails.

Doc polish: drop the variant-name enumeration from `misses` (rot
risk), drop the internal-Rust-type leakage (`OriginFetch::Found`)
from the ADR row, and refresh the BLAKE3-rejection comment to
reflect that `retry_fetch` already enforces `max_blob_bytes`
upstream.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 8, 2026 14:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces four new cache metrics—hits, misses, bytes_returned_total, and pull_through_bytes_total—to the decdn_cache family, aligning with the observability ADR. The changes include logic in CacheEngine to track these metrics during cache hits, misses, and origin pull-throughs, supported by extensive integration tests. Feedback indicates that the misses counter is currently incremented at the start of the pull_through function, which could lead to incorrect accounting if a Store error occurs later; the reviewer suggests ensuring Store errors are excluded from hit/miss metrics as per the stated policy. Corresponding documentation updates are also recommended to clarify this behavior.

Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/cache/src/metrics.rs Resolved
@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 87.90% (was 87.72%, ▲ +0.18%) — 11851/13483 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 91.52% 1004/1097 ▲ +1.04%
crates/node/src/metrics.rs 60.79% 169/278 ▲ +14.92%
History
Commit Total Δ vs base
0c8fc2d 87.90% ▲ +0.18%
a8dc35f (HEAD) 87.90% ▲ +0.18%

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds cache-level observability to help operators (and future decdn node top) reason about cache behavior via OpenMetrics decdn_cache_* counters, including hit/miss and byte accounting.

Changes:

  • Introduces hits, misses, bytes_returned, and pull_through_bytes counters to CacheMetrics and documents their accounting contract.
  • Wires counter bumps into CacheEngine::get (hit path + coalesced waiter-hit) and pull_through (miss + origin-bytes).
  • Expands cache/unit + node/metrics integration tests to assert correct counter behavior and exact OpenMetrics line rendering.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/node/src/metrics.rs Strengthens metrics tests with exact-line matching and adds an integration test that drives a real CacheEngine and asserts rendered cache counters.
crates/cache/src/metrics.rs Adds four new cache counters with module-level documentation describing intended semantics and invariants.
crates/cache/src/engine.rs Increments new counters on hit/miss paths and on origin-byte receipt; extends existing tests and adds new ones for accounting invariants.
adr/appendix-observability.md Documents the two new cache byte counters in the observability appendix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/cache/src/metrics.rs Resolved
The previous module-level doc claimed Store I/O errors propagate
without bumping either counter — too broad. Hit/miss accounting only
applies to the local-lookup phase (`has`, `read_local`); a Store error
there leaves the call genuinely unclassifiable so neither counter
moves. Once `pull_through` is entered the miss is already determined
and origin egress may have already been paid, so a downstream Store
error (e.g. `add_bytes` failure) leaves the earlier `misses` /
`pull_through_bytes` bumps in place — operators tracking origin spend
need those events counted, not silently dropped.

Also drop "peer" from the `hits` doc — coalescing is between
in-process concurrent tasks, not network peers (per #481 review).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thiras
thiras merged commit f536806 into main May 8, 2026
10 checks passed
@thiras
thiras deleted the feat/cache-hit-miss-metrics-418 branch May 8, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cache hit/miss + bytes-from-cache metrics to CacheEngine

2 participants