Skip to content

[Data] 2-phase commit for checkpointing with prefix trie recovery - #61821

Merged
alexeykudinkin merged 2 commits into
ray-project:masterfrom
xinyuangui2:data-simplify-2pc-recovery
Mar 19, 2026
Merged

alexeykudinkin merged 2 commits into
ray-project:masterfrom
xinyuangui2:data-simplify-2pc-recovery

Conversation

@xinyuangui2

@xinyuangui2 xinyuangui2 commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR introduces 2-phase commit (2PC) for Ray Data checkpointing and then simplifies the recovery algorithm using a prefix trie. Together these changes eliminate duplicate data files on failure recovery and provide exactly-once write semantics for file-based datasinks.

Part 1: 2-phase commit for checkpointing to avoid duplicates

Previously, checkpoints were written after data files. If a task failed between writing data and writing the checkpoint, recovery would re-write the same data — producing duplicates. 2PC fixes this:

  1. Pre-write: Write a pending checkpoint (.pending.parquet) containing the expected data file prefix
  2. Write: Write the actual data file
  3. Post-write: Commit the checkpoint (rename .pending.parquet → .parquet)

On recovery, pending checkpoints identify uncommitted data files that should be deleted before retrying.

For non-file datasinks (SQL, MongoDB, etc.), falls back to post-write checkpointing with at-least-once semantics (with warning).

Part 2: Simplify recovery with prefix trie

Removes metadata storage in checkpoint parquet files (DATA_FILE_DIR_METADATA_KEY, DATA_FILE_PREFIX_METADATA_KEY) and replaces per-checkpoint metadata-based recovery with a simpler algorithm:

  1. List pending checkpoint files, build a PrefixTrie from their basenames (strip .pending.parquet)
  2. List all data files recursively (handles partitioned output)
  3. Delete data files whose basename matches a trie prefix
  4. Delete the pending checkpoint files

Other changes

  • Plumb data_file_dir / data_file_filesystem through Planner → LoadCheckpointCallback → BatchBasedCheckpointFilter so recovery can locate data files without reading checkpoint metadata
  • Add deterministic FilenameProvider.get_filename_for_task() so checkpoint ID matches data file prefix. Deprecate get_filename_for_block() and get_filename_for_row()
  • Simplify write_pending_checkpoint() signature: takes only (id_column_data, checkpoint_id) instead of also requiring data_file_dir and data_file_prefix
  • Row-based datasinks (RowBasedFileDatasink) derive per-row filenames from the task filename via _split_base_and_ext, ensuring all files from one task share a common prefix for recovery

Why write checkpoint before data?

pq.write_table is not atomic — if the process is killed mid-write, the file may exist on disk but be corrupted. Writing the pending checkpoint first and renaming it after the data write succeeds ensures we can always detect and clean up incomplete writes.

Test plan

  • New 2PC unit tests: pending checkpoint write/commit, idempotent commit, recovery with prefix trie, partitioned data cleanup
  • Non-file datasink warning test
  • S3 parametrization for all recovery tests (via moto mock server)
  • Updated test_filename_provider.py for new get_filename_for_task() API
  • Row-based dedupe coverage for RowBasedFileDatasink
  • Existing checkpoint tests updated and passing

🤖 Generated with Claude Code

…non-filebased datasink checkpointing

- Remove metadata storage in checkpoint parquet files; use checkpoint filename = data file basename instead
- Replace per-checkpoint metadata-based recovery with prefix trie recovery
- Pass data_file_dir and data_file_filesystem through the recovery call chain
- Simplify write_pending_checkpoint() signature
- Add deterministic FilenameProvider.get_filename_for_task()
- Add warning for checkpointing with non-file datasinks (at-least-once semantics)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@xinyuangui2
xinyuangui2 requested a review from a team as a code owner March 18, 2026 02:19

@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 a significant and well-thought-out simplification of the 2-phase commit recovery logic in Ray Data. By moving to deterministic filenames and a prefix trie for cleanup, the new approach is more robust and easier to maintain. The introduction of a 2-phase commit for file-based datasinks is a key part of this. The code is well-structured, with clear separation of concerns in the planner, writer, and filter components, and the deprecation of older APIs is handled clearly. The extensive test suite, covering various failure and recovery scenarios, provides strong confidence in the correctness of this critical functionality. I have a couple of minor suggestions for improvement.

@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 and found 2 potential issues.

Fix All in Cursor

@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Mar 18, 2026
@xinyuangui2 xinyuangui2 added the go add ONLY when ready to merge, run all tests label Mar 18, 2026
@xinyuangui2 xinyuangui2 changed the title [Data] Simplify 2-phase commit recovery with prefix trie [Data] 2-phase commit for checkpointing with prefix trie recovery Mar 18, 2026
Signed-off-by: xgui <[email protected]>
@alexeykudinkin
alexeykudinkin merged commit 45ea2a7 into ray-project:master Mar 19, 2026
3 checks passed
ryanaoleary pushed a commit to ryanaoleary/ray that referenced this pull request Mar 25, 2026
richardliaw pushed a commit that referenced this pull request May 28, 2026
…63606)

## Why are these changes needed?

`_clean_pending_checkpoints_task` in `checkpoint_filter.py` uses
`FileSelector` without `allow_not_found=True` when listing checkpoint
files:

```python
ckpt_files = checkpoint_filesystem.get_file_info(
    FileSelector(checkpoint_path_unwrapped, recursive=False)
)
```

On S3, listing a non-existent prefix returns an empty result. On Azure
Blob Storage, it raises `FileNotFoundError`. This breaks
`CheckpointConfig` on Azure for first-run scenarios where the checkpoint
directory does not yet exist.

The data file listing in the same function already correctly uses
`allow_not_found=True`:

```python
data_files = data_file_filesystem.get_file_info(
    FileSelector(data_file_dir_unwrapped, recursive=True, allow_not_found=True)
)
```

Similarly, the `load_checkpoint` method in `CheckpointManager` also uses
`allow_not_found=True` for its own `FileSelector` call.

This was introduced in #61821 which was only tested on S3.

## Fix

Add `allow_not_found=True` to the `FileSelector` call in
`_clean_pending_checkpoints_task`.

## Related issue number

N/A

## Checks

- [x] I've signed the [Contributor License
Agreement](https://docs.google.com/forms/d/e/1FAIpQLSfAMq5v7-hRKRvNaRDhx_WP1W0IKTm7lBvpBlOrLAsNP2MKIQ/viewform).
- [ ] 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/` (if
applicable).
- [ ] 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/

*This PR was authored with Cursor assistance.*

Made with [Cursor](https://cursor.com)

---------

Signed-off-by: Michael Pryor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Rruop pushed a commit to Rruop/ray that referenced this pull request May 29, 2026
- Expand Table 1 from 239 to 264 entries (add Dashboard + Core commits)
- Rebuild Table 1 in correct branch commit order
- Add fork point analysis (d60d131)
- Add Revert pair documentation
- Add 2.54.x cherry-pick correspondence table
- Add Build/compilation risk analysis (Bazel 7, gRPC)
- Add PR ray-project#60295, PR ray-project#61821, and Issue ray-project#63544 detailed analysis
- Add Table 2: all 344 missing Core/Data/Dashboard commits
- Add Table 3: 80 commits not covered by Table 1

Co-Authored-By: Claude Opus 4.5 <[email protected]>
rueian pushed a commit to rueian/ray that referenced this pull request Jun 4, 2026
…ay-project#63606)

## Why are these changes needed?

`_clean_pending_checkpoints_task` in `checkpoint_filter.py` uses
`FileSelector` without `allow_not_found=True` when listing checkpoint
files:

```python
ckpt_files = checkpoint_filesystem.get_file_info(
    FileSelector(checkpoint_path_unwrapped, recursive=False)
)
```

On S3, listing a non-existent prefix returns an empty result. On Azure
Blob Storage, it raises `FileNotFoundError`. This breaks
`CheckpointConfig` on Azure for first-run scenarios where the checkpoint
directory does not yet exist.

The data file listing in the same function already correctly uses
`allow_not_found=True`:

```python
data_files = data_file_filesystem.get_file_info(
    FileSelector(data_file_dir_unwrapped, recursive=True, allow_not_found=True)
)
```

Similarly, the `load_checkpoint` method in `CheckpointManager` also uses
`allow_not_found=True` for its own `FileSelector` call.

This was introduced in ray-project#61821 which was only tested on S3.

## Fix

Add `allow_not_found=True` to the `FileSelector` call in
`_clean_pending_checkpoints_task`.

## Related issue number

N/A

## Checks

- [x] I've signed the [Contributor License
Agreement](https://docs.google.com/forms/d/e/1FAIpQLSfAMq5v7-hRKRvNaRDhx_WP1W0IKTm7lBvpBlOrLAsNP2MKIQ/viewform).
- [ ] 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/` (if
applicable).
- [ ] 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/

*This PR was authored with Cursor assistance.*

Made with [Cursor](https://cursor.com)

---------

Signed-off-by: Michael Pryor <[email protected]>
Co-authored-by: Cursor <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…ay-project#63606)

## Why are these changes needed?

`_clean_pending_checkpoints_task` in `checkpoint_filter.py` uses
`FileSelector` without `allow_not_found=True` when listing checkpoint
files:

```python
ckpt_files = checkpoint_filesystem.get_file_info(
    FileSelector(checkpoint_path_unwrapped, recursive=False)
)
```

On S3, listing a non-existent prefix returns an empty result. On Azure
Blob Storage, it raises `FileNotFoundError`. This breaks
`CheckpointConfig` on Azure for first-run scenarios where the checkpoint
directory does not yet exist.

The data file listing in the same function already correctly uses
`allow_not_found=True`:

```python
data_files = data_file_filesystem.get_file_info(
    FileSelector(data_file_dir_unwrapped, recursive=True, allow_not_found=True)
)
```

Similarly, the `load_checkpoint` method in `CheckpointManager` also uses
`allow_not_found=True` for its own `FileSelector` call.

This was introduced in ray-project#61821 which was only tested on S3.

## Fix

Add `allow_not_found=True` to the `FileSelector` call in
`_clean_pending_checkpoints_task`.

## Related issue number

N/A

## Checks

- [x] I've signed the [Contributor License
Agreement](https://docs.google.com/forms/d/e/1FAIpQLSfAMq5v7-hRKRvNaRDhx_WP1W0IKTm7lBvpBlOrLAsNP2MKIQ/viewform).
- [ ] 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/` (if
applicable).
- [ ] 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/

*This PR was authored with Cursor assistance.*

Made with [Cursor](https://cursor.com)

---------

Signed-off-by: Michael Pryor <[email protected]>
Co-authored-by: Cursor <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ay-project#63606)

## Why are these changes needed?

`_clean_pending_checkpoints_task` in `checkpoint_filter.py` uses
`FileSelector` without `allow_not_found=True` when listing checkpoint
files:

```python
ckpt_files = checkpoint_filesystem.get_file_info(
    FileSelector(checkpoint_path_unwrapped, recursive=False)
)
```

On S3, listing a non-existent prefix returns an empty result. On Azure
Blob Storage, it raises `FileNotFoundError`. This breaks
`CheckpointConfig` on Azure for first-run scenarios where the checkpoint
directory does not yet exist.

The data file listing in the same function already correctly uses
`allow_not_found=True`:

```python
data_files = data_file_filesystem.get_file_info(
    FileSelector(data_file_dir_unwrapped, recursive=True, allow_not_found=True)
)
```

Similarly, the `load_checkpoint` method in `CheckpointManager` also uses
`allow_not_found=True` for its own `FileSelector` call.

This was introduced in ray-project#61821 which was only tested on S3.

## Fix

Add `allow_not_found=True` to the `FileSelector` call in
`_clean_pending_checkpoints_task`.

## Related issue number

N/A

## Checks

- [x] I've signed the [Contributor License
Agreement](https://docs.google.com/forms/d/e/1FAIpQLSfAMq5v7-hRKRvNaRDhx_WP1W0IKTm7lBvpBlOrLAsNP2MKIQ/viewform).
- [ ] 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/` (if
applicable).
- [ ] 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/

*This PR was authored with Cursor assistance.*

Made with [Cursor](https://cursor.com)

---------

Signed-off-by: Michael Pryor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Signed-off-by: 400Ping <[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.

3 participants