[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:
Wang Qing
2018-10-19 21:22:32 +08:00
committed by Hao Chen
parent 9d23fa03c9
commit b410ee0d29
11 changed files with 1433 additions and 399 deletions
File diff suppressed because it is too large Load Diff
@@ -15,10 +15,4 @@ import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RayRemote {
/**
* Defines the quantity of various custom resources to reserve
* for this task or for the lifetime of the actor.
* @return an array of custom resource items.
*/
ResourceItem[] resources() default {};
}
@@ -1,28 +0,0 @@
package org.ray.api.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Represents a custom resource, including its name and quantity.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface ResourceItem {
/**
* Name of this resource, must not be null or empty.
*/
String name();
/**
* Quantity of this resource.
*/
double value() default 0;
}
@@ -0,0 +1,18 @@
package org.ray.api.options;
import java.util.Map;
/**
* The options for creating actor.
*/
public class ActorCreationOptions extends BaseTaskOptions {
public ActorCreationOptions() {
super();
}
public ActorCreationOptions(Map<String, Double> resources) {
super(resources);
}
}
@@ -0,0 +1,20 @@
package org.ray.api.options;
import java.util.HashMap;
import java.util.Map;
/**
* The options class for RayCall or ActorCreation.
*/
public abstract class BaseTaskOptions {
public Map<String, Double> resources;
public BaseTaskOptions() {
resources = new HashMap<>();
}
public BaseTaskOptions(Map<String, Double> resources) {
this.resources = resources;
}
}
@@ -0,0 +1,18 @@
package org.ray.api.options;
import java.util.Map;
/**
* The options for RayCall.
*/
public class CallOptions extends BaseTaskOptions {
public CallOptions() {
super();
}
public CallOptions(Map<String, Double> resources) {
super(resources);
}
}
@@ -6,6 +6,9 @@ import org.ray.api.RayObject;
import org.ray.api.WaitResult;
import org.ray.api.function.RayFunc;
import org.ray.api.id.UniqueId;
import org.ray.api.options.ActorCreationOptions;
import org.ray.api.options.BaseTaskOptions;
import org.ray.api.options.CallOptions;
/**
* Base interface of a Ray runtime.
@@ -65,9 +68,10 @@ public interface RayRuntime {
*
* @param func The remote function to run.
* @param args The arguments of the remote function.
* @param options The options for this call.
* @return The result object.
*/
RayObject call(RayFunc func, Object[] args);
RayObject call(RayFunc func, Object[] args, CallOptions options);
/**
* Invoke a remote function on an actor.
@@ -85,7 +89,9 @@ public interface RayRuntime {
* @param actorFactoryFunc A remote function whose return value is the actor object.
* @param args The arguments for the remote function.
* @param <T> The type of the actor object.
* @param options The options for creating actor.
* @return A handle to the actor.
*/
<T> RayActor<T> createActor(RayFunc actorFactoryFunc, Object[] args);
<T> RayActor<T> createActor(RayFunc actorFactoryFunc, Object[] args,
ActorCreationOptions options);
}