Skip to content

[Data] Add batch_size to MCAPDatasource for streaming reads - #65787

Closed
lorenzozanee wants to merge 2 commits into
ray-project:masterfrom
lorenzozanee:fix/mcap-batch-size
Closed

lorenzozanee wants to merge 2 commits into
ray-project:masterfrom
lorenzozanee:fix/mcap-batch-size

Conversation

@lorenzozanee

Copy link
Copy Markdown

Description

MCAPDatasource._read_stream previously accumulated all messages of a file into a single block and yielded once at the end. For large MCAP recordings this makes peak memory scale with file size times concurrent read tasks and prevents downstream stages from overlapping with the read within a file.

This change adds an optional batch_size (message count) to MCAPDatasource and ray.data.read_mcap. When set, _read_stream yields one block per batch instead of one block per file. batch_size=None (default) keeps the current behavior exactly; output schema and predicate pushdown are unchanged, only block boundaries change. The implementation follows the existing Iterator[Block] protocol already used by CSV/JSON/Parquet datasources and aligns with Daft's read_mcap(batch_size=1000) precedent.

Related issues

Closes #65641

No open PR currently addresses this issue. The change is limited to the MCAP datasource and its public read API.

Additional information

  • Validation: stubbed local reproduction with mcap 1.4.0 + pyarrow 25.0.1 confirms batch boundaries (5 messages: None -> [5], 2 -> [2,2,1], 1 -> [1,1,1,1,1]), invalid batch_size raises ValueError, empty file yields 0 blocks, topic-filtered reads respect filtered count, and schemas are stable across batches. python -m py_compile and git diff --check pass.
  • Compatibility: default None preserves byte-identical behavior; existing test_mcap.py expectations remain valid. Full test_mcap.py suite requires a complete Ray build and is expected to run in CI.

MCAPDatasource previously buffered an entire file into a single block
before yielding, which makes peak memory scale with file size and
prevents downstream operators from overlapping with the read within a
file. Add an optional batch_size to MCAPDatasource and
ray.data.read_mcap. When set, _read_stream yields one block per batch
(counted after filtering) and reduces peak memory to one batch.
Default None preserves the existing single-block behavior.

Closes #65641

Signed-off-by: lorenzozanee <[email protected]>
@lorenzozanee
lorenzozanee requested a review from a team as a code owner August 29, 2026 15:10

@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 adds a batch_size parameter to read_mcap and MCAPDatasource, allowing users to configure the number of messages per output block rather than yielding one block per file. Feedback on this change highlights a potential issue where boolean values (which are subclasses of int in Python) could bypass the type validation and be treated as a batch size of 1, potentially causing performance degradation. It is recommended to explicitly exclude boolean types from the batch_size validation.

Comment on lines +125 to +128
if not isinstance(batch_size, int) or batch_size <= 0:
raise ValueError(
f"batch_size must be a positive integer, got {batch_size!r}"
)

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

In Python, bool is a subclass of int. Therefore, passing batch_size=True will pass the isinstance(batch_size, int) check and bypass the batch_size <= 0 check (since True > 0). This would result in a batch_size of 1 (as True behaves as 1 in numeric contexts), causing the reader to yield a block for every single message. For large MCAP files, this can lead to severe performance degradation, high memory overhead, or driver OOM due to millions of tiny blocks.

To prevent this, explicitly exclude bool types from the validation.

Suggested change
if not isinstance(batch_size, int) or batch_size <= 0:
raise ValueError(
f"batch_size must be a positive integer, got {batch_size!r}"
)
if not isinstance(batch_size, int) or isinstance(batch_size, bool) or batch_size <= 0:
raise ValueError(
f"batch_size must be a positive integer, got {batch_size!r}"
)

MCAPDatasource previously buffered an entire file into a single block
before yielding, which makes peak memory scale with file size and
prevents downstream operators from overlapping with the read within a
file. Add an optional batch_size to MCAPDatasource and
ray.data.read_mcap. When set, _read_stream yields one block per batch
(counted after filtering) and reduces peak memory to one batch.
Default None preserves the existing single-block behavior.

Closes #65641

Signed-off-by: lorenzozanee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] MCAPDatasource: batched (streaming) message reads to reduce peak memory and enable intra-file pipelining

1 participant