Skip to content

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

Closed

Description

@thiras

Follow-up from #1247 (PR #1263), which deliberately scoped it out.

Problem

PR #1263 collapsed the 134 single-forward recorders in impl Metrics onto a recorders! table. Six hand-written survivors remain that are also mechanically identical to each other — three {slash, staker_set, origin_directory} watchers × a backoff_started / cycle_established pair.

They are out of scope for #1247 because they touch Metrics's own Mutex<Option<Instant>> fields rather than only self.decdn, so the single-forward macro cannot express them. But the three copies of each are byte-identical modulo the watcher name:

pub fn slash_watcher_backoff_started(&self) {
    if let Ok(mut down_since) = self.slash_watcher_down_since.lock()
        && down_since.is_none()
    {
        *down_since = Some(Instant::now());
        self.decdn.slash_watcher_restarts.inc();
    }
}

staker_set_watcher_backoff_started and origin_directory_watcher_backoff_started differ only in the two field names (one carries an extra explanatory comment). The three *_cycle_established are the same shape around *down_since = None.

encode() then hand-repeats the paired down-seconds recompute three times as well (staker_set, origin_directory, slash) — same shape, same i64::MAX-on-poisoned-lock rationale, three copies.

Each watcher therefore costs 3 hand-maintained copies of the same state machine, and the invariant that binds them (open the drift window on the None -> Some edge, count once, clear on recovery, report i64::MAX on a poisoned lock so an outage is never masked) is only enforced by copy-paste discipline.

Proposed fix

A second macro family over {watcher_ident, down_since_field, restarts_field, down_seconds_field} generating the pair plus, ideally, the encode() recompute — so adding a fourth watcher is one line rather than five edits across two functions.

Acceptance

Activity

  1. added
    rust
    prio:low
    on Jul 17, 2026
  2. altanoruc commented on Jul 17, 2026

    @altanoruc
    Contributor

    Triage note for a later run: this is small, but the issue leaves one scope decision open: must the new macro also generate the three encode() down-seconds recomputations, or only the six backoff/cycle recorders? Please make that requirement explicit before automated dispatch.

  3. self-assigned this
    on Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

prio:lowrust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions