ray icon indicating copy to clipboard operation
ray copied to clipboard

[Data] performance regression in ray data scheduling loop caused by per object backpressure RPC

Open chaoqin-li1123 opened this issue 4 weeks ago • 5 comments

What happened + What you expected to happen

We observe performance regression in streaming executor loop caused by the new backpressure RPC implementation https://github.com/ray-project/ray/pull/63310, each next_sync call now invoke UpdateGeneratorBackpressureConsumed call stack, which contribute to around 20% of the driver cpu time in our use case. I wonder if it is possible to apply some optimization to avoid this excessive overhead

Image

Versions / Dependencies

Ray 2.58

Reproduction script

import time

import numpy as np

import ray
from ray.data import ActorPoolStrategy, DataContext
from ray.data.datasource import DummyOutputDatasink
from ray.data.context import AutoscalingConfig

ray.init(log_to_driver=False)

NUM_BLOCKS = 3_000_000
NUM_OPS = 5 
CONCURRENCY = 10000 
TASK_DURATION_S = 100 
BLOCK_SIZE = 256 * 1024 * 1024 
FANOUT = 100

SEED_BLOCKS = max(1, NUM_BLOCKS // FANOUT)

ctx = DataContext.get_current()
ctx.target_max_block_size = BLOCK_SIZE
ctx.max_errored_blocks = 10000


def make_block():
    import pyarrow as pa
    return pa.table({"payload": pa.array(np.zeros(BLOCK_SIZE, dtype=np.uint8))})


class Sleep:
    def __call__(self, batch):
        time.sleep(TASK_DURATION_S)
        return batch


class FanOutAndSleep:
    def __init__(self, fanout: int, sleep_s: int):
        self.fanout = fanout
        self.sleep_s = sleep_s

    def __call__(self, batch):
        for _ in range(self.fanout):
            time.sleep(self.sleep_s)
            yield make_block()  


ds = ray.data.range(SEED_BLOCKS, override_num_blocks=SEED_BLOCKS, concurrency=1000)


ds = ds.map_batches(
    FanOutAndSleep,
    fn_constructor_kwargs={"fanout": FANOUT, "sleep_s": TASK_DURATION_S},
    batch_size=None,  # 1 seed block = 1 task
    batch_format="pyarrow",
    concurrency=CONCURRENCY,
    num_cpus=0.6,
)


for _ in range(NUM_OPS - 1):
    ds = ds.map_batches(
        Sleep,
        batch_size=None,
        batch_format="pyarrow",
        zero_copy_batch=True,
        concurrency=CONCURRENCY,
        num_cpus=0.6,
    )

for _ in ds.iter_internal_ref_bundles():
    pass

Issue Severity

No response

chaoqin-li1123 avatar Nov 05 '26 12:11 chaoqin-li1123

@sampan-s-nayak I put the repro script in this issue, you can try reproduce with smaller number of actor which yield blocks faster

chaoqin-li1123 avatar Nov 05 '26 12:11 chaoqin-li1123

@sampan-s-nayak If this is open for contribution I would like to pick this up

vaishdho1 avatar Nov 24 '26 18:11 vaishdho1

@vaishdho1 I raised https://github.com/ray-project/ray/pull/67160 as a short term fix for this, we can try and see if we can improve perf even more

sampan-s-nayak avatar Nov 25 '26 05:11 sampan-s-nayak

@sampan-s-nayak I tried coalescing the backpressure consumed updates on the owner, so that only the latest count is sent to the executor instead of one RPC per read.

If a send for that generator and executor is still queued on io_service_, a new read just bumps the count in that queued send instead of posting another one. It never waits on replies, so an update never goes out later than it does today.

I used your #67160 microbenchmark (8 generators × 20k items, timing _next_sync), and also measured driver CPU across all threads:

µs/read (reading thread) driver CPU µs/read RPCs/read
master, bp off 4.0 3.9 0
master, bp on 6.5 36.0 1.0
coalescing, bp on 5.2 13.5 0.09

On a scaled down version of this issue's Ray Data repro (5 map_batches stages × 4 actors, ~1 KB blocks):

master coalescing
backpressure RPCs per item report ~1.4 ~0.85
driver CPU per bundle (bp on, never blocking) 8,516 µs 8,157 µs
total job time, (4 objects, workers actually pause) 760 s 742 s

How much it merges depends on how busy io_service_ is. When it's idle, every read still sends right away, same as today. Even at ~17% busy in my Ray Data runs, ~40% of sends were saved, mostly because Ray Data reads a block and its metadata back to back.

This was all on one MacBook Pro (M2 Pro, 10 cores, 16 GB), where about half of driver CPU is Ray Data's own scheduling loop, so the overall gain is small. I'm not sure yet how it holds at the scale this was reported.

If this looks good to you, I can open a PR with this change, or is there another direction you think we should pursue?

vaishdho1 avatar Dec 03 '26 01:12 vaishdho1

@vaishdho1 thanks for digging deeper, the poc numbers look good, can we ensure that coalescing does not cause problems when using actor bp (_generator_backpressure_num_objects sets per streaming gen task bp, _actor_generator_backpressure_num_objects sets a per actor bp, when max_concurrency > 1 every streaming gen task will share the backpressure count and coalescing may slow down reporting). apart from that I think the direction seems right would love to look into the implementation and try this out on a larger (multi node cluster)

cc: @rueian incase you have any thoughts on this as well

sampan-s-nayak avatar Dec 03 '26 05:12 sampan-s-nayak