Skip to content

[Autoscaler] Handle deleted node types in metrics reporter#64184

Merged
edoakes merged 3 commits into
ray-project:masterfrom
Kevin-Li-2025:kevin/fix-autoscaler-metrics-deleted-group
Jun 26, 2026
Merged

[Autoscaler] Handle deleted node types in metrics reporter#64184
edoakes merged 3 commits into
ray-project:masterfrom
Kevin-Li-2025:kevin/fix-autoscaler-metrics-deleted-group

Conversation

@Kevin-Li-2025

@Kevin-Li-2025 Kevin-Li-2025 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes autoscaler v2 metrics reporting when historical instance records reference a worker group that has already been removed from the current node type config.

The reporter now:

  • keeps reporting instance gauges for historical node types, so deleted node type labels can be driven back to zero instead of staying stale
  • avoids using missing node type configs when computing resource gauges
  • adds a regression test for deleted node type records

Fixes #64177.

Validation

  • python3 -m py_compile python/ray/autoscaler/v2/metrics_reporter.py python/ray/autoscaler/v2/tests/test_metrics_reporter.py
  • isolated importlib/stub verification of deleted node type metrics behavior

pytest -q python/ray/autoscaler/v2/tests/test_metrics_reporter.py is blocked in this local source checkout because Ray is not built and ray._raylet is unavailable.

@Kevin-Li-2025 Kevin-Li-2025 requested a review from a team as a code owner June 17, 2026 18:09

@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 refactors the metrics reporter to safely handle deleted node types by skipping them during resource aggregation and dynamically initializing status counts for unexpected instance types. It also moves a test helper to the module level and adds a unit test for this scenario. The review feedback highlights a performance concern in report_instances where using dict.setdefault causes redundant dictionary allocations on every iteration, suggesting a more efficient lookup pattern instead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +45 to +47
status_count = status_count_by_type.setdefault(
instance.instance_type, _new_status_count()
)

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 dict.setdefault(key, default) always evaluates the default argument (calling _new_status_count() and allocating a new dictionary) on every iteration of the loop, even if the key already exists in the dictionary. For a large number of instances, this results in unnecessary overhead and garbage collection pressure.

Instead, you can check if the key is present first, or use dict.get() to avoid redundant dictionary allocations.

Suggested change
status_count = status_count_by_type.setdefault(
instance.instance_type, _new_status_count()
)
status_count = status_count_by_type.get(instance.instance_type)
if status_count is None:
status_count = _new_status_count()
status_count_by_type[instance.instance_type] = status_count

Signed-off-by: Kevin-Li-2025 <2242139@qq.com>
@Kevin-Li-2025 Kevin-Li-2025 force-pushed the kevin/fix-autoscaler-metrics-deleted-group branch from b8e42cc to c949b41 Compare June 17, 2026 18:10
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Jun 17, 2026
@Kevin-Li-2025

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in 6743ebc: replaced setdefault(..., _new_status_count()) with a lazy get/insert path, so configured node types no longer allocate a throwaway status dictionary for every instance while preserving support for deleted node types. Validation: git diff --check, py_compile, and Ruff check/format all pass. The focused pytest file cannot import in this source-only checkout because ray._raylet is not built.

Signed-off-by: Kevin-Li-2025 <2242139@qq.com>
@Kevin-Li-2025 Kevin-Li-2025 force-pushed the kevin/fix-autoscaler-metrics-deleted-group branch from 6743ebc to 58da2d1 Compare June 19, 2026 02:00
@rueian rueian self-assigned this Jun 24, 2026
@Kevin-Li-2025

Copy link
Copy Markdown
Contributor Author

I checked the failing buildkite/microcheck status. The failed step is :database: data: arrow v23 tests [g7_s11], running the Ray Data/AIR test matrix (//python/ray/data/... //python/ray/air/...) rather than the autoscaler metrics reporter path changed in this PR. I cannot see a PR-specific failure trace from the public status page; could a maintainer rerun microcheck or share the failed log if there is an actionable failure here?

@rueian rueian added the go add ONLY when ready to merge, run all tests label Jun 24, 2026
@rueian rueian enabled auto-merge (squash) June 25, 2026 23:05
@Kevin-Li-2025

Copy link
Copy Markdown
Contributor Author

I checked the new failing buildkite/premerge run 68952. The single failed step is :ray-serve: serve: serve minimal [g16_s14], running //python/ray/serve/tests/.... This appears unrelated to this PR's autoscaler metrics reporter change path. Could a maintainer rerun the premerge job or share the failing serve log if there is an actionable PR-specific failure?

@edoakes edoakes disabled auto-merge June 26, 2026 19:42
@edoakes edoakes merged commit 13d29c8 into ray-project:master Jun 26, 2026
6 of 7 checks passed
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…ct#64184)

## Summary

Fixes autoscaler v2 metrics reporting when historical instance records
reference a worker group that has already been removed from the current
node type config.

The reporter now:
- keeps reporting instance gauges for historical node types, so deleted
node type labels can be driven back to zero instead of staying stale
- avoids using missing node type configs when computing resource gauges
- adds a regression test for deleted node type records

Fixes ray-project#64177.

## Validation

- `python3 -m py_compile python/ray/autoscaler/v2/metrics_reporter.py
python/ray/autoscaler/v2/tests/test_metrics_reporter.py`
- isolated importlib/stub verification of deleted node type metrics
behavior

`pytest -q python/ray/autoscaler/v2/tests/test_metrics_reporter.py` is
blocked in this local source checkout because Ray is not built and
`ray._raylet` is unavailable.

---------

Signed-off-by: Kevin-Li-2025 <2242139@qq.com>
Co-authored-by: Kevin-Li-2025 <2242139@qq.com>
Co-authored-by: Rueian <rueiancsie@gmail.com>
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 core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Autoscaler v2] metrics_reporter crashes with KeyError when historical instances reference a deleted worker group

3 participants