Remove outdated Java doc and run demo code in CI (#10698)

This commit is contained in:
Hao Chen 2020-09-11 19:15:52 +08:00 committed by GitHub
parent 23051385a4
commit f7558ed2a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 95 additions and 771 deletions

View file

@ -59,20 +59,20 @@ Ray provides Python and Java API. And Ray uses Tasks (functions) and Actors (Cla
public class RayDemo {
public static int f(int x) {
public static int square(int x) {
return x * x;
}
public static class Counter {
private int n = 0;
private int value = 0;
public void increment() {
this.n += 1;
this.value += 1;
}
public int read() {
return this.n;
return this.value;
}
}
@ -81,10 +81,10 @@ Ray provides Python and Java API. And Ray uses Tasks (functions) and Actors (Cla
Ray.init();
{
List<ObjectRef<Integer>> objectRefList = new ArrayList<>();
// Invoke the `f` method 4 times remotely as Ray tasks.
// Invoke the `square` method 4 times remotely as Ray tasks.
// The tasks will run in parallel in the background.
for (int i = 0; i < 4; i++) {
objectRefList.add(Ray.task(RayDemo::f, i).remote());
objectRefList.add(Ray.task(RayDemo::square, i).remote());
}
// Get the actual results of the tasks with `get`.
System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9]

View file

@ -50,7 +50,7 @@ Parallelizing Python/Java Functions with Ray Tasks
public class RayDemo {
public static int f(int x) {
public static int square(int x) {
return x * x;
}
@ -58,10 +58,10 @@ Parallelizing Python/Java Functions with Ray Tasks
// Intialize Ray runtime.
Ray.init();
List<ObjectRef<Integer>> objectRefList = new ArrayList<>();
// Invoke the `f` method 4 times remotely as Ray tasks.
// Invoke the `square` method 4 times remotely as Ray tasks.
// The tasks will run in parallel in the background.
for (int i = 0; i < 4; i++) {
objectRefList.add(Ray.task(RayDemo::f, i).remote());
objectRefList.add(Ray.task(RayDemo::square, i).remote());
}
// Get the actual results of the tasks.
System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9]
@ -114,14 +114,14 @@ maintain its own internal state.
public static class Counter {
private int n = 0;
private int value = 0;
public void increment() {
this.n += 1;
this.value += 1;
}
public int read() {
return this.n;
return this.value;
}
}

View file

@ -13,7 +13,6 @@ all_modules = [
"api",
"runtime",
"test",
"tutorial",
]
java_import(
@ -89,15 +88,6 @@ define_java_module(
],
)
define_java_module(
name = "tutorial",
deps = [
":io_ray_ray_api",
":io_ray_ray_runtime",
"@maven//:com_google_guava_guava",
],
)
define_java_module(
name = "test",
deps = [
@ -231,7 +221,6 @@ genrule(
WORK_DIR="$$(pwd)"
cp -f $(location //java:io_ray_ray_api_pom) "$$WORK_DIR/java/api/pom.xml"
cp -f $(location //java:io_ray_ray_runtime_pom) "$$WORK_DIR/java/runtime/pom.xml"
cp -f $(location //java:io_ray_ray_tutorial_pom) "$$WORK_DIR/java/tutorial/pom.xml"
cp -f $(location //java:io_ray_ray_test_pom) "$$WORK_DIR/java/test/pom.xml"
date > $@
""",

View file

@ -1,83 +0,0 @@
Quick start
===========
Configuration
-------------
Ray will read your configurations in the following order:
* Java system properties: e.g., ``-Dray.run-mode=SINGLE_PROCESS``.
* A ``ray.conf`` file in the classpath: `example <https://github.com/ray-project/ray/blob/master/java/example.conf>`_.
* Customise your own ``ray.conf`` path using system property ``-Dray.config=/path/to/ray.conf``
For all available config items and default values, see `this file <https://github.com/ray-project/ray/blob/master/java/runtime/src/main/resources/ray.default.conf>`_.
Starting Ray
------------
.. code:: java
Ray.init();
Read and write remote objects
-----------------------------
Each remote object is considered a ``RayObject<T>`` where ``T`` is the
type for this object. You can use ``Ray.put`` and ``RayObject<T>.get``
to write and read the objects.
.. code:: java
Integer x = 1;
RayObject<Integer> obj = Ray.put(x);
Integer x1 = obj.get();
assert (x.equals(x1));
Remote functions
----------------
Here is an ordinary java code piece for composing
``hello world example``.
.. code:: java
public class ExampleClass {
public static void main(String[] args) {
String str1 = add("hello", "world");
String str = add(str1, "example");
System.out.println(str);
}
public static String add(String a, String b) {
return a + " " + b;
}
}
We use ``@RayRemote`` to indicate that a function is remote, and use
``Ray.call`` to invoke it. The result from the latter is a
``RayObject<R>`` where ``R`` is the return type of the target function.
The following shows the changed example with ``add`` annotated, and
correspondent calls executed on remote machines.
.. code:: java
public class ExampleClass {
public static void main(String[] args) {
Ray.init();
RayObject<String> objStr1 = Ray.call(ExampleClass::add, "hello", "world");
RayObject<String> objStr2 = Ray.call(ExampleClass::add, objStr1, "example");
String str = objStr2.get();
System.out.println(str);
}
@RayRemote
public static String add(String a, String b) {
return a + " " + b;
}
}
More information
================
- `Installation <https://github.com/ray-project/ray/tree/master/java/doc/installation.rst>`_
- `API document <https://github.com/ray-project/ray/tree/master/java/doc/api.rst>`_
- `Tutorial <https://github.com/ray-project/ray/tree/master/java/tutorial>`_

View file

@ -1,158 +0,0 @@
Ray Java API
============
Basic API
---------
``Ray.init()``
~~~~~~~~~~~~~~
``Ray.init`` is used to initialize Ray runtime. It should be called befored using
any other Ray APIs.
``@RayRemote``
~~~~~~~~~~~~~~
The ``@RayRemote`` annotation can be used to decorate static java
methods and classes.
When the annotation is used on a static method, the target method becomes
a remote function.
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.
``Ray.call``
~~~~~~~~~~~~
``Ray.call`` is used to invoke a remote function.
.. code:: java
RayObject<R> call(RayFunc func, ...);
The parameters of ``Ray.call`` are the target method ``func``, followed by
its original parameters.
- The return type of ``func`` must be ``R``.
- Currently at most 6 parameters of ``func`` are allowed.
- Each parameter can either be the raw type ``T``, or ``RayObject<T>``.
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.
Example:
.. code:: java
public class Echo {
@RayRemote
public static String echo(String str) { return str; }
}
RayObject<String> res = Ray.call(Echo::echo, "hello");
``Ray.put``
~~~~~~~~~~~
You can also invoke ``Ray.put`` to explicitly place an object into the object
store.
.. code:: java
public static <T> RayObject<T> put(T object);
Example:
.. code:: java
RayObject<String> fooObject = Ray.put("foo");
``RayObject<T>.get``
~~~~~~~~~~~~~~~~~~~~
.. code:: java
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();
``Ray.wait``
~~~~~~~~~~~~
``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
``timeoutMs`` has passed.
.. code:: java
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();
Actor Support
-------------
Create Actors
~~~~~~~~~~~~~
A regular class annotated with ``@RayRemote`` is an actor class.
.. code:: java
@RayRemote
public class Adder {
private int sum;
public Adder(int initValue) {
sum = initValue;
}
public int add(int n) {
return sum += n;
}
}
To create an actor instance, use ``Ray.createActor()``.
.. code:: java
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.
Call Actor Methods
~~~~~~~~~~~~~~~~~~
``Ray.call`` is also used to call actor methods, where the actor instance must be the first parameter after the remote function.
.. code:: java
RayObject<Integer> result1 = Ray.call(Adder::add, adder, 1);
System.out.println(result1.get()); // 1
RayObject<Integer> result2 = Ray.call(Adder::add, adder, 10);
System.out.println(result2.get()); // 11

View file

@ -1,56 +0,0 @@
Install Ray for Java
====================
Ray works for Java 8 and above. Currently, we only support building Ray from source.
Build from source
-----------------
Install dependencies
^^^^^^^^^^^^^^^^^^^^
First, make sure JDK 8 or above is installed on your machine. If not, you can download it from `here <http://www.oracle.com/technetwork/java/javase/downloads/index.html>`_.
Then install the following dependencies.
For Ubuntu users, run the following commands:
::
sudo apt-get update
sudo apt-get install -y maven build-essential curl unzip psmisc python # we install python here because python2 is required to build the webui
pip install cython==0.29.0
For macOS users, run the following commands:
::
brew update
brew install maven wget
pip install cython==0.29.0
Build Ray
^^^^^^^^^
Then we can start building Ray with the following commands:
::
git clone https://github.com/ray-project/ray.git
cd ray
# Install Bazel.
ci/travis/install-bazel.sh
# build native components
./build.sh -l java
# build java API
cd java
mvn clean install -Dmaven.test.skip
Run tests
^^^^^^^^^
::
# in `ray/java` directory
mvn test

View file

@ -1,24 +0,0 @@
# This is an example ray config file.
# To use this file, copy it to your classpath and rename it to 'ray.conf'.
# For all available config items and default values,
# see 'java/runtime/src/main/resources/ray.default.conf'.
# For config file format, see 'https://github.com/lightbend/config/blob/master/HOCON.md'.
ray {
// Run mode, available options are:
//
// `SINGLE_PROCESS`: Ray is running in one single Java process, without Raylet backend,
// object store, and GCS. It's useful for debug.
// `CLUSTER`: Ray is running on one or more nodes, with multiple processes.
run-mode = CLUSTER
// Available resources on this node.
resources: "CPU:4"
// The address of the redis server to connect, in format `ip:port`.
// If not provided, Ray processes will be started locally, including
// Redis server, Raylet and object store.
redis.address = ""
}

View file

@ -57,7 +57,6 @@
<module>api</module>
<module>runtime</module>
<module>test</module>
<module>tutorial</module>
</modules>
<properties>

View file

@ -50,7 +50,7 @@ echo "Running tests under single-process mode."
# bazel test //java:all_tests --jvmopt="-Dray.run-mode=SINGLE_PROCESS" --config=ci || single_exit_code=$?
run_testng java -Dray.run-mode="SINGLE_PROCESS" -cp "$ROOT_DIR"/../bazel-bin/java/all_tests_deploy.jar "${TEST_ARGS[@]}" org.testng.TestNG -d /tmp/ray_java_test_output "$ROOT_DIR"/testng.xml
echo "Running connecting existing cluster tests"
echo "Running connecting existing cluster tests."
case "${OSTYPE}" in
linux*) ip=$(hostname -I | awk '{print $1}');;
darwin*) ip=$(ipconfig getifaddr en0);;
@ -60,8 +60,19 @@ RAY_BACKEND_LOG_LEVEL=debug ray start --head --redis-port=6379 --redis-password=
RAY_BACKEND_LOG_LEVEL=debug java -cp bazel-bin/java/all_tests_deploy.jar -Dray.redis.address="$ip:6379"\
-Dray.redis.password='123456' -Dray.job.code-search-path="$PWD/bazel-bin/java/all_tests_deploy.jar" io.ray.test.MultiDriverTest
ray stop
echo "Running documentation demo code."
docdemo_path="java/test/src/main/java/io/ray/docdemo/"
for file in "$docdemo_path"*.java; do
file=${file#"$docdemo_path"}
class=${file%".java"}
echo "Running $class"
java -cp bazel-bin/java/all_tests_deploy.jar "io.ray.docdemo.$class"
done
popd
pushd "$ROOT_DIR"
echo "Testing maven install."
mvn -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN clean install -DskipTests

View file

@ -0,0 +1,69 @@
package io.ray.docdemo;
import io.ray.api.ActorHandle;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* This class contains demo code of the Ray introduction doc
* (https://docs.ray.io/en/master/index.html and https://docs.ray.io/en/master/ray-overview/index.html).
*
* Please keep them in sync.
*/
public class RayDemo {
public static int square(int x) {
return x * x;
}
public static class Counter {
private int value = 0;
public void increment() {
this.value += 1;
}
public int read() {
return this.value;
}
}
public static void main(String[] args) {
// Intialize Ray runtime.
Ray.init();
{
List<ObjectRef<Integer>> objectRefList = new ArrayList<>();
// Invoke the `square` method 4 times remotely as Ray tasks.
// The tasks will run in parallel in the background.
for (int i = 0; i < 4; i++) {
objectRefList.add(Ray.task(RayDemo::square, i).remote());
}
// Get the actual results of the tasks with `get`.
System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9]
}
{
List<ActorHandle<Counter>> counters = new ArrayList<>();
// Create 4 actors from the `Counter` class.
// They will run in remote worker processes.
for (int i = 0; i < 4; i++) {
counters.add(Ray.actor(Counter::new).remote());
}
// Invoke the `increment` method on each actor.
// This will send an actor task to each remote actor.
for (ActorHandle<Counter> counter : counters) {
counter.task(Counter::increment).remote();
}
// Invoke the `read` method on each actor, and print the results.
List<ObjectRef<Integer>> objectRefList = counters.stream()
.map(counter -> counter.task(Counter::read).remote())
.collect(Collectors.toList());
System.out.println(Ray.get(objectRefList)); // [1, 1, 1, 1]
}
}
}

View file

@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit;
import org.testng.Assert;
/**
* This class contains demo code of the Ray core Using Actors doc (https://docs.ray.io/en/latest/actors.html).
* This class contains demo code of the Ray core Using Actors doc (https://docs.ray.io/en/master/actors.html).
*
* Please keep them in sync.
*/

View file

@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit;
import org.testng.Assert;
/**
* This class contains demo code of the Ray core walkthrough doc (https://docs.ray.io/en/latest/walkthrough.html).
* This class contains demo code of the Ray core walkthrough doc (https://docs.ray.io/en/master/walkthrough.html).
*
* Please keep them in sync.
*/

View file

@ -1,25 +0,0 @@
Ray Java Tutorial
=================
- `Installation guide <https://github.com/ray-project/ray/tree/master/java/doc/installation.rst>`_
- `API document <https://github.com/ray-project/ray/tree/master/java/doc/api.rst>`_
Exercises
---------
Each file of ``java/example/src/main/java/io/ray/exercise/Exercise*.java`` is a separate exercise.
To run them, execute the following command under ``ray/java`` folder.
.. code-block:: shell
java -classpath "tutorial/target/ray-tutorial-1.0.jar:tutorial/lib/*" Exercise01
`Exercise 1 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/io/ray/exercise/Exercise01.java>`_: Define a remote function, and execute multiple remote functions in parallel.
`Exercise 2 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/io/ray/exercise/Exercise02.java>`_: Execute remote functions in parallel with some dependencies.
`Exercise 3 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/io/ray/exercise/Exercise03.java>`_: Call remote functions from within remote functions.
`Exercise 4 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/io/ray/exercise/Exercise04.java>`_: Use ``Ray.wait`` to ignore stragglers.
`Exercise 5 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/io/ray/exercise/Exercise08.java>`_: Actor Support of create Actor and call Actor method.

View file

@ -1,82 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is auto-generated by Bazel from pom_template.xml, do not modify it. -->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>io.ray</groupId>
<artifactId>ray-superpom</artifactId>
<version>0.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ray-tutorial</artifactId>
<name>java tutorial</name>
<description>Tutorial of using Ray with Java</description>
<packaging>jar</packaging>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>io.ray</groupId>
<artifactId>ray-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.ray</groupId>
<artifactId>ray-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<environmentVariables>
<RAY_CONFIG>${basedir}/../ray.config.ini</RAY_CONFIG>
</environmentVariables>
<argLine>-ea
-Djava.library.path=${basedir}/../../build/src/plasma:${basedir}/../../build/src/ray/raylet
-noverify
-DlogOutput=console
</argLine>
<testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,78 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
{auto_gen_header}
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>io.ray</groupId>
<artifactId>ray-superpom</artifactId>
<version>0.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ray-tutorial</artifactId>
<name>java tutorial</name>
<description>Tutorial of using Ray with Java</description>
<packaging>jar</packaging>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>io.ray</groupId>
<artifactId>ray-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.ray</groupId>
<artifactId>ray-runtime</artifactId>
<version>${project.version}</version>
</dependency>
{generated_bzl_deps}
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<environmentVariables>
<RAY_CONFIG>${basedir}/../ray.config.ini</RAY_CONFIG>
</environmentVariables>
<argLine>-ea
-Djava.library.path=${basedir}/../../build/src/plasma:${basedir}/../../build/src/ray/raylet
-noverify
-DlogOutput=console
</argLine>
<testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,42 +0,0 @@
package io.ray.exercise;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
import java.io.Serializable;
/**
* Define a remote function, and execute multiple remote functions in parallel.
*/
public class Exercise01 implements Serializable {
/**
* A plain remote function.
*/
public static String sayHello() {
String ret = "hello";
System.out.println(ret);
return ret;
}
public static String sayWorld() {
String ret = "world!";
System.out.println(ret);
return ret;
}
public static void main(String[] args) throws Exception {
try {
// Use `Ray.init` to initialize the Ray runtime.
Ray.init();
// Use `Ray.call` to call a remote function.
ObjectRef<String> hello = Ray.task(Exercise01::sayHello).remote();
ObjectRef<String> world = Ray.task(Exercise01::sayWorld).remote();
System.out.println("First remote call result:" + hello.get());
System.out.println("Second remote call result:" + world.get());
} catch (Throwable t) {
t.printStackTrace();
} finally {
Ray.shutdown();
}
}
}

View file

@ -1,49 +0,0 @@
package io.ray.exercise;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
/**
* Execute remote functions in parallel with some dependencies.
*/
public class Exercise02 {
public static String sayHello() {
String ret = "hello";
System.out.println(ret);
return ret;
}
public static String sayWorld() {
String ret = "world!";
System.out.println(ret);
return ret;
}
/**
* A remote function with dependency.
*/
public static String merge(String hello, String world) {
return hello + "," + world;
}
public static String sayHelloWorld() {
ObjectRef<String> hello = Ray.task(Exercise02::sayHello).remote();
ObjectRef<String> world = Ray.task(Exercise02::sayWorld).remote();
// Pass unfinished results as the parameters to another remote function.
return Ray.task(Exercise02::merge, hello, world).remote().get();
}
public static void main(String[] args) throws Exception {
try {
Ray.init();
String helloWorld = Exercise02.sayHelloWorld();
System.out.println(helloWorld);
assert helloWorld.equals("hello,world!");
} catch (Throwable t) {
t.printStackTrace();
} finally {
Ray.shutdown();
}
}
}

View file

@ -1,41 +0,0 @@
package io.ray.exercise;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
/**
* Call a remote function from within another remote function.
*/
public class Exercise03 {
/**
* A remote function which will call another remote function.
*/
public static String sayHelloWithWorld() {
String ret = "hello";
System.out.println(ret);
ObjectRef<String> world = Ray.task(Exercise03::sayWorld).remote();
return ret + "," + world.get();
}
/**
* A remote function which will be called by another remote function.
*/
public static String sayWorld() {
String ret = "world!";
System.out.println(ret);
return ret;
}
public static void main(String[] args) throws Exception {
try {
Ray.init();
String helloWithWorld = Ray.task(Exercise03::sayHelloWithWorld).remote().get();
System.out.println(helloWithWorld);
} catch (Throwable t) {
t.printStackTrace();
} finally {
Ray.shutdown();
}
}
}

View file

@ -1,59 +0,0 @@
package io.ray.exercise;
import com.google.common.collect.ImmutableList;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
import io.ray.api.WaitResult;
import java.util.List;
/**
* Use Ray.wait to ignore stragglers
*/
public class Exercise04 {
public static String f1() {
System.out.println("Executing f1");
return "f1";
}
public static String f2() {
System.out.println("Executing f2");
return "f2";
}
/**
* A slow remote function.
*/
public static String f3() {
System.out.println("Executing f3");
try {
Thread.sleep(5000L);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished executing f3");
return "f3";
}
public static void main(String[] args) throws Exception {
try {
Ray.init();
List<ObjectRef<String>> waitList = ImmutableList.of(
Ray.task(Exercise04::f1).remote(),
Ray.task(Exercise04::f2).remote(),
Ray.task(Exercise04::f3).remote()
);
// Ray.wait will block until specified number of results are ready
// or specified timeout have passed.
// In this case, the result of f3 will be ignored.
WaitResult<String> waitResult = Ray.wait(waitList, 2, 3000);
System.out.printf("%d ready object(s): \n", waitResult.getReady().size());
waitResult.getReady().forEach(rayObject -> System.out.println(rayObject.get()));
System.out.printf("%d unready object(s): \n", waitResult.getUnready().size());
} catch (Throwable t) {
t.printStackTrace();
} finally {
Ray.shutdown();
}
}
}

View file

@ -1,44 +0,0 @@
package io.ray.exercise;
import io.ray.api.ActorHandle;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
/**
* Show usage of actors.
*/
public class Exercise05 {
public static void main(String[] args) {
try {
Ray.init();
// `Ray.createActor` creates an actor instance.
ActorHandle<Adder> adder = Ray.actor(Adder::new, 0).remote();
// Use `Ray.task(actor, parameters).remote()` to call an actor method.
ObjectRef<Integer> result1 = adder.task(Adder::add, 1).remote();
System.out.println(result1.get());
ObjectRef<Integer> result2 = adder.task(Adder::add, 10).remote();
System.out.println(result2.get());
} catch (Throwable t) {
t.printStackTrace();
} finally {
Ray.shutdown();
}
}
/**
* An example actor.
*/
public static class Adder {
public Adder(int initValue) {
sum = initValue;
}
public int add(int n) {
return sum += n;
}
private int sum;
}
}

View file

@ -1,3 +0,0 @@
ray {
run-mode: CLUSTER
}