Repository navigation
fix(registrar): kubernetes backend derives the drain's phase-2 UNHEALTHY from the Pod, so every replica closes pools before SIGTERM (#1144) - #1153
Merged
Conversation
…THY from the Pod, so every replica closes pools before SIGTERM (#1144) The destination agent's second drain step (UNHEALTHY ~1 s before SIGTERM, #152) is a write the kubernetes backend ignores, so it reached only the registrar replica the agent is connected to; sources on other replicas kept their pools to the pod until its Ready condition dropped, after SIGTERM. The phase-2 moment is pod-visible: the deletion request time is deletionTimestamp - deletionGracePeriodSeconds, and the agent's delay is a function of the pod spec alone (sleep preStop - 1 s, floor 2 s, capped by the grace). That function moves to common/drain and both the agent and the backend call it; podHealth lists a terminating, still-Ready pod UNHEALTHY from drain.PoolCloseAt, and the pod informer arms one timer per pod that wakes the sync at that moment. No API writes, no RBAC change. e2e/drain-propagation.sh polls the source proxy's /clusters for the victim's health flags and reports mark -> source pool close per run, split same/cross replica. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
bpalermo
force-pushed
the
fix/1144-k8s-phase2-unhealthy
branch
from
October 3, 2026 18:09
2f6d7ef to
469aef3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1144
Stacked on #1152 (#1145). Base is that branch; retarget to
mainonce it merges.What was wrong
The two-phase drain (#152) has two steps:
close_connections_on_host_health_failurecloses their pools while the app still serves.On the kubernetes backend the agent's phase-2 write is a no-op, so it reached only the registrar replica the destination agent is connected to. #1124 / #1142 made phase 1 pod-derived. Phase 2 was still per-replica.
Measured first (kind,
REGISTRY_BACKEND=kubernetes e2e/drain-propagation.sh, leg S)Setup: 2 registrar replicas,
quic-awith a 15 s sleep preStop and 30 s grace.verifynow polls the source proxy's admin/clustersevery 100 ms for the victim's health flags. The first/failed_eds_healthis the source's pool close. Times are from the destination agent's drain mark; SIGTERM is at about 15 s.Before this PR, on a cross-replica source the host went straight from DRAINING to removed at about 17.7 s, about 2.7 s after SIGTERM. It was never UNHEALTHY before removal, so no source closed its pools before the app exited.
It turned out worse than the issue predicted ("UNHEALTHY when Ready drops"). The pod went from Ready to gone with no listing in between that showed it not-Ready.
With this PR, the cross-replica pool close (14.05–14.32 s) matches the same-replica one (13.71–14.48 s), about 1 s before SIGTERM.
Also seen on PR 1 alone: in one same-replica run the source saw UNHEALTHY at 14.4 s, then DRAINING again for about 3 s. That is the receiving replica's next sync replacing the agent's mark with the Pod view, as #1152 notes. With this PR, 0 of 6 runs fell back to DRAINING after the pool close.
Request routing is unchanged: in every run, the last request reached the victim 0.19–0.69 s after the mark, with 0 non-200.
These runs use the #1152 agent image; this PR's agent change is a pure refactor.
The choice
Three ways to make the phase-2 signal reach every replica:
A pod annotation or condition set by the agent. Accurate to the agent's own mark. But it needs
patch podsRBAC for a node DaemonSet. RBAC cannot scope that to the agent's own node, so every agent could rewrite any pod'sendpoint.aether.io/*routing annotations. It also adds an API write per terminating pod on the churn path, and a chart bump.Forwarding health marks between replicas over the registrar gRPC. Needs peer discovery, replica-to-replica mTLS and a new RPC. It keeps the state per replica, which goes against the principle fix(registrar): kubernetes backend pushes a pod's drain to every replica, not only the one the agent's mark reached (#1124) #1142 set.
A time rule derived from the Pod (chosen). Every input to the agent's phase-2 moment is on the Pod:
deletionTimestamp − deletionGracePeriodSeconds;terminationGracePeriod − 1 swould be wrong. SIGTERM happens when the preStop ends, not at the grace deadline. So the rule reuses the agent's own formula, moved verbatim tocommon/drain.PoolCloseDelay, which both the agent and the backend call so they cannot drift.Cost of the time rule:
deletionTimestampis serialized at one-second resolution (truncated), so the derived moment can be up to 1 s earlier than the agent's. It is never later. Pools close at most 2 s rather than 1 s before SIGTERM, which is the safe side of feat(agent): two-phase drain — DRAINING first, pool-close after delay #152's intent.This option needs no writes, no RBAC and no chart change. It also works when the destination agent is down.
Change
common/drain(new):PoolCloseFloor,PoolCloseDelay(moved fromagent/internal/cni/server.drainDelayForPod, behaviour unchanged) andPoolCloseAt(pod).registry/internal/k8s:podHealth(pod, now)lists a terminating, still-Ready pod DRAINING untilPoolCloseAt, then UNHEALTHY.time.AfterFuncper (pod UID, due time) that signals a re-list. Re-lists go through the syncer's 200 ms debounce.deletionTimestamp(a second delete with a shorter grace) now counts as an endpoint-input change.e2e/drain-propagation.sh: polls the source proxy for the victim's health flags and reports mark → source DRAINING, pool close and last-listed per run. Pool close is summarised split same/cross replica.Tests
//registrar/internal/serverTestPeerReplicaListsPhaseTwoUnhealthyWhenItIsDue: two replicas overbackend.New("kubernetes")with a 1 h poll. The pod has a 3 s sleep preStop and 5 s grace; the agent's UNHEALTHY goes to replica A only. Replica B must list UNHEALTHY within 1 s, and A must not fall back to DRAINING on its next re-list.Condition never satisfied(the peer replica never listed the pod UNHEALTHY at phase 2). Green here.//registry/internal/k8s:TestPodHealth_PoolCloseFromThePod: DRAINING at +13.999 s, UNHEALTHY at +14 s for a 15 s preStop.TestArmPoolClose_WakesTheSyncWhenPhaseTwoIsDue: one timer per pod and due time with the right wait; it signals when it fires; nothing is armed for a not-Ready pod.//common/drain:TestPoolCloseDelay(the agent's table) andTestPoolCloseAt.bazel test --config=raceon//common/drain:all //registry/... //registrar/internal/server:all //registrar/internal/cmd:all //agent/internal/cni/server:all: pass.make gazelle,make format-check, andbazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation //...: clean.No
charts/, RBAC, proto or go.mod changes.🤖 Generated with Claude Code
https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr