Skip to content

[Data] Export task USS distribution metrics - #65666

Merged
bveeramani merged 4 commits into
ray-project:masterfrom
viiccwen:data-max-uss-grafana
Aug 27, 2026
Merged

bveeramani merged 4 commits into
ray-project:masterfrom
viiccwen:data-max-uss-grafana

Conversation

@viiccwen

@viiccwen viiccwen commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Expose the existing OpRuntimeMetrics.max_uss_bytes distribution as the Prometheus Gauges ray_data_max_uss_bytes_mean and ray_data_max_uss_bytes_max, and add a "Max Task USS Memory per Operator" chart to the Ray Data dashboard's Tasks row.

Ray Data already records each completed task's peak unique set size (USS) in a DistributionTracker, but this distribution was marked as unsupported for Prometheus export. Consequently, users couldn't query the operator-level maximum from Prometheus or visualize it in the Data dashboard.

This change introduces MetricsType.Distribution. A distribution metric declares which summary statistics to export through the existing metrics_args metadata; _StatsActor creates one Gauge per statistic and updates those Gauges from the serialized distribution snapshot. max_uss_bytes exports mean and max.

The previous scalar average_max_uss_per_task property is removed. The issue_detector now reads max_uss_bytes.mean directly.

Additional information

image

@viiccwen
viiccwen requested a review from a team as a code owner August 22, 2026 07:44

@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 new Distribution metric type to track task metrics like max_uss_bytes (with both mean and max statistics) instead of just a single average value. It updates the Prometheus exporter, Grafana dashboard panels, and high memory detector to support these distribution metrics, while removing the deprecated average_max_uss_per_task metric. The review feedback suggests improving the early-return guard in _record for dictionary-based metrics to ensure that metrics without a num_samples key are not accidentally ignored.

Comment thread python/ray/data/_internal/stats.py Outdated
Comment on lines +761 to +762
if value.get("num_samples", 0) == 0:
return

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

Using value.get('num_samples', 0) == 0 to guard against empty distributions will cause the function to return early and silently ignore any future dictionary-based metrics that do not contain a 'num_samples' key (since it defaults to 0).

To make this more robust and future-proof, consider explicitly checking if 'num_samples' is present in the dictionary and is equal to 0 before returning early.

Suggested change
if value.get("num_samples", 0) == 0:
return
if 'num_samples' in value and value['num_samples'] == 0:
return

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Aug 22, 2026

@bveeramani bveeramani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice

Comment thread python/ray/data/tests/test_stats.py Outdated


def test_stats_actor_exports_distribution_metrics():
actor = _StatsActor.__ray_metadata__.modified_class()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does this statement do/what's it intent?

I've never seen either __ray_metadata__ or modified_class()

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.

Bcz _StatsActor is wrapped by @ray.remote, so it can't be instantiated locally like a regular python class. I found __ray_metadata__ gives the underlying python class, and () create a local instance so we can patch and verify the Gauges at same time.

This follow the same pattern like:

def test_update_iteration_metrics_exports_new_iter_metrics(
split_index_arg, expected_split_label
):
stats = DatasetStats(metadata={}, parent=None)
stats.iter_total_s.add(11.0)
stats.iter_blocked_production_wait_s.add(1.0)
stats.iter_blocked_data_transfer_s.add(1.5)
stats.iter_blocked_batching_s.add(2.0)
stats.iter_blocked_format_s.add(3.0)
stats.iter_blocked_collate_s.add(4.0)
stats.iter_blocked_finalize_s.add(5.0)
stats.iter_batches_total = 7
stats.iter_rows_total = 8
actor = _StatsActor.__ray_metadata__.modified_class.__new__(
_StatsActor.__ray_metadata__.modified_class
)
recorded = {}

If needed, I can add a comment to explain it, Instantiate the Python class wrapped by @ray.remote locally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

__ray_metadata__ is a private dunder attribute that seems extremely brittle. The test_stats example was contributed by an external contributor, but even if it were contributed by a Ray committer, I think we should strongly avoid magic attributes in our tests because they're hard to read and maintain

Could we remove this for now, and re-add tests if we refactor the source code to make it more testable?

Since this is non-critical piece of code (a chart for observability), I would rather err on the side of less coverage + readable tests than more coverage + brittle tests

Comment thread python/ray/data/tests/test_stats.py Outdated
assert update_fn.call_args_list[-1].args[2] is None


def test_stats_actor_exports_distribution_metrics():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I had difficulty reading this test and confirming that this will effectively verify the behavior we care about.

Is there an easier way to test the same logic? I know StatsActor isn't really testable to begin with, so there might not be an easy way

@viiccwen viiccwen Aug 24, 2026 •

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.

Agreed. This test verifies 3 things,

  1. the actor creates mean / max Gauges
  2. empty distribution snapshots don't overwrite them
  3. non-empty snapshots update both Gauges with the expected tags.

I could add comments to explain it, WDYT?

And I explain the initialize way of StatsActor above.

Comment thread python/ray/data/_internal/stats.py Resolved
Comment thread python/ray/data/tests/test_stats.py Outdated


def test_stats_actor_exports_distribution_metrics():
actor = _StatsActor.__ray_metadata__.modified_class()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

__ray_metadata__ is a private dunder attribute that seems extremely brittle. The test_stats example was contributed by an external contributor, but even if it were contributed by a Ray committer, I think we should strongly avoid magic attributes in our tests because they're hard to read and maintain

Could we remove this for now, and re-add tests if we refactor the source code to make it more testable?

Since this is non-critical piece of code (a chart for observability), I would rather err on the side of less coverage + readable tests than more coverage + brittle tests

Comment thread python/ray/data/_internal/stats.py Resolved
@viiccwen
viiccwen force-pushed the data-max-uss-grafana branch from 2b3e72c to 1edae2d Compare August 26, 2026 08:43
@bveeramani bveeramani added the go add ONLY when ready to merge, run all tests label Aug 27, 2026
@bveeramani
bveeramani enabled auto-merge (squash) August 27, 2026 02:52
@bveeramani
bveeramani merged commit 0e48816 into ray-project:master Aug 27, 2026
8 checks passed
@viiccwen
viiccwen deleted the data-max-uss-grafana branch August 27, 2026 07:38
bveeramani pushed a commit that referenced this pull request Sep 17, 2026
## Description

Refactor Ray Data's Prometheus metric export logic so distribution
metrics can be tested without accessing the private `__ray_metadata__`
attribute of the remote StatsActor.

The changes:
- Define the dictionary schema returned by
`DistributionTracker.as_dict()`.
- Extract Prometheus metric creation and recording into helper
functions.
- Add direct tests for creating and updating distribution Gauges. This
is a follow-up to #65666.
- Use real `Gauge` instances in the metric creation test and inspect
their public `info` property.

## Duplicate work

This PR is a focused follow-up to #65666 and does not duplicate another
implementation.

## Tests

- `pytest python/ray/data/tests/test_stats.py -q -k
'test_create_distribution_prometheus_metric or
test_record_distribution_prometheus_metric'` — 2 passed, 108 deselected.
- `pre-commit run --files python/ray/data/tests/test_stats.py` — passed.

AI assistance was used. The submitter reviewed the changes and test
results.

Signed-off-by: viiccwen <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## Description

Expose the existing `OpRuntimeMetrics.max_uss_bytes` distribution as the
Prometheus Gauges `ray_data_max_uss_bytes_mean` and
`ray_data_max_uss_bytes_max`, and add a "Max Task USS Memory per
Operator" chart to the Ray Data dashboard's Tasks row.

Ray Data already records each completed task's peak unique set size
(USS) in a `DistributionTracker`, but this distribution was marked as
unsupported for Prometheus export. Consequently, users couldn't query
the operator-level maximum from Prometheus or visualize it in the Data
dashboard.

This change introduces `MetricsType.Distribution`. A distribution metric
declares which summary statistics to export through the existing
`metrics_args` metadata; `_StatsActor` creates one Gauge per statistic
and updates those Gauges from the serialized distribution snapshot.
`max_uss_bytes` exports `mean` and `max`.

The previous scalar `average_max_uss_per_task` property is removed. The
`issue_detector` now reads `max_uss_bytes.mean` directly.

## Additional information

<img width="1440" height="778" alt="image"
src="https://github.com/user-attachments/assets/d9dc583e-6db3-42a2-b12d-dc9a8e490b27"
/>

---------

Signed-off-by: viiccwen <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…#65750)

## Description

Refactor Ray Data's Prometheus metric export logic so distribution
metrics can be tested without accessing the private `__ray_metadata__`
attribute of the remote StatsActor.

The changes:
- Define the dictionary schema returned by
`DistributionTracker.as_dict()`.
- Extract Prometheus metric creation and recording into helper
functions.
- Add direct tests for creating and updating distribution Gauges. This
is a follow-up to ray-project#65666.
- Use real `Gauge` instances in the metric creation test and inspect
their public `info` property.

## Duplicate work

This PR is a focused follow-up to ray-project#65666 and does not duplicate another
implementation.

## Tests

- `pytest python/ray/data/tests/test_stats.py -q -k
'test_create_distribution_prometheus_metric or
test_record_distribution_prometheus_metric'` — 2 passed, 108 deselected.
- `pre-commit run --files python/ray/data/tests/test_stats.py` — passed.

AI assistance was used. The submitter reviewed the changes and test
results.

Signed-off-by: viiccwen <[email protected]>
chia7712 pushed a commit to opensource4you/readme that referenced this pull request Oct 23, 2026
Add a nomination under `fellowship/ray/`

Suggested level: 30k

Highlights from 2026-09-01 to 2026-10-11:

- [Contribution](https://github.com/opensource4you/readme/blob/main/fellowship/ray/fellow-contribution/viiccwen/September/report.md)
- Merged 10 Ray Data PRs and filed 8 issues in 6 weeks
- Made shuffle tasks up to 12.84x faster by pinning the fused map function, so large UDF closures are no longer serialized again for every shuffle task ([#65480](ray-project/ray#65480))
- Built a memory observability series to help users avoid OOM: exported task peak USS and logical memory metrics with Grafana charts ([#65666](ray-project/ray#65666), [#66044](ray-project/ray#66044)), added a warning with a suggested memory value when it is set below 1.25x max USS ([#65512](ray-project/ray#65512)), and updated the OOM prevention docs ([#65509](ray-project/ray#65509))
- Found, reported and fixed silent data loss in hash-shuffle (V2) anti joins, where rows were dropped when the other side was empty and had an unknown schema ([#66126](ray-project/ray#66126))
- Reviewed other fellows' PRs with suggestions ([#66878](ray-project/ray#66878))
- Merged the most PRs and filed the most issues of all Ray Fellowship participants
- Solid work for an early contributor
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 go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants