Skip to content

refactor(node): collapse the 6 watcher downtime recorders into a second macro family (#1266) - #1276

Merged
thiras merged 4 commits into
mainfrom
refactor/1266-watcher-downtime-recorders
Jul 17, 2026
Merged

thiras merged 4 commits into
mainfrom
refactor/1266-watcher-downtime-recorders

Conversation

@thiras

@thiras thiras commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1266.

Finishes the thread #1263 opened: it collapsed 134 trivial recorders onto the recorders! table but deliberately scoped out six survivors — the three chain watchers' (slash, staker_set, origin_directory) downtime recorder pairs, which branch and touch Metrics's own Mutex<Option<Instant>> state and so couldn't use the single-forward macro.

What changed (crates/node/src/metrics.rs only)

  • New watcher_downtime_recorders! macro family. Each row spells out both method names (production hooks call them by exact name; no paste) and labels the three backing field idents. Generates the *_backoff_started/*_cycle_established pair per watcher.
  • encode() consolidation. The macro also emits a private refresh_watcher_down_seconds; encode() calls it once instead of hand-repeating three byte-identical down-seconds recompute blocks. Adding a fourth watcher is now one table row instead of five edits across two functions.
  • Tests. Added origin_directory_watcher_down_seconds_tracks_true_downtime (closes a coverage asymmetry) and poisoned_down_since_reports_i64_max_not_zero (pins the "poison → i64::MAX, never mask an outage" invariant across all three watchers via the shared recompute, plus single-scrape isolation).

Correctness — checked, not asserted

The three commits are sequenced to keep the proofs clean (the #1263 discipline):

  1. Method collapse — proven behavior-neutral by normalized cargo expand: all six generated methods are token-identical to the deleted hand-written ones (only the extra impl wrapper differs).
  2. encode() collapse — intentionally token-changing (the three inline blocks weren't uniform), so it is test-gated by the down-seconds guards rather than an empty diff.
  3. Review follow-ups — the poison test + a macro-doc accuracy fix.

Verification

cargo build, cargo clippy --all-targets, cargo fmt --check, cargo doc -D warnings, and the full decdn-node suite (837 tests) all pass. No exported metric series changes.

Review

A four-agent review (code / tests / comments / silent-failure) found no critical or important issues; the two highest-value suggestions (the poison test and the doc fix) are folded into the third commit.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved watcher downtime metrics to accurately track downtime and restart events.
    • Ensured downtime measurements remain consistent across slash, staker-set, and origin-directory watchers.
    • Added safer handling when metric state is unavailable, preventing inaccurate readings for other watchers.
  • Tests

    • Expanded coverage for downtime tracking and recovery behavior.

thiras and others added 3 commits July 17, 2026 23:38
#1266)

Collapse the three chain watchers' (slash, staker_set, origin_directory)
byte-identical backoff/cycle-established recorder pairs onto a second
table-macro, `watcher_downtime_recorders!`. These couldn't join #1263's
`recorders!` because their bodies branch and touch `Metrics`'s own
`Mutex<Option<Instant>>` state; the new macro carries the lock/edge-trigger
shape once, with each row spelling out both method names (production hooks
call them by exact name) and labelling the three struct-field idents.

Behaviour-neutral: normalized `cargo expand` of the metrics module is
token-identical for all six methods (differing only by the extra impl-block
wrapper). encode()'s down-seconds recompute is left for a follow-up commit.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…tcher macro (#1266)

Extend `watcher_downtime_recorders!` to also emit a private
`refresh_watcher_down_seconds` that recomputes every watcher's `*_down_seconds`
gauge from its `down_since` (poisoned lock -> i64::MAX, the alerting
direction). encode() now calls it once instead of hand-repeating the three
byte-identical recompute blocks, so adding a fourth watcher is one table row.

Unlike the method collapse in the previous commit, this changes encode()'s
tokens (the three inline blocks weren't uniform — different local names and
wrapping), so it is gated by the down-seconds tests rather than an empty
`cargo expand` diff. Adds the missing origin_directory down-seconds test to
match the staker_set and slash guards and to exercise the shared recompute.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…acro doc (#1266)

PR-review follow-ups. Adds `poisoned_down_since_reports_i64_max_not_zero`,
which poisons a `*_watcher_down_since` lock (spawn+join, the `dispatch::tests`
pattern) and asserts the shared `refresh_watcher_down_seconds` reports
`i64::MAX` — the load-bearing "never mask an outage" invariant that was
documented but unenforced across all three watchers before #1266 unified the
recompute. It also pins single-scrape isolation: the two healthy gauges still
read 0 in the same encode().

Also corrects the macro doc's "three struct-field idents" to note that only
`down_since` lives on `Metrics`; `restarts`/`down_seconds` are on `self.decdn`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 20:55
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@thiras, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: 57eebcc1-0c7d-44f6-b851-9b0358554ca8

📥 Commits

Reviewing files that changed from the base of the PR and between 1c158c1 and d688513.

📒 Files selected for processing (1)
  • crates/node/src/metrics.rs

Walkthrough

The change replaces three hand-written watcher downtime recorder pairs with a shared macro, centralizes downtime gauge refreshes, wires the three watcher metric sets, and adds coverage for origin-directory downtime and poisoned-lock fallback behavior.

Changes

Watcher downtime metrics

Layer / File(s) Summary
Shared downtime recorder state machine
crates/node/src/metrics.rs
A macro generates backoff and cycle recorders with edge-triggered restart counting, timestamp management, and poisoned-lock handling.
Metric refresh integration and coverage
crates/node/src/metrics.rs
All three watchers use the generated recorders, Metrics::encode calls the shared refresh helper, and tests verify downtime tracking and i64::MAX poison handling.

Estimated code review effort: 4 (Complex) | ~45 minutes


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

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

This PR refactors decdn-node’s internal metrics recorder layer by collapsing the remaining six chain-watcher downtime recorder methods (*_backoff_started / *_cycle_established for slash, staker_set, and origin_directory) into a dedicated watcher_downtime_recorders! macro family, and consolidates the scrape-time “down seconds” recomputation into a single helper invoked from Metrics::encode().

Changes:

  • Introduces watcher_downtime_recorders! to generate the six watcher downtime recorder methods while preserving exact method names used by production hooks.
  • Consolidates the three duplicated *_down_seconds recompute blocks into a shared refresh_watcher_down_seconds() called once per scrape.
  • Adds tests to cover origin_directory downtime semantics and to pin the poisoned-lock → i64::MAX behavior.

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

Comment thread crates/node/src/metrics.rs Resolved
Comment thread crates/node/src/metrics.rs Resolved
Comment thread crates/node/src/metrics.rs Resolved
…keeps climbing" (#1276 review)

The three "keeps climbing" phrasings attached to the *poison* case were
imprecise: a poisoned `down_since` lock is sticky, so `refresh_watcher_down_seconds`
hits its `Err(_) => i64::MAX` arm on every scrape and the gauge is pinned at
`i64::MAX` — "keeps climbing" describes the distinct healthy-lock ongoing-outage
path where `elapsed()` grows. Reword the macro doc, the slash backoff doc, and
the staker_set cycle-established doc to say the poison fallback reports
`i64::MAX`, matching the already-correct staker_set backoff doc, the field docs,
and the `poisoned_down_since_reports_i64_max_not_zero` test. Doc-only; no
behaviour or metric-series change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Jul 17, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 81.50% (was 81.49%, ● 0.00%) — 48593/59626 lines

File Coverage Lines Δ
crates/node/src/metrics.rs 93.25% 829/889 ▲ +0.21%
History
Commit Total Δ vs base
1c158c1 81.49% ● 0.00%
d688513 (HEAD) 81.50% ● 0.00%

@thiras
thiras merged commit c574ef9 into main Jul 17, 2026
23 checks passed
@thiras
thiras deleted the refactor/1266-watcher-downtime-recorders branch July 17, 2026 21:12
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.

refactor(node): collapse the 6 watcher backoff/cycle recorders into a second macro family

2 participants