Skip to content

[Data] Stream MCAP files: seekable open, size-bounded blocks, log_time_order - #66653

Closed
marwan116 wants to merge 1 commit into
masterfrom
marwan/mcap-seekable-read
Closed

marwan116 wants to merge 1 commit into
masterfrom
marwan/mcap-seekable-read

Conversation

@marwan116

@marwan116 marwan116 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part 1 of 6 of the read_mcap redesign. The other five PRs are stacked on this branch.

Problem. MCAPDatasource opens each file as a one-way stream, so the mcap library uses its non-seeking reader:

  • The index at the end of the file is never read, so topics and time_range filter only after a full scan.
  • log_time_order=True loads and sorts the whole file before returning anything.
  • Each file becomes one block, however large it is.

Change. Three small edits to the existing datasource:

  • Open files for random access, so the reader can use the index and skip chunks.
  • Emit a block each time it reaches DataContext.target_max_block_size.
  • Expose log_time_order on read_mcap. The default stays True.
before:  open as a stream ──► read and sort the whole file ──► 1 block
after:   read the index ──► seek to the matching chunks ──► block, block, ...

Files without an index still read, by linear scan.

Measured on a 230 MB recording on local disk:

master this PR
time to first block 0.34 s 0.08 s
time_range over the last 10% 0.31 s 0.05 s
peak memory 912 MiB 795 MiB

Related issues

Additional information

PYTHONPATH=python/ray/data/tests pytest python/ray/data/tests/datasource/test_mcap.py

22 passed (18 on master). pre-commit clean.

AI assistance (Claude Code) was used to draft this change; I reviewed every line and ran the tests above locally.

🤖 Generated with Claude Code

@marwan116
marwan116 requested a review from a team as a code owner October 2, 2026 03:50
@marwan116 marwan116 added the data Ray Data-related issues label Oct 2, 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 enhances the MCAP datasource by opening files for random access to leverage the MCAP chunk index, introducing a log_time_order option to control message sorting, and streaming files in blocks based on target_max_block_size. Feedback on these changes highlights a potential performance bottleneck from calling get_estimated_memory_usage() on every row, suggesting it be checked periodically instead. Additionally, a correctness concern was raised regarding potential schema inconsistencies across blocks when a multi-topic file is split into multiple blocks.

…e_order

`MCAPDatasource` read every file through a sequential stream, so the mcap
library always took its non-seeking path:

- The summary at the file's tail (per-chunk time bounds and byte offsets,
  per-channel message counts) was unreachable. `topics` and `time_range`
  were applied per record over a full scan rather than used to skip the
  chunks that cannot match, despite the class docstring advertising
  predicate pushdown.
- `log_time_order=True` was hard-coded. On the non-seeking reader that
  loads and sorts the entire file before the first message is returned,
  which its own docstring warns about.
- A file was one block, however large it was.

Open the file for random access (`open_input_file`), the hook
`FileBasedDatasource` documents for implementations that need it.
`make_reader` then returns a `SeekingReader`, which prunes chunks by topic
and time from the index and merges chunks by log time as it goes instead of
sorting the file in memory. Files written without a summary still read: the
seeking reader falls back to a linear scan when there is no chunk index.

Cut blocks by estimated size: yield a block as soon as the builder reaches
the context's `target_max_block_size`. Memory per read task is bounded by
the target instead of by the file, and the first block of a large file
arrives before the file has been read to the end. `target_max_block_size =
None` keeps one block per file.

Expose `log_time_order` on `read_mcap`. The default, True, keeps today's
output. False streams a file in the order it was written and skips the
merge, which is the right choice when order does not matter or when the
file's chunks do not overlap in time.

Rewinding between retry attempts is no longer this class's concern:
`FileBasedDatasource.read_files` reopens the file for every attempt (#66011).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Marwan Sarieddine <[email protected]>
@marwan116
marwan116 force-pushed the marwan/mcap-seekable-read branch from 7532acf to 2c5ec5d Compare October 5, 2026 18:31
@marwan116 marwan116 added the go add ONLY when ready to merge, run all tests label Oct 6, 2026
@marwan116
marwan116 removed this pull request from stack #66712 October 7, 2026 00:25
@marwan116

Copy link
Copy Markdown
Contributor Author

Closing: this speeds up the old reader, which only runs when DataSource V2 is turned off once #66654 merges. Its log_time_order argument moved into #66654.

@marwan116 marwan116 closed this Oct 7, 2026
richardliaw pushed a commit that referenced this pull request Nov 6, 2026
…, row_id (#66654)

## Description

Part 1 of 3 of the `read_mcap` move to Datasource V2, based on master.
#67166 is stacked on it, and #67167 on #67166. The rest of the
`read_mcap` stack (#67189 onward) sits on top.

**Problem.** One task reads one whole file:

- Parallelism is capped by the file count: ten 30 GB recordings are ten
tasks.
- Every file is opened, even when its index shows it holds nothing
selected.
- Rows have no stable id, so a checkpointed pipeline cannot resume.

**Change.** `read_mcap` moves to Datasource V2, on by default as for
`read_parquet` (`DataContext.use_datasource_v2`). The legacy datasource
stays as the fallback.

```
files
  │  listing: read each file's index (a few ranged reads)
  │           one row per chunk, weighted by the memory reading it takes
  │           drop chunks that cannot match topics / time_range
  ▼
chunk rows
  │  packing: about 128 MiB per read task
  │           one big file ──► many tasks;  small files ──► one task
  │           with preserve_order: one task per file, in listing order
  ▼
read tasks: seek to their chunks, merge them by log time
```

- New code lives in `datasource_v2/formats/mcap/`. The V2 interfaces do
not change.
- Columns and types match the legacy path, with two changes.
`schema_data` is dictionary-encoded. A new `channel_metadata` column
holds each channel's metadata map as JSON text, dictionary-encoded, so a
block stores it once per channel.
- `data` holds decoded JSON only when every selected channel is JSON.
Otherwise it holds bytes. Planning types it from the selected messages
of the first chunk that holds one, as a read task types the messages it
holds.
- `row_id` is `<path>#c=<chunk offset>:<index>`, stable however the read
is split. It is on with `include_row_id=True`, or automatically under
`CheckpointConfig(id_column="row_id")`.
- New `log_time_order` argument, default `True`. Each task returns each
of its files in `log_time` order. With `preserve_order`, each file is
read by one task, so it comes out in order from start to end, and files
come out in the order they were listed. #67166 splits a large file there
too, only where its order holds.
- A file whose summary cannot be read gets a read task of its own, so
`max_errored_blocks` can skip it alone.
- The task size is `RAY_DATA_MCAP_BIN_PACKING_BYTES` (128 MiB).

**Changed after the first review round.** Each is its own commit:

- Summaries and chunks are read in a few ranged requests, not a few
bytes per GET.
- An unreadable file gets a read task of its own.
- Tables are built column by column, cut counting every column, and
chunks are weighted by the memory reading them takes.
- The read stops at the row limit.
- A file without a chunk index is sorted by log time.
- `channel_metadata` is JSON text, stored once per block.
- With `preserve_order`, each file is kept in one read task, and files
keep their listing order.
- Planning skips a chunk it cannot decode, and types `data` from the
first chunk's messages.

**Measured** on an 845 MB recording with 1,052 chunks: reading its 125
KB summary took about 20,000 GETs and now takes 3.

**Decisions to confirm:**

- Splitting is on by default. Block boundaries change; rows do not.
- With `preserve_order`, this PR reads each file in one task, as the
legacy reader does. #67166 lets it split a large file where its log-time
order holds.
- `ds.schema()` comes from the first chunk that holds a selected
message. A field that first appears later is missing from it; the
docstring says so.

## Related issues

- Related to #65640 and #65641.
- No open PR moves MCAP to Datasource V2. #66524 (ORC) and #66182
(Iceberg) are other formats moving to V2.
- The generated-ID stack (#65886 to #65888) is orthogonal.
- `log_time_order` comes from my #66653, now closed. My legacy-path PRs
#65912, #65913 and #65916 are closed in favour of this one.

## Additional information

```
PYTHONPATH=python/ray/data/tests pytest python/ray/data/tests/datasource/test_mcap.py \
  python/ray/data/tests/unit/datasource_v2/test_mcap_*.py \
  python/ray/data/tests/unit/datasource_v2/test_online_bin_packer.py
```

178 passed, 3 skipped. pre-commit and pyrefly clean.

AI assistance (Claude Code) was used to draft this change; I reviewed
every line and ran the tests above locally.

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

---------

Signed-off-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Claude Fable 5.1 <[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.

1 participant