2018-07-29 08:09:30 +08:00
|
|
|
Ray Java API
|
|
|
|
============
|
|
|
|
|
|
|
|
Basic API
|
|
|
|
---------
|
|
|
|
|
|
|
|
``Ray.init()``
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
``Ray.init`` is used to initialize Ray runtime. It should be called befored using
|
|
|
|
any other Ray APIs.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
``@RayRemote``
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The ``@RayRemote`` annotation can be used to decorate static java
|
2018-09-29 08:05:42 +08:00
|
|
|
methods and classes.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
When the annotation is used on a static method, the target method becomes
|
|
|
|
a remote function.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
When the annotation is used on a class, the class becomes an actor class.
|
|
|
|
An actor is the encapsulation of state shared among many remote functions.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
``Ray.call``
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
2018-07-30 11:41:11 +08:00
|
|
|
``Ray.call`` is used to invoke a remote function.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
RayObject<R> call(RayFunc func, ...);
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-07-30 11:41:11 +08:00
|
|
|
The parameters of ``Ray.call`` are the target method ``func``, followed by
|
|
|
|
its original parameters.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
- The return type of ``func`` must be ``R``.
|
|
|
|
- Currently at most 6 parameters of ``func`` are allowed.
|
2018-07-30 11:41:11 +08:00
|
|
|
- Each parameter can either be the raw type ``T``, or ``RayObject<T>``.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
The returned object is labeled as ``RayObject<R>`` and its value will be
|
|
|
|
put into the object store on the machine where the function call is executed.
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
|
|
|
public class Echo {
|
|
|
|
|
|
|
|
@RayRemote
|
|
|
|
public static String echo(String str) { return str; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RayObject<String> res = Ray.call(Echo::echo, "hello");
|
|
|
|
|
2018-07-29 08:09:30 +08:00
|
|
|
``Ray.put``
|
|
|
|
~~~~~~~~~~~
|
|
|
|
|
|
|
|
You can also invoke ``Ray.put`` to explicitly place an object into the object
|
|
|
|
store.
|
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
public static <T> RayObject<T> put(T object);
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
Example:
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
RayObject<String> fooObject = Ray.put("foo");
|
|
|
|
|
|
|
|
``RayObject<T>.get``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code:: java
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
public class RayObject<T> {
|
|
|
|
public T get();
|
|
|
|
}
|
|
|
|
|
|
|
|
This method is used to fetch the value of this ``RayObject`` from the object store.
|
|
|
|
It will block the current thread until the requested data is locally available.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
|
|
|
String foo = fooObject.get();
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
``Ray.wait``
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
2018-07-30 11:41:11 +08:00
|
|
|
``Ray.wait`` is used to wait for a list of ``RayObject``\s to be locally available.
|
|
|
|
It will block the current thread until ``numReturns`` objects are ready or
|
2018-09-29 08:05:42 +08:00
|
|
|
``timeoutMs`` has passed.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
public static WaitResult<T> wait(List<RayObject<T>> waitList, int numReturns, int timeoutMs);
|
|
|
|
public static WaitResult<T> wait(List<RayObject<T>> waitList, int numReturns);
|
|
|
|
public static WaitResult<T> wait(List<RayObject<T>> waitList);
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
|
|
|
WaitResult<String> waitResult = Ray.wait(waitList, 5, 1000);
|
|
|
|
// `ready` is a list of objects that is already in local object store.
|
|
|
|
List<RayObject<String>> ready = waitResult.getReady();
|
|
|
|
// `unready` is the remaining objects that aren't in local object store.
|
|
|
|
List<RayObject<String>> unready = waitResult.getUnready();
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
Actor Support
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Create Actors
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
A regular class annotated with ``@RayRemote`` is an actor class.
|
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
@RayRemote
|
|
|
|
public class Adder {
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
private int sum;
|
2018-07-29 08:09:30 +08:00
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
public Adder(int initValue) {
|
|
|
|
sum = initValue;
|
2018-07-29 08:09:30 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
public int add(int n) {
|
|
|
|
return sum += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
To create an actor instance, use ``Ray.createActor()``.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
RayActor<Adder> adder = Ray.createActor(Adder::new, 0);
|
|
|
|
|
|
|
|
Similar to ``Ray.call``, the first parameter of ``Ray.createActor`` is a method that returns an instance
|
|
|
|
of the Actor class (the method can be either a constructor, or any factory methods). The rest of the parameters are
|
|
|
|
the arguments of the method.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
Call Actor Methods
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2018-09-29 08:05:42 +08:00
|
|
|
``Ray.call`` is also used to call actor methods, where the actor instance must be the first parameter after the remote function.
|
2018-07-29 08:09:30 +08:00
|
|
|
|
|
|
|
.. code:: java
|
|
|
|
|
|
|
|
RayObject<Integer> result1 = Ray.call(Adder::add, adder, 1);
|
2018-07-30 11:41:11 +08:00
|
|
|
System.out.println(result1.get()); // 1
|
2018-07-29 08:09:30 +08:00
|
|
|
RayObject<Integer> result2 = Ray.call(Adder::add, adder, 10);
|
2018-07-30 11:41:11 +08:00
|
|
|
System.out.println(result2.get()); // 11
|