ray icon indicating copy to clipboard operation
ray copied to clipboard

[Data] Actor-pool scale-down leaves primary object-store pins, so worker nodes never go idle

Open carlpayne opened this issue 1 month ago • 3 comments

What happened + What you expected to happen

Ray Data actor-pool scale-down drops the actor handle and does not call ray.kill (ActorPoolMapOperator._release_running_actor: remove references so lineage can still reconstruct outputs). The actor then dies with The actor is dead because all references to the actor were removed. Primary copies of its returns stay in that worker's object store. LocalResourceManager::UpdateAvailableObjectStoreMemResource starts the idle clock only when used == 0.0; any leftover byte clears it. GetResourceIdleTime() then returns nullopt, so idle_duration_ms stays 0 and node activity is only Resource: object_store_memory currently in use. CPU is free and there are no workers, so an autoscaler idle timeout never starts.

Expected: once the pool has released the actor and nothing downstream will read its outputs, those ObjectRefs are dropped, object-store use hits 0, and the existing idle timeout removes the node.

A byte threshold, or ray.kill on idle scale-down, would drop the only copy of an object the driver still references. The next fetch fails, or reconstruction runs against an actor that is already dead (constructor args in plasma, #53727). #17718 closed as working as intended: destroying the actor when it goes out of scope can lose objects it owned, so the node should stay until those refs are actually gone.

Reproduced on 2.56.1 with the script below, while the dataset was still at 16/17 with one actor alive:

3 actors DEAD, death cause contains "all references to the actor were removed"; 1 ALIVE

emptied worker: cpu_used=0, object_store_memory used=13639977, idle_duration_ms=0
node_activity=["Resource: object_store_memory currently in use."]

Same pattern on a KubeRay cluster, Ray 2.56.1, during a long map_batches. The pool had scaled from thousands of actors down to a few dozen. Hundreds of worker pods had no Python workers and free CPU, with about 1–3 MiB of primary copies (sample: 7 pinned objects, 1,923,463 primary bytes, is_idle: 0). idle_duration_ms was 0 on every node. Requested CPU stayed ~13k while CPU in use had fallen to ~300. The driver still held the finished returns (LOCAL_REFERENCE, task FINISHED). iter_batches discards each batch; the refs live in Ray Data / the core worker until the dataset finishes. scheduler_report_pinned_bytes_only is true, so these are primary copies, not the secondary-copy case in #21870 / #22020.

Versions / Dependencies

Ray 2.56.1, Python 3.12. Repro on macOS. Production observation on Linux, KubeRay, same Ray version. The two functions above are unchanged on master and in the 2.59.0 sources. The script was run on 2.56.1 only.

Reproduction script

import threading
import time

import ray
from ray.autoscaler.v2.sdk import get_cluster_resource_state
from ray.cluster_utils import Cluster
from ray.data import ActorPoolStrategy
from ray._private.state import actors as gcs_actors

HOLD_S = 45

class Encode:
    def __call__(self, batch):
        if 0 in [int(i) for i in batch["id"]]:
            time.sleep(HOLD_S)
        return {"blob": [bytes(1 << 20) for _ in batch["id"]]}

def used(total, avail, key):
    return float(total.get(key, 0)) - float(avail.get(key, 0))

cluster = Cluster()
cluster.add_node(num_cpus=1, include_dashboard=False)
cluster.add_node(num_cpus=2, resources={"encode": 2}, include_dashboard=False)
cluster.add_node(num_cpus=2, resources={"encode": 2}, include_dashboard=False)
ray.init(address=cluster.address)
ctx = ray.data.DataContext.get_current()
ctx.autoscaling_config.actor_pool_util_upscaling_threshold = 0.8
ctx.autoscaling_config.actor_pool_max_upscaling_delta = 4
ds = ray.data.range(17, override_num_blocks=17).map_batches(
    Encode, batch_size=1, num_cpus=1, resources={"encode": 1},
    compute=ActorPoolStrategy(min_size=1, max_size=4, initial_size=4),
)
release = threading.Event()

def consume():
    for batch in ds.iter_batches(batch_size=1, batch_format="numpy"):
        release.wait(timeout=HOLD_S + 30)
        del batch

threading.Thread(target=consume, daemon=True).start()
deadline = time.monotonic() + 20
while time.monotonic() < deadline:
    time.sleep(2)
    state = get_cluster_resource_state(ray._private.worker.global_worker.gcs_client)
    for node in state.node_states:
        total, avail = dict(node.total_resources), dict(node.available_resources)
        if float(total.get("encode", 0)) <= 0:
            continue
        print(
            "cpu", used(total, avail, "CPU"),
            "obj", used(total, avail, "object_store_memory"),
            "idle_ms", node.idle_duration_ms,
            "activity", list(node.node_activity),
        )
    for info in gcs_actors().values():
        if "MapWorker" in info["ActorClassName"]:
            print(info["State"], info["DeathCause"])
release.set()
ray.shutdown()
cluster.shutdown()

Issue Severity

Medium: It is a significant difficulty but I can work around it.

carlpayne avatar Oct 09 '26 21:10 carlpayne

I would like to pick this up.

vaishdho1 avatar Oct 10 '26 00:10 vaishdho1

Hi @vaishdho1 @carlpayne — checking in since it's been about 7 weeks: @vaishdho1 are you still actively working on this?

I reproduced this on current master (even when the consumer immediately deletes each yielded batch) and traced the remaining driver LOCAL_REFERENCE pins to two places:

  1. Dataset._execute_to_iterator (itertools.chain([next(gen)], gen) retains the first RefBundle for the lifetime of the iterator).
  2. Generator frame locals in block_batching (iter_threaded, resolve_block_refs, prefetch_batches_locally, and BatchIterator._iter_batches) staying bound to the previous block/bundle while blocked waiting on a slow straggler task.

If no one has an active PR in progress, I'd be happy to open a minimal fix with regression tests!

m4ru1 avatar Nov 28 '26 15:11 m4ru1

Opened https://github.com/ray-project/ray/pull/68626 with a minimal fix and single-node + multi-node scale-down regression tests — happy to coordinate or adjust if @vaishdho1 already has work in flight.

m4ru1 avatar Nov 28 '26 17:11 m4ru1