Skip to content

[Data] Request each Parquet read task's estimated decode peak as its logical memory [6/6] - #67841

Open
AarryaSaraf wants to merge 42 commits into
aarrya/decoded-size-5-packingfrom
aarrya/decoded-size-6-memory
Open

AarryaSaraf wants to merge 42 commits into
aarrya/decoded-size-5-packingfrom
aarrya/decoded-size-6-memory

Conversation

@AarryaSaraf

@AarryaSaraf AarryaSaraf commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Stacked on #67840.

With #67840, a DSv2 Parquet read task holds row groups whose estimated decoded sizes sum to about the block target, 128 MiB by default, and each row group carries the memory PyArrow is estimated to peak at decoding it (FileChunk.memory_bytes). The task still asks Ray for the memory it always did: nothing, or with DataContext.default_map_logical_memory_enabled a flat 2.4 GiB per CPU (SAFE_DEFAULT_LOGICAL_MEMORY_PER_CPU), however small its bin. Now each read task requests its bin's summed estimate.

How it works:

  1. The bin packer stamps each bin's manifest with the sum of its runs' memory_bytes, as Arrow schema metadata under ray.data.consumer_memory_bytes (online_bin_packer.py, FileManifest.construct_manifest).
  2. ArrowBlockAccessor.get_metadata reads the stamp into a new field, BlockMetadata.consumer_memory_bytes, so it travels with the listing block's metadata to the driver (BlockMetadataWithSchema carries it).
  3. TaskPoolMapOperator, with the new memory_from_input_estimate flag, sums the estimates over a bundle's blocks, caps the total at the safe default, and passes it as the task's memory in .options(...) (_get_dynamic_ray_remote_args).
  4. plan_read_files_op sets the flag on the read operator.
flowchart TD
    FR["Footer reader"] -->|"each row group's memory_bytes"| BP["Bin packer"]
    BP -->|"one manifest per bin, stamped with the sum of its runs' memory_bytes"| LB["Listing block"]
    LB -->|"ArrowBlockAccessor.get_metadata reads the stamp"| MD["BlockMetadata.consumer_memory_bytes, to the driver"]
    MD -->|"summed over the bundle, capped at 2.4 GiB per CPU"| OP["TaskPoolMapOperator with memory_from_input_estimate"]
    OP -->|".options(memory=estimate)"| RT["Read task"]
    U["memory in ray_remote_args"] -->|"wins: the estimate is not used"| OP
Loading
  • The estimate only replaces a default. A memory the caller set, as in read_parquet(..., ray_remote_args={"memory": ...}), wins, and the flag does nothing. The estimate applies whether or not default_map_logical_memory_enabled is set.
  • No floor, and a cap at the safe default: a bin estimated at 400 MiB requests 400 MiB; a single row group estimated at 3 GiB, which [Data] Pack Parquet read tasks by estimated decoded size in DSv2 [5/6] #67840 packs alone, requests 2.4 GiB with num_cpus=1.
  • A bin holding a whole-file row, or a run without an estimate, carries no stamp, so its task keeps today's request.
  • A read fused with the maps after it keeps the flag and still requests its input's estimate (operator_fusion.py). The estimate covers the read only, not the maps.
  • A task under a PlacementGroupSchedulingStrategy doesn't request the estimate: the group's bundles may hold no memory, so the task would never schedule.
  • The operator's admission figures (incremental_resource_usage, per_task_resource_allocation) stay at the static ray_remote_args: the operator only sees a bundle as it schedules it. Ray Core enforces each task's request when it places the task, and current_logical_usage counts the running tasks' requests.
  • Actor pools ignore the flag: an actor's memory is fixed when it is created.

Before and after, for #67840's bin of 41 row groups of 200,000 rows of an int64 and a double, 127 MiB decoded and 400 MiB of summed memory_bytes: before, its task was submitted with no memory, or with memory=2.4 GiB under the default-memory flag; after, with memory=400 MiB. On a node with 48 GiB of logical memory the flat default admits 20 such tasks at once and the estimate 122.

Tests:

  • tests/unit/test_arrow_block.py: the stamp is read into consumer_memory_bytes.
  • tests/unit/datasource_v2/test_online_bin_packer.py: every bin's stamp equals the sum of its runs' memory_bytes, across bin budgets and with coalesced runs split; a bin with a whole-file row or a run without an estimate has none.
  • tests/test_task_pool_map_operator.py: the estimates sum over a bundle's blocks, cap at the safe default, give way to a memory the caller set, replace the default-memory flag's value, and are skipped under a placement group.
  • tests/test_operator_fusion.py: a read fused with a map keeps the flag.
  • tests/datasource/test_read_parquet_v2.py: end to end, with a one-byte bin budget so every row group is its own task, each task's get_assigned_resources()["memory"] is above 0 and below the safe default.

97 passed in the two unit files on Python 3.10 with PyArrow 23.0.0 and 25.0.1 and on Python 3.12 with PyArrow 26.0.1. The other three files need a cluster and run in CI.

Signed-off-by: Aarrya [email protected]

🤖 Generated with Claude Code

@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 mechanism to dynamically estimate the heap memory required by tasks consuming table blocks and request that memory instead of relying on static defaults. It adds consumer_memory_bytes to BlockMetadata, which is populated from Arrow schema metadata stamped by the bin packer. The TaskPoolMapOperator can now dynamically request this estimated memory, capped at a safe default. The review feedback suggests adding robust error handling when parsing the metadata integer to prevent potential ValueError or TypeError exceptions, and defensively validating that the estimated memory is non-negative to avoid task scheduling issues in Ray.

Comment on lines +387 to +394
schema_metadata = self._table.schema.metadata
if schema_metadata and CONSUMER_MEMORY_BYTES_METADATA_KEY in schema_metadata:
metadata = replace(
metadata,
consumer_memory_bytes=int(
schema_metadata[CONSUMER_MEMORY_BYTES_METADATA_KEY]
),
)

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

Parsing the consumer_memory_bytes metadata value directly with int() can raise a ValueError or TypeError if the metadata is malformed or corrupted. Since this is executed in a critical path, wrapping it in a try-except block ensures robustness by gracefully falling back to None if parsing fails.

Suggested change
schema_metadata = self._table.schema.metadata
if schema_metadata and CONSUMER_MEMORY_BYTES_METADATA_KEY in schema_metadata:
metadata = replace(
metadata,
consumer_memory_bytes=int(
schema_metadata[CONSUMER_MEMORY_BYTES_METADATA_KEY]
),
)
schema_metadata = self._table.schema.metadata
if schema_metadata and CONSUMER_MEMORY_BYTES_METADATA_KEY in schema_metadata:
try:
metadata = replace(
metadata,
consumer_memory_bytes=int(
schema_metadata[CONSUMER_MEMORY_BYTES_METADATA_KEY]
),
)
except (ValueError, TypeError):
logger.warning(
"Failed to parse consumer_memory_bytes from schema metadata: %r",
schema_metadata[CONSUMER_MEMORY_BYTES_METADATA_KEY],
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Only Ray writes this key, so a value that doesn't parse is a bug to surface rather than skip.

Comment on lines +204 to +207
for metadata in bundle.metadata:
if metadata.consumer_memory_bytes is None:
return None
total += metadata.consumer_memory_bytes

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

If consumer_memory_bytes is negative (e.g., due to corrupted metadata or an estimation bug), the total requested memory could become negative, which is invalid and can cause issues when scheduling tasks in Ray. Adding a defensive check to ensure the estimate is non-negative is highly recommended.

Suggested change
for metadata in bundle.metadata:
if metadata.consumer_memory_bytes is None:
return None
total += metadata.consumer_memory_bytes
for metadata in bundle.metadata:
if metadata.consumer_memory_bytes is None or metadata.consumer_memory_bytes < 0:
return None
total += metadata.consumer_memory_bytes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Every estimate is a sum of non-negative byte counts, so it can't go below zero.

@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from d5c0db8 to a97badd Compare November 4, 2026 22:36
@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from a97badd to c448349 Compare November 4, 2026 23:28
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Nov 5, 2026
@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from c448349 to 7775228 Compare November 5, 2026 02:06
@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from 7775228 to 6ab15d1 Compare November 5, 2026 03:46
@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from 6ab15d1 to 6d6e936 Compare November 5, 2026 21:20
@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from 6d6e936 to d8d0b89 Compare November 5, 2026 22:31
@AarryaSaraf
AarryaSaraf force-pushed the aarrya/decoded-size-6-memory branch from d8d0b89 to 23cf682 Compare November 6, 2026 21:10
@AarryaSaraf AarryaSaraf added the go add ONLY when ready to merge, run all tests label Nov 7, 2026
AarryaSaraf and others added 19 commits November 9, 2026 12:00
Signed-off-by: Aarrya <[email protected]>

# Conflicts:
#	python/ray/data/_internal/datasource_v2/common/online_bin_packer.py
#	python/ray/data/_internal/datasource_v2/interfaces/file_manifest.py
#	python/ray/data/block.py
#	python/ray/data/tests/unit/datasource_v2/test_online_bin_packer.py
…her than its packed bytes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aarrya <[email protected]>
…s after it

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Aarrya <[email protected]>
@AarryaSaraf
AarryaSaraf requested a review from a team as a code owner November 17, 2026 22:09
Signed-off-by: Aarrya <[email protected]>

# Conflicts:
#	python/ray/data/_internal/datasource_v2/common/online_bin_packer.py
#	python/ray/data/_internal/execution/operators/map_operator.py
#	python/ray/data/_internal/execution/operators/task_pool_map_operator.py

This branch has not been deployed

No deployments
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