[Autoscaler] Handle deleted node types in metrics reporter#64184
Conversation
There was a problem hiding this comment.
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.
| status_count = status_count_by_type.setdefault( | ||
| instance.instance_type, _new_status_count() | ||
| ) |
There was a problem hiding this comment.
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.
| 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>
b8e42cc to
c949b41
Compare
|
Addressed the review feedback in 6743ebc: replaced |
Signed-off-by: Kevin-Li-2025 <2242139@qq.com>
6743ebc to
58da2d1
Compare
|
I checked the failing |
|
I checked the new failing |
…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>
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:
Fixes #64177.
Validation
python3 -m py_compile python/ray/autoscaler/v2/metrics_reporter.py python/ray/autoscaler/v2/tests/test_metrics_reporter.pypytest -q python/ray/autoscaler/v2/tests/test_metrics_reporter.pyis blocked in this local source checkout because Ray is not built andray._rayletis unavailable.