Repository navigation
[Serve] Throttle serve_deployment_replica_healthy gauge recording on the controller - #60823
Conversation
Signed-off-by: abrar <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces a performance optimization for the Serve controller by throttling the recording of the serve_deployment_replica_healthy metric. This is achieved by adding a time-based cache to DeploymentState, which avoids redundant Gauge.set() calls for replicas with unchanged health status. The implementation is clean, and the new logic is well-tested with unit tests covering various scenarios like cache hits, TTL expiration, and cache cleanup. My only suggestion is to use time.monotonic() instead of time.time() for measuring time intervals to make the caching logic robust against system clock changes.
| every control-loop iteration while still refreshing the metric often | ||
| enough for Prometheus export. | ||
| """ | ||
| now = time.time() |
There was a problem hiding this comment.
time.time() is not guaranteed to be monotonic. If the system clock is adjusted backwards, now - cached[1] could be negative, which could lead to the gauge not being updated for a long time, even past the intended interval. It's recommended to use time.monotonic() for measuring time durations to avoid this issue.
| now = time.time() | |
| now = time.monotonic() |
Signed-off-by: abrar <[email protected]>
PR ray-project#60823 throttled the serve_deployment_replica_healthy gauge with a 10s cache TTL and bumped the timeout in test_metrics.py to 40s, but missed applying the same fix to the HAProxy variant. The default 10s wait_for_condition timeout is too short now that the gauge is cached. Increase the timeout to 40s to match test_metrics.py. Signed-off-by: Seiji Eicher <[email protected]>
PR ray-project#60823 throttled the serve_deployment_replica_healthy gauge with a 10s cache TTL and bumped the timeout in test_metrics.py to 40s, but missed applying the same fix to the HAProxy variant. The default 10s wait_for_condition timeout is too short now that the gauge is cached. Increase the timeout to 40s to match test_metrics.py. Signed-off-by: Seiji Eicher <[email protected]>
The HAProxy BUILD target was missing the RAY_SERVE_REPLICA_HEALTH_GAUGE_REPORT_INTERVAL_S=0.1 env var that the non-HAProxy metrics tests already set. Without it, the health gauge uses the default 10s cache TTL, causing scrapes to miss gauges that haven't been refreshed within the export window. The non-HAProxy test_metrics.py target has had this override since PR ray-project#60823 added the caching optimization. Signed-off-by: Seiji Eicher <[email protected]>
…#60953) ## Why are these changes needed? `test_metrics_haproxy.py::test_replica_metrics_fields` is failing in postmerge. - #60823 added a 10s cache on the health gauge, with `RAY_SERVE_REPLICA_HEALTH_GAUGE_REPORT_INTERVAL_S=0.1` in the metrics BUILD target to keep tests passing - #60914 added a HAProxy BUILD target that re-runs serve tests with `RAY_SERVE_ENABLE_HA_PROXY=1`, but didn't carry over that env var - Without it, the health gauge goes stale between scrapes and the test misses one deployment's metric Fix: add the missing env var to the HAProxy target. Example failure: https://buildkite.com/ray-project/postmerge/builds/15966#019c49dc-52b4-40dd-a67e-9f5ea5c61755 --------- Signed-off-by: Seiji Eicher <[email protected]>
…the controller (ray-project#60823) ### Why Profiling the Serve controller under load shows `Gauge.set()` for the `serve_deployment_replica_healthy` metric consuming ~5.9% of CPU. The call is made for **every running replica on every control-loop iteration**, even when the value hasn't changed. At 128+ replicas this is O(num_replicas) Cython FFI calls per loop — pure waste in steady state. <img width="1466" height="613" alt="image" src="https://github.com/user-attachments/assets/4529ba1a-880d-4c40-baca-1bf2d2ed26dc" /> ### What Add a time-based cache (`_health_gauge_cache`) to `DeploymentState` that tracks the last-reported `(value, timestamp)` per replica. `_set_health_gauge()` skips the `Gauge.set()` call when the value is unchanged and the entry is younger than `RAY_SERVE_HEALTH_GAUGE_REPORT_INTERVAL_S` (default 10s). The gauge is still re-recorded periodically so it remains visible across OpenCensus export windows, and is always recorded immediately on state transitions (healthy ↔ unhealthy). Cache entries are cleaned up when replicas are fully stopped. The interval is configurable via the `RAY_SERVE_HEALTH_GAUGE_REPORT_INTERVAL_S` env var and set to `0.1` in CI to stay within the fast test metrics export cadence. ### Test plan - [x] New unit test `test_health_gauge_caching` — verifies cache hits, TTL expiry re-records, value-change bypass, and cleanup on replica stop - [x] `test_replica_metrics_fields` integration test passes (metric still appears in Prometheus) Related to ray-project#60680 --------- Signed-off-by: abrar <[email protected]> Signed-off-by: Adel Nour <[email protected]>
…ray-project#60953) ## Why are these changes needed? `test_metrics_haproxy.py::test_replica_metrics_fields` is failing in postmerge. - ray-project#60823 added a 10s cache on the health gauge, with `RAY_SERVE_REPLICA_HEALTH_GAUGE_REPORT_INTERVAL_S=0.1` in the metrics BUILD target to keep tests passing - ray-project#60914 added a HAProxy BUILD target that re-runs serve tests with `RAY_SERVE_ENABLE_HA_PROXY=1`, but didn't carry over that env var - Without it, the health gauge goes stale between scrapes and the test misses one deployment's metric Fix: add the missing env var to the HAProxy target. Example failure: https://buildkite.com/ray-project/postmerge/builds/15966#019c49dc-52b4-40dd-a67e-9f5ea5c61755 --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Adel Nour <[email protected]>
…the controller (ray-project#60823) ### Why Profiling the Serve controller under load shows `Gauge.set()` for the `serve_deployment_replica_healthy` metric consuming ~5.9% of CPU. The call is made for **every running replica on every control-loop iteration**, even when the value hasn't changed. At 128+ replicas this is O(num_replicas) Cython FFI calls per loop — pure waste in steady state. <img width="1466" height="613" alt="image" src="https://github.com/user-attachments/assets/4529ba1a-880d-4c40-baca-1bf2d2ed26dc" /> ### What Add a time-based cache (`_health_gauge_cache`) to `DeploymentState` that tracks the last-reported `(value, timestamp)` per replica. `_set_health_gauge()` skips the `Gauge.set()` call when the value is unchanged and the entry is younger than `RAY_SERVE_HEALTH_GAUGE_REPORT_INTERVAL_S` (default 10s). The gauge is still re-recorded periodically so it remains visible across OpenCensus export windows, and is always recorded immediately on state transitions (healthy ↔ unhealthy). Cache entries are cleaned up when replicas are fully stopped. The interval is configurable via the `RAY_SERVE_HEALTH_GAUGE_REPORT_INTERVAL_S` env var and set to `0.1` in CI to stay within the fast test metrics export cadence. ### Test plan - [x] New unit test `test_health_gauge_caching` — verifies cache hits, TTL expiry re-records, value-change bypass, and cleanup on replica stop - [x] `test_replica_metrics_fields` integration test passes (metric still appears in Prometheus) Related to ray-project#60680 --------- Signed-off-by: abrar <[email protected]>
…ray-project#60953) ## Why are these changes needed? `test_metrics_haproxy.py::test_replica_metrics_fields` is failing in postmerge. - ray-project#60823 added a 10s cache on the health gauge, with `RAY_SERVE_REPLICA_HEALTH_GAUGE_REPORT_INTERVAL_S=0.1` in the metrics BUILD target to keep tests passing - ray-project#60914 added a HAProxy BUILD target that re-runs serve tests with `RAY_SERVE_ENABLE_HA_PROXY=1`, but didn't carry over that env var - Without it, the health gauge goes stale between scrapes and the test misses one deployment's metric Fix: add the missing env var to the HAProxy target. Example failure: https://buildkite.com/ray-project/postmerge/builds/15966#019c49dc-52b4-40dd-a67e-9f5ea5c61755 --------- Signed-off-by: Seiji Eicher <[email protected]>
Why
Profiling the Serve controller under load shows
Gauge.set()for theserve_deployment_replica_healthymetric consuming ~5.9% of CPU. The call is made for every running replica on every control-loop iteration, even when the value hasn't changed. At 128+ replicas this is O(num_replicas) Cython FFI calls per loop — pure waste in steady state.What
Add a time-based cache (
_health_gauge_cache) toDeploymentStatethat tracks the last-reported(value, timestamp)per replica._set_health_gauge()skips theGauge.set()call when the value is unchanged and the entry is younger thanRAY_SERVE_HEALTH_GAUGE_REPORT_INTERVAL_S(default 10s). The gauge is still re-recorded periodically so it remains visible across OpenCensus export windows, and is always recorded immediately on state transitions (healthy ↔ unhealthy). Cache entries are cleaned up when replicas are fully stopped.The interval is configurable via the
RAY_SERVE_HEALTH_GAUGE_REPORT_INTERVAL_Senv var and set to0.1in CI to stay within the fast test metrics export cadence.Test plan
test_health_gauge_caching— verifies cache hits, TTL expiry re-records, value-change bypass, and cleanup on replica stoptest_replica_metrics_fieldsintegration test passes (metric still appears in Prometheus)Related to #60680