Skip to content

[Data] Emit Seeded Disk Random Shuffle Partitions in Order - #68319

Merged
richardliaw merged 1 commit into
ray-project:masterfrom
rayhhome:random-shuffle-seed-order
Nov 19, 2026
Merged

richardliaw merged 1 commit into
ray-project:masterfrom
rayhhome:random-shuffle-seed-order

Conversation

@rayhhome

Copy link
Copy Markdown
Contributor

Description

A seeded random_shuffle() promises the same output across runs. On the disk-based shuffle v2 path each partition's rows were reproducible, but the reduce emitted partitions in completion order unless execution_options.preserve_order was set, so two runs with the same seed could return the partitions in a different order. This has made test_random_e2e.py::test_random_shuffle[shuffle_v2_disk] flaky.

Preserve partition order whenever the user gave a seed. An unseeded shuffle (pinned to a timestamp seed for retries) keeps emitting partitions as they finish.

Additional information

No other PRs address this flakiness issue. In tests, we added seeded vs. unseeded partition assertions in test_random_shuffle_planner_routes_to_disk_shuffle_v2. This pr is created with AI assistance (Claude) and has been manually reviewed.

@rayhhome
rayhhome requested a review from a team as a code owner November 18, 2026 23:07
@rayhhome rayhhome added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Nov 18, 2026
Copilot AI balanced review requested due to automatic review settings November 18, 2026 23:07

@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 ensures that partition order is preserved during a random shuffle if a user seed is specified, guaranteeing reproducible outputs across runs. The tests have been updated to verify this behavior for both seeded and unseeded shuffles. The review feedback recommends adding a defensive check to ensure logical_op.seed_config is not None before accessing its seed attribute, preventing a potential AttributeError.

Comment on lines +433 to +436
preserve_partition_order=(
logical_op.seed_config.seed is not None
or data_context.execution_options.preserve_order
),

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

To prevent a potential AttributeError if logical_op.seed_config is None, defensively check that logical_op.seed_config is not None before accessing its seed attribute.

Suggested change
preserve_partition_order=(
logical_op.seed_config.seed is not None
or data_context.execution_options.preserve_order
),
preserve_partition_order=(
(logical_op.seed_config is not None and logical_op.seed_config.seed is not None)
or data_context.execution_options.preserve_order
),

Copilot AI 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.

🟡 Changes recommended

Seeded shuffles remain nondeterministic because upstream completion order changes writer task indices and derived seeds, leaving the end-to-end equality assertion flaky.

1 open issue
What changed in this PR

This PR addresses seeded shuffle reproducibility in Ray Data’s disk-based shuffle v2 by preserving reduce partition order.

Changes:

  • Emit seeded shuffle partitions in order while retaining completion-order emission for unseeded shuffles unless ordering is explicitly enabled.
  • Update planner assertions and exercise seeded end-to-end reproducibility without enabling global ordering.
File Description
python/​ray/​data/​tests/​test_random_shuffle_v2.py Checks seeded and unseeded ordering behavior and removes the end-to-end test’s global ordering override.
python/​ray/​data/​_internal/​planner/​plan_all_to_all_op.py Enables reduce partition ordering when a user supplies a seed.

Review effort: Balanced

Comment on lines +433 to +435
preserve_partition_order=(
logical_op.seed_config.seed is not None
or data_context.execution_options.preserve_order

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.

Reproduced this on beddb5a. Disk shuffle v2, random_shuffle(seed=0), default preserve_order=False, with a map_batches before the shuffle that sleeps a different amount per block (8 blocks of 100 rows, 0 to 1.75 s), so the upstream tasks finish in a different order between runs. Two runs with the same sleeps already returned different rows, and reversing the sleeps changed them again. Partition order is fixed now, so it's the rows inside each partition that change. With preserve_order=True all runs match.

For the flaky test the PR does help. I repeated the seeded call from test_random_shuffle (range(100, override_num_blocks=5).random_shuffle(seed=0), shuffle_input_batch_bytes=0 like the fixture) 40 times: the merge base 2bdf4df gave 3 distinct outputs with 2 CPUs and 5 with 8 CPUs, this PR gave 1 in both. So the test passes because its upstream is a fast read, not because seeded output is reproducible in general.

The random_shuffle docstring calls a fixed seed "Fully deterministic across executions". Would it work to number the writer tasks by input order for seeded shuffles? If that's too much for this PR, maybe say in the description and the docstring that it still needs preserve_order=True when upstream tasks can finish out of order.

A seeded random_shuffle() promises the same output across runs. On the
disk-based shuffle v2 path each partition's rows were reproducible, but
the reduce emitted partitions in completion order unless
execution_options.preserve_order was set, so two runs with the same seed
could return the partitions in a different order. This made
test_random_e2e.py::test_random_shuffle[shuffle_v2_disk] flaky.

Preserve partition order whenever the user gave a seed. An unseeded
shuffle (pinned to a timestamp seed for retries) keeps emitting
partitions as they finish.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
@rayhhome
rayhhome force-pushed the random-shuffle-seed-order branch from 382f7e9 to beddb5a Compare November 19, 2026 01:00
@richardliaw
richardliaw merged commit ff9b503 into ray-project:master Nov 19, 2026
6 checks passed
@rayhhome
rayhhome deleted the random-shuffle-seed-order branch November 19, 2026 22:28
richardliaw pushed a commit that referenced this pull request Nov 21, 2026
…uffle (#68417)

## Summary
`random_shuffle(seed=...)` on the disk-based shuffle v2 could return a
different order across runs. The plan now requires `preserve_order` for
a seeded shuffle on that path, the same mechanism `zip` uses, so each
input block always gets the same map task index and therefore the same
seed.

## Before
Two things must hold for a seeded shuffle to be repeatable: every block
must get the same seed, and partitions must come out in the same order.
#68319 fixed the second (the reduce emits partitions in id order and
sorts mapper handles by map id). The first still varied:
`RandomShufflePartitioner` seeds a block from `(seed, map_task_index,
block_index)`, and the disk writer's map task index is handed out in
input arrival order because it does not fuse with the read. When read
tasks finish out of order, a block gets a different seed. This is the
flake behind `test_random_shuffle[shuffle_v2_disk]` (postmerge 21143,
failed once, passed on retry). The legacy shuffle avoids it only because
its upstream map fuses into the shuffle map stage.

## After
`LogicalPlan.require_preserve_order()` returns True for a
`RandomShuffle` with a user seed when the context selects `SHUFFLE_V2`
with `use_disk_based_hash_shuffle`. `Dataset._start_execution` already
turns that into `preserve_order=True` on the executor, so upstream
operators emit in task-index order and each block's map task index is
fixed. Unseeded shuffles and the legacy paths are untouched.

```mermaid
flowchart TD
    subgraph Before
        subgraph BP[Logical planning]
            B1[require_preserve_order: Zip only]
        end
        subgraph BE[Execution]
            B2[Read emits in completion order] --> B3[Map: map_id = arrival order, seed = f seed, map_id]
            B3 --> B4[Reduce: partitions in id order, since #68319]
        end
    end
    subgraph After
        subgraph AP[Logical planning]
            A1[require_preserve_order: Zip, or seeded RandomShuffle on disk v2]
        end
        subgraph AE[Execution]
            A2[Read emits in task-index order] --> A3[Map: map_id = input index, seed = f seed, map_id]
            A3 --> A4[Reduce: partitions in id order, since #68319]
        end
    end
    classDef changed fill:#fff9c4,stroke:#f9a825,color:#000
    class A1,A2,A3 changed
```

## Subsystems
Logical planning (the change), shuffle / aggregation (the affected
path).

## Testing
- `test_optimize.py::test_seeded_disk_shuffle_requires_preserve_order`:
the hook fires only for a seeded shuffle under disk v2.
-
`test_random_e2e.py::test_seeded_random_shuffle_ignores_arrival_order`:
makes the last read block finish first and asserts the seeded output
matches the in-order run, under every shuffle strategy. Fails on master
for `shuffle_v2_disk`.

```
pytest python/ray/data/tests/test_optimize.py python/ray/data/tests/test_random_e2e.py \
  -k "preserve_order or ignores_arrival_order or (test_random_shuffle and shuffle_v2_disk)"
14 passed
```

## Notes
- `preserve_order=True` disables Ray Data lineage reconstruction for
that execution, with the existing warning. A seeded shuffle already
promises a reproducible order, so this is the right trade.
- Not covered, as for `zip` today: operators that read the flag at plan
time, namely the v2 hash-shuffle reducers (join, groupby, keyed
repartition) and parallel file listing. A seeded shuffle directly after
one of those still follows arrival order unless
`execution_options.preserve_order` is set on the context before
execution. No existing test places a seeded shuffle after those.
- Not a duplicate: #68319 addressed reduce-side partition order; no open
PR touches the map-side seed. Written with AI assistance.

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

---------

Signed-off-by: Sirui Huang <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
elliot-barn added a commit that referenced this pull request Nov 23, 2026
…nto releases/2.61.0 (#68472)

## Description

Cherry-pick of #68433 (`1729626bbb`) onto `releases/2.61.0`. Lets the
upstream read or map fuse into the disk-shuffle writer, and sets the
polars thread limit inside the task rather than through the runtime env.

The pick applied without conflicts. Seven master commits that touch the
same files are not on the release branch (#67143, #68158, #68202 and its
revert #68383, #68319, #67570, #68118), so the picked files differ from
master in five places. Reviewers from Data, please confirm none of those
is a prerequisite.

## Testing

Ran locally against the `releases/2.61.0` wheel (`7c8ca5ea51`) with the
picked files overlaid: `test_hash_shuffle_v2.py`,
`test_operator_fusion.py` and `test_execution_optimizer_advanced.py`,
232 passed, 2 skipped.

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

Signed-off-by: You-Cheng Lin <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
Co-authored-by: You-Cheng Lin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants