Skip to content

supervisor: epoch-heartbeat writeState runs its full mkdir+flock+read+write+rename dance every second #649

Closed as not planned

Description

@bpalermo

Summary

With #646/#647 removing the admin-probe JSON cost, the supervisor's top remaining Go frame in the eBPF CPU profile is onLiveEpoch → writeState (~5.1% of supervisor CPU pre-#647, measured 2026-08-30): the liveness loop persists the epoch heartbeat every second, and each write does MkdirAll + open/flock lock file + readState + write-tmp + rename.

Why it's every second

watchLiveness calls onLiveEpoch on every LIVE tick (1s cadence), and onLiveEpoch unconditionally calls writeState to keep the heartbeat timestamp fresh for the cross-pod attach logic (successor reads it to decide the predecessor is alive at epoch E).

Proposed fix

Keep the on-disk freshness contract but decouple it from the tick rate:

  • write only when the epoch CHANGES, or when the file's timestamp is about to exceed the staleness threshold the reader uses (write at ~1/3 of the staleness budget), whichever comes first;
  • skip the MkdirAll after the first success;
  • keep the flock + tmp/rename atomicity as-is (it protects cross-pod attach).

Expected: heartbeat writes drop from 1/s to ~1 per staleness-window with identical reader-visible guarantees; the frame should mostly vanish from the profile.

Guardrails

Baseline evidence: Pyroscope process_cpu, unknown_service:supervisor, 2026-08-30 — onLiveEpoch inlined writeState 17s of 332s total (5.1%). Re-measure after #647 deploys; it will be the top Go frame.

Activity

  1. bpalermo commented on Sep 2, 2026

    @bpalermo
    OwnerAuthor

    Design exploration (2026-09-02): conditional GO on a de-scoped version, and a correction to this issue's headline number

    The win is ~1.2 millicores fleet-wide, not "5.1% of CPU"

    The 5.1% figure is real but misleading: it is 5.1% of a very small Go process. Working it through — heartbeat ≈ 5.1% × ~1% of a core per node ≈ 0.5 millicore/node, ~2.5 mc fleet-wide; a 2s cadence saves ~0.25 mc/node, ~1.2 mc fleet. Post-#647 the share will look larger (it becomes the top Go frame, as predicted) while the absolute cost is unchanged — the percentage should not do the arguing.

    Note the Pyroscope series this issue cites (unknown_service:supervisor) no longer exists; supervisor frames now roll up under service_name="aether-proxy" after the workload-identity naming change, so a fresh per-frame number needs profilecli/selectMergeStacktraces rather than the MCP DOT view.

    A won't-fix decision here is entirely defensible — ~1.2 mc is not a resource problem, is not user-visible, and this component's failure mode is a node-wide data-plane gap with three prior incidents (#111 errno-98, #132 errno-111, the 2026-06-11 mid-handoff kill). If that is the call, the reader inventory below is the durable value: it records that the 1s cadence is 8× the required rate but harmless.

    Reader inventory (the thing that makes this decidable)

    State file ${StateDir}/epoch = "<epoch> <unixMillis>\n". Exactly two consumers of the timestamp, both applying the same tolerance:

    Reader Tolerance Decision Direction of harm
    initStartEpoch predecessorStale = 8s attach at E+1 vs reset to epoch 0 stale-too-early is the incident (epoch 0 against a live predecessor = errno-98, #111)
    writeState's downgrade guard same 8s may a lower epoch overwrite a higher one stale-too-early lets a draining predecessor clobber a live successor

    Grepped for other readers (Go, charts, docs, scripts): none. No shell probe, no e2e/soak harness, no dashboard, no alert. The readiness probe reads a different file (ReadyMarkerPath).

    Two properties that drive the design: a stale file alone resets to epoch 0 even when admin reports LIVE at E; a fresh file is only necessary, never sufficient (adminLiveAtEpoch must agree). So over-fresh is benign, under-fresh is the incident — the only question a cadence change must answer.

    The de-scoped proposal

    Implement only a call-site rate gate in onLiveEpoch (~20-25 lines, writeState's body untouched apart from returning an outcome). heartbeatInterval = predecessorStale / 4 = 2s, derived, not a flag (a flag here is a footgun: set it above the budget and #111 returns fleet-wide, silently).

    Fail-safe by construction — the timer advances only on a proven-durable write:

    • written → record → next write in 2s;
    • suppressed (downgrade guard fired, i.e. an in-flight handoff) → do not record → retry next tick, restoring today's exact 1/s pattern during handoffs;
    • failed → do not record → retry next tick.

    Plus an immediate write on any epoch change — !=, not >, because resetEpochForRetry can move the epoch down to 0 after a bind-collision retry.

    Worst-case on-disk age goes 2s → 3s against the 8s budget (37.5%); tolerated consecutive write failures 8 → 5. Detection latency changes only in the safe direction (a dead predecessor is declared stale ~2s earlier).

    Explicitly rejected from this issue's proposal

    • MkdirAll memo — ~1 syscall per 2s after the gate; adds a state variable and an error-path subtlety (today, deleting the state dir self-heals on the next write). Not worth the review surface.
    • "Avoid the read+flock when nothing changed" — the read IS the guard. It must be taken under the lock at write time; the only sound way to skip it is to skip the write, which is what the gate does. The flock closes a real two-supervisor TOCTOU during surge overlap (predecessor reads E → successor writes E+1 → predecessor's rename clobbers back to E). Halving the write rate halves the chances of hitting it, which is a bonus, not a reason to remove it.

    If the implementation ever needs to weaken readState, the downgrade guard, or the flock to hit the target — stop and close as won't-fix.

    Validation

    Fake-clock tests (a now func() field, not a repo-wide clock dep): 300s of 1s ticks at constant epoch asserting write count 150±1 and that on-disk age never exceeds heartbeatInterval + readyPollInterval; immediate write on epoch change in both directions; retry-every-tick on suppressed/failed; and a constants test pinning heartbeatInterval + readyPollInterval <= predecessorStale/2 so a future edit to either value cannot silently erode the margin. Plus a two-writer -race test guarding the flock under the reduced cadence, and an aether.supervisor.heartbeat_writes counter with a reason attribute (steady state should read ~0.5/s/node, reason=refresh only) as the fleet-visible proof.

    Ship on an already-planned proxy roll, never a dedicated one — the change lands via the agent image on a proxy DaemonSet roll, which is the very operation this code protects.

    Adjacent finding, filed separately

    #666: adminReverifyInterval (15s) is justified in its comment as below ParentShutdownTime (60s), but the deployed chart sets parentShutdownTime: 15s — so in production they are equal and the margin lesson 6 relies on is gone.

  2. bpalermo commented on Sep 4, 2026

    @bpalermo
    OwnerAuthor

    Measured — the premise does not hold

    This issue predicted that after #647 removed the admin-probe JSON decode, writeState / onLiveEpoch state persistence would emerge as the supervisor's top Go frame at roughly 5% of supervisor CPU, making it worth batching or debouncing the state writes.

    Profiled during the 2026-09-04 soak (rev191/0.91.3-cf68f1b), Pyroscope process_cpu:cpu:nanoseconds, window 07:05–07:30Z, max_node_depth=400, effective cutoff 0.201% of supervisor CPU:

    predicted frame measured
    writeState absent
    onLiveEpoch absent
    flock absent
    mkdir absent
    Rename absent
    adminProbe absent

    Not "unresolved" and not "too deep to see" — the tree was walked to depth 400 and every one of these is below 0.201%. Against a predicted ~5%, that is wrong by at least 25×.

    The window is a fair test: it contains zero proxy rolls and zero container restarts, but the supervisor writes state on every epoch event and reverifies on a 15s ticker regardless, so the persistence path was exercised throughout. If it were 5%, it would be visible at a 0.2% cutoff by a factor of 25.

    Where the supervisor's CPU actually goes

    Filed as #673. The dominant term is not inside the supervisor's own logic at all — it is the readiness probe re-executing the entire agent binary every 2 seconds. runtime.main 20.27%, runtime.doInit1 19.75%, SchemeBuilder.AddToScheme 14.81%, gcBgMarkStartWorkers 10.84%, page faults ~13%. ≥31% of supervisor CPU is Go process-startup overhead, against cobra.ExecuteContext at 0.52% for the check the probe exists to perform.

    So the supervisor does have a large avoidable cost, and this issue was right that one existed — it was just pointing at the wrong frame. Batching state writes would recover something below 0.2%; fixing the probe recovers something above 31%.

    Methodology note for re-measurement

    The supervisor rolls up under service_name="aether-proxy". k8s_container_name only yields ["authz","proxy"] and cannot separate the supervisor from Envoy — an earlier attempt to resolve these frames failed for exactly that reason and was mistaken for "the frames are not resolvable." The label that works is process_executable_name="supervisor".

    Closing as not reproduced. Reopen if a future build shows the persistence path above ~1%.

  3. bpalermo commented on Sep 5, 2026

    @bpalermo
    OwnerAuthor

    Correction to the closure — the conclusion stands, the measurement did not

    The 09-04 profiling pass reported writeState, onLiveEpoch and adminProbe as absent below a 0.201% cutoff, and closed this issue as "wrong by ≥25×". The 09-05 pass, with the supervisor's launch overhead removed by #673, measured the same path directly and shows that reading was a symbol-name miss (adminProbe vs the real adminProber.probe / adminServerInfo), not a real absence:

    path (fleet-wide, per 25 min) 09-05 core-seconds implied share of 09-04's 125.8 core-s 09-04 claim
    adminProber.probe → adminServerInfo 9.05 ~7.2% "absent <0.201%"
    onLiveEpoch 2.60 ~2.1% "absent"
    writeState 0.85 ~0.68% "absent"

    All three were above the cutoff on 09-04; the admin-probe rate is identical across the two releases (4.998/s then, 5.000/s now), so the work was there.

    What this issue predicted vs what is true. The prediction was writeState at ~5% of supervisor CPU. Against the post-#673 denominator (19.70 core-s) it is 4.3% — the share prediction was roughly right once the exec waste was gone. Against the 09-04 denominator it was 0.68%. In absolute terms it is 0.85 core-seconds per 25 minutes fleet-wide ≈ 0.0006 fleet cores.

    The closure stands on absolute grounds, not on the "≥25×" claim. Batching or debouncing state writes would recover at most 0.0006 fleet cores. The "wrong by at least 25×" and "absent — resolved and not there" statements in the closing comment are withdrawn; the "not worth doing" conclusion is unchanged. Recorded so the next reader does not inherit a false measurement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions