mirror of
https://github.com/wassname/ray.git
synced 2026-06-28 15:22:56 +08:00
[Java] Support dynamically defining resources when submitting task. (#3070)
## What do these changes do?
Before this PR, if we want to specify some resources, we must do as following codes:
```java
@RayRemote(Resources={ResourceItem("CPU", 10)})
public static void f1() {
// do sth
}
@RayRemote(Resources={ResourceItem("CPU", 10)})
class Demo {
// sth
}
```
Unfortunately, it's no way for us to create another actor or task with different resources required.
After this PR, the thing will be:
```java
ActorCreationOptions option = new ActorCreationOptions();
option.resources.put("CPU", 4.0);
RayActor<Echo> echo1 = Ray.createActor(Echo::new, option);
option.resources.put("Res-A", 4.0);
RayActor<Echo> echo2 = Ray.createActor(Echo::new, option);
//if we don't specify resource, the resources will be `{"cpu":0.0}` by default.
Ray.call(Echo::echo, echo2, 100);
```
## Related issue number
N/A
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import jdk.nashorn.internal.ir.annotations.Immutable;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -9,7 +11,8 @@ import org.ray.api.RayActor;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.WaitResult;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
import org.ray.api.annotation.ResourceItem;
|
||||
import org.ray.api.options.ActorCreationOptions;
|
||||
import org.ray.api.options.CallOptions;
|
||||
|
||||
/**
|
||||
* Resources Management Test.
|
||||
@@ -17,36 +20,13 @@ import org.ray.api.annotation.ResourceItem;
|
||||
@RunWith(MyRunner.class)
|
||||
public class ResourcesManagementTest {
|
||||
|
||||
@RayRemote(resources = {@ResourceItem(name = "CPU", value = 4),
|
||||
@ResourceItem(name = "GPU", value = 0)})
|
||||
public static Integer echo1(Integer number) {
|
||||
@RayRemote
|
||||
public static Integer echo(Integer number) {
|
||||
return number;
|
||||
}
|
||||
|
||||
@RayRemote(resources = {@ResourceItem(name = "CPU", value = 4),
|
||||
@ResourceItem(name = "GPU", value = 2)})
|
||||
public static Integer echo2(Integer number) {
|
||||
return number;
|
||||
}
|
||||
|
||||
@RayRemote(resources = {@ResourceItem(name = "CPU", value = 2),
|
||||
@ResourceItem(name = "GPU", value = 0)})
|
||||
public static class Echo1 {
|
||||
public Integer echo(Integer number) {
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
@RayRemote(resources = {@ResourceItem(name = "CPU", value = 8),
|
||||
@ResourceItem(name = "GPU", value = 0)})
|
||||
public static class Echo2 {
|
||||
public Integer echo(Integer number) {
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
@RayRemote(resources = {@ResourceItem(name = "RES-A", value = 4)})
|
||||
public static class Echo3 {
|
||||
@RayRemote
|
||||
public static class Echo {
|
||||
public Integer echo(Integer number) {
|
||||
return number;
|
||||
}
|
||||
@@ -54,12 +34,18 @@ public class ResourcesManagementTest {
|
||||
|
||||
@Test
|
||||
public void testMethods() {
|
||||
CallOptions callOptions1 = new CallOptions(ImmutableMap.of("CPU", 4.0, "GPU", 0.0));
|
||||
|
||||
// This is a case that can satisfy required resources.
|
||||
RayObject<Integer> result1 = Ray.call(ResourcesManagementTest::echo1, 100);
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
RayObject<Integer> result1 = Ray.call(ResourcesManagementTest::echo, 100, callOptions1);
|
||||
Assert.assertEquals(100, (int) result1.get());
|
||||
|
||||
CallOptions callOptions2 = new CallOptions(ImmutableMap.of("CPU", 4.0, "GPU", 2.0));
|
||||
|
||||
// This is a case that can't satisfy required resources.
|
||||
final RayObject<Integer> result2 = Ray.call(ResourcesManagementTest::echo2, 200);
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
final RayObject<Integer> result2 = Ray.call(ResourcesManagementTest::echo, 200, callOptions2);
|
||||
WaitResult<Integer> waitResult = Ray.wait(ImmutableList.of(result2), 1, 1000);
|
||||
|
||||
Assert.assertEquals(0, waitResult.getReady().size());
|
||||
@@ -68,28 +54,29 @@ public class ResourcesManagementTest {
|
||||
|
||||
@Test
|
||||
public void testActors() {
|
||||
|
||||
ActorCreationOptions actorCreationOptions1 =
|
||||
new ActorCreationOptions(ImmutableMap.of("CPU", 2.0, "GPU", 0.0));
|
||||
|
||||
// This is a case that can satisfy required resources.
|
||||
RayActor<ResourcesManagementTest.Echo1> echo1 = Ray.createActor(Echo1::new);
|
||||
final RayObject<Integer> result1 = Ray.call(Echo1::echo, echo1, 100);
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
RayActor<Echo> echo1 = Ray.createActor(Echo::new, actorCreationOptions1);
|
||||
final RayObject<Integer> result1 = Ray.call(Echo::echo, echo1, 100);
|
||||
Assert.assertEquals(100, (int) result1.get());
|
||||
|
||||
// This is a case that can't satisfy required resources.
|
||||
RayActor<ResourcesManagementTest.Echo2> echo2 = Ray.createActor(Echo2::new);
|
||||
final RayObject<Integer> result2 = Ray.call(Echo2::echo, echo2, 100);
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
ActorCreationOptions actorCreationOptions2 =
|
||||
new ActorCreationOptions(ImmutableMap.of("CPU", 8.0, "GPU", 0.0));
|
||||
|
||||
RayActor<ResourcesManagementTest.Echo> echo2 =
|
||||
Ray.createActor(Echo::new, actorCreationOptions2);
|
||||
final RayObject<Integer> result2 = Ray.call(Echo::echo, echo2, 100);
|
||||
WaitResult<Integer> waitResult = Ray.wait(ImmutableList.of(result2), 1, 1000);
|
||||
|
||||
Assert.assertEquals(0, waitResult.getReady().size());
|
||||
Assert.assertEquals(1, waitResult.getUnready().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActorAndMemberMethods() {
|
||||
// Note(qwang): This case depends on the following line.
|
||||
// https://github.com/ray-project/ray/blob/master/java/test/src/main/java/org/ray/api/test/TestListener.java#L13
|
||||
// If we change the static resources configuration item, this case may not pass.
|
||||
// Then we should change this case too.
|
||||
RayActor<Echo3> echo3 = Ray.createActor(Echo3::new);
|
||||
Assert.assertEquals(100, (int) Ray.call(Echo3::echo, echo3, 100).get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user