Repository navigation
[serve] Deflake test_metrics_2 - fix proxy shutdown - #66151
abrarsheikh merged 2 commits into
Conversation
The duration is only observed by is_ready_for_shutdown, which iterates _proxy_states, so a proxy stopped while the controller keeps running (drained node, unhealthy restart) was popped from that dict before anything polled it and was never recorded at all. Stopped proxies are now held until their actor is confirmed gone, and the manager counts them when deciding whether shutdown is complete, so it no longer reports ready by having forgotten them. On the serve.shutdown() path the value was recorded but wrong: the controller calls ProxyState.shutdown() on every control-loop tick, which re-armed the start time each time, so the histogram collected one sample per tick measuring that tick rather than the shutdown. The start time is now armed once. Signed-off-by: john.taylor <[email protected]>
test_proxy_shutdown_duration_metric asserted the metric after serve.shutdown(), which races the controller against its own exit: the duration is recorded in the same control-loop tick that calls ray.kill on the controller, and a metric needs about one export interval to reach the agent. Widening the budget cannot help, since once that window is missed nothing further is ever exported. Measured with a standalone actor that observes a histogram and then kills itself: a 0.3s gap loses the sample, 1.0s lands it. The window is whatever teardown work outlives the proxy, which is why this fails on the HAProxy shard and passes elsewhere. With that step env (RAY_SERVE_ENABLE_HA_PROXY=1, direct ingress off, RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S=0.01) controller shutdown finishes in about 0.5s, measured 0.41s and 0.55s, against 2.04-2.59s across 100 runs without the overlay. Killing the proxy instead exercises the path where the controller stops and replaces it while staying alive, so the export always has time. 5/5 in the HAProxy env that previously failed deterministically, 3/3 without it, and the whole file passes in both. Signed-off-by: john.taylor <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request ensures that proxy shutdown duration metrics are correctly recorded and tracked when a proxy is stopped or replaced while the controller continues to run. It introduces a _stopping_proxy_states list to track proxies undergoing shutdown, prevents re-arming the shutdown start time on repeated shutdown calls, and updates the unit and integration tests to verify this behavior. I have no feedback to provide as there are no review comments.
Empty commit to retrigger premerge after a flaky test_proxy_shutdown_duration_metric run (see ray-project#66151). Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Qisheng He <[email protected]>
| # Kill the proxy so the controller stops it and starts a replacement. Going | ||
| # through serve.shutdown() races the controller against its own exit: it | ||
| # records the duration in the tick that kills itself, too late to export. | ||
| node_id = ray.get_runtime_context().get_node_id() | ||
| proxy = ray.get_actor( | ||
| format_actor_name(SERVE_PROXY_NAME, node_id), namespace=SERVE_NAMESPACE | ||
| ) | ||
| ray.kill(proxy, no_restart=True) |
There was a problem hiding this comment.
i think we should retain the serve.shutdown() and figure out a way to flush the metrics deterministically before shutting down controller?
There was a problem hiding this comment.
If we want to keep serve.shutdown(), there is no clean fix that does not introduce an API change, which is to expose the existing Ray Flush. This would change around 30 lines of production code.
Remembering how long it look my last ray-core change to land, I decided to also investigate a serve-only change that restores serve.shutdown, but there is none other than what is already in the PR. Keeping serve.shutdown requires a graceful controller exit, which never completes because the controller always has in-flight tasks that never finish, so the metric is lost anyway.
So, I recommend we merge the fix as-is to improve the tests flakiness and open a subsequent PR that adds the API, and after than, incorporate the API change into the test.
There was a problem hiding this comment.
ok, to merge, but file a GH issue to expose that API from core. @edoakes any objection to exposing that API?
The HAProxy shard is red on test_proxy_shutdown_duration_metric, a known test_metrics_2 flake that ray-project#66151 fixes. This PR changes no target that runs on that shard: the guarded block is gated behind a gang placement-group check and test_metrics_2 has no gang deployments. Co-Authored-By: Claude <[email protected]> Signed-off-by: john.taylor <[email protected]>
…#66180) ## Why `DisconnectServices` already exports before exit via `opencensus::stats::StatsExporter::ExportNow()`, but has no OpenTelemetry equivalent, and OpenCensus is off by default. So anything recorded since the last periodic push dies with the process. That hook is on the force path too, since `ExecuteForceShutdown` calls it before `QuickExit`, which is why Ray Serve lost `ray_serve_proxy_shutdown_duration_ms` in #66151 (roughly a 40% pass rate on the HAProxy shard). ## What - `OpenTelemetryMetricRecorder::ForceFlush(timeout)`, the flush `Shutdown()` already does, without the teardown. - `ray::stats::FlushMetrics(timeout_ms)`, false when stats are disabled or OpenTelemetry is off. - Call it in `DisconnectServices` beside `ExportNow()`, bounded at 500ms because `ForceFlush` blocks where `ExportNow` does not, and every worker exit takes this path. No caller changes needed, including in Serve. ## Testing A metric recorded before a force kill now reaches the endpoint, with the report interval set to 60s so only the flush can deliver it. C++ case covers `ForceFlush` leaving the recorder usable. `core_worker_lib`, `//:_raylet` and the stats and observability targets build clean; recorder test 5/5. Closes #66178. --------- Signed-off-by: john.taylor <[email protected]> Co-authored-by: Claude <[email protected]>
## Why `test_metrics_2.py::TestProxyStateMetrics::test_proxy_shutdown_duration_metric` is the top flaker on the `serve: HAProxy tests` shard (premerge 73158 failed it on both attempts). It fails as a 90s wait that never sees the series, with no exception, which is why two earlier budget-widening deflakes did not help. The test asserted the metric after `serve.shutdown()`, which races the controller against its own exit: `is_ready_for_shutdown` observes the histogram, and `Controller.shutdown` calls `ray.kill` on the controller in that same tick. A metric needs about one export interval to reach the agent (measured with a standalone actor: 0.3s gap loses the sample, 1.0s lands it), so once that window is missed no budget helps. What covers the window is teardown work that outlives the proxy. The shard sets `RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S=0.01`, which removes a 30s drain, and controller shutdown then finishes in ~0.5s: measured 0.41s and 0.55s there, versus 2.04-2.59s across 100 runs without the overlay. HAProxy alone is not the trigger; on its own it widens the window to 30.4s. ## What - Record the duration for every stopped proxy. It is only observed by `is_ready_for_shutdown`, which iterates `_proxy_states`, so a proxy stopped while the controller keeps running (drained node, unhealthy restart) was popped before anything polled it and never recorded. - Arm the shutdown start time once. The controller calls `ProxyState.shutdown()` every tick, re-arming it each time, so the histogram measured a tick (~20 samples of 3-6ms), not the shutdown. - Point the test at that repaired path: kill the proxy so the controller stops and replaces it while staying alive, and the export always has time. ## Testing 5/5 in the HAProxy env that previously failed deterministically, 3/3 without HAProxy. Whole file: 11 passed / 3 skipped with the overlay, 14 passed without. `tests/unit/test_proxy_state.py` 38 passed, `test_shutdown.py` 14 passed. Each product fix has a regression test that fails when that fix is reverted. The test costs ~25s more, spent detecting the killed proxy (3 health checks at `PROXY_HEALTH_CHECK_PERIOD_S=10`), against a 90s budget, and the shard gets faster since it no longer burns a 90s timeout. --------- Signed-off-by: john.taylor <[email protected]>
…ray-project#66180) ## Why `DisconnectServices` already exports before exit via `opencensus::stats::StatsExporter::ExportNow()`, but has no OpenTelemetry equivalent, and OpenCensus is off by default. So anything recorded since the last periodic push dies with the process. That hook is on the force path too, since `ExecuteForceShutdown` calls it before `QuickExit`, which is why Ray Serve lost `ray_serve_proxy_shutdown_duration_ms` in ray-project#66151 (roughly a 40% pass rate on the HAProxy shard). ## What - `OpenTelemetryMetricRecorder::ForceFlush(timeout)`, the flush `Shutdown()` already does, without the teardown. - `ray::stats::FlushMetrics(timeout_ms)`, false when stats are disabled or OpenTelemetry is off. - Call it in `DisconnectServices` beside `ExportNow()`, bounded at 500ms because `ForceFlush` blocks where `ExportNow` does not, and every worker exit takes this path. No caller changes needed, including in Serve. ## Testing A metric recorded before a force kill now reaches the endpoint, with the report interval set to 60s so only the flush can deliver it. C++ case covers `ForceFlush` leaving the recorder usable. `core_worker_lib`, `//:_raylet` and the stats and observability targets build clean; recorder test 5/5. Closes ray-project#66178. --------- Signed-off-by: john.taylor <[email protected]> Co-authored-by: Claude <[email protected]>
Why
test_metrics_2.py::TestProxyStateMetrics::test_proxy_shutdown_duration_metricis the topflaker on the
serve: HAProxy testsshard (premerge 73158 failed it on both attempts). Itfails as a 90s wait that never sees the series, with no exception, which is why two earlier
budget-widening deflakes did not help.
The test asserted the metric after
serve.shutdown(), which races the controller against itsown exit:
is_ready_for_shutdownobserves the histogram, andController.shutdowncallsray.killon the controller in that same tick. A metric needs about one export interval toreach the agent (measured with a standalone actor: 0.3s gap loses the sample, 1.0s lands it),
so once that window is missed no budget helps.
What covers the window is teardown work that outlives the proxy. The shard sets
RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S=0.01, which removes a 30s drain, andcontroller shutdown then finishes in ~0.5s: measured 0.41s and 0.55s there, versus 2.04-2.59s
across 100 runs without the overlay. HAProxy alone is not the trigger; on its own it widens the
window to 30.4s.
What
is_ready_for_shutdown,which iterates
_proxy_states, so a proxy stopped while the controller keeps running(drained node, unhealthy restart) was popped before anything polled it and never recorded.
ProxyState.shutdown()every tick,re-arming it each time, so the histogram measured a tick (~20 samples of 3-6ms), not the
shutdown.
while staying alive, and the export always has time.
Testing
5/5 in the HAProxy env that previously failed deterministically, 3/3 without HAProxy. Whole file:
11 passed / 3 skipped with the overlay, 14 passed without.
tests/unit/test_proxy_state.py38passed,
test_shutdown.py14 passed. Each product fix has a regression test that fails whenthat fix is reverted. The test costs ~25s more, spent detecting the killed proxy (3 health
checks at
PROXY_HEALTH_CHECK_PERIOD_S=10), against a 90s budget, and the shard gets fastersince it no longer burns a 90s timeout.