mirror of
https://github.com/wassname/ray.git
synced 2026-06-27 20:06:31 +08:00
[Java] Add inner class Builder to build call options. (#4956)
* Add Builder class * format * Refactor by IDE * Remove uncessary dependency
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.ray.api.options;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -12,19 +13,32 @@ public class ActorCreationOptions extends BaseTaskOptions {
|
||||
|
||||
public final int maxReconstructions;
|
||||
|
||||
public ActorCreationOptions() {
|
||||
super();
|
||||
this.maxReconstructions = NO_RECONSTRUCTION;
|
||||
}
|
||||
|
||||
public ActorCreationOptions(Map<String, Double> resources) {
|
||||
super(resources);
|
||||
this.maxReconstructions = NO_RECONSTRUCTION;
|
||||
}
|
||||
|
||||
|
||||
public ActorCreationOptions(Map<String, Double> resources, int maxReconstructions) {
|
||||
private ActorCreationOptions(Map<String, Double> resources, int maxReconstructions) {
|
||||
super(resources);
|
||||
this.maxReconstructions = maxReconstructions;
|
||||
}
|
||||
|
||||
/**
|
||||
* The inner class for building ActorCreationOptions.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private Map<String, Double> resources = new HashMap<>();
|
||||
private int maxReconstructions = NO_RECONSTRUCTION;
|
||||
|
||||
public Builder setResources(Map<String, Double> resources) {
|
||||
this.resources = resources;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMaxReconstructions(int maxReconstructions) {
|
||||
this.maxReconstructions = maxReconstructions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ActorCreationOptions createActorCreationOptions() {
|
||||
return new ActorCreationOptions(resources, maxReconstructions);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.ray.api.options;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -7,12 +8,24 @@ import java.util.Map;
|
||||
*/
|
||||
public class CallOptions extends BaseTaskOptions {
|
||||
|
||||
public CallOptions() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CallOptions(Map<String, Double> resources) {
|
||||
private CallOptions(Map<String, Double> resources) {
|
||||
super(resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* This inner class for building CallOptions.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private Map<String, Double> resources = new HashMap<>();
|
||||
|
||||
public Builder setResources(Map<String, Double> resources) {
|
||||
this.resources = resources;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CallOptions createCallOptions() {
|
||||
return new CallOptions(resources);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.ray.api.test;
|
||||
import static org.ray.runtime.util.SystemUtil.pid;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.ray.api.Checkpointable;
|
||||
@@ -47,7 +46,8 @@ public class ActorReconstructionTest extends BaseTest {
|
||||
@Test
|
||||
public void testActorReconstruction() throws InterruptedException, IOException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
ActorCreationOptions options = new ActorCreationOptions(new HashMap<>(), 1);
|
||||
ActorCreationOptions options =
|
||||
new ActorCreationOptions.Builder().setMaxReconstructions(1).createActorCreationOptions();
|
||||
RayActor<Counter> actor = Ray.createActor(Counter::new, options);
|
||||
// Call increase 3 times.
|
||||
for (int i = 0; i < 3; i++) {
|
||||
@@ -127,8 +127,8 @@ public class ActorReconstructionTest extends BaseTest {
|
||||
@Test
|
||||
public void testActorCheckpointing() throws IOException, InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
|
||||
ActorCreationOptions options = new ActorCreationOptions(new HashMap<>(), 1);
|
||||
ActorCreationOptions options =
|
||||
new ActorCreationOptions.Builder().setMaxReconstructions(1).createActorCreationOptions();
|
||||
RayActor<CheckpointableCounter> actor = Ray.createActor(CheckpointableCounter::new, options);
|
||||
// Call increase 3 times.
|
||||
for (int i = 0; i < 3; i++) {
|
||||
|
||||
@@ -23,7 +23,8 @@ public class DynamicResourceTest extends BaseTest {
|
||||
@Test
|
||||
public void testSetResource() {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
CallOptions op1 = new CallOptions(ImmutableMap.of("A", 10.0));
|
||||
CallOptions op1 =
|
||||
new CallOptions.Builder().setResources(ImmutableMap.of("A", 10.0)).createCallOptions();
|
||||
RayObject<String> obj = Ray.call(DynamicResourceTest::sayHi, op1);
|
||||
WaitResult<String> result = Ray.wait(ImmutableList.of(obj), 1, 1000);
|
||||
Assert.assertEquals(result.getReady().size(), 0);
|
||||
|
||||
@@ -33,4 +33,5 @@ public class HelloWorldTest extends BaseTest {
|
||||
String helloWorld = Ray.call(HelloWorldTest::merge, hello, world).get();
|
||||
Assert.assertEquals("hello,world!", helloWorld);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,14 +46,16 @@ public class ResourcesManagementTest extends BaseTest {
|
||||
@Test
|
||||
public void testMethods() {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
CallOptions callOptions1 = new CallOptions(ImmutableMap.of("CPU", 4.0));
|
||||
CallOptions callOptions1 =
|
||||
new CallOptions.Builder().setResources(ImmutableMap.of("CPU", 4.0)).createCallOptions();
|
||||
|
||||
// This is a case that can satisfy required resources.
|
||||
// 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));
|
||||
CallOptions callOptions2 =
|
||||
new CallOptions.Builder().setResources(ImmutableMap.of("CPU", 4.0)).createCallOptions();
|
||||
|
||||
// This is a case that can't satisfy required resources.
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
@@ -64,7 +66,8 @@ public class ResourcesManagementTest extends BaseTest {
|
||||
Assert.assertEquals(0, waitResult.getUnready().size());
|
||||
|
||||
try {
|
||||
CallOptions callOptions3 = new CallOptions(ImmutableMap.of("CPU", 0.0));
|
||||
CallOptions callOptions3 =
|
||||
new CallOptions.Builder().setResources(ImmutableMap.of("CPU", 0.0)).createCallOptions();
|
||||
Assert.fail();
|
||||
} catch (RuntimeException e) {
|
||||
// We should receive a RuntimeException indicates that we should not
|
||||
@@ -76,9 +79,8 @@ public class ResourcesManagementTest extends BaseTest {
|
||||
public void testActors() {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
|
||||
ActorCreationOptions actorCreationOptions1 =
|
||||
new ActorCreationOptions(ImmutableMap.of("CPU", 2.0));
|
||||
|
||||
ActorCreationOptions actorCreationOptions1 = new ActorCreationOptions.Builder()
|
||||
.setResources(ImmutableMap.of("CPU", 2.0)).createActorCreationOptions();
|
||||
// This is a case that can satisfy required resources.
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
RayActor<Echo> echo1 = Ray.createActor(Echo::new, actorCreationOptions1);
|
||||
@@ -87,8 +89,8 @@ public class ResourcesManagementTest extends BaseTest {
|
||||
|
||||
// This is a case that can't satisfy required resources.
|
||||
// The static resources for test are "CPU:4,RES-A:4".
|
||||
ActorCreationOptions actorCreationOptions2 =
|
||||
new ActorCreationOptions(ImmutableMap.of("CPU", 8.0));
|
||||
ActorCreationOptions actorCreationOptions2 = new ActorCreationOptions.Builder()
|
||||
.setResources(ImmutableMap.of("CPU", 8.0)).createActorCreationOptions();
|
||||
|
||||
RayActor<ResourcesManagementTest.Echo> echo2 =
|
||||
Ray.createActor(Echo::new, actorCreationOptions2);
|
||||
|
||||
Reference in New Issue
Block a user