Repository navigation
[Data] Cluster autoscaler requests a bundle larger than the node it was copied from, because node memory is rounded up - #65446
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the memory quantization logic in the cluster autoscaler to round down to the nearest 1 GiB, ensuring that the generated node resource spec never claims more memory than the physical node actually has. This prevents the autoscaler from treating requests as infeasible. Unit tests have been added to verify this behavior. Feedback on the changes suggests restoring a defensive guard to handle negative memory values, as the new floor division logic could otherwise result in negative values and trigger an assertion error.
| # more memory than the node it was derived from: a bundle bigger than | ||
| # its source node doesn't fit on that node type, so the autoscaler would | ||
| # treat the request as infeasible instead of scaling up. | ||
| mem = math.floor(mem / _MEMORY_QUANTIZATION_BYTES) * _MEMORY_QUANTIZATION_BYTES |
There was a problem hiding this comment.
If mem is negative, math.floor(mem / _MEMORY_QUANTIZATION_BYTES) will produce a negative value, which will cause an AssertionError in _NodeResourceSpec.__post_init__ due to the assert self.mem >= 0 check.
The original implementation safely guarded against this by defaulting to 0 when mem <= 0 (using if mem > 0 else 0). We should preserve this defensive guard to handle any unexpected negative or zero memory values robustly.
| mem = math.floor(mem / _MEMORY_QUANTIZATION_BYTES) * _MEMORY_QUANTIZATION_BYTES | |
| mem = math.floor(mem / _MEMORY_QUANTIZATION_BYTES) * _MEMORY_QUANTIZATION_BYTES if mem > 0 else 0 |
There was a problem hiding this comment.
Pull request overview
Fixes a Ray Data cluster autoscaler infeasibility bug where node-derived resource bundles could request more memory than the source node, due to rounding memory up during node-shape quantization. The change ensures autoscaler bundle memory is always <= the node memory it was derived from, preserving schedulability while still grouping similar node shapes.
Changes:
- Quantize node memory down (floor) when constructing
_NodeResourceSpec, using a new_MEMORY_QUANTIZATION_BYTESconstant. - Add unit tests to assert (1)
_NodeResourceSpecnever exceeds input node memory and (2) bundles produced from live node resources fit back onto the source node resources.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/ray/data/_internal/cluster_autoscaler/default_cluster_autoscaler_v2.py |
Floors quantized memory to prevent node-derived bundles from exceeding the source node’s memory and becoming infeasible. |
python/ray/data/tests/test_default_cluster_autoscaler_v2.py |
Adds invariant and end-to-end tests ensuring derived specs/bundles remain schedulable on their originating node shapes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
rebased on current master, applies clean. checked it locally against a commit matched master wheel. the whole file passes with the change, 39 of 39, and the new case fails on master, so the rounding is doing real work rather than restating current behaviour. small one, would appreciate a look when someone has a minute. |
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
Signed-off-by: pjdurden <[email protected]>
|
still relevant, so rebased on current master and it applies clean. the only upstream change to this file since the last rebase was #65317, the autoscaler shutdown timeout warning, which doesn't touch on the negative restating the bug for anyone picking this up cold: a node reporting 14.87 GiB is 15966540922 bytes, and round-to-nearest-0.1-GiB gives a spec of 15998753177, so the bundle is 30.7 MiB larger than the node it was derived from. that bundle doesn't fit the node type it came from, so the autoscaler treats the request as infeasible instead of scaling up. rounding down can only ever under-claim. one behaviour change worth calling out: a node under 1 GiB now quantizes to |
iamjustinhsu
left a comment
There was a problem hiding this comment.
Makes sense, thanks!
…as copied from, because node memory is rounded up (ray-project#65446) # Issue ray-project#65237 — autoscaler requests a bundle larger than the node it was copied from ## 1. Root cause `DefaultClusterAutoscalerV2` builds its autoscaler request out of node *shapes*: `_get_node_resource_spec_and_count()` turns every live worker node's resource dict into a `_NodeResourceSpec`, and `try_trigger_scaling()` sends `spec.to_bundle()` back to the autoscaler as active bundles (one per existing node) and pending bundles (the scale-up delta). `_NodeResourceSpec.of()` quantized memory with a *nearest*-multiple rounding (`python/ray/data/_internal/cluster_autoscaler/default_cluster_autoscaler_v2.py:64` before the fix): ```python mem = int(round(mem / GiB, 1) * GiB) if mem > 0 else 0 ``` The quantization exists so that two nodes of the same instance type that report slightly different physical memory (Ray derives the `memory` resource from memory available at init time, so it jitters) collapse into one shape instead of two. Rounding to the *nearest* 0.1 GiB rounds **up** for anything in the upper half of a bucket. A node reporting 14.87 GiB becomes a 14.9 GiB bundle: strictly larger than the node it was copied from. That bundle does not fit on any node of that type, so the autoscaler treats the demand as infeasible rather than launching the node, and the cluster never scales up (CPU and GPU were already floored, so memory was the only resource with this defect). ## 2. The fix and why Quantize memory **down** instead of to the nearest multiple: ```python mem = math.floor(mem / _MEMORY_QUANTIZATION_BYTES) * _MEMORY_QUANTIZATION_BYTES ``` Rounding down makes `spec.mem <= node_memory` an invariant, which is the property the request actually needs: a bundle must be schedulable on the node shape it describes. The error is now asymmetric in the safe direction — under-requesting memory still lands on a node of that type, over-requesting can never be satisfied. The granularity is a named constant `_MEMORY_QUANTIZATION_BYTES = GiB`. It was widened from 0.1 GiB to 1 GiB because flooring at 0.1 GiB would have *lost* the grouping property the quantization was added for: 14.87 GiB and 14.93 GiB floor to 14.8 and 14.9, i.e. two shapes for one instance type (this is exactly what `test_nodes_with_similar_memory_grouped` covers). A 1 GiB bucket keeps both nodes on the same shape and gives 10x more slack against reported-memory jitter than the original rounding did, while never exceeding the source node. Matching CPU/GPU are still exact, so the coarser memory bucket does not realistically make the request match a different (smaller) instance type. ## 3. Files changed - `python/ray/data/_internal/cluster_autoscaler/default_cluster_autoscaler_v2.py` — added `_MEMORY_QUANTIZATION_BYTES`; `_NodeResourceSpec.of()` now floors memory to it. - `python/ray/data/tests/test_default_cluster_autoscaler_v2.py` — added `test_spec_never_exceeds_node_memory` (parametrized invariant check) and `test_bundles_fit_on_the_nodes_they_were_derived_from` (end-to-end through `_get_node_resource_spec_and_count`, asserting every resource in the emitted bundle fits the source node). ## 4. Risk / uncertainty - Memory demand per node is now under-reported by up to 1 GiB (previously by up to 0.05 GiB, or over-reported by up to 0.05 GiB). Under-reporting is safe for feasibility. It does slightly loosen `cap_resource_request_to_limits`: with a user-configured `resource_limits.memory`, pending bundles are measured with the floored value, so a scale-up can overshoot the configured memory limit by up to 1 GiB per pending bundle (bounded by `cluster_scaling_up_delta`, default 1, times the number of node shapes). The old 0.1 GiB rounding had the same failure mode, an order of magnitude smaller. - Nodes with less than 1 GiB of Ray `memory` now produce `memory: 0` bundles (the old code did this below 0.05 GiB). Such worker nodes are not realistic in an autoscaling Ray Data cluster, and CPU/GPU still carry the demand. - The choice of 1 GiB is a judgement call: it is the smallest round bucket that keeps the existing same-node-type grouping test passing under flooring. A maintainer may prefer a different constant; it is isolated in one named variable. - Head-node-group detection (`node_group_config` shape vs. the running head node's shape) also compares quantized specs. Coarser buckets make that comparison neither better nor worse in a way I could establish, since a configured shape and the reported shape can straddle a bucket boundary at any granularity. ## 5. How I verified it Ray is not installed in this environment (`import ray` fails), so the test suite could not be executed here. Verification done: - Extracted the real `_NodeResourceSpec` class from the patched source with `ast` and exec'd it against stubs, then checked, for `mem` in `{0, 1000 B, 0.5, 14.87, 14.93, 32, 8, 1.999, 2 GiB}`: the quantized value is always `<= mem`, is an `int` (the frozen dataclass asserts this in `__post_init__`), and `of(mem=14.87 GiB) == of(mem=14.93 GiB)` still holds. All passed; the pre-fix code returns 14.9 GiB for a 14.87 GiB node, reproducing the issue. - Hand-checked every existing memory value in `test_default_cluster_autoscaler_v2.py` against the new quantization: the small byte-valued fixtures (1000/2000/3000) already quantized to 0 under the old rounding and still do; the GiB-valued fixtures (2, 4, 8, 32 GiB) are exact multiples and are unchanged, including the `memory: 8.0GiB` log-message assertion. - `grep` confirmed `_NodeResourceSpec` / `_get_node_resource_spec_and_count` have no consumers outside this module and its test file. - `python -m py_compile` on both changed files; no line exceeds the configured ruff `line-length = 88`. Suggested command for a human to run before submitting: ```bash pytest -q python/ray/data/tests/test_default_cluster_autoscaler_v2.py ``` Signed-off-by: pjdurden <[email protected]>
Issue #65237 — autoscaler requests a bundle larger than the node it was copied from
1. Root cause
DefaultClusterAutoscalerV2builds its autoscaler request out of node shapes:_get_node_resource_spec_and_count()turns every live worker node's resource dict into a_NodeResourceSpec, andtry_trigger_scaling()sendsspec.to_bundle()back to theautoscaler as active bundles (one per existing node) and pending bundles (the scale-up
delta).
_NodeResourceSpec.of()quantized memory with a nearest-multiple rounding(
python/ray/data/_internal/cluster_autoscaler/default_cluster_autoscaler_v2.py:64before the fix):
The quantization exists so that two nodes of the same instance type that report slightly
different physical memory (Ray derives the
memoryresource from memory available atinit time, so it jitters) collapse into one shape instead of two.
Rounding to the nearest 0.1 GiB rounds up for anything in the upper half of a
bucket. A node reporting 14.87 GiB becomes a 14.9 GiB bundle: strictly larger than the
node it was copied from. That bundle does not fit on any node of that type, so the
autoscaler treats the demand as infeasible rather than launching the node, and the
cluster never scales up (CPU and GPU were already floored, so memory was the only
resource with this defect).
2. The fix and why
Quantize memory down instead of to the nearest multiple:
Rounding down makes
spec.mem <= node_memoryan invariant, which is the property therequest actually needs: a bundle must be schedulable on the node shape it describes.
The error is now asymmetric in the safe direction — under-requesting memory still lands
on a node of that type, over-requesting can never be satisfied.
The granularity is a named constant
_MEMORY_QUANTIZATION_BYTES = GiB. It was widenedfrom 0.1 GiB to 1 GiB because flooring at 0.1 GiB would have lost the grouping
property the quantization was added for: 14.87 GiB and 14.93 GiB floor to 14.8 and
14.9, i.e. two shapes for one instance type (this is exactly what
test_nodes_with_similar_memory_groupedcovers). A 1 GiB bucket keeps both nodes on thesame shape and gives 10x more slack against reported-memory jitter than the original
rounding did, while never exceeding the source node.
Matching CPU/GPU are still exact, so the coarser memory bucket does not realistically
make the request match a different (smaller) instance type.
3. Files changed
python/ray/data/_internal/cluster_autoscaler/default_cluster_autoscaler_v2.py— added
_MEMORY_QUANTIZATION_BYTES;_NodeResourceSpec.of()now floors memory to it.python/ray/data/tests/test_default_cluster_autoscaler_v2.py— added
test_spec_never_exceeds_node_memory(parametrized invariant check) andtest_bundles_fit_on_the_nodes_they_were_derived_from(end-to-end through_get_node_resource_spec_and_count, asserting every resource in the emitted bundlefits the source node).
4. Risk / uncertainty
0.05 GiB, or over-reported by up to 0.05 GiB). Under-reporting is safe for
feasibility. It does slightly loosen
cap_resource_request_to_limits: with auser-configured
resource_limits.memory, pending bundles are measured with thefloored value, so a scale-up can overshoot the configured memory limit by up to 1 GiB
per pending bundle (bounded by
cluster_scaling_up_delta, default 1, times the numberof node shapes). The old 0.1 GiB rounding had the same failure mode, an order of
magnitude smaller.
memorynow producememory: 0bundles (the oldcode did this below 0.05 GiB). Such worker nodes are not realistic in an autoscaling
Ray Data cluster, and CPU/GPU still carry the demand.
the existing same-node-type grouping test passing under flooring. A maintainer may
prefer a different constant; it is isolated in one named variable.
node_group_configshape vs. the running head node'sshape) also compares quantized specs. Coarser buckets make that comparison neither
better nor worse in a way I could establish, since a configured shape and the reported
shape can straddle a bucket boundary at any granularity.
5. How I verified it
Ray is not installed in this environment (
import rayfails), so the test suite couldnot be executed here. Verification done:
_NodeResourceSpecclass from the patched source withastandexec'd it against stubs, then checked, for
memin{0, 1000 B, 0.5, 14.87, 14.93, 32, 8, 1.999, 2 GiB}: the quantized value is always<= mem, is anint(the frozen dataclass asserts this in__post_init__), andof(mem=14.87 GiB) == of(mem=14.93 GiB)still holds. All passed; the pre-fix codereturns 14.9 GiB for a 14.87 GiB node, reproducing the issue.
test_default_cluster_autoscaler_v2.pyagainst the new quantization: the smallbyte-valued fixtures (1000/2000/3000) already quantized to 0 under the old rounding
and still do; the GiB-valued fixtures (2, 4, 8, 32 GiB) are exact multiples and are
unchanged, including the
memory: 8.0GiBlog-message assertion.grepconfirmed_NodeResourceSpec/_get_node_resource_spec_and_counthave noconsumers outside this module and its test file.
python -m py_compileon both changed files; no line exceeds the configured ruffline-length = 88.Suggested command for a human to run before submitting: