Skip to content

[serve][in-place-updates][3/n] Restore code defaults when config overrides are removed - #66266

Merged
abrarsheikh merged 5 commits into
masterfrom
serve/inplace-3-config-overrides-from-build
Sep 24, 2026
Merged

abrarsheikh merged 5 commits into
masterfrom
serve/inplace-3-config-overrides-from-build

Conversation

@qishengxhe

@qishengxhe qishengxhe commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Problem:

An in-place update applies a new config to an existing application. When the code version is unchanged, Serve called override_deployment_info with the previous target state's deployment infos as the base.

This breaks rollback after a failed in-place update. For example, an update adds a ray_actor_options.runtime_env that prevents replicas from starting. Reapplying the last healthy config should remove that override, but Serve keeps it and the deployment stays DEPLOY_FAILED.

Fix:

Serve now stores the original build results and uses them as the base for each config. Removing an override restores the value defined in code, so reapplying the last healthy config can undo settings introduced by a failed update.
The build results survive controller restarts. Applications recovered from older checkpoints rebuild once on their next config update.
image

Backward Compatibility:

ServeInstanceDetails reports restores_unset_config_options=True when supported. The control plane can use this field to determine whether automatic rollback is supported for an in-place update. Older versions omit the field, so no Ray version check is needed.

Alternatives:

Option A (Chosen in this PR): store the original build alongside the effective deployment infos

Keep the untouched build output in build and apply same-version config updates to it, rather than to the previous result.

  • Pros: Millisecond, build-free rollback with no additional build failures. Existing deployment_infos readers stay unchanged.
  • Cons: Stores two sets of DeploymentInfo
  • Storage impact: Only config-sized structures are duplicated in the checkpoint. The large serialized deployment definition is immutable bytes shared by both copies and written once by pickle.

Option B: rebuild when config keys or deployments are removed

Compare the old and new configs in apply_app_config and use the existing rebuild path on removal, even when the code version matches. Only removals require a rebuild because overrides replace top-level keys wholesale.

  • Pros: Smallest change, with no new state or checkpoint format changes. Replicas are reused.
  • Cons: Rollback takes longer due to rebuild, and can fail during build steps such as runtime environment setup. The path taken depends on config history, even when the effective result is identical.

Option C: store only the build and config; derive effective infos

Replace stored deployment_infos with a memoized result of override_deployment_info(build, config).

  • Pros: One source of truth; stale overrides cannot persist. No duplicated stored state.
  • Cons: Larger refactor across readers and _set_target_state. Imperative apps need separate handling, and memoization must avoid recomputing on every controller tick.

Tests

Covers removed overrides, older checkpoints, and capability reporting. The rollback test sends continuous traffic through the failed update, optional controller restart, and rollback, asserting no request failures and reuse of the surviving replica.

python -m pytest python/ray/serve/tests/unit/test_application_state.py python/ray/serve/tests/unit/test_schema.py
python -m pytest python/ray/serve/tests/test_deploy_app_2.py -k "sparse_config_rollback or instance_details_report"

The three integration cases passed in an isolated Linux container using this PR’s Serve code. Formatting and lint checks passed.

Duplicate checks found no overlapping fix. Claude Code assisted with implementation.

@qishengxhe qishengxhe changed the title [serve] Apply config overrides onto the build's deployment infos [serve][in-place-updates][3/n] Apply config overrides onto the build's deployment infos Sep 18, 2026
@qishengxhe
qishengxhe force-pushed the serve/inplace-3-config-overrides-from-build branch from 0614b71 to 4d0b129 Compare September 18, 2026 04:38
@qishengxhe qishengxhe added go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue labels Sep 18, 2026
@qishengxhe
qishengxhe force-pushed the serve/inplace-2-terminal-deploy-failed branch from 9b49a9d to 7c670ce Compare September 18, 2026 18:25
@qishengxhe
qishengxhe force-pushed the serve/inplace-3-config-overrides-from-build branch 2 times, most recently from b7ede56 to d8d7a30 Compare September 18, 2026 19:25
@qishengxhe
qishengxhe added this pull request to stack #66293 September 18, 2026 19:51
@qishengxhe
qishengxhe marked this pull request as ready for review September 18, 2026 21:14
@qishengxhe
qishengxhe requested a review from a team as a code owner September 18, 2026 21:14

@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 an AppBuild dataclass to cache original application build results in ApplicationState, ensuring that removing configuration overrides correctly restores values defined in the code. It updates the controller to track and report this capability via restores_unset_config_options and includes comprehensive unit and integration tests to verify the rollback behavior. The review identified a critical issue regarding potential in-memory mutation of cached build results during configuration overrides, suggesting the use of copy.deepcopy to prevent corruption of the cached state.

@qishengxhe qishengxhe changed the title [serve][in-place-updates][3/n] Apply config overrides onto the build's deployment infos [serve][in-place-updates][3/n] Restore code defaults when config overrides are removed Sep 18, 2026
@qishengxhe
qishengxhe force-pushed the serve/inplace-2-terminal-deploy-failed branch from 2dc616d to e7bf0a4 Compare September 21, 2026 18:53
@qishengxhe
qishengxhe force-pushed the serve/inplace-3-config-overrides-from-build branch from 97418a5 to 28fe00c Compare September 21, 2026 18:54
@qishengxhe
qishengxhe removed this pull request from stack #66293 September 21, 2026 19:13
Base automatically changed from serve/inplace-2-terminal-deploy-failed to master September 23, 2026 18:02
@qishengxhe
qishengxhe marked this pull request as draft September 23, 2026 18:42
A config for an already built code version was overridden onto the
previously overridden deployment infos, so an option a later config
stopped setting kept its previous override instead of returning to its
code-defined value. Re-applying a sparse config after an update that had
added a failing `ray_actor_options.runtime_env` was therefore a replica-level
no-op and the deployment stayed DEPLOY_FAILED: rollback by re-applying the
previous config only worked for options that config set explicitly.

`ApplicationTargetState` now keeps the build (`AppBuild`: the code-defined
deployment infos and the serialized classes overrides may reference) and the
fast path overrides onto it, so removed overrides return to their
code-defined values. The build is checkpointed and restored across a
controller restart. A target state recovered from a checkpoint written
before the build was kept has none: the next config for the same code
rebuilds the application once instead of overriding the accumulated infos,
so removed overrides are restored on any application once this version
runs. An older Ray image keeps the accumulating behavior.

Tests: unit tests for a removed override returning to the code-defined value,
a sparse rollback after a controller restart, and a checkpoint without a
build rebuilding on the next config; integration test (with and without a
controller restart) that a sparse config re-applied after a failing added
override restores the decorator-defined `max_ongoing_requests`, reuses the
surviving replica and serves again.

Signed-off-by: Qisheng He <[email protected]>
@qishengxhe
qishengxhe force-pushed the serve/inplace-3-config-overrides-from-build branch from 28fe00c to ec1aeea Compare September 23, 2026 18:48
A checkpoint is only ever read by the Ray version that wrote it, so the
target state never lacks build results when code_version is set. Read the
field directly on recovery and assert the invariant instead of rebuilding.

Signed-off-by: Qisheng He <[email protected]>
@qishengxhe
qishengxhe marked this pull request as ready for review September 23, 2026 20:02

@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 the AppBuild dataclass to store original application build results before config overrides, allowing code-defined defaults to be restored when overrides are removed. It also adds a restores_unset_config_options field to the ServeInstanceDetails schema to indicate this capability. Feedback on the changes identifies a potential AssertionError when recovering from older checkpoints where self._target_state.build is None and a new config is applied with the same code_version. To resolve this, it is suggested to check that self._target_state.build is not None in the if condition, allowing a graceful fallback to the rebuild path.

@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

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d0dd41f. Configure here.

Comment thread python/ray/serve/schema.py Resolved
Comment thread python/ray/serve/schema.py Resolved
…oval

Add the e2e cases requested in review: a config that keeps one override
while dropping another, removing autoscaling_config, and two deployments
whose overrides are removed independently, including dropping one from the
deployments list. The sparse rollback test now keeps traffic flowing from
before the failing update through the optional controller restart and
asserts every response is 200. Move TestConfigOverridesFromBuild above
the __main__ guard so it runs under bazel.

The controller gains a testing hook exposing whether the autoscaler still
tracks a deployment; the chain fixture sets distinct decorator values so
the multi-deployment test can tell which deployment reverted.

Signed-off-by: qishengxhe <[email protected]>
…nd in-place updates

The controller cannot import an application-level autoscaling policy that
lives only in the application's runtime_env, so the build task cloudpickles
it and returns the bytes, which the target state and checkpoint carry as
`serialized_application_autoscaling_policy_def`. Two `_set_target_state`
callers never forwarded the field: `recover_target_state_from_checkpoint`
and the same-code-version branch of `apply_app_config`. The policy was
still registered from the checkpoint's bytes on that first recovery, so
nothing failed immediately, but the new target state held None and the
next checkpoint persisted it. The recovery after a second controller
restart, or the first restart after an in-place update, then built
`AutoscalingPolicy` without bytes, imported the policy by path inside the
controller, and raised ModuleNotFoundError out of ServeController.__init__,
leaving the controller in a crash loop.

Forward the field at both call sites. A same-code-version update cannot
change the policy module, so the previous target state's bytes remain
correct. The unit test uses a policy path that is not importable in the
test process to mirror a runtime_env-only module.

Signed-off-by: qishengxhe <[email protected]>
@qishengxhe

Copy link
Copy Markdown
Contributor Author

Also fixes a pre-existing bug on master that Abrar found: recover_target_state_from_checkpoint and the same-code-version path of apply_app_config dropped serialized_application_autoscaling_policy_def, so a runtime_env-only app-level policy crash-looped the controller on the second restart, or on the first restart after an in-place update. Both call sites now forward the bytes, with a unit test that uses an unimportable policy path in commit 2c5fa6f9cd

# the build rather than the previous target restores the values in
# code for options the new config no longer sets.
build = self._target_state.build
assert build is not None

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.

Should we take the rebuild branch when build is None instead of asserting, since a checkpoint written before this PR (e.g. GCS FT on Redis across a Ray upgrade) hits this during __init__ recovery and kills the controller permanently?

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.

I learnt that we never restore the checkpoints written by an old cluster into a new cluster, so a checkpoint is only read by the ray version that wrote it. Also during a startup recovery, controller init goes through recover_target_state_from_checkpoint. this assert sits in apply_app_config which is never invoked by controller init.

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.

Oh but wouldn't controller init reach this through _recover_state_from_checkpoint -> apply_config -> apply_app_config? Also how do we know checkpoints from an old cluster won't get written into a new cluster on OSS setups?

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.

You're right about the path :) controller init does reach apply_app_config .
On the second point, I double-checked. it's possible that on OSS someone pins RAY_external_storage_namespace to restore checkpoints across clusters / ray versions,but that's unsupported: KubeRay's docs recommend against it since the new cluster reads the old cluster's serve status from Redis and the operator flips traffic too early (doc link). and cross-version checkpoints already fail earlier in the same init: required fields like external_scaler_enabled and serialized_application_autoscaling_policy_def have no default and would and raise AttributeError for an older checkpoint before we get to this assert. so we should be good.

The AppBuild return value was documented on _determine_app_status, which
returns a 2-tuple. Move it to _reconcile_build_app_task, which returns it.

Signed-off-by: qishengxhe <[email protected]>
@abrarsheikh
abrarsheikh merged commit fba9b5b into master Sep 24, 2026
7 checks passed
@abrarsheikh
abrarsheikh deleted the serve/inplace-3-config-overrides-from-build branch September 24, 2026 05:12
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
sai-miduthuri pushed a commit that referenced this pull request Oct 16, 2026
…rcent` for start-before-stop rolling updates (#66267) (#67094)

Cherry-pick of #66267 (master commit
9491664) onto `releases/2.60.0`.

## Why

The first three PRs of the in-place-updates series (#66252, #66290,
#66266) are already on `releases/2.60.0`. This picks the fourth, which
adds `max_surge_percent` for start-before-stop rolling updates, so
2.60.0 ships the complete series.

## Cherry-pick details

`git cherry-pick -x` conflicted in one file,
`python/ray/serve/tests/unit/test_deployment_state.py`. The conflict was
positional only: on master, #66243 (push-based replica health) had
appended `TestPushedHealthEndToEnd` and related tests to the end of that
file, and #66267 inserts `TestMaxSurge` right before them. This branch
doesn't have #66243, so git couldn't anchor the hunk and offered the
#66243 tests as part of the incoming side. Resolved by inserting only
the PR's `TestMaxSurge` block (plus its `defaultdict` import) before the
`__main__` guard. The added and removed lines in each of the 17 files
are identical to the master commit's diff.

After the pick, 11 of the 17 files are byte-identical to master at
9491664. The other 6 differ only by master commits that aren't on
this branch and that #66267 doesn't depend on:

- `_private/deployment_state.py`, `tests/unit/test_deployment_state.py`:
#66243.
- `schema.py`, `tests/BUILD.bazel`, `tests/test_controller.py`,
`tests/test_direct_ingress.py`: #66339 and #66940.

No commit on `releases/2.60.0` after the branch cut touched any of these
files.

Not a duplicate: no PR against `releases/2.60.0` references #66267.

## Testing

Run on this branch with the Serve protobuf regenerated from the branch's
`serve.proto` (the pick adds the `max_surge_percent` field).

```
python -m pytest python/ray/serve/tests/unit/test_deployment_state.py python/ray/serve/tests/unit/test_config.py python/ray/serve/tests/unit/test_deployment_class.py python/ray/serve/tests/unit/test_schema.py
3530 passed, 12 skipped
```

```
python -m pytest python/ray/serve/tests/test_deploy_app_3.py python/ray/serve/tests/test_deploy_app_2.py -k surge
13 passed, 41 deselected in 282s
```

`pre-commit run --files <changed files>` passed.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Qisheng He <[email protected]>
Signed-off-by: qishengxhe <[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.

3 participants