Skip to content

fix(agent): close upstream pools at drain-mark — eliminates the P2 app-drain gap - #143

Merged
bpalermo merged 1 commit into
mainfrom
fix/drain-close-connections
Jun 11, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/drain-close-connections

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

The last open mesh bug. Two instrumented svc-1 rolls (per-fail timestamps correlated against each old pod's deletionTimestamp, plus Envoy reset/retry stat deltas) pinned the residual ~3–7 fails per pod delete.

What the data showed

  • The drain pipeline is fast and correct: deletionTimestamp → EDS mark on every client in <800ms; zero fails in the propagation window and the preStop window.
  • 100% of fails land at app exit (+3–7s, the GOAWAY moment).
  • upstream_rq_rx_reset = 0 everywhere — the app never broke a response.
  • retry_success == retry_total — every retry that could fire succeeded.
  • The first pod deleted in each roll produced zero fails (its pools were fully idle at GOAWAY).

Mechanism: Envoy's DRAINING health excludes a host from new picks but deliberately keeps established H2 pool connections open. They idle through preStop and die at the app's shutdown GOAWAY — and streams the server had claimed but not yet answered at that instant fail non-retriably (last_stream_id race). Plus two retry_overflow rejections from the default 3-concurrent retry breaker.

The fix

  1. Draining endpoints now report UNHEALTHY (not DRAINING) + close_connections_on_host_health_failure: true on service clusters: pools close at drain-mark time (~t0+0.8s) while the app is healthy and the pools are idle; anything caught mid-transmit resets pre-request and retries successfully onto a live endpoint. Envoy's connection-level "drain gradually" semantics are redundant here — the mesh already does the gradual part at the EDS level with the app serving through preStop.
  2. healthy_panic_threshold: 0: with drainers counting as unhealthy, a heavy simultaneous roll could cross the default 50% panic threshold and spray traffic at known-unhealthy hosts. Deterministic exclusion + per-request retries + the registry health pipeline are the mesh's answer (same call Istio makes).
  3. max_retries breaker 3 → 16: the drain-time pool close produces a brief burst of pre-request resets; the default breaker rejected retries under exactly that burst (observed retry_overflow).

Validation

  • Unit: DRAINING→UNHEALTHY mapping + cluster shape pinned in tests; full suite 38/38.
  • Post-deploy: re-run the instrumented single-svc roll — expect 0 fails (the fix makes every pod look like the first-deleted pod, which already measured 0).
  • Bonus from instrumentation: zero 404s under churn — fix(agent,registrar): close the agent-roll 404 gaps #142's retention guard confirmed holding in exactly the scenario it was built for.

🤖 Generated with Claude Code

…p-drain gap

Instrumented two controlled svc rolls (2026-06-11) and pinned the residual
~3-7 fails per pod delete: the drain pipeline itself is fast and correct
(deletionTimestamp -> EDS mark on every client in <800ms, zero fails in
the propagation window), but Envoy's DRAINING health keeps established H2
pool connections OPEN. They sit idle through preStop and die at the app's
shutdown GOAWAY — and streams the server had claimed but not yet answered
at that instant fail non-retriably (upstream_rq_rx_reset=0 across all
nodes: the app never broke a response; retry_success == retry_total:
every retry that could fire succeeded; the fails were the GOAWAY
last_stream_id race plus two retry_overflow rejections).

Fix: report draining endpoints as UNHEALTHY instead of DRAINING and set
close_connections_on_host_health_failure on the service clusters. Pools
now close at drain-mark time (~t0+0.8s) while the app is healthy and the
pools are idle; anything caught mid-transmit resets pre-request and
retries successfully onto a live endpoint. The app-side preStop window is
unchanged and still covers genuinely in-progress responses.

Supporting changes on the service clusters:
- healthy_panic_threshold: 0 — with drainers now counting as unhealthy, a
  heavy simultaneous roll could cross Envoy's default 50% panic threshold
  and spray traffic at known-unhealthy hosts; deterministic exclusion +
  per-request retries + the registry health pipeline are the mesh's
  answer, not panic spraying.
- max_retries circuit breaker 3 -> 16 — the drain-time pool close briefly
  produces a burst of pre-request resets; the default breaker rejected
  retries under exactly that burst (retry_overflow observed).

Validation plan: re-run the instrumented single-svc roll; expect 0 fails
(first-pod-deleted rolls already showed 0 — the fix makes every pod look
like the first). Also re-confirmed during instrumentation: zero 404s
under churn (the #142 retention guard holding).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@bpalermo
bpalermo merged commit 60a7d6f into main Jun 11, 2026
10 checks passed
@bpalermo
bpalermo deleted the fix/drain-close-connections branch June 11, 2026 17:04
bpalermo added a commit that referenced this pull request Jun 12, 2026
…#152)

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

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]>

* feat(agent): scale the drain window with the workload's preStop sleep

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]>

---------

Co-authored-by: Claude Opus 4.8 <[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