Skip to content

feat(agent): two-phase drain — DRAINING first, pool-close after delay - #152

Merged
bpalermo merged 2 commits into
mainfrom
feat/two-phase-drain
Jun 12, 2026
Merged

bpalermo merged 2 commits into
mainfrom
feat/two-phase-drain

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Goal

Eliminate the last ~6–7 code=000 fails per svc roll without hard-closing active streams (directive: in-flight must complete).

Why two phases

Both single-phase variants are instrumented and both leak:

variant mechanism leak
Envoy DRAINING alone no new picks, conns stay open app-exit GOAWAY strands claimed-but-unanswered streams (~3–7/pod delete, non-retriable)
UNHEALTHY + pool close (#143, current) pools close at drain-mark (~t0+0.8s) streams active at the close get cut (~6–7/roll)

Composed temporally, each phase covers the other's gap:

  1. t0 (deletionTimestamp) → endpoint DRAINING (now mapped to Envoy DRAINING): new selections stop within ~1s, in-flight completes.
  2. t0+2s (drainPoolCloseDelay) → termination watch re-registers UNHEALTHY: close_connections_on_host_health_failure shuts the by-then-idle pools while the app is still in its preStop window (workload-requirements: preStop ≥ 3s) — pre-empting the GOAWAY race.

Safety

  • Phase 2 re-checks (under lifecycleMu) that the pod still exists and is still terminating — a CNI DEL that won the race is never resurrected (same pattern as the liveness loop).
  • ctx cancellation aborts the pending phase 2.
  • Delay is a server field; tests default it to 1h so nothing races the goroutine, with explicit opt-in for the phase-2 test.

Validation

  • 37/37 test targets pass; new tests: phase-2 fires after delay, resurrection guard.
  • E2e after deploy: svc-1/svc-2 rolls under load — success = fails < the 6–7 baseline, ideally 0; verify no DRAINING-related regression in steady state and proxy rolls.

🤖 Generated with Claude Code

bpalermo and others added 2 commits June 11, 2026 20:58
Never hard-close active streams on a graceful pod exit (directive
2026-06-11): in-flight requests must complete.

Both single-phase drain variants were instrumented and both leak:
- Envoy DRAINING alone leaves idle H2 pool connections open until the
  app's exit GOAWAY/RST, stranding claimed-but-unanswered streams
  non-retriably (~3-7 per pod delete).
- UNHEALTHY + close_connections_on_host_health_failure (#143) closes
  pools at drain-mark but cuts streams active at that instant (~6-7
  per roll, all code=000 — the current residue).

Compose them temporally instead:
- Phase 1 (deletionTimestamp): the endpoint goes DRAINING and now maps
  to Envoy DRAINING — excluded from new selections within ~1s of
  propagation, established streams run to completion.
- Phase 2 (t0 + drainPoolCloseDelay 2s): the termination watch
  re-registers the endpoint UNHEALTHY; clients'
  close_connections_on_host_health_failure shuts the by-then-idle
  pools while the app is still inside its preStop window — ahead of
  the exit-GOAWAY race phase 1 alone would lose.

Phase 2 re-checks pod existence + terminating under lifecycleMu so a
CNI DEL that won the race is never resurrected; ctx cancellation
aborts it. Delay is a server field (overridable in tests; production
default 2s, sized to land inside the workload-requirements preStop
>= 3s window).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The phase-2 bound is not the 30s termination grace — it is the moment
the app receives SIGTERM (end of preStop), because the pool close must
land while the app still serves to pre-empt the exit-GOAWAY race. A
sleep preStop is machine-readable from the terminating Pod object, so
derive the delay per pod: close 1s before SIGTERM, capped 2s short of
the deletion grace (kubelet hard kill). preStop sleep 15 buys in-flight
requests a 14s window; the e2e workloads' sleep 3 keeps today's 2s;
exec hooks and hookless pods keep the conservative floor.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@bpalermo

Copy link
Copy Markdown
Owner Author

Pushed fd42390: the drain window now scales with the workload's preStop sleep instead of a fixed 2s.

Rationale (from review discussion): the phase-2 bound is not the 30s termination grace — it's the SIGTERM moment (end of preStop), since the pool close must land while the app still serves to beat the exit-GOAWAY race. The sleep preStop is machine-readable on the terminating Pod, so:

workload drain window (phase 1 → close)
preStop: sleep 15, grace 30 14s
preStop: sleep 3 (e2e workloads) 2s (unchanged)
sleep 60, grace 30 28s (capped 2s short of hard kill)
exec hook / no hook 2s floor (opaque)

Workloads opt into generosity by sizing their own preStop — no mesh-side config. Unit-tested (TestDrainDelayForPod); 37/37 targets pass.

@bpalermo
bpalermo merged commit 22ec3d3 into main Jun 12, 2026
9 checks passed
@bpalermo
bpalermo deleted the feat/two-phase-drain branch June 12, 2026 00:11
bpalermo added a commit that referenced this pull request Jun 12, 2026
… 10s for zero-loss rolls (#153)

The two-phase drain (#152) scales the in-flight completion window with
the workload's sleep preStop (window = sleep - 1s, capped 2s short of
the termination grace). Update the guidance and example manifest from
sleep 3 to sleep 10, with the measured results: sleep 10 -> 0 failed
requests per roll under full load; sleep 3 (supported minimum) -> ~1
blip per pod. Document drain phase 2 in the termination sequence.

Co-authored-by: Claude Opus 4.8 <[email protected]>
bpalermo added a commit that referenced this pull request Jun 12, 2026
…Phase 4)

Service clusters no longer carry the per-client MeshReadyPath health
check or IgnoreNewHostsUntilFirstHc: routability is EDS health
(delegated liveness — endpoints register UNHEALTHY, the destination
gateway vets them, promotion propagates in ~1s), and fast local
failure detection is outlier detection, which is O(actual traffic)
instead of O(clients x endpoints). Consecutive local-origin failures
(3) catch the dead-pod-EDS-lag case; consecutive 5xx (5) catch a
sick app; max_ejection_percent 50 keeps a local ejection wave from
emptying a cluster EDS still believes healthy (panic threshold
remains 0). Two-phase drain semantics (#152) are unchanged:
DRAINING/UNHEALTHY still arrive via EDS and pool close still rides
close_connections_on_host_health_failure.

This retires the ~200M probes/sec mesh-wide HC term at the 3k-svc /
10k-node design point.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
bpalermo added a commit that referenced this pull request Jun 12, 2026
…Phase 4) (#161)

Service clusters no longer carry the per-client MeshReadyPath health
check or IgnoreNewHostsUntilFirstHc: routability is EDS health
(delegated liveness — endpoints register UNHEALTHY, the destination
gateway vets them, promotion propagates in ~1s), and fast local
failure detection is outlier detection, which is O(actual traffic)
instead of O(clients x endpoints). Consecutive local-origin failures
(3) catch the dead-pod-EDS-lag case; consecutive 5xx (5) catch a
sick app; max_ejection_percent 50 keeps a local ejection wave from
emptying a cluster EDS still believes healthy (panic threshold
remains 0). Two-phase drain semantics (#152) are unchanged:
DRAINING/UNHEALTHY still arrive via EDS and pool close still rides
close_connections_on_host_health_failure.

This retires the ~200M probes/sec mesh-wide HC term at the 3k-svc /
10k-node design point.

Co-authored-by: Claude Opus 4.8 <[email protected]>
bpalermo added a commit that referenced this pull request Oct 3, 2026
…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 added a commit that referenced this pull request Oct 3, 2026
…THY from the Pod, so every replica closes pools before SIGTERM (#1144) (#1153)

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.


Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

Co-authored-by: Claude Fable 5.1 <[email protected]>
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