Skip to content

[Serve] Throttle serve_deployment_replica_healthy gauge recording on the controller - #60823

Merged
abrarsheikh merged 5 commits into
masterfrom
60680-abrar-guage
Feb 10, 2026
Merged

abrarsheikh merged 5 commits into
masterfrom
60680-abrar-guage

Conversation

@abrarsheikh

@abrarsheikh abrarsheikh commented Feb 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

image

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

  • New unit test test_health_gauge_caching — verifies cache hits, TTL expiry re-records, value-change bypass, and cleanup on replica stop
  • test_replica_metrics_fields integration test passes (metric still appears in Prometheus)

Related to #60680

@abrarsheikh
abrarsheikh requested a review from a team as a code owner February 7, 2026 04:17
@abrarsheikh abrarsheikh added the go add ONLY when ready to merge, run all tests label Feb 7, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
now = time.time()
now = time.monotonic()

Comment thread python/ray/serve/_private/constants.py Resolved
Comment thread python/ray/serve/tests/BUILD.bazel Resolved
Signed-off-by: abrar <[email protected]>
@abrarsheikh
abrarsheikh merged commit d043552 into master Feb 10, 2026
3 checks passed
@abrarsheikh
abrarsheikh deleted the 60680-abrar-guage branch February 10, 2026 07:41
eicherseiji added a commit to eicherseiji/ray that referenced this pull request Feb 11, 2026
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]>
eicherseiji added a commit to eicherseiji/ray that referenced this pull request Feb 11, 2026
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]>
eicherseiji added a commit to eicherseiji/ray that referenced this pull request Feb 14, 2026
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]>
abrarsheikh pushed a commit that referenced this pull request Feb 15, 2026
…#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]>
ans9868 pushed a commit to ans9868/ray that referenced this pull request Feb 18, 2026
…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]>
ans9868 pushed a commit to ans9868/ray that referenced this pull request Feb 18, 2026
…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]>
Aydin-ab pushed a commit to kunling-anyscale/ray that referenced this pull request Feb 20, 2026
…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]>
Aydin-ab pushed a commit to kunling-anyscale/ray that referenced this pull request Feb 20, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants