Skip to content

feat(mesh-dns): keep answering after SIGTERM until the successor is proven serving (#729) - #734

Merged
bpalermo merged 1 commit into
mainfrom
feat/729-mesh-dns-lame-duck
Sep 6, 2026
Merged

bpalermo merged 1 commit into
mainfrom
feat/729-mesh-dns-lame-duck

Conversation

@bpalermo

@bpalermo bpalermo commented Sep 6, 2026

Copy link
Copy Markdown
Owner

Closes #729.

The mechanism (from #726 Phase 1)

aether-mesh-dns rolls with maxSurge: 1 / maxUnavailable: 0, SO_REUSEPORT, readiness initialDelaySeconds: 1, no preStop and no drain. The DaemonSet controller deletes the predecessor the instant the successor passes its first readiness probe, the kubelet SIGTERMs it, and Server.Start closed the UDP + TCP sockets immediately on <-ctx.Done().

While both sockets are in the reuseport group the kernel hashes each arriving datagram at one of them. Anything already steered into the departing socket's queue is silently discarded by close(). #726 measured exactly that: a 5.106 s hole (12:30:40.841Z → 12:30:45.947Z) in one prober's mesh-domain access log, 16 timeouts, and zero non-200 responses anywhere in Envoy — the requests never reached the data plane. One lost datagram, amplified because Go's resolver singleflights by name, so every concurrent lookup of that name waited out the same retransmit.

#728 shrank the client-side cost (options timeout:1 attempts:3, dns_timeout now reachable). The datagram loss itself was untouched, and that is this PR.

Design as implemented

1. Lame duck on SIGTERM (agent/internal/meshdns/lameduck.go). On SIGTERM the resolver:

  • removes its pod-local ready marker (so mesh-dns-ready reports NotReady) and stops the wedge watchdog, so nothing can restore the marker,
  • keeps the UDP + TCP listeners serving,
  • closes them only when a successor is observed serving, --lame-duck-max elapses, or a second signal arrives,
  • then closes and exits 0.

2. Successor detection is evidence, not a timer. The resolver answers a reserved identity name — _instance._aether.<mesh-domain> TXT, TTL 0 — with 64 random bits minted at process start. The stamp is random rather than pid-derived on purpose: predecessor and successor each run as pid 1 in their own container, so a pid stamp would make them indistinguishable and every window would run to its deadline.

The lame duck queries that name over the wire at its own listen address every 200 ms (150 ms timeout). Both processes hold a socket in the reuseport group, so the kernel hashes each probe's 4-tuple at one of them, and a fresh ephemeral source port per probe re-rolls that hash. A reply carrying someone else's stamp proves both halves of what must be true before closing: the successor is in the group (the kernel will steer to it) and it answers (what it is steered gets a reply). Our own stamp, or no reply at all, is "not yet" — never "no successor".

The name is shaped like the existing self-check probe (_selfcheck._aether.…) and for the same reasons: under the mesh domain so it is answered authoritatively and never forwarded to kube-dns, and both labels start with _, which is not a legal DNS-1123 namespace or Service name, so the record table can never hold a colliding key. It is matched before isMeshName in serve, or it would just be a permanently missing service. Exposure: any pod on the node can ask for it and learn 16 hex characters that mean nothing outside this handoff.

3. Second SIGTERM aborts. signal.NotifyContext consumes the first signal; the abort handler is registered only after the shutdown begins, so the first signal can never be mistaken for the second.

4. Readiness during lame duck. The marker goes first, via the same pod-local marker mechanism the proxy supervisor uses, so the DaemonSet controller's view is honest. This does not affect datagram steering — for a hostNetwork SO_REUSEPORT listener the kernel picks which co-bound socket receives each datagram and neither knows nor cares what the kubelet thinks of the pod. That is exactly why the socket has to stay open; readiness alone would change nothing.

5. Residual — documented in runLameDuck, in the chart, and here. The instant of close() on a reuseport socket can still drop a datagram the kernel queued between the last recvfrom and the close. That is a kernel race and is not closable from userspace; eBPF sk_reuseport steering (drain the departing socket out of the group first) would be the complete fix. The lame duck reduces the exposure from "every roll" to that sub-millisecond race.

New flag and values keys

flag --lame-duck-max (duration, default 10s; 0 disables and restores the pre-#729 close-on-SIGTERM)
values agent.meshDnsDaemon.lameDuckMax: "10s"
chart 0.92.7 → 0.92.8

terminationGracePeriodSeconds is derived from lameDuckMax (+5 s) by a new aether.meshDns.lameDuckSeconds helper rather than hand-maintained beside it: a grace period shorter than the window means the kubelet SIGKILLs mid-drain, which is precisely the abrupt close this removes. Rendered: "10s" → 15, "0s" → 5, "2m" → 125, "500ms" → 6, unset → 15 (it follows the binary's own default, not zero). The flag uses the existing {{- if not (kindIs "invalid" …) }} guard so "0s" renders as a real kill switch. No preStop — the process handles SIGTERM itself, and a preStop sleep would only add a fixed delay in front of a window that already ends on evidence. maxSurge: 1 / maxUnavailable: 0 unchanged.

Metrics and log lines, as exported by Prometheus

OTel instrument Prometheus
aether.mesh_dns.lame_duck.exits (counter, attr reason) aether_mesh_dns_lame_duck_exits_total{reason="successor|deadline|signal"}
aether.mesh_dns.lame_duck.duration (histogram, unit s, attr reason) aether_mesh_dns_lame_duck_duration_seconds_{bucket,sum,count}

Buckets are seconds-oriented: 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 5, 7.5, 10, 15, 30. That is deliberate — #732/#733 was seconds recorded against OTel's millisecond-tuned defaults, which flattened every value below 5 s into one bucket and is exactly why the #726 validation could not measure this window. The shared meterBuilder.histogram now requires boundaries instead of defaulting to one instrument's set, so the next histogram cannot repeat it.

aether.mesh_dns.queries gains one bounded result value, result="instance_id" (a handful per roll).

INFO lines, from the mesh-dns logger:

lame duck started              instance=<self> max=10s probeInterval=200ms
successor observed             instance=<self> successor=<peer>
lame duck deadline reached     instance=<self> max=10s
lame duck aborted by a second termination signal   instance=<self>
listener closed                addr=<host:18054> instance=<self>

Validated locally

  • bazel test --test_arg=-test.short //agent/internal/meshdns/... //agent/cmd/mesh-dns/... //agent/cmd/mesh-dns-ready/... //common/... — 21/21 pass; full (non-short) meshdns run passes too.
  • Unit tests over the state machine with a fake clock and a scripted detector: successor observed → reason=successor with the peer's stamp and the exact window duration; inconclusive probes keep the window open; deadline → reason=deadline with the ticker armed at --lame-duck-max; second signal → reason=signal, immediate, winning over a detector that would have succeeded. Plus: stamp uniqueness (100 draws), the identity name answered authoritatively as TXT/NODATA with TTL 0 and not shadowed by a colliding record-table key or by case, probeDialAddr wildcard→loopback, --lame-duck-max=0 closing immediately.
  • Integration-style, hermetic (no Docker): two real resolvers co-bind one SO_REUSEPORT UDP/TCP port on 127.0.0.1 (ephemeral), a client streams mesh A queries throughout, the predecessor's context is cancelled (the SIGTERM equivalent). Asserts the predecessor exits with lame_duck.exits{reason="successor"} == 1 read off an OTel ManualReader, its ready marker is gone, and the query stream stays answered (tolerating at most the one documented close-instant residual). Observed handoff: 0.3–0.8 s.
  • make gazelle, make format-check, bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation (gocognit 15) — all clean.
  • helm lint charts/aether clean; helm template verified for the default and for 0s / 2m / 500ms / null.

On-cluster validation recipe

Not run here — this is deploy-and-measure work for the main session.

  1. Deploy chart 0.92.8 with the new image, confirm appVersion after upgrade (publish: concurrent runs race on the same chart version tag — older build overwrote newer (77a7d45 over 95b158e), deploy silently lacked #689 #692 publish race) and that every aether-mesh-dns pod carries --lame-duck-max=10s and terminationGracePeriodSeconds: 15.
  2. Under steady prober load (5 rq/s per path per prober, 5 probers), do ≥10 kubectl rollout restart daemonset/aether-mesh-dns cycles, waiting for full convergence + a 120 s settle between them.
  3. Per roll, per path, per prober: raw per-series counter deltas (not rate(), not increase()) of timeout + dns_timeout + connection_error. Pass = 0 on every roll on both paths (echo.aether-test.aether.internal:18081 and echo.aether-test.svc.cluster.local:18081).
  4. Per roll: aether_mesh_dns_lame_duck_exits_total{reason} per node. Expect reason="successor" on every node, every roll. A deadline means the successor never answered — the exposure is unchanged for that node and the roll should be treated as a finding.
  5. Lame-duck duration per node: aether_mesh_dns_lame_duck_duration_seconds_bucket — expect the mass at le=0.25/le=0.5. Anything near le=10 is a deadline in disguise.
  6. Access-log gap method (prober/mesh-dns: a mesh-dns roll drops the in-flight UDP query (SO_REUSEPORT close, no lame-duck) → 5 s resolv.conf retransmit + Go singleflight stall every lookup of that name → 16–31 prober 'timeout' misclassified as HTTP (dns_timeout was unreachable: probe budget 2 s < retransmit 5 s) #726 Phase 1), the only direct measurement of the residual. For each roll, LogsQL log_name:"aether_access_logs" reporter:"source" pod_name:"<prober>" authority:"echo.aether-test.aether.internal:18081" across the roll window; compute consecutive-record gaps. Pass = no gap ≥ 1 s on the mesh-domain path, with the svc.cluster.local path as the in-window control. Control-test every negative (response_flags:"URX" field syntax) — LogsQL false zeros have burned three investigations.
  7. Also assert: mesh-dns restarts 0, aether_mesh_dns_ready/watch_active = 1 and records steady on all nodes, no alerts in #alerts during the window.

Worth a follow-up either way: an alert on increase(aether_mesh_dns_lame_duck_exits_total{reason="deadline"}[1h]) > 0 outside a genuine scale-down, in the GitOps repo where the deployed rules live.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

…roven serving (#729)

On an `aether-mesh-dns` roll (maxSurge:1/maxUnavailable:0, SO_REUSEPORT,
readiness initialDelaySeconds:1, no preStop, no drain) the DaemonSet controller
deletes the predecessor the instant the successor passes its first readiness
probe, and `Server.Start` closed the UDP+TCP sockets on the resulting SIGTERM.
Every datagram the kernel had already steered into the departing socket's queue
was discarded. #726 Phase 1 traced a 5.106 s hole in the mesh-domain access log
to exactly one such lost query: Go's resolver singleflights by name, so one drop
stalls every concurrent lookup of that name for a full retransmit timeout.
#728 shrank that to ~1 s from the client side; the datagram loss was untouched.

Lame duck. On SIGTERM the resolver now removes its ready marker (the pod reports
NotReady, which is honest — it IS leaving) and KEEPS SERVING until either a
successor is observed answering on the same address or `--lame-duck-max`
(default 10s) elapses; only then does it close and exit 0.

Successor detection is evidence, not a timer. The resolver answers a reserved
identity name, `_instance._aether.<mesh-domain>` TXT, with 64 random bits minted
at process start — random, because predecessor and successor are each pid 1 in
their own container, so a pid stamp could never tell them apart. The lame duck
queries that name OVER THE WIRE at its own listen address every 200 ms. Both
processes hold a socket in the reuseport group, so the kernel hashes each probe
(fresh source port, fresh hash) at one of them; a reply carrying someone else's
stamp proves both halves of what must be true before closing — the successor is
IN the group and it ANSWERS. Our own stamp, or no reply, is "not yet".

A second SIGTERM cuts the window short (reason=signal); the handler is
registered only after the shutdown begins, so the first signal can never be
mistaken for it.

Residual, stated in the code and unfixable from userspace: close() on a
reuseport socket can still discard a datagram queued between the last recvfrom
and the close. eBPF sk_reuseport steering would be the complete fix. This
narrows the exposure from every roll to that instant.

Metrics: aether.mesh_dns.lame_duck.exits{reason=successor|deadline|signal} and
aether.mesh_dns.lame_duck.duration (seconds, EXPLICIT seconds buckets — the
mistake #732/#733 just swept out of the repo). The histogram builder now
requires boundaries rather than defaulting, so the next instrument cannot repeat
it. INFO lines: "lame duck started", "successor observed" (both stamps),
"lame duck deadline reached", "listener closed".

Chart 0.92.7 -> 0.92.8: `agent.meshDnsDaemon.lameDuckMax` (default "10s") and a
terminationGracePeriodSeconds DERIVED from it (+5s) so the kubelet can never
SIGKILL mid-window; no preStop, since the process handles SIGTERM itself.
maxSurge:1/maxUnavailable:0 unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant