Skip to content

fix(registrar): kubernetes backend derives the drain's phase-2 UNHEALTHY from the Pod, so every replica closes pools before SIGTERM (#1144) - #1153

Merged
bpalermo merged 2 commits into
mainfrom
fix/1144-k8s-phase2-unhealthy
Oct 3, 2026
Merged

bpalermo merged 2 commits into
mainfrom
fix/1144-k8s-phase2-unhealthy

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 3, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1144

Stacked on #1152 (#1145). Base is that branch; retarget to main once it merges.

What was wrong

The two-phase drain (#152) has two steps:

  1. DRAINING at the deletion request.
  2. UNHEALTHY about 1 s before SIGTERM, so sources' close_connections_on_host_health_failure closes 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-a with a 15 s sleep preStop and 30 s grace. verify now polls the source proxy's admin /clusters every 100 ms for the victim's health flags. The first /failed_eds_health is the source's pool close. Times are from the destination agent's drain mark; SIGTERM is at about 15 s.

registrar placement mark → source DRAINING mark → source pool close host last listed
#1152 (PR 1) same replica 0.31, 0.64, 0.71 s 14.24, 14.33, 14.33, 14.41 s 17.66–17.80 s
#1152 (PR 1) cross replica 0.46, 0.49, 0.52, 0.59 s never: 4 of 4 runs 17.72–17.76 s
this PR same replica 0.26, 0.31, 0.74 s 13.71, 14.30, 14.48 s 17.79–17.91 s
this PR cross replica 0.49, 0.58, 0.60 s 14.05, 14.12, 14.32 s 17.72–17.74 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 pods RBAC for a node DaemonSet. RBAC cannot scope that to the agent's own node, so every agent could rewrite any pod's endpoint.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:

    • the request time is deletionTimestamp − deletionGracePeriodSeconds;
    • the delay is computed from the pod spec alone: sleep preStop − 1 s, floor 2 s, capped at grace − 2 s. The floor is a compile-time constant, not a flag.
    • Using terminationGracePeriod − 1 s would be wrong. SIGTERM happens when the preStop ends, not at the grace deadline. So the rule reuses the agent's own formula, moved verbatim to common/drain.PoolCloseDelay, which both the agent and the backend call so they cannot drift.

    Cost of the time rule:

    • Up to 1 s early. deletionTimestamp is 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.
    • A timer per pod. Nothing on the Pod changes at that moment, so the backend arms one timer per terminating Pod to wake the sync.

    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 from agent/internal/cni/server.drainDelayForPod, behaviour unchanged) and PoolCloseAt(pod).
  • registry/internal/k8s:
    • podHealth(pod, now) lists a terminating, still-Ready pod DRAINING until PoolCloseAt, then UNHEALTHY.
    • The Pod informer handler arms one time.AfterFunc per (pod UID, due time) that signals a re-list. Re-lists go through the syncer's 200 ms debounce.
    • A moved 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.
  • Runbook: the registrar (kubernetes backend): a DRAINING mark reaches only the replica that received it — cross-replica propagation not measured #1124 paragraph now describes phase 2.

Tests

  • //registrar/internal/server TestPeerReplicaListsPhaseTwoUnhealthyWhenItIsDue: two replicas over backend.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.
  • //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) and TestPoolCloseAt.
  • bazel test --config=race on //common/drain:all //registry/... //registrar/internal/server:all //registrar/internal/cmd:all //agent/internal/cni/server:all: pass.
  • make gazelle, make format-check, and bazel 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

…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
bpalermo force-pushed the fix/1144-k8s-phase2-unhealthy branch from 2f6d7ef to 469aef3 Compare October 3, 2026 18:09
@bpalermo
bpalermo merged commit c35029f into main Oct 3, 2026
24 checks passed
@bpalermo
bpalermo deleted the fix/1144-k8s-phase2-unhealthy branch October 3, 2026 18:44
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.

registrar (kubernetes backend): the agent's second drain step (UNHEALTHY ~1 s before SIGTERM) reaches only the receiving replica

1 participant