Skip to content

[serve] Fix test_deployment_scheduler_downscale flake under HAProxy - #64325

Merged
eicherseiji merged 8 commits into
ray-project:masterfrom
eicherseiji:seiji/haproxy-downscale-test-ports
Jun 30, 2026
Merged

eicherseiji merged 8 commits into
ray-project:masterfrom
eicherseiji:seiji/haproxy-downscale-test-ports

Conversation

@eicherseiji

@eicherseiji eicherseiji commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

What this test checks

TestScaleDownReplicaSelection verifies which replicas Serve removes first when an autoscaling deployment scales down. There is a priority order, for example drop replicas on fallback nodes before primary nodes, and never drop the head node. Each test:

  1. Deploys on a multi-node cluster with autoscaling min=1, max=N.
  2. Drives load so the deployment scales up to the cap, spread across nodes.
  3. Stops load so it scales back down to 1.
  4. Asserts the surviving replica is on the node the priority rules expect.

Step 4 is the actual assertion. Steps 2 and 3 are setup to get something to scale down from.

The failure

Under the HAProxy CI step the test timed out waiting to reach the max replica count, so it never reached the assertion it exists to make. Failing job: premerge #68597, serve: HAProxy tests.

The upscale was driven by polling the deployment handle one request at a time, which did double duty as both the load that drives the autoscaler and the convergence check (counting distinct replica tags seen). That load is intermittent: each get_info round-trip is a few ms, then the loop sleeps 50ms, so the deployment is idle ~90% of the time. The autoscaler aggregates ongoing requests as max over a 0.5s window, so it mostly samples zero in-flight and scales back down about 2s later (downscale_delay_s=2); a later poll that catches a request scales it up again. The deployment flaps 1<->target.

That flap is only fatal when replicas are slow to reach RUNNING. Under HAProxy direct ingress is forced on, so every replica binds a host port and climbs past collisions on the single-host cluster (the failing job shows 15 bind failures, replicas climbing to e.g. 30003). So only a few replicas come up per cycle before the flap reverses, and the count never reaches the cap. Without direct ingress replicas come up fast enough to hit the cap before the flap matters, which is why this only fails under HAProxy.

The fix

Stop disguising load as polling. Hold max_replicas requests in flight, each blocked on a SignalActor, so the autoscaler reaches and holds the cap while replicas start. Then release the signal so, with no load, it scales back down. Check the replica count via check_num_replicas_eq (serve.status) rather than counting distinct tags through the handle. Test-only, no production changes. Also adds the file to ci/ray_ci/serve_hap_test_names.txt so it runs under HAProxy.

Validation

Reproduced the original failure locally by CPU-saturating the host (46 busy processes on a 48-core box, the HAProxy env) so replicas reach RUNNING slower than the downscale window. The old serial-poll version failed; this version holds:

Variant Result under load
serial-poll (before) FAIL at the upscale wait, reached 1-2 of 8 replicas
signal-held load (this PR) PASS (reached 8), and PASS on repeat runs

All three tests also pass with no stress.

These scheduler tests simulate a multi-node cluster on a single host. Under
HAProxy direct ingress is forced on, so every replica binds a host port the
controller allocates per node starting from the same base. Replicas on
different nodes collide on those ports; the controller blocks the taken port
and reallocates until each finds a free one. Freed ports are held in
quarantine for ~60s before returning to the pool, which starves that retry
loop so the upscale step does not converge within the wait.

Disable port quarantine for these tests so freed ports return to the pool
immediately, and raise the upscale wait to 60s. Add the file to the HAProxy
test allowlist so it runs under HAProxy in CI.

Signed-off-by: Seiji Eicher <[email protected]>

@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 adds the 'test_deployment_scheduler_downscale' test to the HAProxy test suite. It introduces an autouse fixture '_recycle_direct_ingress_ports' in 'test_deployment_scheduler_downscale.py' to disable port quarantine by setting 'RAY_SERVE_PORT_QUARANTINE_S' to '0', which helps avoid port allocation collisions during multi-node simulations on a single host. Additionally, it increases the upscale wait timeouts from 30 to 60 seconds across several test cases to prevent flakiness. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

… holds

_wait_for_upscale polled the handle serially, so on a slow or loaded host
(direct ingress under HAProxy makes replica startup slow) ongoing requests
dropped to 0 between polls and the autoscaler downscaled the deployment
mid-upscale. It flapped 1<->target and the distinct-replica count never
reached the target within the wait.

Keep expected*2 requests in flight during the wait so the autoscaler never
observes ongoing=0 and the deployment holds at the target until every replica
is up. Raise the wait to 60s for headroom.

Reproduced the CI failure locally under CPU saturation (faithful HAProxy env):
the old serial poll failed 2/2 at _wait_for_upscale; this passes 3/3.

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji eicherseiji changed the title [serve] Make test_deployment_scheduler_downscale pass under HAProxy [serve] Fix test_deployment_scheduler_downscale flake under HAProxy Jun 24, 2026
… docstring

`_wait_for_upscale` accumulates distinct replica tags; a restarted replica adds
a new tag, so the count can pass the target in a single batched poll. Use `>=`
so the wait still completes (== could be skipped and time out).

Signed-off-by: Seiji Eicher <[email protected]>
The bind-failure retry path releases with block_port=True, which skips
quarantine, so RAY_SERVE_PORT_QUARANTINE_S=0 did nothing for the port
contention it was meant to address. Verified: the flap fix without the
override passes 3/3 under 46-core CPU load, including a run with 14 bind
failures.

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji eicherseiji added the go add ONLY when ready to merge, run all tests label Jun 24, 2026
@eicherseiji eicherseiji self-assigned this Jun 24, 2026
@eicherseiji
eicherseiji marked this pull request as ready for review June 25, 2026 16:56
@eicherseiji
eicherseiji requested a review from a team as a code owner June 25, 2026 16:56
@akyang-anyscale

Copy link
Copy Markdown
Contributor

if the retry interval for the request is 50ms, shouldn't we avoid downscaling while the replicas are finding ports to bind?

@staticmethod
def _wait_for_upscale(
    expected_num_replicas: int,
    handle,
    timeout: int = 30,
):
    replica_tag_set = set()

    def check_new_replica():
        replica_tag_set.add(handle.get_info.remote().result()["replica_tag"])
        return len(replica_tag_set) == expected_num_replicas

    wait_for_condition(check_new_replica, timeout=timeout, retry_interval_ms=50)

@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Jun 25, 2026
…t load

The scale-down-selection tests drove the upscale by polling the handle one
request at a time, which doubled as both the load that drives the autoscaler
and the convergence check (counting distinct replica tags). On a slow or
loaded host (direct ingress under HAProxy) that load is too intermittent to
hold the deployment at its target, so the autoscaler scales back down
mid-upscale and the count never reaches the cap.

Hold max_replicas requests in flight, blocked on a SignalActor, as explicit
load so the autoscaler reaches and holds the cap while replicas start, then
release them to scale back down. Check the replica count via
check_num_replicas_eq (serve.status) instead of counting tags through the
handle.

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji
eicherseiji enabled auto-merge (squash) June 29, 2026 23:11
@eicherseiji
eicherseiji merged commit fac2104 into ray-project:master Jun 30, 2026
4 checks passed
abrarsheikh added a commit that referenced this pull request Jul 9, 2026
#64345)

## Summary

Several gang scheduling tests inferred replica placement by issuing
repeated
`DeploymentHandle` calls and counting the distinct nodes / gangs /
replicas
observed in the responses. Request routing is not a scheduler-state API:
with
locality-aware routing (added per-deployment and parameterized in
#63629),
repeated calls may legitimately prefer local replicas, so these tests
can
undercount nodes, gangs, or replicas even when scheduler state is
correct.

This PR makes the tests read placement directly from controller replica
state
instead of inferring it from handle responses.

## Changes

In `test_gang_scheduling.py`:
- Add helpers to read RUNNING replicas (and their node / gang IDs) from
  controller state.
- Verify placement via controller state in `test_incomplete_deployment`,
  `test_gang_scheduling_spread_strategy`, `test_gang_context`,
  `test_scale_gang_boundary`, and `test_gang_migration`.
- Keep handle calls where the test checks request serving or
zero-downtime, and
add comments documenting the remaining handle-based loops that are safe
under
  locality routing (PACK / placement-group invariant checks, single-node
  discovery).

Note: the originally-planned `test_deployment_scheduler_downscale.py`
change was
dropped — #64325 already converted that test to controller-state waiting
on
master.

## Related issues
- Related to #59929
- Depends on / unblocks #63629

## Tests

```bash
pytest python/ray/serve/tests/test_gang_scheduling.py -q
```

---------

Signed-off-by: wanadzhar913 <[email protected]>
Signed-off-by: abrar <[email protected]>
Co-authored-by: abrar <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ay-project#64325)

## What this test checks

`TestScaleDownReplicaSelection` verifies which replicas Serve removes
first when an autoscaling deployment scales down. There is a priority
order, for example drop replicas on fallback nodes before primary nodes,
and never drop the head node. Each test:

1. Deploys on a multi-node cluster with autoscaling min=1, max=N.
2. Drives load so the deployment scales up to the cap, spread across
nodes.
3. Stops load so it scales back down to 1.
4. Asserts the surviving replica is on the node the priority rules
expect.

Step 4 is the actual assertion. Steps 2 and 3 are setup to get something
to scale down from.

## The failure

Under the HAProxy CI step the test timed out waiting to reach the max
replica count, so it never reached the assertion it exists to make.
Failing job: [premerge ray-project#68597, `serve: HAProxy
tests`](https://buildkite.com/ray-project/premerge/builds/68597#019edcdd-db89-40e8-926c-e69dd26f89b3).

The upscale was driven by polling the deployment handle one request at a
time, which did double duty as both the load that drives the autoscaler
and the convergence check (counting distinct replica tags seen). That
load is intermittent: each `get_info` round-trip is a few ms, then the
loop sleeps 50ms, so the deployment is idle ~90% of the time. The
autoscaler aggregates ongoing requests as `max` over a 0.5s window, so
it mostly samples zero in-flight and scales back down about 2s later
(`downscale_delay_s=2`); a later poll that catches a request scales it
up again. The deployment flaps 1<->target.

That flap is only fatal when replicas are slow to reach RUNNING. Under
HAProxy direct ingress is forced on, so every replica binds a host port
and climbs past collisions on the single-host cluster (the failing job
shows 15 bind failures, replicas climbing to e.g. 30003). So only a few
replicas come up per cycle before the flap reverses, and the count never
reaches the cap. Without direct ingress replicas come up fast enough to
hit the cap before the flap matters, which is why this only fails under
HAProxy.

## The fix

Stop disguising load as polling. Hold `max_replicas` requests in flight,
each blocked on a `SignalActor`, so the autoscaler reaches and holds the
cap while replicas start. Then release the signal so, with no load, it
scales back down. Check the replica count via `check_num_replicas_eq`
(`serve.status`) rather than counting distinct tags through the handle.
Test-only, no production changes. Also adds the file to
`ci/ray_ci/serve_hap_test_names.txt` so it runs under HAProxy.

## Validation

Reproduced the original failure locally by CPU-saturating the host (46
busy processes on a 48-core box, the HAProxy env) so replicas reach
RUNNING slower than the downscale window. The old serial-poll version
failed; this version holds:

| Variant | Result under load |
| --- | --- |
| serial-poll (before) | FAIL at the upscale wait, reached 1-2 of 8
replicas |
| signal-held load (this PR) | PASS (reached 8), and PASS on repeat runs
|

All three tests also pass with no stress.

---------

Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: 400Ping <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
ray-project#64345)

## Summary

Several gang scheduling tests inferred replica placement by issuing
repeated
`DeploymentHandle` calls and counting the distinct nodes / gangs /
replicas
observed in the responses. Request routing is not a scheduler-state API:
with
locality-aware routing (added per-deployment and parameterized in
ray-project#63629),
repeated calls may legitimately prefer local replicas, so these tests
can
undercount nodes, gangs, or replicas even when scheduler state is
correct.

This PR makes the tests read placement directly from controller replica
state
instead of inferring it from handle responses.

## Changes

In `test_gang_scheduling.py`:
- Add helpers to read RUNNING replicas (and their node / gang IDs) from
  controller state.
- Verify placement via controller state in `test_incomplete_deployment`,
  `test_gang_scheduling_spread_strategy`, `test_gang_context`,
  `test_scale_gang_boundary`, and `test_gang_migration`.
- Keep handle calls where the test checks request serving or
zero-downtime, and
add comments documenting the remaining handle-based loops that are safe
under
  locality routing (PACK / placement-group invariant checks, single-node
  discovery).

Note: the originally-planned `test_deployment_scheduler_downscale.py`
change was
dropped — ray-project#64325 already converted that test to controller-state waiting
on
master.

## Related issues
- Related to ray-project#59929
- Depends on / unblocks ray-project#63629

## Tests

```bash
pytest python/ray/serve/tests/test_gang_scheduling.py -q
```

---------

Signed-off-by: wanadzhar913 <[email protected]>
Signed-off-by: abrar <[email protected]>
Co-authored-by: abrar <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[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 serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants