Skip to content

Commit

Permalink
[Java] Allow actor handle to be serialized without forking (ray-proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
jovany-wang authored and raulchen committed Jan 5, 2019
1 parent 03fe760 commit 692fdc6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions java/api/src/main/java/org/ray/api/RayActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface RayActor<T> {
* @return The id of this actor handle.
*/
UniqueId getHandleId();

}
18 changes: 16 additions & 2 deletions java/runtime/src/main/java/org/ray/runtime/RayActorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,36 @@ public int increaseTaskCounter() {
return taskCounter++;
}

private UniqueId computeNextActorHandleId() {
public RayActorImpl<T> fork() {
RayActorImpl<T> ret = new RayActorImpl<>();
ret.id = this.id;
ret.taskCounter = 0;
ret.numForks = 0;
ret.taskCursor = this.taskCursor;
ret.handleId = this.computeNextActorHandleId();
return ret;
}

protected UniqueId computeNextActorHandleId() {
byte[] bytes = Sha1Digestor.digest(handleId.getBytes(), ++numForks);
return new UniqueId(bytes);
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.id);
out.writeObject(this.computeNextActorHandleId());
out.writeObject(this.handleId);
out.writeObject(this.taskCursor);
out.writeObject(this.taskCounter);
out.writeObject(this.numForks);
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.id = (UniqueId) in.readObject();
this.handleId = (UniqueId) in.readObject();
this.taskCursor = (UniqueId) in.readObject();
this.taskCounter = (int) in.readObject();
this.numForks = (int) in.readObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ray.runtime.util;

import java.io.IOException;
import org.nustaq.serialization.FSTBasicObjectSerializer;
import org.nustaq.serialization.FSTClazzInfo;
import org.nustaq.serialization.FSTClazzInfo.FSTFieldInfo;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;
import org.ray.runtime.RayActorImpl;

public class RayActorSerializer extends FSTBasicObjectSerializer {

@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo,
FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
((RayActorImpl) toWrite).fork().writeExternal(out);
}

@Override
public void readObject(FSTObjectInput in, Object toRead, FSTClazzInfo clzInfo,
FSTFieldInfo referencedBy) throws Exception {
super.readObject(in, toRead, clzInfo, referencedBy);
((RayActorImpl) toRead).readExternal(in);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.ray.runtime.util;

import org.nustaq.serialization.FSTConfiguration;
import org.ray.runtime.RayActorImpl;

/**
* Java object serialization TODO: use others (e.g. Arrow) for higher performance
*/
public class Serializer {

static final ThreadLocal<FSTConfiguration> conf = ThreadLocal.withInitial(
FSTConfiguration::createDefaultConfiguration);
private static final ThreadLocal<FSTConfiguration> conf = ThreadLocal.withInitial(() -> {
FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
conf.registerSerializer(RayActorImpl.class, new RayActorSerializer(), true);
return conf;
});

public static byte[] encode(Object obj) {
return conf.get().asByteArray(obj);
Expand Down
11 changes: 10 additions & 1 deletion java/test/src/main/java/org/ray/api/test/ActorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.ray.api.annotation.RayRemote;
import org.ray.api.function.RayFunc2;
import org.ray.api.id.UniqueId;
import org.ray.runtime.RayActorImpl;

public class ActorTest extends BaseTest {

Expand Down Expand Up @@ -69,10 +70,18 @@ public static int testActorAsSecondParameter(int delta, RayActor<Counter> actor)
@Test
public void testPassActorAsParameter() {
RayActor<Counter> actor = Ray.createActor(Counter::new, 0);
RayFunc2<RayActor, Integer, Integer> f = ActorTest::testActorAsFirstParameter;
Assert.assertEquals(Integer.valueOf(1),
Ray.call(ActorTest::testActorAsFirstParameter, actor, 1).get());
Assert.assertEquals(Integer.valueOf(11),
Ray.call(ActorTest::testActorAsSecondParameter, 10, actor).get());
}

@Test
public void testForkingActorHandle() {
RayActor<Counter> counter = Ray.createActor(Counter::new, 100);
Assert.assertEquals(Integer.valueOf(101), Ray.call(Counter::increase, counter, 1).get());
RayActor<Counter> counter2 = ((RayActorImpl<Counter>) counter).fork();
Assert.assertEquals(Integer.valueOf(103), Ray.call(Counter::increase, counter2, 2).get());
}

}

0 comments on commit 692fdc6

Please sign in to comment.