[Java] Named java actor (#9037)

This commit is contained in:
chaokunyang
2020-07-16 11:31:18 +08:00
committed by GitHub
parent 5e2571e214
commit 9318e76b81
21 changed files with 405 additions and 40 deletions
@@ -7,6 +7,7 @@ import io.ray.api.runtime.RayRuntimeFactory;
import io.ray.api.runtimecontext.RuntimeContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
/**
@@ -137,6 +138,34 @@ public final class Ray extends RayCall {
return runtime.wait(waitList, waitList.size(), Integer.MAX_VALUE);
}
/**
* Get a handle to a named actor of current job.
* <p>
* Gets a handle to a named actor with the given name. The actor must
* have been created with name specified.
*
* @param name The name of the named actor.
* @return an ActorHandle to the actor if the actor of specified name exists or an
* Optional.empty()
*/
public static <T extends BaseActorHandle> Optional<T> getActor(String name) {
return runtime.getActor(name, false);
}
/**
* Get a handle to a global named actor.
* <p>
* Gets a handle to a global named actor with the given name. The actor must
* have been created with global name specified.
*
* @param name The global name of the named actor.
* @return an ActorHandle to the actor if the actor of specified name exists or an
* Optional.empty()
*/
public static <T extends BaseActorHandle> Optional<T> getGlobalActor(String name) {
return runtime.getActor(name, true);
}
/**
* If users want to use Ray API in their own threads, call this method to get the async context
* and then call {@link #setAsyncContext} at the beginning of the new thread.
@@ -19,6 +19,12 @@ public class ActorCreator<A> extends BaseActorCreator<ActorCreator<A>> {
}
/**
* Set the JVM options for the Java worker that this actor is running in.
*
* Note, if this is set, this actor won't share Java worker with other actors or tasks.
*
* @param jvmOptions JVM options for the Java worker that this actor is running in.
* @return self
* @see io.ray.api.options.ActorCreationOptions.Builder#setJvmOptions(java.lang.String)
*/
public ActorCreator<A> setJvmOptions(String jvmOptions) {
@@ -1,5 +1,6 @@
package io.ray.api.call;
import io.ray.api.Ray;
import io.ray.api.options.ActorCreationOptions;
import java.util.Map;
@@ -11,6 +12,35 @@ import java.util.Map;
public class BaseActorCreator<T extends BaseActorCreator> {
protected ActorCreationOptions.Builder builder = new ActorCreationOptions.Builder();
/**
* Set the actor name of a named actor.
* This named actor is only accessible from this job by this name via
* {@link Ray#getActor(java.lang.String)}. If you want create a named actor that is accessible
* from all jobs, use {@link BaseActorCreator#setGlobalName(java.lang.String)} instead.
*
* @param name The name of the named actor.
* @return self
* @see io.ray.api.options.ActorCreationOptions.Builder#setName(String)
*/
public T setName(String name) {
builder.setName(name);
return self();
}
/**
* Set the name of this actor. This actor will be accessible from all jobs by this name via
* {@link Ray#getGlobalActor(java.lang.String)}. If you want to create a named actor that is
* only accessible from this job, use {@link BaseActorCreator#setName(java.lang.String)} instead.
*
* @param name The name of the named actor.
* @return self
* @see io.ray.api.options.ActorCreationOptions.Builder#setGlobalName(String)
*/
public T setGlobalName(String name) {
builder.setGlobalName(name);
return self();
}
/**
* Set a custom resource requirement to reserve for the lifetime of this actor.
* This method can be called multiple times. If the same resource is set multiple times,
@@ -55,9 +85,9 @@ public class BaseActorCreator<T extends BaseActorCreator> {
}
/**
/**
* /**
* Set the max number of concurrent calls to allow for this actor.
*
* <p>
* The max concurrency defaults to 1 for threaded execution.
* Note that the execution order is not guaranteed when max_concurrency > 1.
*
@@ -1,5 +1,6 @@
package io.ray.api.options;
import io.ray.api.Ray;
import java.util.HashMap;
import java.util.Map;
@@ -7,15 +8,17 @@ import java.util.Map;
* The options for creating actor.
*/
public class ActorCreationOptions extends BaseTaskOptions {
public final boolean global;
public final String name;
public final int maxRestarts;
public final String jvmOptions;
public final int maxConcurrency;
private ActorCreationOptions(Map<String, Double> resources, int maxRestarts,
String jvmOptions, int maxConcurrency) {
private ActorCreationOptions(boolean global, String name, Map<String, Double> resources,
int maxRestarts, String jvmOptions, int maxConcurrency) {
super(resources);
this.global = global;
this.name = name;
this.maxRestarts = maxRestarts;
this.jvmOptions = jvmOptions;
this.maxConcurrency = maxConcurrency;
@@ -25,12 +28,42 @@ public class ActorCreationOptions extends BaseTaskOptions {
* The inner class for building ActorCreationOptions.
*/
public static class Builder {
private boolean global;
private String name;
private Map<String, Double> resources = new HashMap<>();
private int maxRestarts = 0;
private String jvmOptions = null;
private int maxConcurrency = 1;
/**
* Set the actor name of a named actor.
* This named actor is only accessible from this job by this name via
* {@link Ray#getActor(java.lang.String)}. If you want create a named actor that is accessible
* from all jobs, use {@link Builder#setGlobalName(java.lang.String)} instead.
*
* @param name The name of the named actor.
* @return self
*/
public Builder setName(String name) {
this.name = name;
this.global = false;
return this;
}
/**
* Set the name of this actor. This actor will be accessible from all jobs by this name via
* {@link Ray#getGlobalActor(java.lang.String)}. If you want to create a named actor that is
* only accessible from this job, use {@link Builder#setName(java.lang.String)} instead.
*
* @param name The name of the named actor.
* @return self
*/
public Builder setGlobalName(String name) {
this.name = name;
this.global = true;
return this;
}
/**
* Set a custom resource requirement to reserve for the lifetime of this actor.
* This method can be called multiple times. If the same resource is set multiple times,
@@ -73,7 +106,7 @@ public class ActorCreationOptions extends BaseTaskOptions {
/**
* Set the JVM options for the Java worker that this actor is running in.
*
* <p>
* Note, if this is set, this actor won't share Java worker with other actors or tasks.
*
* @param jvmOptions JVM options for the Java worker that this actor is running in.
@@ -86,7 +119,7 @@ public class ActorCreationOptions extends BaseTaskOptions {
/**
* Set the max number of concurrent calls to allow for this actor.
*
* <p>
* The max concurrency defaults to 1 for threaded execution.
* Note that the execution order is not guaranteed when max_concurrency > 1.
*
@@ -104,7 +137,7 @@ public class ActorCreationOptions extends BaseTaskOptions {
public ActorCreationOptions build() {
return new ActorCreationOptions(
resources, maxRestarts, jvmOptions, maxConcurrency);
global, name, resources, maxRestarts, jvmOptions, maxConcurrency);
}
}
@@ -9,12 +9,14 @@ import io.ray.api.function.PyActorClass;
import io.ray.api.function.PyActorMethod;
import io.ray.api.function.PyFunction;
import io.ray.api.function.RayFunc;
import io.ray.api.id.ActorId;
import io.ray.api.id.ObjectId;
import io.ray.api.id.UniqueId;
import io.ray.api.options.ActorCreationOptions;
import io.ray.api.options.CallOptions;
import io.ray.api.runtimecontext.RuntimeContext;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
/**
@@ -82,6 +84,20 @@ public interface RayRuntime {
*/
void setResource(String resourceName, double capacity, UniqueId nodeId);
<T extends BaseActorHandle> T getActorHandle(ActorId actorId);
/**
* Get a handle to a named actor.
* <p>
* Gets a handle to a named actor with the given name. The actor must
* have been created with name specified.
*
* @param name The name of the named actor.
* @param global Whether the named actor is global.
* @return ActorHandle to the actor.
*/
<T extends BaseActorHandle> Optional<T> getActor(String name, boolean global);
/**
* Kill the actor immediately.
*