Skip to content

[serve] Gracefully drain gang replicas under migration, rollouts, downscaling, and deletion - #67172

Merged
jeffreywang88 merged 4 commits into
masterfrom
gang-drain
Oct 20, 2026
Merged

jeffreywang88 merged 4 commits into
masterfrom
gang-drain

Conversation

@jeffreywang88

@jeffreywang88 jeffreywang88 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Gracefully drain gang replicas independently, then kill all members in one controller tick after completion or timeout.

  • Graceful termination: node drains, migration, rollouts, downscaling, and deletion.
  • Forceful termination: health check failures.

Fixes the gang request-loss issue explicitly excluded from #67165.

Approach

Each replica runs the existing graceful shutdown independently. A controller barrier waits for every stopping gang member to finish or time out, then kills them in the same tick.

Validation

  • Real HTTP requests survive gang rollout and rollback; test_gang_surge_rolling_update_and_rollback fails before the fix.
  • Rollout, surge, downscale, deletion, and migration wait for the whole gang.
  • Placement groups stay allocated until termination.
  • Timeouts and checkpoint recovery still work.
  • Deployments converge to healthy capacity or complete deletion.

Related issues

Link related issues: "Fixes #1234", "Closes #1234", or "Related to #1234".

Additional information

Optional: Add implementation details, API changes, usage examples, screenshots, etc.

@jeffreywang88
jeffreywang88 requested a review from a team as a code owner October 19, 2026 23:18
@jeffreywang88 jeffreywang88 added the go add ONLY when ready to merge, run all tests label Oct 19, 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 modifies the graceful shutdown behavior for gang deployments in Ray Serve, ensuring that all gang members are allowed to drain or reach their shutdown deadlines before any of them are killed. It introduces a kill_actor parameter to check_stopped to poll shutdown status without terminating the actor, along with a check_ready_to_stop helper, and updates migration and stopping logic to handle partly started gangs. The review feedback suggests caching the result of check_ready_to_stop to prevent redundant GCS lookups (ray.get_actor) across controller ticks, which is a valuable optimization to reduce overhead.

Comment on lines +2394 to +2399
def check_ready_to_stop(self) -> bool:
"""Whether graceful shutdown completed or its deadline expired, without killing."""
return (
self._actor.check_stopped(kill_actor=False)
or time.time() >= self._shutdown_deadline
)

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

In a gang deployment, check_ready_to_stop() is called repeatedly in every controller tick for all stopping gang members until the entire gang is ready to be stopped. Since self._actor.check_stopped(kill_actor=False) performs GCS lookups (ray.get_actor), calling it repeatedly for replicas that have already finished draining can introduce unnecessary overhead and GCS load.

We can cache the ready status on the replica once it evaluates to True to avoid redundant GCS lookups and polling in subsequent ticks.

Suggested change
def check_ready_to_stop(self) -> bool:
"""Whether graceful shutdown completed or its deadline expired, without killing."""
return (
self._actor.check_stopped(kill_actor=False)
or time.time() >= self._shutdown_deadline
)
def check_ready_to_stop(self) -> bool:
"""Whether graceful shutdown completed or its deadline expired, without killing."""
if not getattr(self, "_ready_to_stop", False):
self._ready_to_stop = (
self._actor.check_stopped(kill_actor=False)
or time.time() >= self._shutdown_deadline
)
return self._ready_to_stop

]
)

partly_started_gang_members: Set[ReplicaID] = set()

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.

Notes for reviewers

If we dismiss partly_started_gang_members, for example:

  • A enters PENDING_MIGRATION, waiting for a replacement.
  • B enters STOPPING immediately.
  • The shutdown barrier only considers STOPPING replicas, so it can kill B once B drains, while A is still serving.

That violates “every gang member drains before any actor is killed.” This block puts both replicas into STOPPING, and the barrier waits for both to finish draining or reach their deadlines before killing their actors together.

def test_gang_pg_removed_only_after_last_member_stops(
self, mock_deployment_state_manager
@pytest.mark.parametrize("operation", ["rollout", "surge", "downscale", "delete"])
def test_graceful_shutdown_waits_for_every_gang_member(

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.

This test still preserves the following assertions from test_gang_pg_removed_only_after_last_member_stops and extends to cover rollout, surge, downscale, and deletion:

  • No PG removal after the first member drains
  • Exactly one removal after the last drains.


def test_actor_uninitialized_before_recover(mock_deployment_state_manager):
@pytest.mark.parametrize("gang_size", [None, 2])
def test_actor_uninitialized_before_recover(mock_deployment_state_manager, gang_size):

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.

Adds coverage for failed recovery without gang metadata.

@jeffreywang88 jeffreywang88 changed the title [serve] Gracefully drain gang replicas before killing actors together [serve] Gracefully drain gang replicas under migration, rollouts, downscaling, and deletion Oct 19, 2026
@johntaylor-cell

Copy link
Copy Markdown
Contributor
  1. deployment_state.py:7551: This now blocks all gang replacements while any gang drains (a gang failing mid-rollout stays unreplaced); can it be narrowed?
  2. deployment_state.py:4339: STARTING members can stay force-stopped here and in _stop_old_replicas; the barrier still holds them, and draining pins the PG for the full timeout.
  3. deployment_state.py:6266: Pre-existing: a non-gang to gang redeploy raises here on gang_context=None after the pop, dropping replicas from tracking.
  4. deployment_state.py:5956: Key this on r.gang_context, not the target config, so dropping the gang config mid-drain doesn't skip the barrier.
  5. test_deploy_app_3.py:427: Both requests release together, so this would pass without the barrier; keep one member idle and assert it stays alive.
  6. deployment_state.py:6270: UPDATING is untested; narrowing this to STARTING still passes all gang tests.
  7. unit/test_deployment_state.py:4161: Keep the old total=2 check for gang_size=None; it still passes on this branch.
  8. deployment_state.py:2397: Check the deadline first and poll the shutdown ref directly to skip a ray.get_actor per drained member per tick.
  9. deployment_state.py:4546: nit: this rule also lives in _stop_or_update_outdated_version_replicas; a shared helper avoids drift.
  10. test_deploy_app_3.py:476: This traffic now spans a gang drain, which is 0.01s on the HAProxy shard and can drop requests; consider adding RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S: "5" to request_env.

@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Oct 20, 2026
@jeffreywang88

jeffreywang88 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author
  1. I think that waiting for stopping gangs to drain and release resources before reserving replacements is actually the right behavior. Graceful shutdown extends that wait, but recovery resumes after completion or timeout.
  2. Makes sense because STARTING replicas haven't served any requests so they don't need to drain requests gracefully. Done in 657a3cb.
  3. This won't happen in production.
  4. This won't happen in production either, but I think we can make it fail more loudly in a separate PR.
  5. test_graceful_shutdown_waits_for_every_gang_member covers this case.
  6. Fixed in d5d833a.
  7. Fixed in d5d833a.
  8. Named actors are cached so get_actor should be cheap.
  9. Doesn't add much value because this condition is small.
  10. Fixed in d5d833a.

@jeffreywang88

Copy link
Copy Markdown
Contributor Author
  • Before 657a3cb: superseded STARTING gang replicas waited for graceful shutdown, potentially until timeout if initialization was stuck.

  • After 657a3cb: they skip that wait in regular and surge rollouts. In mixed gangs, they still wait for RUNNING siblings to drain before all actors are killed together.

@johntaylor-cell

Copy link
Copy Markdown
Contributor

Re 1: Agreed for the draining gang's own replacement, but the gate is deployment-wide, so a different gang that already died and freed its PG also can't be replaced until this drain ends.

Re 2: The fix isn't applied at deployment_state.py:6291, so a gang on a draining node still waits out a STARTING member's grace period after its RUNNING siblings drain; test_starting_or_updating_replicas_stopped[0-False] and [1-False] currently assert that wait.

@jeffreywang88

jeffreywang88 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Re 1: Agreed for the draining gang's own replacement, but the gate is deployment-wide, so a different gang that already died and freed its PG also can't be replaced until this drain ends.

Makes sense, I'd follow up this in a separate PR as this is an existing limitation + to keep this PR targeted.

Re 2: The fix isn't applied at deployment_state.py:6291, so a gang on a draining node still waits out a STARTING member's grace period after its RUNNING siblings drain; test_starting_or_updating_replicas_stopped[0-False] and [1-False] currently assert that wait.

Fixed in 47da5e5.

@jeffreywang88
jeffreywang88 enabled auto-merge (squash) October 20, 2026 18:43
@jeffreywang88
jeffreywang88 merged commit 6edbab3 into master Oct 20, 2026
7 checks passed
@jeffreywang88
jeffreywang88 deleted the gang-drain branch October 20, 2026 20:13
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.

Ray fails to serialize self-reference objects

2 participants