Skip to content

feat: get draining nodes in Ray Serve controller - #65502

Merged
abrarsheikh merged 5 commits into
ray-project:masterfrom
manhld0206:feat/get_draining_nodes_ray_serve
Sep 11, 2026
Merged

abrarsheikh merged 5 commits into
ray-project:masterfrom
manhld0206:feat/get_draining_nodes_ray_serve

Conversation

@manhld0206

@manhld0206 manhld0206 commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

  • Ray Serve doesn't perform replicas graceful shutdown during node preemption (in-flight requests will fail immediately)
  • PR #64454 marks a Ray node as draining when ray start --block receives SIGTERM. This allows time for Controller to draining and graceful shutdown replicas running on the nodes.
  • There is already logic for draining and migrating replicas on draining nodes
  • However, the default Ray Serve controller node cache always returned an empty draining-node map. As a result, Serve’s existing proxy and replica draining logic could not observe nodes marked as draining in GCS.
  • This change allow the serve controller to query draining nodes from GCS to enable draining of running replicas during node preemption

@manhld0206
manhld0206 requested review from a team as code owners August 15, 2026 10:57

@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 synchronous support for retrieving draining nodes from the GCS. It exposes GetDrainingNodes in the C++ GCS client, binds it via Cython to Python's GcsClient, and integrates it into the Ray Serve DefaultClusterNodeInfoCache to keep track of draining nodes during cache updates. A unit test has also been added to verify this behavior. There are no review comments, so I have no feedback to provide.

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue community-contribution Contributed by the community labels Aug 15, 2026
super().__init__(gcs_client)
self._cached_draining_nodes: Dict[str, int] = dict()

def update(self):

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.

Can this go in the base ClusterNodeInfoCache.update instead? Every implementation needs the same fetch.

}
return

self._cached_draining_nodes = {

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.

We should handle the case where the fetched deadline is 0 so that these replicas don't get stopped before their replacements come up. 0 means no deadline (_private/state.py:978) but _partition_groups_to_stop reads deadlines as absolute epoch ms (deployment_state.py:5492), so 0 is treated as already expired and skips the migrate first path. Substituting now + <default draining timeout> before caching should work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From this comment #65502 (review)

For simplicity I intent to use 2**63 - 1

== cluster_node_info_cache.get_active_node_ids()
)

# Keep a lease on the worker so the raylet remains alive while draining.

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.

We should split this into its own test_get_draining_nodes so the alive node assertions after remove_node don't run against a drained worker, and so the two features fail independently.

worker_node_id: draining_deadline_ms
}

wait_for_condition(cache_has_draining_worker)

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.

Once the deadline 0 substitution is in, we should cover it here: drain with deadline 0, assert the cached deadline is > 0, then assert it doesn't change across several update() calls. A cache that recomputes now + default on each tick would pass a single check and keep pushing the forced stop out forever, so the repeated read is what pins the fix.

}

wait_for_condition(cache_has_draining_worker)
assert cluster_node_info_cache.get_active_node_ids() == {head_node_id}

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.

The two new branches are the alive node filter and the except path, and neither is exercised here. A stubbed gcs_client that raises, plus one reporting a node that is no longer alive, would cover both without a real cluster.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b7a1778. Configure here.

@YashwanthRanjanSingaravel

Copy link
Copy Markdown
Contributor

Great work!

@YashwanthRanjanSingaravel YashwanthRanjanSingaravel added the go add ONLY when ready to merge, run all tests label Aug 31, 2026
eicherseiji pushed a commit that referenced this pull request Sep 9, 2026
…eregistration window ends (#65698)

## Description

What happens today when a direct-ingress replica behind HAProxy
(`RAY_SERVE_ENABLE_HA_PROXY=1`) shuts down, in sequence:

1. A replica starts draining (scale-down, upgrade, or its node is being
removed).
2. Every node's HAProxy receives the updated backend list and reloads.
3. A reload starts a **new** HAProxy process. The **old** process keeps
running until its connections finish (bounded by `hard-stop-after`,
minutes) — and it still routes with the **old** backend list. It cannot
be corrected: config reloads and runtime commands only reach the new
process.
4. So a stale worker can keep sending brand-new requests to the draining
replica for minutes.
5. Today the replica accepts them: it keeps serving for as long as
requests arrive, not just for the deregistration window
(`RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S`, default 30s).
6. The replica/node is only guaranteed to live through that window. When
the node then goes away (e.g. spot interruption), the late-admitted
requests are cut mid-flight — the client sees an error nothing can
safely retry.

We measured ~40 such failures per spot-interruption event in a staging
fleet.

**The fix** — behind HAProxy, drain in two phases:

1. Stay fully reachable for the deregistration window, exactly as today.
2. Then close the direct-ingress HTTP listener and wait for in-flight
requests.

A late request from a stale worker is now refused at connect time
instead of admitted. HAProxy retries refused connections on another
replica (`retry-on conn-failure` + `option redispatch`), so the client
sees no error.

**Without HAProxy nothing changes**: there is no retrying proxy in
front, a refusal would reach the client, so that path keeps the existing
drain.

## Related issues

N/A (follow-up to the HAProxy stability series, most recently #63996).

## Additional information

- Adds a `Replica._is_direct_ingress` property (mirrors the one on
`ReplicaMetricsManager`), used to pick the drain path.
- Only the HTTP listener closes early; direct-ingress gRPC keeps the
existing behavior until the post-drain quiesce. Candidate follow-up.
- Gated on `RAY_SERVE_ENABLE_HA_PROXY` because the early refusal is only
safe with a retrying proxy in front.
- A live-cluster reproduction with keep-alive connections against a
stale HAProxy worker is a planned follow-up.

### Not a duplicate

Checked open PRs before and after opening this one (`gh pr list --search
"haproxy"`, `--search "drain replica graceful shutdown"`): no open PR or
issue touches replica drain or direct-ingress listener shutdown. The
nearest neighbours are unrelated (#65502 draining-node accessor on the
controller, #65542 lazy jinja2 import).

### Tests run

```
pytest python/ray/serve/tests/unit/test_replica_quiesce.py -v
```

12 passed — the 7 pre-existing tests plus 5 new ones covering:
drain-path selection with and without HAProxy, phase ordering, the
deregistration window being served in full before the listener closes,
the minimum period not being applied twice, and the no-HTTP-server case.

```
pre-commit run --files python/ray/serve/_private/replica.py python/ray/serve/tests/unit/test_replica_quiesce.py
```

All hooks passed, including `mypy (ray serve)` and `pyrefly (ray
serve)`.

### AI assistance

This change was developed with AI assistance (Claude Code). A human
author reviewed every changed line and ran the tests above locally
before requesting review.

---------

Signed-off-by: harshit-anyscale <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>

@MortalHappiness MortalHappiness left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Stamp for Ray core changes.

NightWing1998 pushed a commit to NightWing1998/ray that referenced this pull request Sep 11, 2026
…eregistration window ends (ray-project#65698)

## Description

What happens today when a direct-ingress replica behind HAProxy
(`RAY_SERVE_ENABLE_HA_PROXY=1`) shuts down, in sequence:

1. A replica starts draining (scale-down, upgrade, or its node is being
removed).
2. Every node's HAProxy receives the updated backend list and reloads.
3. A reload starts a **new** HAProxy process. The **old** process keeps
running until its connections finish (bounded by `hard-stop-after`,
minutes) — and it still routes with the **old** backend list. It cannot
be corrected: config reloads and runtime commands only reach the new
process.
4. So a stale worker can keep sending brand-new requests to the draining
replica for minutes.
5. Today the replica accepts them: it keeps serving for as long as
requests arrive, not just for the deregistration window
(`RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S`, default 30s).
6. The replica/node is only guaranteed to live through that window. When
the node then goes away (e.g. spot interruption), the late-admitted
requests are cut mid-flight — the client sees an error nothing can
safely retry.

We measured ~40 such failures per spot-interruption event in a staging
fleet.

**The fix** — behind HAProxy, drain in two phases:

1. Stay fully reachable for the deregistration window, exactly as today.
2. Then close the direct-ingress HTTP listener and wait for in-flight
requests.

A late request from a stale worker is now refused at connect time
instead of admitted. HAProxy retries refused connections on another
replica (`retry-on conn-failure` + `option redispatch`), so the client
sees no error.

**Without HAProxy nothing changes**: there is no retrying proxy in
front, a refusal would reach the client, so that path keeps the existing
drain.

## Related issues

N/A (follow-up to the HAProxy stability series, most recently ray-project#63996).

## Additional information

- Adds a `Replica._is_direct_ingress` property (mirrors the one on
`ReplicaMetricsManager`), used to pick the drain path.
- Only the HTTP listener closes early; direct-ingress gRPC keeps the
existing behavior until the post-drain quiesce. Candidate follow-up.
- Gated on `RAY_SERVE_ENABLE_HA_PROXY` because the early refusal is only
safe with a retrying proxy in front.
- A live-cluster reproduction with keep-alive connections against a
stale HAProxy worker is a planned follow-up.

### Not a duplicate

Checked open PRs before and after opening this one (`gh pr list --search
"haproxy"`, `--search "drain replica graceful shutdown"`): no open PR or
issue touches replica drain or direct-ingress listener shutdown. The
nearest neighbours are unrelated (ray-project#65502 draining-node accessor on the
controller, ray-project#65542 lazy jinja2 import).

### Tests run

```
pytest python/ray/serve/tests/unit/test_replica_quiesce.py -v
```

12 passed — the 7 pre-existing tests plus 5 new ones covering:
drain-path selection with and without HAProxy, phase ordering, the
deregistration window being served in full before the listener closes,
the minimum period not being applied twice, and the no-HTTP-server case.

```
pre-commit run --files python/ray/serve/_private/replica.py python/ray/serve/tests/unit/test_replica_quiesce.py
```

All hooks passed, including `mypy (ray serve)` and `pyrefly (ray
serve)`.

### AI assistance

This change was developed with AI assistance (Claude Code). A human
author reviewed every changed line and ran the tests above locally
before requesting review.

---------

Signed-off-by: harshit-anyscale <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
@abrarsheikh
abrarsheikh merged commit ad82bf4 into ray-project:master Sep 11, 2026
7 checks passed
@manhld0206
manhld0206 deleted the feat/get_draining_nodes_ray_serve branch September 12, 2026 00:22
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…eregistration window ends (ray-project#65698)

## Description

What happens today when a direct-ingress replica behind HAProxy
(`RAY_SERVE_ENABLE_HA_PROXY=1`) shuts down, in sequence:

1. A replica starts draining (scale-down, upgrade, or its node is being
removed).
2. Every node's HAProxy receives the updated backend list and reloads.
3. A reload starts a **new** HAProxy process. The **old** process keeps
running until its connections finish (bounded by `hard-stop-after`,
minutes) — and it still routes with the **old** backend list. It cannot
be corrected: config reloads and runtime commands only reach the new
process.
4. So a stale worker can keep sending brand-new requests to the draining
replica for minutes.
5. Today the replica accepts them: it keeps serving for as long as
requests arrive, not just for the deregistration window
(`RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S`, default 30s).
6. The replica/node is only guaranteed to live through that window. When
the node then goes away (e.g. spot interruption), the late-admitted
requests are cut mid-flight — the client sees an error nothing can
safely retry.

We measured ~40 such failures per spot-interruption event in a staging
fleet.

**The fix** — behind HAProxy, drain in two phases:

1. Stay fully reachable for the deregistration window, exactly as today.
2. Then close the direct-ingress HTTP listener and wait for in-flight
requests.

A late request from a stale worker is now refused at connect time
instead of admitted. HAProxy retries refused connections on another
replica (`retry-on conn-failure` + `option redispatch`), so the client
sees no error.

**Without HAProxy nothing changes**: there is no retrying proxy in
front, a refusal would reach the client, so that path keeps the existing
drain.

## Related issues

N/A (follow-up to the HAProxy stability series, most recently ray-project#63996).

## Additional information

- Adds a `Replica._is_direct_ingress` property (mirrors the one on
`ReplicaMetricsManager`), used to pick the drain path.
- Only the HTTP listener closes early; direct-ingress gRPC keeps the
existing behavior until the post-drain quiesce. Candidate follow-up.
- Gated on `RAY_SERVE_ENABLE_HA_PROXY` because the early refusal is only
safe with a retrying proxy in front.
- A live-cluster reproduction with keep-alive connections against a
stale HAProxy worker is a planned follow-up.

### Not a duplicate

Checked open PRs before and after opening this one (`gh pr list --search
"haproxy"`, `--search "drain replica graceful shutdown"`): no open PR or
issue touches replica drain or direct-ingress listener shutdown. The
nearest neighbours are unrelated (ray-project#65502 draining-node accessor on the
controller, ray-project#65542 lazy jinja2 import).

### Tests run

```
pytest python/ray/serve/tests/unit/test_replica_quiesce.py -v
```

12 passed — the 7 pre-existing tests plus 5 new ones covering:
drain-path selection with and without HAProxy, phase ordering, the
deregistration window being served in full before the listener closes,
the minimum period not being applied twice, and the no-HTTP-server case.

```
pre-commit run --files python/ray/serve/_private/replica.py python/ray/serve/tests/unit/test_replica_quiesce.py
```

All hooks passed, including `mypy (ray serve)` and `pyrefly (ray
serve)`.

### AI assistance

This change was developed with AI assistance (Claude Code). A human
author reviewed every changed line and ran the tests above locally
before requesting review.

---------

Signed-off-by: harshit-anyscale <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## Description

- Ray Serve doesn't perform replicas graceful shutdown during node
preemption (in-flight requests will fail immediately)
- [PR ray-project#64454](ray-project#64454) marks a Ray
node as draining when `ray start
--block` receives SIGTERM. This allows time for Controller to draining
and graceful shutdown replicas running on the nodes.
- There is already logic for draining and migrating replicas on draining
nodes
- However, the default Ray Serve controller node cache always returned
an empty draining-node map. As a result, Serve’s existing proxy and
replica draining logic could not observe nodes marked as draining in
GCS.
- This change allow the serve controller to query draining nodes from
GCS to enable draining of running replicas during node preemption

---------

Signed-off-by: Le Duc Manh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community 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.

4 participants