Repository navigation
[Data] [Core] [2.5/n] Remove BlockRefCounter.clear() and expand callback throughput benchmark - #64521
Conversation
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
There was a problem hiding this comment.
Pull request overview
Adds a symmetric “remove” API for Ray Core’s object out-of-scope/freed callback mechanism so callers (notably Python) can explicitly deregister callbacks and trigger their cleanup logic deterministically, rather than relying on callbacks becoming lazy no-ops.
Changes:
- Introduces
ReferenceCounterInterface::RemoveObjectOutOfScopeOrFreedCallbacksand implements it inReferenceCounter. - Plumbs the API through
CoreWorkerand exposes it to Python vialibcoreworker.pxdand_raylet.pyx. - Adds a C++ unit test covering “remove fires once; natural out-of-scope does not fire again”.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/ray/core_worker/tests/reference_counter_test.cc | Adds a unit test validating removal fires callbacks exactly once. |
| src/ray/core_worker/reference_counter.h | Declares the new removal API on the concrete reference counter. |
| src/ray/core_worker/reference_counter.cc | Implements callback firing + clearing for the new removal API. |
| src/ray/core_worker/reference_counter_interface.h | Adds the new API to the core reference counter interface and documents behavior. |
| src/ray/core_worker/core_worker.h | Declares a CoreWorker wrapper for the new removal API. |
| src/ray/core_worker/core_worker.cc | Implements the CoreWorker wrapper by delegating to the reference counter. |
| python/ray/includes/libcoreworker.pxd | Exposes the CoreWorker removal method to Cython. |
| python/ray/_raylet.pyx | Adds a Python-facing internal method to trigger callback removal/cleanup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Removes and fires all out-of-scope/freed callbacks for `object_id`. | ||
| /// Firing allows per-callback cleanup (e.g. Py_DECREF on the Python side). | ||
| /// After this call, no further out-of-scope callbacks will fire for this object. |
| """Remove and fire all out-of-scope callbacks for the given object. | ||
|
|
||
| Firing allows per-callback cleanup (e.g. Py_DECREF). After this | ||
| call, no further out-of-scope callbacks will fire for this object. | ||
|
|
clear is Called
clear is Calledclear is Called
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
200944a to
c3a6c68
Compare
Kunchd
left a comment
There was a problem hiding this comment.
I don't have any big concerns with this PR, but I'm struggling to determine the use case for this. We currently clean up the callback when the object goes out of scope and the callback is invoked. Why is explicit clean up necessary? Could we include the motivation in the PR description?
| for (const auto &callback : it->second.on_object_out_of_scope_or_freed_callbacks) { | ||
| callback(object_id); | ||
| } |
There was a problem hiding this comment.
Why is invoking all the callbacks neecessary before removing the callbacks? Doesn't removing the callback mean we don't care about them anymore?
There was a problem hiding this comment.
BlockRefCounter's clear() is called when StreamingExecutor calls shutdown(), which happens when a dataset execution ends. In multi-epoch training (or any jobs with many dataset executions), this would be triggered many times. Since the callbacks have Py_DECREF to preserve the _on_object_freed closures, not triggering them would result in minor leaks. What's your take on this?
There was a problem hiding this comment.
Do we expect object produced within a specific epoch to stay around after the epoch finishes?
rayhhome
left a comment
There was a problem hiding this comment.
This PR is mainly for aligning the behavior of clear() with what the function name suggests. In the current implementation, only the Data side dictionary is cleared when clear() is called. The Core callbacks will still be fired when ObjectRefs go out of scope, which seems unintuitive based on what the function is intended to do.
However, I think this does raise the question on whether we need the clear() function at all, considering that no code reads the accounting after the shutdown. Will check this offline and come back with the decision.
| for (const auto &callback : it->second.on_object_out_of_scope_or_freed_callbacks) { | ||
| callback(object_id); | ||
| } |
There was a problem hiding this comment.
BlockRefCounter's clear() is called when StreamingExecutor calls shutdown(), which happens when a dataset execution ends. In multi-epoch training (or any jobs with many dataset executions), this would be triggered many times. Since the callbacks have Py_DECREF to preserve the _on_object_freed closures, not triggering them would result in minor leaks. What's your take on this?
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
clear is CalledBlockRefCounter.clear() and expand callback throughput benchmark
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
| self._clear_topology_queues_post_shutdown(force, exception) | ||
| # Queues have been drained; any remaining Ray Core callbacks that fire | ||
| # after this point should be no-ops. | ||
| self._block_ref_counter.clear() |
There was a problem hiding this comment.
If we keep this code and there are 100k callbacks queued up, what actually happens? Does the ray core thread to fire callbacks add some latency on shutdown? Or is it a daemon thread that exits anyways once our main driver process exits?
There was a problem hiding this comment.
The callback thread is a daemon thread that exits anyways once our main driver process exits.
There was a problem hiding this comment.
Got it, so even if we kept clear(), the callbacks wouldn't block the process exit right?
| """Reset all accounting, e.g. on executor shutdown. | ||
|
|
||
| Any previously registered Ray Core callbacks firing after clear() | ||
| will be silently ignored because _registered_ids is empty. |
There was a problem hiding this comment.
can you add some note about this in the BlockRefCounter top-level docstring?
There was a problem hiding this comment.
Added explanation: the callback-firing thread is a daemon thread in Ray Core and is killed automatically when the driver process exits. Any callbacks firing after shutdown would only decrement counters that are no longer read.
justinvyu
left a comment
There was a problem hiding this comment.
Can you add a summary of the benchmark results in the PR description?
If I have X block frees queued up, what is my expected latency? Aka what is my expected accounting lag?
Kunchd
left a comment
There was a problem hiding this comment.
Thanks for adding the new benchmarks!
| dict mapping id_binary -> per-block drop timestamp. | ||
| """ | ||
| if isinstance(drop_times, dict): | ||
| latencies = [fire_times[id_b] - drop_times[id_b] for id_b in id_binaries] |
There was a problem hiding this comment.
Is it possible for us to get negative latency here? if the while loop processing the consumer completion can't keep up with the object freeing speed?
There was a problem hiding this comment.
I think we'll never see negative latency here; the drop_times timestamps are recorded before their corresponding del ref, and the callback can only fire after the ObjectRef is deleted.
| return results | ||
|
|
||
|
|
||
| reg = _run_at_scales("Registration cost", test_registration_cost, SCALES) |
There was a problem hiding this comment.
Do we want a new cluster per test? Otherwise subsequent runs might suffer if previous runs aren't cleaned up properly or something broke.
There was a problem hiding this comment.
I think it might be unnecessary? each test waits for all its callbacks to fire before returning (except for test_registration_cost, which I patched in new commit), and the tests also use distinct ObjectRefs from each other so there's no cross-contamination.
Signed-off-by: Sirui Huang <[email protected]>
rayhhome
left a comment
There was a problem hiding this comment.
Thanks a lot for the reviews! Added a commit addressing them.
| """Reset all accounting, e.g. on executor shutdown. | ||
|
|
||
| Any previously registered Ray Core callbacks firing after clear() | ||
| will be silently ignored because _registered_ids is empty. |
There was a problem hiding this comment.
Added explanation: the callback-firing thread is a daemon thread in Ray Core and is killed automatically when the driver process exits. Any callbacks firing after shutdown would only decrement counters that are no longer read.
| self._clear_topology_queues_post_shutdown(force, exception) | ||
| # Queues have been drained; any remaining Ray Core callbacks that fire | ||
| # after this point should be no-ops. | ||
| self._block_ref_counter.clear() |
There was a problem hiding this comment.
The callback thread is a daemon thread that exits anyways once our main driver process exits.
| dict mapping id_binary -> per-block drop timestamp. | ||
| """ | ||
| if isinstance(drop_times, dict): | ||
| latencies = [fire_times[id_b] - drop_times[id_b] for id_b in id_binaries] |
There was a problem hiding this comment.
I think we'll never see negative latency here; the drop_times timestamps are recorded before their corresponding del ref, and the callback can only fire after the ObjectRef is deleted.
| return results | ||
|
|
||
|
|
||
| reg = _run_at_scales("Registration cost", test_registration_cost, SCALES) |
There was a problem hiding this comment.
I think it might be unnecessary? each test waits for all its callbacks to fire before returning (except for test_registration_cost, which I patched in new commit), and the tests also use distinct ObjectRefs from each other so there's no cross-contamination.
…lback throughput benchmark (ray-project#64521) - Remove `BlockRefCounter.clear()` and its shutdown wiring - Expand callback throughput release benchmark --------- Signed-off-by: Sirui Huang <[email protected]>
…lback throughput benchmark (ray-project#64521) - Remove `BlockRefCounter.clear()` and its shutdown wiring - Expand callback throughput release benchmark --------- Signed-off-by: Sirui Huang <[email protected]> Signed-off-by: 400Ping <[email protected]>
ray-project#64521 removed `clear()`, but two comments still explain the `_registered_ids` membership check in terms of it, pointing a reader at a method that is no longer there. With `clear()` gone the check guards a repeat notification for an ID that is already gone, so say that instead. Comment-only, and bundled here because this PR is already editing the file. Signed-off-by: Marwan Sarieddine <[email protected]>
ray-project#64521 removed `clear()`, but two comments still explain the `_registered_ids` membership check in terms of it, pointing a reader at a method that is no longer there. With `clear()` gone the check guards a repeat notification for an ID that is already gone, so say that instead. Comment-only, and bundled here because this PR is already editing the file. Signed-off-by: Marwan Sarieddine <[email protected]>
Reverts 6dc81a0 to keep this PR to the bug fix alone. Those two comments do name a method that ray-project#64521 removed, so the cleanup is correct on master. But it is unrelated to the crash, and on releases/2.57.0 -- the earliest branch this fix has to reach, since BlockRefCounter arrived in 2.57 -- `clear()` still exists. Reworded there, the comments would be wrong. Leaving them untouched keeps the backport a single hunk-for-hunk cherry-pick with nothing a backporter has to remember to drop. Signed-off-by: Marwan Sarieddine <[email protected]>
…lback throughput benchmark (ray-project#64521) - Remove `BlockRefCounter.clear()` and its shutdown wiring - Expand callback throughput release benchmark --------- Signed-off-by: Sirui Huang <[email protected]>
Summary
BlockRefCounter.clear()and its shutdown wiringDescription
Removes
BlockRefCounter.clear()and the_block_ref_counter.clear()call inStreamingExecutor.shutdown(). Theclear()method was added for deterministic cleanup on executor shutdown, but profiling and benchmarking showed:clear()had no measurable impact on release test benchmarks.Also expands
test_callback_throughput.py(previously only had an incremental pipeline test) with additional benchmark tests at multiple scales (100 to 10,000 blocks):BlockRefCounter.on_block_producedBlockRefCounterPython dict, matching how BRC is used in productionBenchmark results
Averaged across 4 builds (102169, 101688, 101413, 101369). Each latency metric measures the time from ref drop to individual callback firing. p50, p95, p99, and max are all recorded by the benchmark, but p95 is selected here for horizontal comparison.
Note: Registration cost at 1k (32.73us) is inflated by 3 of 4 builds showing ~40us, while the 4th shows 1.8us. Other scales are consistently ~2us, likely a warmup outlier rather than a scaling issue.
Related issues
Depends on #64157 (
BlockRefCounterimplementation).Related to #63601 (prototype), #63074 (previous manual
BlockRefCounter).