-
Notifications
You must be signed in to change notification settings - Fork 6.6k
[core] Implement a thread pool and call the CPython API on all threads within the same concurrency group #52575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4aa0cee
update
kevin85421 0a2b539
update
kevin85421 2b692dc
update
kevin85421 1950902
Merge remote-tracking branch 'upstream/master' into laptop-ray3-20250423
kevin85421 e63ba54
add test
kevin85421 f93ee4a
fix lint
kevin85421 26feff9
fix tests
kevin85421 ec0c28b
fix tests
kevin85421 d39ac82
add test / comment
kevin85421 940cc81
debug
kevin85421 6cf1eab
fix lint
kevin85421 5356c7c
update
kevin85421 1130e71
address comments
kevin85421 40c0c04
address comments
kevin85421 a5d444e
Merge remote-tracking branch 'upstream/master' into laptop-ray3-20250423
kevin85421 430ef36
improve readability
kevin85421 fd48457
add timeout
kevin85421 3a641b4
add comment
kevin85421 4f19b33
update tests
kevin85421 8123c32
update tests
kevin85421 77879b7
update tests
kevin85421 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
|
||
import ray | ||
from ray._common.utils import get_or_create_event_loop | ||
from ray._private.test_utils import run_string_as_driver | ||
from ray._private.test_utils import run_string_as_driver, SignalActor | ||
|
||
|
||
# This tests the methods are executed in the correct eventloop. | ||
|
@@ -197,8 +197,12 @@ def get_thread_local(self) -> Tuple[Any, int]: | |
|
||
class TestThreadingLocalData: | ||
""" | ||
This test verifies that synchronous tasks can access thread local data | ||
that was set by previous synchronous tasks. | ||
This test verifies that synchronous tasks can access thread-local data that | ||
was set by previous synchronous tasks when the concurrency group has only | ||
one thread. For concurrency groups with multiple threads, it doesn't promise | ||
access to the same thread-local data because Ray currently doesn't expose APIs | ||
for users to specify which thread the task will be scheduled on in the same | ||
concurrency group. | ||
""" | ||
|
||
def test_tasks_on_default_executor(self, ray_start_regular_shared): | ||
|
@@ -236,6 +240,58 @@ def test_tasks_on_different_executors(self, ray_start_regular_shared): | |
assert value == "f2" | ||
|
||
|
||
def test_multiple_threads_in_same_group(ray_start_regular_shared): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
""" | ||
This test verifies that all threads in the same concurrency group are still | ||
alive from the Python interpreter's perspective even if Ray tasks have finished, so that | ||
thread-local data will not be garbage collected. | ||
""" | ||
|
||
@ray.remote | ||
class Actor: | ||
def __init__(self, signal: SignalActor, max_concurrency: int): | ||
self._thread_local_data = threading.local() | ||
self.signal = signal | ||
self.thread_id_to_data = {} | ||
self.max_concurrency = max_concurrency | ||
|
||
def set_thread_local(self, value: int) -> int: | ||
# If the thread-local data were garbage collected after the previous | ||
# task on the same thread finished, `self.data` would be incremented | ||
# more than once for the same thread. | ||
assert not hasattr(self._thread_local_data, "value") | ||
self._thread_local_data.value = value | ||
self.thread_id_to_data[threading.current_thread().ident] = value | ||
ray.get(self.signal.wait.remote()) | ||
|
||
def check_thread_local_data(self) -> bool: | ||
assert len(self.thread_id_to_data) == self.max_concurrency | ||
assert hasattr(self._thread_local_data, "value") | ||
assert ( | ||
self._thread_local_data.value | ||
== self.thread_id_to_data[threading.current_thread().ident] | ||
) | ||
ray.get(self.signal.wait.remote()) | ||
|
||
max_concurrency = 5 | ||
signal = SignalActor.remote() | ||
a = Actor.options(max_concurrency=max_concurrency).remote(signal, max_concurrency) | ||
|
||
refs = [] | ||
for i in range(max_concurrency): | ||
refs.append(a.set_thread_local.remote(i)) | ||
|
||
ray.get(signal.send.remote()) | ||
ray.get(refs) | ||
|
||
refs = [] | ||
for _ in range(max_concurrency): | ||
refs.append(a.check_thread_local_data.remote()) | ||
|
||
ray.get(signal.send.remote()) | ||
ray.get(refs) | ||
|
||
|
||
def test_invalid_concurrency_group(): | ||
"""Verify that when a concurrency group has max concurrency set to 0, | ||
an error is raised when the actor is created. This test uses | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -576,5 +576,6 @@ ray_cc_library( | |
deps = [ | ||
"//src/ray/util:logging", | ||
"@boost//:asio", | ||
"@boost//:thread", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2017 The Ray Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "ray/core_worker/transport/thread_pool.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <atomic> | ||
#include <boost/thread/latch.hpp> | ||
#include <future> | ||
|
||
namespace ray { | ||
namespace core { | ||
|
||
TEST(BoundedExecutorTest, InitializeThreadCallbackAndReleaserAreCalled) { | ||
kevin85421 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constexpr int kNumThreads = 3; | ||
std::atomic<int> init_count{0}; | ||
std::atomic<int> release_count{0}; | ||
|
||
// The callback increments init_count and returns a releaser that increments | ||
// release_count. | ||
auto initialize_thread_callback = [&]() { | ||
init_count++; | ||
return [&]() { release_count++; }; | ||
}; | ||
|
||
{ | ||
BoundedExecutor executor(kNumThreads, initialize_thread_callback); | ||
// At this point, all threads should have called the initializer. | ||
ASSERT_EQ(init_count.load(), kNumThreads); | ||
ASSERT_EQ(release_count.load(), 0); | ||
|
||
std::atomic<int> task_count{0}; | ||
auto callback = [&]() { | ||
task_count++; | ||
while (task_count.load() < kNumThreads) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
} | ||
}; | ||
|
||
// Make sure all threads can run tasks. | ||
for (int i = 0; i < kNumThreads; i++) { | ||
executor.Post(callback); | ||
} | ||
|
||
// Join the pool, which should call the releasers. | ||
executor.Join(); | ||
ASSERT_EQ(task_count.load(), kNumThreads); | ||
} | ||
// After join, all releasers should have been called. | ||
ASSERT_EQ(release_count.load(), kNumThreads); | ||
} | ||
|
||
TEST(BoundedExecutorTest, InitializationTimeout) { | ||
constexpr int kNumThreads = 3; | ||
|
||
// Create a callback that will hang indefinitely to trigger the timeout | ||
auto initialize_thread_callback = [&]() { | ||
while (true) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
return nullptr; | ||
}; | ||
|
||
// Verify that the constructor fails with the expected error message | ||
EXPECT_DEATH( | ||
BoundedExecutor executor( | ||
kNumThreads, initialize_thread_callback, boost::chrono::milliseconds(10)), | ||
"Failed to initialize threads in 10 milliseconds"); | ||
} | ||
|
||
TEST(BoundedExecutorTest, PostBlockingIfFull) { | ||
constexpr int kNumThreads = 3; | ||
BoundedExecutor executor(kNumThreads); | ||
|
||
boost::latch latch(kNumThreads); | ||
std::atomic<bool> block{true}; | ||
auto callback = [&]() { | ||
latch.count_down(); | ||
while (block.load()) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
} | ||
}; | ||
|
||
for (int i = 0; i < kNumThreads; i++) { | ||
executor.Post(callback); | ||
} | ||
latch.wait(); | ||
|
||
// Submit a new task. It should not run immediately | ||
// because the thread pool is full. | ||
std::atomic<bool> running{false}; | ||
std::promise<void> promise; | ||
std::future<void> future = promise.get_future(); | ||
executor.Post([&]() { | ||
running = true; | ||
promise.set_value(); | ||
}); | ||
|
||
// Make sure the task is not running yet after 50 ms. | ||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
ASSERT_FALSE(running.load()); | ||
|
||
// Unblock the threads. The task should run immediately. | ||
block.store(false); | ||
|
||
// Wait for the task with a timeout | ||
auto status = future.wait_for(std::chrono::milliseconds(500)); | ||
ASSERT_EQ(status, std::future_status::ready) << "Task did not complete within timeout"; | ||
ASSERT_TRUE(running.load()); | ||
|
||
executor.Join(); | ||
} | ||
|
||
} // namespace core | ||
} // namespace ray | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.