-
Notifications
You must be signed in to change notification settings - Fork 7k
[Java] Support calling Ray APIs from multiple threads #3646
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
11 commits
Select commit
Hold shift + click to select a range
52c2447
Add multi-threading test
81f3edb
Support multi-threading
1dce505
Fix
490cb43
Add thread task id
414f6a0
Fix lint
e8e69d6
Fix
0d408a5
Remove unnecessary comments.
9fc839a
Add wait test in multi threads
5260ce9
Address comments.
5bad6e9
Fix
3bb7816
fi
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
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
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
112 changes: 112 additions & 0 deletions
112
java/test/src/main/java/org/ray/api/test/MultiThreadingTest.java
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,112 @@ | ||
| package org.ray.api.test; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.ray.api.Ray; | ||
| import org.ray.api.RayActor; | ||
| import org.ray.api.RayObject; | ||
| import org.ray.api.WaitResult; | ||
| import org.ray.api.annotation.RayRemote; | ||
|
|
||
|
|
||
| public class MultiThreadingTest extends BaseTest { | ||
|
|
||
| private static final int LOOP_COUNTER = 1000; | ||
| private static final int NUM_THREADS = 20; | ||
|
|
||
| @RayRemote | ||
| public static Integer echo(int num) { | ||
| return num; | ||
| } | ||
|
|
||
| @RayRemote | ||
| public static class Echo { | ||
|
|
||
| @RayRemote | ||
| public Integer echo(int num) { | ||
| return num; | ||
| } | ||
| } | ||
|
|
||
| public static String testMultiThreading() { | ||
| Random random = new Random(); | ||
| // Test calling normal functions. | ||
| runTestCaseInMultipleThreads(() -> { | ||
| int arg = random.nextInt(); | ||
| RayObject<Integer> obj = Ray.call(MultiThreadingTest::echo, arg); | ||
| Assert.assertEquals(arg, (int) obj.get()); | ||
| }, LOOP_COUNTER); | ||
|
|
||
| // Test calling actors. | ||
| RayActor<Echo> echoActor = Ray.createActor(Echo::new); | ||
| runTestCaseInMultipleThreads(() -> { | ||
| int arg = random.nextInt(); | ||
| RayObject<Integer> obj = Ray.call(Echo::echo, echoActor, arg); | ||
| Assert.assertEquals(arg, (int) obj.get()); | ||
| }, LOOP_COUNTER); | ||
|
|
||
| // Test put and get. | ||
| runTestCaseInMultipleThreads(() -> { | ||
| int arg = random.nextInt(); | ||
| RayObject<Integer> obj = Ray.put(arg); | ||
| Assert.assertEquals(arg, (int) Ray.get(obj.getId())); | ||
| }, LOOP_COUNTER); | ||
|
|
||
| // Test wait for one object in multi threads. | ||
| RayObject<Integer> obj = Ray.call(MultiThreadingTest::echo, 100); | ||
| runTestCaseInMultipleThreads(() -> { | ||
| WaitResult<Integer> result = Ray.wait(ImmutableList.of(obj), 1, 1000); | ||
| Assert.assertEquals(1, result.getReady().size()); | ||
| }, 1); | ||
|
|
||
| return "ok"; | ||
| } | ||
|
|
||
| @Test | ||
| public void testInDriver() { | ||
| testMultiThreading(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInWorker() { | ||
| RayObject<String> obj = Ray.call(MultiThreadingTest::testMultiThreading); | ||
| Assert.assertEquals("ok", obj.get()); | ||
| } | ||
|
|
||
| private static void runTestCaseInMultipleThreads(Runnable testCase, int numRepeats) { | ||
| ExecutorService service = Executors.newFixedThreadPool(NUM_THREADS); | ||
|
|
||
| try { | ||
| List<Future<String>> futures = new ArrayList<>(); | ||
| for (int i = 0; i < NUM_THREADS; i++) { | ||
| Callable<String> task = () -> { | ||
| for (int j = 0; j < numRepeats; j++) { | ||
| TimeUnit.MILLISECONDS.sleep(1); | ||
| testCase.run(); | ||
| } | ||
| return "ok"; | ||
| }; | ||
| futures.add(service.submit(task)); | ||
| } | ||
| for (Future<String> future : futures) { | ||
| try { | ||
| Assert.assertEquals(future.get(), "ok"); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Test case failed.", e); | ||
| } | ||
| } | ||
| } finally { | ||
| service.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these also be reset to 0 in
setCurrentTask?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, @jovan-wong can you fix this?