Skip to content

[Data] Refactor Planner to avoid storing plan-specific state#53955

Merged
bveeramani merged 2 commits intomasterfrom
refactor-planner
Jun 19, 2025
Merged

[Data] Refactor Planner to avoid storing plan-specific state#53955
bveeramani merged 2 commits intomasterfrom
refactor-planner

Conversation

@bveeramani
Copy link
Copy Markdown
Member

@bveeramani bveeramani commented Jun 19, 2025

Why are these changes needed?

The current Planner implementation stores plan-specific state (like the op_map) as a class attribute. This makes the planner technically stateful across calls, which could lead to incorrect results if .plan() is called multiple times on the same instance:

planner = Planner()
planner.plan(logical_plan1)
# This will lead to incorrect results because it reuses state form the first plan
planner.plan(logical_plan2)

While this doesn’t currently happen in practice, it’s fragile and could easily lead to bugs.

This PR refactors the planner to avoid storing any state tied to a specific plan, making it safe to reuse across multiple calls.

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>

[Data] Insert checkpoint layers during planning instead of during physical optimization (#1860)

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
Copilot AI review requested due to automatic review settings June 19, 2025 17:34
@bveeramani bveeramani requested a review from a team as a code owner June 19, 2025 17:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the Planner to remove plan-specific state by replacing direct instantiations of Planner with a factory function (create_planner), and updates the planning logic to use a functional approach with a mapping of logical operator types to planning functions.

  • Replace direct Planner instantiation with create_planner in tests and logical optimizers.
  • Change the PLAN_LOGICAL_OP_FNS collection from a list to a dictionary and update the planning recursion accordingly.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
python/ray/data/tests/test_randomize_block_order.py Updated Planner instantiation to use create_planner
python/ray/data/tests/test_operator_fusion.py Updated multiple Planner instantiations with create_planner
python/ray/data/tests/test_execution_optimizer.py Replaced Planner() calls with create_planner() in tests
python/ray/data/_internal/planner/planner.py Refactored internal planning logic and mapping collection
python/ray/data/_internal/planner/init.py Added create_planner factory method
python/ray/data/_internal/logical/optimizers.py Updated get_execution_plan to use create_planner
Comments suppressed due to low confidence (1)

python/ray/data/_internal/planner/planner.py:182

  • The function plan_recursively's return type annotation and its docstring appear to be mismatched; the docstring suggests a mapping from physical to logical operators, while the annotation indicates Dict[LogicalOperator, PhysicalOperator]. Consider updating the annotation and docstring to accurately reflect that the mapping is from PhysicalOperator to LogicalOperator.
def plan_recursively(

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
"""

def __init__(self):
self._physical_op_to_logical_op: Dict[PhysicalOperator, LogicalOperator] = {}
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.

@bveeramani i don't think there's anything wrong with the planner being stateful component

I think issue you're running into could be addressed much more easily:

  • Make Planner.plan class/static method
  • Planner.plan initializes planner and makes sure that instances aren't shared b/w invocations

@bveeramani bveeramani enabled auto-merge (squash) June 19, 2025 18:14
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Jun 19, 2025
@bveeramani bveeramani merged commit 1d54fc7 into master Jun 19, 2025
7 checks passed
@bveeramani bveeramani deleted the refactor-planner branch June 19, 2025 19:52
minerharry pushed a commit to minerharry/ray that referenced this pull request Jun 27, 2025
…roject#53955)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

<!-- Please give a short summary of the change and the problem this
solves. -->

The current `Planner` implementation stores plan-specific state (like
the `op_map`) as a class attribute. This makes the planner technically
stateful across calls, which could lead to incorrect results if
`.plan()` is called multiple times on the same instance:

```
planner = Planner()
planner.plan(logical_plan1)
# This will lead to incorrect results because it reuses state form the first plan
planner.plan(logical_plan2)
```

While this doesn’t currently happen in practice, it’s fragile and could
easily lead to bugs.

This PR refactors the planner to avoid storing any state tied to a
specific plan, making it safe to reuse across multiple calls.

## Related issue number

<!-- For example: "Closes ray-project#1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
elliot-barn pushed a commit that referenced this pull request Jul 2, 2025
<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

<!-- Please give a short summary of the change and the problem this
solves. -->

The current `Planner` implementation stores plan-specific state (like
the `op_map`) as a class attribute. This makes the planner technically
stateful across calls, which could lead to incorrect results if
`.plan()` is called multiple times on the same instance:

```
planner = Planner()
planner.plan(logical_plan1)
# This will lead to incorrect results because it reuses state form the first plan
planner.plan(logical_plan2)
```

While this doesn’t currently happen in practice, it’s fragile and could
easily lead to bugs.

This PR refactors the planner to avoid storing any state tied to a
specific plan, making it safe to reuse across multiple calls.

## Related issue number

<!-- For example: "Closes #1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
snorkelopsstgtesting1-spec pushed a commit to snorkel-marlin-repos/ray-project_ray_pr_53955_d6a41f93-e10a-44d7-b4c3-8d181e0cde7d that referenced this pull request Oct 22, 2025
snorkelopstesting4-web added a commit to snorkel-marlin-repos/ray-project_ray_pr_53955_d6a41f93-e10a-44d7-b4c3-8d181e0cde7d that referenced this pull request Oct 22, 2025
…n-specific state

Merged from original PR #53955
Original: ray-project/ray#53955
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.

4 participants