Skip to content

feat(agent): node-ownership lock and standby mode for a surge roll of the agent (proposal 041) - #1155

Merged
bpalermo merged 4 commits into
mainfrom
feat/041-agent-standby
Oct 3, 2026
Merged

bpalermo merged 4 commits into
mainfrom
feat/041-agent-standby

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Refs #1123. Part 1 of 4 of proposal 041 (the standby agent). Stack: this PR ← agent-ready probe ← chart ← e2e gate (Fixes #1123).

Nothing changes for a delete-then-create roll or a plain restart: the lock is free when the agent starts, so it owns the node as the manager starts and no takeover step runs. The surge strategy is turned on by the chart PR.

What it does

Node ownership (agent/internal/ownership).

  • The agent tries an exclusive flock on /run/aether/agent.lock (--node-lock) once, at process start, before local storage is read.
  • Free lock: the agent owns the node.
  • Taken lock: the agent is a standby. A goroutine blocks on the lock, and the kernel releases it the instant the owner's process dies, however it dies.
  • After acquiring the lock, before announcing ownership, the standby:
    1. waits until no server answers on xds.sock/cni.sock. This covers an agent that predates the lock: the first surge roll onto this version.
    2. runs its takeover steps in order (below).

Standby builds everything a first serve needs: identity, registry watch, listeners from storage, capture projection, client certificates. It holds back:

  • Node sockets: xds.sock and cni.sock. There is a new SetBindGate in common/xds, called between PreListen and bind.
  • Registry and storage writers: the liveness loop, termination watch and ghost sweep. The SPIRE resubscription of stored pods still runs, because client certificates are a first-serve gate.
  • Node files:
    • the observed-upstreams/QUIC-pairs file and the mesh-DNS snapshot (SnapshotCache.SetNodeWriteGate);
    • the conflist repair (Reasserter.Owned; it still observes, because readiness reads it).
  • Node and API writes: the taint remover (TaintRemover.Owned) and the metrics port.
    • The metrics port is no longer bound by the manager. A runnable binds it once the node is owned, and retries EADDRINUSE for up to 30 s, never fatally. The kernel can hand over the flock while the old process is still closing its listening socket.

Takeover steps (only on a contended start):

  1. SnapshotCache.ReloadNodeState:

  2. CNIServer.ReconcileStorage (open question 1). storage.Reloader (CachedLocalStorage.Reload) returns added/updated/removed records against the standby's view:

    • DEL during the overlap: RemovePod + SVID unsubscribe;
    • ADD: AddPod + SVID subscribe;
    • rewrite (termination watch → Terminating): AddPod.

    It waits for the xDS server's own load from storage, which is the view the diff is taken against. It runs before either socket binds.

Then xds.sock binds, after a ≤1 s wait for certificates of pods added by the takeover. Then cni.sock binds and the writers start.

Readiness. A standby check passes once the xDS first snapshot is complete (FirstServeReady: every #1085/#1099/#1118 gate passed). It always passes once the node is owned. This is what a surge roll waits on.

Socket ownership.

  • SetUnlinkOnClose(false), plus an inode check before the server unlinks its own socket on shutdown.
  • A server exiting never deletes a successor's socket.

Registrar watch keyed per agent. Found in implementation, not in the proposal:

  • The registrar keyed WatchEndpoints streams by cluster/node. With two agents on one node, each Subscribe closes the other's stream with DataLoss and forces a full resync, and they ping-pong for the whole overlap.
  • WatchEndpointsRequest.instance (field 5, additive) carries the pod name and is now part of the key. When it is empty, the old key is kept.

Hazard table (proposal 041 § "What the overlap must not break")

row closed by test
hostNetwork ports metrics bind after ownership, EADDRINUSE retried (this PR); ports: dropped and exec probes over a pod socket (PRs 2, 3) TestOwnedRunnable*
Socket ownership bind gate; SetUnlinkOnClose(false) + inode check; live-server wait for a lock-less predecessor TestBindGate_*, TestShutdownLeavesASuccessorsSocketAlone, TestAServerWithoutTheLockStillHoldsTheNode, TestAStaleSocketIsNotAServer
CNI ADD during the overlap standby binds no cni.sock; takeover diff incl. DEL TestReconcileStorage_AppliesTheOverlap (ADD + DEL + rewrite over a real shared dir), …ADDThenDELInsideTheOverlapLeavesNothing; e2e in PR 4
Persisted state, two writers write gate + merge at takeover; storage writers after ownership; conflist repair gated TestNodeWriteGate_*, TestReloadNodeState_*, TestStandbyObservesButDoesNotRepair
Registry writes, drain marks, SPIRE streams standby writes nothing to the registry (writers start on ownership); Broker subscriptions allowed. Verified: the old agent deregisters nothing on its way out (its loops just stop on ctx). The watch-key collision above is fixed TestSurgeAgentsOnOneNodeKeepBothWatches, TestWatcherIDOf
Node taint (033) remover requires ownership (a standby stats the other agent's socket) TestReconcileStandbyLeavesTheTaint
Readiness meaning standby check TestStandbyReadiness, TestPreListen_SignalsTheStandbyMilestones, TestSetOwnership_BindsOnlyOnceOwned
Plugin/agent version skew the CNI gRPC API (api/aether/cni) is untouched by this stack —
Capacity chart PR —

Also argued, not gated: the GAMMA/L4 reconcilers write route status. Every agent on every node already writes the same deterministic status (only on change), and a standby is one more identical writer.

Lock/handoff state machine tests (agent/internal/ownership):

  • free lock → owned, no steps;
  • held → standby; steps run in order before ownership;
  • handoff < 200 ms after release;
  • a failing step still owns;
  • a standby stopped early never owns;
  • two standbys → one owner at a time;
  • --node-lock="".

Checks

  • bazel test --test_tag_filters=-integration //agent/... //common/xds:all //registrar/... //registry/...: all pass.
  • --config=race on ownership, storage, common/xds, cni/server, xds/cache, xds/server, node, cniconflist, registrar/server and cmd: all pass.
  • bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation on the touched trees: clean. gocognit pushed ReconcileStorage, runAgent and Reasserter.Start into helpers.
  • make gazelle, make format. No go.mod change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

… the agent (proposal 041)

A node agent now claims its node with an exclusive flock on
/run/aether/agent.lock, tried once at process start before local storage is
read. A free lock is today's behaviour: the agent owns the node as the manager
starts. A taken lock makes it a standby that builds everything a first serve
needs (identity, registry watch, listeners from storage, capture projection,
client certificates) but binds neither xds.sock nor cni.sock and writes no node
file until the kernel releases the lock.

Takeover steps, between the lock and ownership: merge the observed upstreams
and QUIC pairs the old agent persisted (its final flush included), then diff
local storage against the standby's load and apply the overlap's CNI ADD/DEL
(storage.Reloader; a DEL now removes listeners and the SVID subscription).
Then the sockets bind (bind gate in common/xds), and the liveness loop,
termination watch, ghost sweep, observed-set flush, mesh-DNS snapshot, conflist
repair, taint remover and metrics server start.

- common/xds: SetBindGate; SetUnlinkOnClose(false) plus an inode check, so a
  server exiting never unlinks a successor's socket.
- readiness: a "standby" check passes once the first snapshot is complete,
  and always once the node is owned.
- registrar: WatchEndpointsRequest.instance (the pod name) keys the watch, so
  two agents on one node no longer replace each other's stream.
- a lock-free node with a live server on its sockets (an agent predating the
  lock) is also treated as owned by someone else until nothing answers.

Refs #1123

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
…es passed, not timed out (proposal 041 review)

FirstServeReady also closes when the registry gate (15 s, local-only start) or
the capture gate (10 s) times out: right for a node nobody else serves, wrong
for a surge standby. Its Ready lets the DaemonSet delete a healthy old agent,
and the takeover would then publish local-only CDS/EDS over the proxy's working
config, the #740 clobber. A registrar + agent upgrade makes that a real
scenario.

- AgentXdsServer.StandbyComplete closes once the first snapshot is built, the
  registry actually loaded (initially or by the background retry), and the
  capture projection landed. The "standby" readiness check waits on it, so a
  standby that cannot reach the registrar stalls the roll (maxUnavailable 0)
  instead. The client-certificate gate stays lenient.
- The post-takeover certificate wait covers only pods the takeover added: a
  takeover step notes the backlog before the reconcile (MarkTakeoverBaseline),
  and the bind waits (<= 1 s) only while the backlog is above it. One pod whose
  SVID never comes no longer adds 1 s to every takeover.
- Proposal: both recorded, plus the in-flight-ADD-at-SIGTERM gap cost and the
  liveServer note.

Refs #1123

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo

bpalermo commented Oct 3, 2026

Copy link
Copy Markdown
Owner Author

Review fixes pushed (b0b6d39):

  • Standby Ready only when the gates PASSED. New AgentXdsServer.StandbyComplete: first snapshot built AND the registry actually loaded (initial load or the background retry) AND the capture projection landed. The standby readiness check waits on it. A standby that can't reach the registrar stays NotReady, so the roll stalls (maxUnavailable 0) instead of hitting the agent: startup is fatally coupled to the SPIRE Workload API — a 25 s SVID wait exits the process (2–4 restarts per node on every cold boot; #662 family) #740 clobber. The client-certificate gate stays lenient. Tests: TestStandbyComplete_ATimedOutRegistryGateIsNotComplete, …CaptureGate…, …AllGatesPassed.
  • Takeover cert wait counts only what the takeover added. MarkTakeoverBaseline is a takeover step before the CNI reconcile, and the bind waits (≤1 s) only while the backlog is above it. Tests: TestTakeoverCertificateWait_*.
  • Proposal records both, plus the in-flight-ADD-at-SIGTERM gap cost (≤~2 s, envoyAckTimeout) and the liveServer note.

Re-validated on kind with surge on: 20 rolls under load, gap median 0.080 s / p99 0.169 s, 0 non-200 of 18,107, gate 1.0 s green. The overlap ADD+DEL e2e is green again.

🤖 Generated with Claude Code

@bpalermo
bpalermo marked this pull request as ready for review October 3, 2026 18:36
@bpalermo
bpalermo merged commit ff9fc53 into main Oct 3, 2026
24 checks passed
@bpalermo
bpalermo deleted the feat/041-agent-standby branch October 3, 2026 18:54
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.

1 participant