Skip to content

[Data] Cluster autoscaler requests a bundle larger than the node it was copied from, because node memory is rounded up - #65446

Merged
richardliaw merged 1 commit into
ray-project:masterfrom
pjdurden:fix/65237
Sep 30, 2026

Conversation

@pjdurden

Copy link
Copy Markdown
Contributor

Issue #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):

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:

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:

pytest -q python/ray/data/tests/test_default_cluster_autoscaler_v2.py

@pjdurden
pjdurden requested a review from a team as a code owner August 13, 2026 03:17
Copilot AI lite review requested due to automatic review settings August 13, 2026 03:17

@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 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

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 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.

Suggested change
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

Copilot AI 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.

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_BYTES constant.
  • Add unit tests to assert (1) _NodeResourceSpec never 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.

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

Copy link
Copy Markdown
Contributor Author

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.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

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.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Sep 3, 2026
@pjdurden

pjdurden commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

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 _NodeResourceSpec.

on the negative mem point from the automated review, i left that guard off on purpose. cpu and gpu both go through math.floor with no > 0 guard, so a negative there already trips __post_init__ on master today. mem was the odd one out: the else 0 turned a negative into 0 silently instead of failing the assert self.mem >= 0 the dataclass already declares. putting it back restores that inconsistency and keeps a bad value invisible. in practice mem comes from bundle.get("memory", 0) off the ray node table so it isn't negative anyway, and the zero case the guard actually served is already handled, floor(0) * GiB is 0.

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 memory 0 rather than its rounded size. that is the safe direction, an under-claim is still schedulable where an over-claim isn't, and test_spec_never_exceeds_node_memory covers 0.5 GiB explicitly. if you'd rather keep sub-GiB resolution the quantum is a single constant and i can drop it to 0.1 GiB.

@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Sep 4, 2026

@iamjustinhsu iamjustinhsu 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.

Makes sense, thanks!

@iamjustinhsu iamjustinhsu added the go add ONLY when ready to merge, run all tests label Sep 30, 2026
@richardliaw
richardliaw merged commit e227e00 into ray-project:master Sep 30, 2026
4 checks passed
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…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]>
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 unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants