[ID Refactor] Refactor ActorID, TaskID and ObjectID (#5286)

* Refactor ActorID, TaskID on the Java side.

Left a TODO comment

WIP for ObjectID

ADD test

Fix

Add java part

Fix Java test

Fix

Refine test.

Enable test in CI

* Extra a helper function.

* Resolve TODOs

* Fix Python CI

* Fix Java lint

* Update .travis.yml

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Address some comments.

Address some comments.

Add id_specification.rst

Reanme id_specification.rst to id_specification.md

typo

Address zhijun's comments.

Fix test

Address comments.

Fix lint

Address comments

* Fix test

* Address comments.

* Fix build error

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Address comments

* Update src/ray/common/id.h

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/common/id.h

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/common/id.h

Co-Authored-By: Stephanie Wang <swang@cs.berkeley.edu>

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Hao Chen <chenh1024@gmail.com>

* Update src/ray/design_docs/id_specification.md

Co-Authored-By: Hao Chen <chenh1024@gmail.com>

* Address comments.

* Address comments.

* Address comments.

* Update C++ part to make sure task id is generated determantic

* WIP

* Fix core worker

* Fix Java part

* Fix comments.

* Add Python side

* Fix python

* Address comments

* Fix linting

* Fix

* Fix C++ linting

* Add JobId() method to TaskID

* Fix linting

* Update src/ray/common/id.h

Co-Authored-By: Hao Chen <chenh1024@gmail.com>

* Update java/api/src/main/java/org/ray/api/id/TaskId.java

Co-Authored-By: Hao Chen <chenh1024@gmail.com>

* Update java/api/src/main/java/org/ray/api/id/TaskId.java

Co-Authored-By: Hao Chen <chenh1024@gmail.com>

* Update java/api/src/main/java/org/ray/api/id/ActorId.java

Co-Authored-By: Hao Chen <chenh1024@gmail.com>

* Address comments

* Add DriverTaskId embeding job id

* Fix tests

* Add python dor_fake_driver_id

* Address comments and fix linting

* Fix CI
This commit is contained in:
Qing Wang
2019-08-07 11:04:51 +08:00
committed by GitHub
parent 50b93bf179
commit d372f24e3c
71 changed files with 1368 additions and 586 deletions
@@ -1,6 +1,8 @@
package org.ray.api;
import java.util.List;
import org.ray.api.id.ActorId;
import org.ray.api.id.UniqueId;
public interface Checkpointable {
@@ -10,7 +12,7 @@ public interface Checkpointable {
/**
* Actor's ID.
*/
public final UniqueId actorId;
public final ActorId actorId;
/**
* Number of tasks executed since last checkpoint.
*/
@@ -20,8 +22,8 @@ public interface Checkpointable {
*/
public final long timeElapsedMsSinceLastCheckpoint;
public CheckpointContext(UniqueId actorId, int numTasksSinceLastCheckpoint,
long timeElapsedMsSinceLastCheckpoint) {
public CheckpointContext(ActorId actorId, int numTasksSinceLastCheckpoint,
long timeElapsedMsSinceLastCheckpoint) {
this.actorId = actorId;
this.numTasksSinceLastCheckpoint = numTasksSinceLastCheckpoint;
this.timeElapsedMsSinceLastCheckpoint = timeElapsedMsSinceLastCheckpoint;
@@ -67,7 +69,7 @@ public interface Checkpointable {
* @param checkpointId An ID that represents this actor's current state in GCS. You should
* save this checkpoint ID together with actor's checkpoint data.
*/
void saveCheckpoint(UniqueId actorId, UniqueId checkpointId);
void saveCheckpoint(ActorId actorId, UniqueId checkpointId);
/**
* Load actor's previous checkpoint, and restore actor's state.
@@ -83,7 +85,7 @@ public interface Checkpointable {
* @return The ID of the checkpoint from which the actor was resumed, or null if the actor should
* restart from the beginning.
*/
UniqueId loadCheckpoint(UniqueId actorId, List<Checkpoint> availableCheckpoints);
UniqueId loadCheckpoint(ActorId actorId, List<Checkpoint> availableCheckpoints);
/**
* Delete an expired checkpoint;
@@ -95,5 +97,5 @@ public interface Checkpointable {
* @param actorId ID of the actor.
* @param checkpointId ID of the checkpoint that has expired.
*/
void checkpointExpired(UniqueId actorId, UniqueId checkpointId);
void checkpointExpired(ActorId actorId, UniqueId checkpointId);
}
@@ -0,0 +1,6 @@
package org.ray.api;
public enum ObjectType {
PUT_OBJECT,
RETURN_OBJECT,
}
@@ -1,5 +1,6 @@
package org.ray.api;
import org.ray.api.id.ActorId;
import org.ray.api.id.UniqueId;
/**
@@ -12,7 +13,7 @@ public interface RayActor<T> {
/**
* @return The id of this actor.
*/
UniqueId getId();
ActorId getId();
/**
* @return The id of this actor handle.
@@ -0,0 +1,54 @@
package org.ray.api.id;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Random;
public class ActorId extends BaseId implements Serializable {
private static final int UNIQUE_BYTES_LENGTH = 4;
public static final int LENGTH = UNIQUE_BYTES_LENGTH + JobId.LENGTH;
public static final ActorId NIL = nil();
private ActorId(byte[] id) {
super(id);
}
public static ActorId fromByteBuffer(ByteBuffer bb) {
return new ActorId(byteBuffer2Bytes(bb));
}
public static ActorId fromBytes(byte[] bytes) {
return new ActorId(bytes);
}
public static ActorId generateActorId(JobId jobId) {
byte[] uniqueBytes = new byte[ActorId.UNIQUE_BYTES_LENGTH];
new Random().nextBytes(uniqueBytes);
byte[] bytes = new byte[ActorId.LENGTH];
ByteBuffer wbb = ByteBuffer.wrap(bytes);
wbb.order(ByteOrder.LITTLE_ENDIAN);
System.arraycopy(uniqueBytes, 0, bytes, 0, ActorId.UNIQUE_BYTES_LENGTH);
System.arraycopy(jobId.getBytes(), 0, bytes, ActorId.UNIQUE_BYTES_LENGTH, JobId.LENGTH);
return new ActorId(bytes);
}
/**
* Generate a nil ActorId.
*/
private static ActorId nil() {
byte[] b = new byte[LENGTH];
Arrays.fill(b, (byte) 0xFF);
return new ActorId(b);
}
@Override
public int size() {
return LENGTH;
}
}
@@ -14,7 +14,7 @@ public abstract class BaseId implements Serializable {
/**
* Create a BaseId instance according to the input byte array.
*/
public BaseId(byte[] id) {
protected BaseId(byte[] id) {
if (id.length != size()) {
throw new IllegalArgumentException("Failed to construct BaseId, expect " + size()
+ " bytes, but got " + id.length + " bytes.");
@@ -2,8 +2,10 @@ package org.ray.api.id;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Random;
import org.ray.api.ObjectType;
/**
* Represents the id of a Ray object.
@@ -11,14 +13,22 @@ import java.util.Random;
public class ObjectId extends BaseId implements Serializable {
public static final int LENGTH = 20;
public static final ObjectId NIL = genNil();
/**
* Create an ObjectId from a hex string.
*/
public static ObjectId fromHexString(String hex) {
return new ObjectId(hexString2Bytes(hex));
}
private static int CREATED_BY_TASK_FLAG_BITS_OFFSET = 15;
private static int OBJECT_TYPE_FLAG_BITS_OFFSET = 14;
private static int TRANSPORT_TYPE_FLAG_BITS_OFFSET = 11;
private static int FLAGS_BYTES_POS = TaskId.LENGTH;
private static int FLAGS_BYTES_LENGTH = 2;
private static int INDEX_BYTES_POS = FLAGS_BYTES_POS + FLAGS_BYTES_LENGTH;
private static int INDEX_BYTES_LENGTH = 4;
/**
* Create an ObjectId from a ByteBuffer.
@@ -39,12 +49,54 @@ public class ObjectId extends BaseId implements Serializable {
/**
* Generate an ObjectId with random value.
*/
public static ObjectId randomId() {
public static ObjectId fromRandom() {
byte[] b = new byte[LENGTH];
new Random().nextBytes(b);
return new ObjectId(b);
}
/**
* Compute the object ID of an object put by the task.
*/
public static ObjectId forPut(TaskId taskId, int putIndex) {
short flags = 0;
flags = setCreatedByTaskFlag(flags, true);
// Set a default transport type with value 0.
flags = (short) (flags | (0x0 << TRANSPORT_TYPE_FLAG_BITS_OFFSET));
flags = setObjectTypeFlag(flags, ObjectType.PUT_OBJECT);
byte[] bytes = new byte[ObjectId.LENGTH];
System.arraycopy(taskId.getBytes(), 0, bytes, 0, TaskId.LENGTH);
ByteBuffer wbb = ByteBuffer.wrap(bytes);
wbb.order(ByteOrder.LITTLE_ENDIAN);
wbb.putShort(FLAGS_BYTES_POS, flags);
wbb.putInt(INDEX_BYTES_POS, putIndex);
return new ObjectId(bytes);
}
/**
* Compute the object ID of an object return by the task.
*/
public static ObjectId forReturn(TaskId taskId, int returnIndex) {
short flags = 0;
flags = setCreatedByTaskFlag(flags, true);
// Set a default transport type with value 0.
flags = (short) (flags | (0x0 << TRANSPORT_TYPE_FLAG_BITS_OFFSET));
flags = setObjectTypeFlag(flags, ObjectType.RETURN_OBJECT);
byte[] bytes = new byte[ObjectId.LENGTH];
System.arraycopy(taskId.getBytes(), 0, bytes, 0, TaskId.LENGTH);
ByteBuffer wbb = ByteBuffer.wrap(bytes);
wbb.order(ByteOrder.LITTLE_ENDIAN);
wbb.putShort(FLAGS_BYTES_POS, flags);
wbb.putInt(INDEX_BYTES_POS, returnIndex);
return new ObjectId(bytes);
}
public ObjectId(byte[] id) {
super(id);
}
@@ -56,7 +108,23 @@ public class ObjectId extends BaseId implements Serializable {
public TaskId getTaskId() {
byte[] taskIdBytes = Arrays.copyOf(getBytes(), TaskId.LENGTH);
return new TaskId(taskIdBytes);
return TaskId.fromBytes(taskIdBytes);
}
private static short setCreatedByTaskFlag(short flags, boolean createdByTask) {
if (createdByTask) {
return (short) (flags | (0x1 << CREATED_BY_TASK_FLAG_BITS_OFFSET));
} else {
return (short) (flags | (0x0 << CREATED_BY_TASK_FLAG_BITS_OFFSET));
}
}
private static short setObjectTypeFlag(short flags, ObjectType objectType) {
if (objectType == ObjectType.RETURN_OBJECT) {
return (short)(flags | (0x1 << OBJECT_TYPE_FLAG_BITS_OFFSET));
} else {
return (short)(flags | (0x0 << OBJECT_TYPE_FLAG_BITS_OFFSET));
}
}
}
@@ -2,6 +2,7 @@ package org.ray.api.id;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Random;
@@ -10,7 +11,10 @@ import java.util.Random;
*/
public class TaskId extends BaseId implements Serializable {
public static final int LENGTH = 16;
private static final int UNIQUE_BYTES_LENGTH = 6;
public static final int LENGTH = UNIQUE_BYTES_LENGTH + ActorId.LENGTH;
public static final TaskId NIL = genNil();
/**
@@ -27,6 +31,22 @@ public class TaskId extends BaseId implements Serializable {
return new TaskId(byteBuffer2Bytes(bb));
}
/**
* Creates a TaskId from given bytes.
*/
public static TaskId fromBytes(byte[] bytes) {
return new TaskId(bytes);
}
/**
* Get the id of the actor to which this task belongs
*/
public ActorId getActorId() {
byte[] actorIdBytes = new byte[ActorId.LENGTH];
System.arraycopy(getBytes(), UNIQUE_BYTES_LENGTH, actorIdBytes, 0, ActorId.LENGTH);
return ActorId.fromByteBuffer(ByteBuffer.wrap(actorIdBytes));
}
/**
* Generate a nil TaskId.
*/
@@ -36,16 +56,7 @@ public class TaskId extends BaseId implements Serializable {
return new TaskId(b);
}
/**
* Generate an TaskId with random value.
*/
public static TaskId randomId() {
byte[] b = new byte[LENGTH];
new Random().nextBytes(b);
return new TaskId(b);
}
public TaskId(byte[] id) {
private TaskId(byte[] id) {
super(id);
}
@@ -1,8 +1,8 @@
package org.ray.api.runtimecontext;
import java.util.List;
import org.ray.api.id.ActorId;
import org.ray.api.id.JobId;
import org.ray.api.id.UniqueId;
/**
* A class used for getting information of Ray runtime.
@@ -19,7 +19,7 @@ public interface RuntimeContext {
*
* Note, this can only be called in actors.
*/
UniqueId getCurrentActorId();
ActorId getCurrentActorId();
/**
* Returns true if the current actor was reconstructed, false if it's created for the first time.
@@ -19,6 +19,8 @@ import org.ray.api.RayPyActor;
import org.ray.api.WaitResult;
import org.ray.api.exception.RayException;
import org.ray.api.function.RayFunc;
import org.ray.api.id.ActorId;
import org.ray.api.id.JobId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.UniqueId;
@@ -34,10 +36,10 @@ import org.ray.runtime.functionmanager.PyFunctionDescriptor;
import org.ray.runtime.gcs.GcsClient;
import org.ray.runtime.objectstore.ObjectStoreProxy;
import org.ray.runtime.raylet.RayletClient;
import org.ray.runtime.raylet.RayletClientImpl;
import org.ray.runtime.task.ArgumentsBuilder;
import org.ray.runtime.task.TaskLanguage;
import org.ray.runtime.task.TaskSpec;
import org.ray.runtime.util.IdUtil;
import org.ray.runtime.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -123,9 +125,8 @@ public abstract class AbstractRayRuntime implements RayRuntime {
@Override
public <T> RayObject<T> put(T obj) {
ObjectId objectId = IdUtil.computePutId(
workerContext.getCurrentTaskId(), workerContext.nextPutIndex());
ObjectId objectId = ObjectId.forPut(workerContext.getCurrentTaskId(),
workerContext.nextPutIndex());
put(objectId, obj);
return new RayObjectImpl<>(objectId);
}
@@ -144,8 +145,8 @@ public abstract class AbstractRayRuntime implements RayRuntime {
* @return A RayObject instance that represents the in-store object.
*/
public RayObject<Object> putSerialized(byte[] obj) {
ObjectId objectId = IdUtil.computePutId(
workerContext.getCurrentTaskId(), workerContext.nextPutIndex());
ObjectId objectId = ObjectId.forPut(workerContext.getCurrentTaskId(),
workerContext.nextPutIndex());
TaskId taskId = workerContext.getCurrentTaskId();
LOGGER.debug("Putting serialized object {}, for task {} ", objectId, taskId);
objectStoreProxy.putSerialized(objectId, obj);
@@ -212,7 +213,7 @@ public abstract class AbstractRayRuntime implements RayRuntime {
Object[] args, ActorCreationOptions options) {
TaskSpec spec = createTaskSpec(actorFactoryFunc, null, RayActorImpl.NIL,
args, true, false, options);
RayActorImpl<?> actor = new RayActorImpl(new UniqueId(spec.returnIds[0].getBytes()));
RayActorImpl<?> actor = new RayActorImpl(spec.taskId.getActorId());
actor.increaseTaskCounter();
actor.setTaskCursor(spec.returnIds[0]);
rayletClient.submitTask(spec);
@@ -272,7 +273,7 @@ public abstract class AbstractRayRuntime implements RayRuntime {
*
* @param func The target remote function.
* @param pyFunctionDescriptor Descriptor of the target Python function, if the task is a Python
* task.
* task.
* @param actor The actor handle. If the task is not an actor task, actor id must be NIL.
* @param args The arguments for the remote function.
* @param isActorCreationTask Whether this task is an actor creation task.
@@ -284,16 +285,22 @@ public abstract class AbstractRayRuntime implements RayRuntime {
boolean isActorCreationTask, boolean isActorTask, BaseTaskOptions taskOptions) {
Preconditions.checkArgument((func == null) != (pyFunctionDescriptor == null));
TaskId taskId = rayletClient.generateTaskId(workerContext.getCurrentJobId(),
workerContext.getCurrentTaskId(), workerContext.nextTaskIndex());
int numReturns = actor.getId().isNil() ? 1 : 2;
ObjectId[] returnIds = IdUtil.genReturnIds(taskId, numReturns);
UniqueId actorCreationId = UniqueId.NIL;
ActorId actorCreationId = ActorId.NIL;
TaskId taskId = null;
final JobId currentJobId = workerContext.getCurrentJobId();
final TaskId currentTaskId = workerContext.getCurrentTaskId();
final int taskIndex = workerContext.nextTaskIndex();
if (isActorCreationTask) {
actorCreationId = new UniqueId(returnIds[0].getBytes());
taskId = RayletClientImpl.generateActorCreationTaskId(currentJobId, currentTaskId, taskIndex);
actorCreationId = taskId.getActorId();
} else if (isActorTask) {
taskId = RayletClientImpl.generateActorTaskId(currentJobId, currentTaskId, taskIndex, actor.getId());
} else {
taskId = RayletClientImpl.generateNormalTaskId(currentJobId, currentTaskId, taskIndex);
}
int numReturns = actor.getId().isNil() ? 1 : 2;
Map<String, Double> resources;
if (null == taskOptions) {
resources = new HashMap<>();
@@ -337,7 +344,7 @@ public abstract class AbstractRayRuntime implements RayRuntime {
actor.getId(),
actor.getHandleId(),
actor.increaseTaskCounter(),
previousActorTaskDummyObjectId,
previousActorTaskDummyObjectId,
actor.getNewActorHandles().toArray(new UniqueId[0]),
ArgumentsBuilder.wrap(args, language == TaskLanguage.PYTHON),
numReturns,
@@ -7,6 +7,7 @@ import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
import org.ray.api.RayActor;
import org.ray.api.id.ActorId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.UniqueId;
import org.ray.runtime.util.Sha1Digestor;
@@ -18,7 +19,7 @@ public class RayActorImpl<T> implements RayActor<T>, Externalizable {
/**
* Id of this actor.
*/
protected UniqueId id;
protected ActorId id;
/**
* Handle id of this actor.
*/
@@ -47,14 +48,14 @@ public class RayActorImpl<T> implements RayActor<T>, Externalizable {
protected List<UniqueId> newActorHandles;
public RayActorImpl() {
this(UniqueId.NIL, UniqueId.NIL);
this(ActorId.NIL, UniqueId.NIL);
}
public RayActorImpl(UniqueId id) {
public RayActorImpl(ActorId id) {
this(id, UniqueId.NIL);
}
public RayActorImpl(UniqueId id, UniqueId handleId) {
public RayActorImpl(ActorId id, UniqueId handleId) {
this.id = id;
this.handleId = handleId;
this.taskCounter = 0;
@@ -64,7 +65,7 @@ public class RayActorImpl<T> implements RayActor<T>, Externalizable {
}
@Override
public UniqueId getId() {
public ActorId getId() {
return id;
}
@@ -120,7 +121,7 @@ public class RayActorImpl<T> implements RayActor<T>, Externalizable {
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.id = (UniqueId) in.readObject();
this.id = (ActorId) in.readObject();
this.handleId = (UniqueId) in.readObject();
this.taskCursor = (ObjectId) in.readObject();
this.taskCounter = (int) in.readObject();
@@ -4,11 +4,11 @@ import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.ray.api.RayPyActor;
import org.ray.api.id.UniqueId;
import org.ray.api.id.ActorId;
public class RayPyActorImpl extends RayActorImpl implements RayPyActor {
public static final RayPyActorImpl NIL = new RayPyActorImpl(UniqueId.NIL, null, null);
public static final RayPyActorImpl NIL = new RayPyActorImpl(ActorId.NIL, null, null);
/**
* Module name of the Python actor class.
@@ -24,7 +24,7 @@ public class RayPyActorImpl extends RayActorImpl implements RayPyActor {
// since it'll be needed when deserializing.
public RayPyActorImpl() {}
public RayPyActorImpl(UniqueId id, String moduleName, String className) {
public RayPyActorImpl(ActorId id, String moduleName, String className) {
super(id);
this.moduleName = moduleName;
this.className = className;
@@ -2,8 +2,9 @@ package org.ray.runtime;
import com.google.common.base.Preconditions;
import java.util.List;
import org.ray.api.id.ActorId;
import org.ray.api.id.JobId;
import org.ray.api.id.UniqueId;
import org.ray.api.runtimecontext.NodeInfo;
import org.ray.api.runtimecontext.RuntimeContext;
import org.ray.runtime.config.RunMode;
@@ -23,7 +24,7 @@ public class RuntimeContextImpl implements RuntimeContext {
}
@Override
public UniqueId getCurrentActorId() {
public ActorId getCurrentActorId() {
Worker worker = runtime.getWorker();
Preconditions.checkState(worker != null && !worker.getCurrentActorId().isNil(),
"This method should only be called from an actor.");
@@ -7,12 +7,14 @@ import org.ray.api.Checkpointable;
import org.ray.api.Checkpointable.Checkpoint;
import org.ray.api.Checkpointable.CheckpointContext;
import org.ray.api.exception.RayTaskException;
import org.ray.api.id.ActorId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.UniqueId;
import org.ray.runtime.config.RunMode;
import org.ray.runtime.functionmanager.RayFunction;
import org.ray.runtime.task.ArgumentsBuilder;
import org.ray.runtime.task.TaskSpec;
import org.ray.runtime.util.IdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,7 +39,7 @@ public class Worker {
/**
* Id of the current actor object, if the worker is an actor, otherwise NIL.
*/
private UniqueId currentActorId = UniqueId.NIL;
private ActorId currentActorId = ActorId.NIL;
/**
* The exception that failed the actor creation task, if any.
@@ -64,7 +66,7 @@ public class Worker {
this.runtime = runtime;
}
public UniqueId getCurrentActorId() {
public ActorId getCurrentActorId() {
return currentActorId;
}
@@ -92,7 +94,7 @@ public class Worker {
Thread.currentThread().setContextClassLoader(rayFunction.classLoader);
if (spec.isActorCreationTask()) {
currentActorId = new UniqueId(returnId.getBytes());
currentActorId = spec.taskId.getActorId();
}
// Get local actor object and arguments.
@@ -118,9 +120,10 @@ public class Worker {
if (spec.isActorTask()) {
maybeSaveCheckpoint(actor, spec.actorId);
}
runtime.put(returnId, result);
} else {
maybeLoadCheckpoint(result, new UniqueId(returnId.getBytes()));
maybeLoadCheckpoint(result, spec.taskId.getActorId());
currentActor = result;
}
LOGGER.debug("Finished executing task {}", spec.taskId);
@@ -136,7 +139,7 @@ public class Worker {
}
}
private void maybeSaveCheckpoint(Object actor, UniqueId actorId) {
private void maybeSaveCheckpoint(Object actor, ActorId actorId) {
if (!(actor instanceof Checkpointable)) {
return;
}
@@ -161,7 +164,7 @@ public class Worker {
checkpointable.saveCheckpoint(actorId, checkpointId);
}
private void maybeLoadCheckpoint(Object actor, UniqueId actorId) {
private void maybeLoadCheckpoint(Object actor, ActorId actorId) {
if (!(actor instanceof Checkpointable)) {
return;
}
@@ -48,7 +48,7 @@ public class WorkerContext {
* for other threads, this method returns a random ID.
*/
public TaskId getCurrentTaskId() {
return new TaskId(nativeGetCurrentTaskId(nativeWorkerContextPointer));
return TaskId.fromBytes(nativeGetCurrentTaskId(nativeWorkerContextPointer));
}
/**
@@ -9,6 +9,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.ray.api.Checkpointable.Checkpoint;
import org.ray.api.id.ActorId;
import org.ray.api.id.BaseId;
import org.ray.api.id.JobId;
import org.ray.api.id.TaskId;
@@ -117,7 +118,7 @@ public class GcsClient {
/**
* If the actor exists in GCS.
*/
public boolean actorExists(UniqueId actorId) {
public boolean actorExists(ActorId actorId) {
byte[] key = ArrayUtils.addAll(
TablePrefix.ACTOR.toString().getBytes(), actorId.getBytes());
return primary.exists(key);
@@ -136,7 +137,7 @@ public class GcsClient {
/**
* Get the available checkpoints for the given actor ID.
*/
public List<Checkpoint> getCheckpointsForActor(UniqueId actorId) {
public List<Checkpoint> getCheckpointsForActor(ActorId actorId) {
List<Checkpoint> checkpoints = new ArrayList<>();
final String prefix = TablePrefix.ACTOR_CHECKPOINT_ID.toString();
final byte[] key = ArrayUtils.addAll(prefix.getBytes(), actorId.getBytes());
@@ -9,7 +9,6 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.ray.api.id.ObjectId;
import org.ray.runtime.WorkerContext;
import org.ray.runtime.util.IdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,7 +36,7 @@ public class MockObjectInterface implements ObjectInterface {
@Override
public ObjectId put(NativeRayObject obj) {
ObjectId objectId = IdUtil.computePutId(workerContext.getCurrentTaskId(),
ObjectId objectId = ObjectId.forPut(workerContext.getCurrentTaskId(),
workerContext.nextPutIndex());
put(obj, objectId);
return objectId;
@@ -57,7 +57,8 @@ public class ObjectInterfaceImpl implements ObjectInterface {
@Override
public void delete(List<ObjectId> objectIds, boolean localOnly, boolean deleteCreatingTasks) {
nativeDelete(nativeObjectInterfacePointer, toBinaryList(objectIds), localOnly, deleteCreatingTasks);
nativeDelete(nativeObjectInterfacePointer,
toBinaryList(objectIds), localOnly, deleteCreatingTasks);
}
public void destroy() {
@@ -18,7 +18,7 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
import org.ray.api.RayObject;
import org.ray.api.WaitResult;
import org.ray.api.id.JobId;
import org.ray.api.id.ActorId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.UniqueId;
@@ -43,7 +43,7 @@ public class MockRayletClient implements RayletClient {
private final RayDevRuntime runtime;
private final ExecutorService exec;
private final Deque<Worker> idleWorkers;
private final Map<UniqueId, Worker> actorWorkers;
private final Map<ActorId, Worker> actorWorkers;
private final ThreadLocal<Worker> currentWorker;
public MockRayletClient(RayDevRuntime runtime, int numberThreads) {
@@ -154,11 +154,6 @@ public class MockRayletClient implements RayletClient {
throw new RuntimeException("invalid execution flow here");
}
@Override
public TaskId generateTaskId(JobId jobId, TaskId parentTaskId, int taskIndex) {
return TaskId.randomId();
}
@Override
public <T> WaitResult<T> wait(List<RayObject<T>> waitFor, int numReturns, int
timeoutMs, TaskId currentTaskId) {
@@ -188,12 +183,12 @@ public class MockRayletClient implements RayletClient {
@Override
public UniqueId prepareCheckpoint(UniqueId actorId) {
public UniqueId prepareCheckpoint(ActorId actorId) {
throw new NotImplementedException("Not implemented.");
}
@Override
public void notifyActorResumedFromCheckpoint(UniqueId actorId, UniqueId checkpointId) {
public void notifyActorResumedFromCheckpoint(ActorId actorId, UniqueId checkpointId) {
throw new NotImplementedException("Not implemented.");
}
@@ -3,7 +3,7 @@ package org.ray.runtime.raylet;
import java.util.List;
import org.ray.api.RayObject;
import org.ray.api.WaitResult;
import org.ray.api.id.JobId;
import org.ray.api.id.ActorId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.UniqueId;
@@ -18,16 +18,14 @@ public interface RayletClient {
TaskSpec getTask();
TaskId generateTaskId(JobId jobId, TaskId parentTaskId, int taskIndex);
<T> WaitResult<T> wait(List<RayObject<T>> waitFor, int numReturns, int
timeoutMs, TaskId currentTaskId);
void freePlasmaObjects(List<ObjectId> objectIds, boolean localOnly, boolean deleteCreatingTasks);
UniqueId prepareCheckpoint(UniqueId actorId);
UniqueId prepareCheckpoint(ActorId actorId);
void notifyActorResumedFromCheckpoint(UniqueId actorId, UniqueId checkpointId);
void notifyActorResumedFromCheckpoint(ActorId actorId, UniqueId checkpointId);
void setResource(String resourceName, double capacity, UniqueId nodeId);
@@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
@@ -13,10 +14,11 @@ import java.util.stream.Collectors;
import org.ray.api.RayObject;
import org.ray.api.WaitResult;
import org.ray.api.exception.RayException;
import org.ray.api.id.JobId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.ActorId;
import org.ray.api.id.UniqueId;
import org.ray.api.id.JobId;
import org.ray.api.id.TaskId;
import org.ray.api.id.ObjectId;
import org.ray.runtime.functionmanager.JavaFunctionDescriptor;
import org.ray.runtime.generated.Common;
import org.ray.runtime.generated.Common.TaskType;
@@ -93,12 +95,6 @@ public class RayletClientImpl implements RayletClient {
return parseTaskSpecFromProtobuf(bytes);
}
@Override
public TaskId generateTaskId(JobId jobId, TaskId parentTaskId, int taskIndex) {
byte[] bytes = nativeGenerateTaskId(jobId.getBytes(), parentTaskId.getBytes(), taskIndex);
return new TaskId(bytes);
}
@Override
public void freePlasmaObjects(List<ObjectId> objectIds, boolean localOnly,
boolean deleteCreatingTasks) {
@@ -107,15 +103,30 @@ public class RayletClientImpl implements RayletClient {
}
@Override
public UniqueId prepareCheckpoint(UniqueId actorId) {
public UniqueId prepareCheckpoint(ActorId actorId) {
return new UniqueId(nativePrepareCheckpoint(client, actorId.getBytes()));
}
@Override
public void notifyActorResumedFromCheckpoint(UniqueId actorId, UniqueId checkpointId) {
public void notifyActorResumedFromCheckpoint(ActorId actorId, UniqueId checkpointId) {
nativeNotifyActorResumedFromCheckpoint(client, actorId.getBytes(), checkpointId.getBytes());
}
public static TaskId generateActorCreationTaskId(JobId jobId, TaskId parentTaskId, int taskIndex) {
byte[] bytes = nativeGenerateActorCreationTaskId(jobId.getBytes(), parentTaskId.getBytes(), taskIndex);
return TaskId.fromBytes(bytes);
}
public static TaskId generateActorTaskId(JobId jobId, TaskId parentTaskId, int taskIndex, ActorId actorId) {
byte[] bytes = nativeGenerateActorTaskId(jobId.getBytes(), parentTaskId.getBytes(), taskIndex, actorId.getBytes());
return TaskId.fromBytes(bytes);
}
public static TaskId generateNormalTaskId(JobId jobId, TaskId parentTaskId, int taskIndex) {
byte[] bytes = nativeGenerateNormalTaskId(jobId.getBytes(), parentTaskId.getBytes(), taskIndex);
return TaskId.fromBytes(bytes);
}
/**
* Parse `TaskSpec` protobuf bytes.
*/
@@ -160,13 +171,13 @@ public class RayletClientImpl implements RayletClient {
);
// Parse ActorCreationTaskSpec.
UniqueId actorCreationId = UniqueId.NIL;
ActorId actorCreationId = ActorId.NIL;
int maxActorReconstructions = 0;
UniqueId[] newActorHandles = new UniqueId[0];
List<String> dynamicWorkerOptions = new ArrayList<>();
if (taskSpec.getType() == Common.TaskType.ACTOR_CREATION_TASK) {
Common.ActorCreationTaskSpec actorCreationTaskSpec = taskSpec.getActorCreationTaskSpec();
actorCreationId = UniqueId
actorCreationId = ActorId
.fromByteBuffer(actorCreationTaskSpec.getActorId().asReadOnlyByteBuffer());
maxActorReconstructions = (int) actorCreationTaskSpec.getMaxActorReconstructions();
dynamicWorkerOptions = ImmutableList
@@ -174,18 +185,18 @@ public class RayletClientImpl implements RayletClient {
}
// Parse ActorTaskSpec.
UniqueId actorId = UniqueId.NIL;
ActorId actorId = ActorId.NIL;
UniqueId actorHandleId = UniqueId.NIL;
ObjectId previousActorTaskDummyObjectId = ObjectId.NIL;
int actorCounter = 0;
if (taskSpec.getType() == Common.TaskType.ACTOR_TASK) {
Common.ActorTaskSpec actorTaskSpec = taskSpec.getActorTaskSpec();
actorId = UniqueId.fromByteBuffer(actorTaskSpec.getActorId().asReadOnlyByteBuffer());
actorId = ActorId.fromByteBuffer(actorTaskSpec.getActorId().asReadOnlyByteBuffer());
actorHandleId = UniqueId
.fromByteBuffer(actorTaskSpec.getActorHandleId().asReadOnlyByteBuffer());
actorCounter = (int) actorTaskSpec.getActorCounter();
previousActorTaskDummyObjectId = ObjectId.fromByteBuffer(
actorTaskSpec.getPreviousActorTaskDummyObjectId().asReadOnlyByteBuffer());
actorTaskSpec.getPreviousActorTaskDummyObjectId().asReadOnlyByteBuffer());
newActorHandles = actorTaskSpec.getNewActorHandlesList().stream()
.map(byteString -> UniqueId.fromByteBuffer(byteString.asReadOnlyByteBuffer()))
.toArray(UniqueId[]::new);
@@ -193,8 +204,8 @@ public class RayletClientImpl implements RayletClient {
return new TaskSpec(jobId, taskId, parentTaskId, parentCounter, actorCreationId,
maxActorReconstructions, actorId, actorHandleId, actorCounter,
previousActorTaskDummyObjectId, newActorHandles, args, numReturns, resources,
TaskLanguage.JAVA, functionDescriptor, dynamicWorkerOptions);
previousActorTaskDummyObjectId, newActorHandles, args, numReturns, resources,
TaskLanguage.JAVA, functionDescriptor, dynamicWorkerOptions);
}
/**
@@ -255,13 +266,16 @@ public class RayletClientImpl implements RayletClient {
builder.setType(TaskType.ACTOR_TASK);
List<ByteString> newHandles = Arrays.stream(task.newActorHandles)
.map(id -> ByteString.copyFrom(id.getBytes())).collect(Collectors.toList());
final ObjectId actorCreationDummyObjectId = IdUtil.computeActorCreationDummyObjectId(
ActorId.fromByteBuffer(ByteBuffer.wrap(task.actorId.getBytes())));
builder.setActorTaskSpec(
Common.ActorTaskSpec.newBuilder()
.setActorId(ByteString.copyFrom(task.actorId.getBytes()))
.setActorHandleId(ByteString.copyFrom(task.actorHandleId.getBytes()))
.setActorCreationDummyObjectId(ByteString.copyFrom(task.actorId.getBytes()))
.setActorCreationDummyObjectId(
ByteString.copyFrom(actorCreationDummyObjectId.getBytes()))
.setPreviousActorTaskDummyObjectId(
ByteString.copyFrom(task.previousActorTaskDummyObjectId.getBytes()))
ByteString.copyFrom(task.previousActorTaskDummyObjectId.getBytes()))
.setActorCounter(task.actorCounter)
.addAllNewActorHandles(newHandles)
);
@@ -307,9 +321,6 @@ public class RayletClientImpl implements RayletClient {
private static native boolean[] nativeWaitObject(long conn, byte[][] objectIds,
int numReturns, int timeout, boolean waitLocal, byte[] currentTaskId) throws RayException;
private static native byte[] nativeGenerateTaskId(byte[] jobId, byte[] parentTaskId,
int taskIndex);
private static native void nativeFreePlasmaObjects(long conn, byte[][] objectIds,
boolean localOnly, boolean deleteCreatingTasks) throws RayException;
@@ -320,4 +331,13 @@ public class RayletClientImpl implements RayletClient {
private static native void nativeSetResource(long conn, String resourceName, double capacity,
byte[] nodeId) throws RayException;
private static native byte[] nativeGenerateActorCreationTaskId(byte[] jobId, byte[] parentTaskId,
int taskIndex);
private static native byte[] nativeGenerateActorTaskId(byte[] jobId, byte[] parentTaskId,
int taskIndex, byte[] actorId);
private static native byte[] nativeGenerateNormalTaskId(byte[] jobId, byte[] parentTaskId,
int taskIndex);
}
@@ -1,18 +1,17 @@
package org.ray.runtime.task;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.ray.api.id.ActorId;
import org.ray.api.id.JobId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.UniqueId;
import org.ray.runtime.functionmanager.FunctionDescriptor;
import org.ray.runtime.functionmanager.JavaFunctionDescriptor;
import org.ray.runtime.functionmanager.PyFunctionDescriptor;
import org.ray.runtime.util.IdUtil;
/**
* Represents necessary information of a task for scheduling and executing.
@@ -32,13 +31,13 @@ public class TaskSpec {
public final int parentCounter;
// Id for createActor a target actor
public final UniqueId actorCreationId;
public final ActorId actorCreationId;
public final int maxActorReconstructions;
// Actor ID of the task. This is the actor that this task is executed on
// or NIL_ACTOR_ID if the task is just a normal task.
public final UniqueId actorId;
public final ActorId actorId;
// ID per actor client for session consistency
public final UniqueId actorHandleId;
@@ -87,9 +86,9 @@ public class TaskSpec {
TaskId taskId,
TaskId parentTaskId,
int parentCounter,
UniqueId actorCreationId,
ActorId actorCreationId,
int maxActorReconstructions,
UniqueId actorId,
ActorId actorId,
UniqueId actorHandleId,
int actorCounter,
ObjectId previousActorTaskDummyObjectId,
@@ -117,7 +116,7 @@ public class TaskSpec {
returnIds = new ObjectId[numReturns];
for (int i = 0; i < numReturns; ++i) {
returnIds[i] = IdUtil.computeReturnId(taskId, i + 1);
returnIds[i] = ObjectId.forReturn(taskId, i + 1);
}
this.resources = resources;
this.language = language;
@@ -1,15 +1,11 @@
package org.ray.runtime.util;
import com.google.common.base.Preconditions;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.List;
import org.ray.api.id.BaseId;
import org.ray.api.id.JobId;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.UniqueId;
import org.ray.api.id.ActorId;
/**
* Helper method for different Ids.
@@ -17,60 +13,6 @@ import org.ray.api.id.UniqueId;
* in src/ray/common/id.h
*/
public class IdUtil {
public static final int OBJECT_INDEX_POS = 16;
/**
* Compute the object ID of an object returned by the task.
*
* @param taskId The task ID of the task that created the object.
* @param returnIndex What number return value this object is in the task.
* @return The computed object ID.
*/
public static ObjectId computeReturnId(TaskId taskId, int returnIndex) {
return computeObjectId(taskId, returnIndex);
}
/**
* Compute the object ID from the task ID and the index.
* @param taskId The task ID of the task that created the object.
* @param index The index which can distinguish different objects in one task.
* @return The computed object ID.
*/
private static ObjectId computeObjectId(TaskId taskId, int index) {
byte[] bytes = new byte[ObjectId.LENGTH];
System.arraycopy(taskId.getBytes(), 0, bytes, 0, taskId.size());
ByteBuffer wbb = ByteBuffer.wrap(bytes);
wbb.order(ByteOrder.LITTLE_ENDIAN);
wbb.putInt(OBJECT_INDEX_POS, index);
return new ObjectId(bytes);
}
/**
* Compute the object ID of an object put by the task.
*
* @param taskId The task ID of the task that created the object.
* @param putIndex What number put this object was created by in the task.
* @return The computed object ID.
*/
public static ObjectId computePutId(TaskId taskId, int putIndex) {
// We multiply putIndex by -1 to distinguish from returnIndex.
return computeObjectId(taskId, -1 * putIndex);
}
/**
* Generate the return ids of a task.
*
* @param taskId The ID of the task that generates returnsIds.
* @param numReturns The number of returnIds.
* @return The Return Ids of this task.
*/
public static ObjectId[] genReturnIds(TaskId taskId, int numReturns) {
ObjectId[] ret = new ObjectId[numReturns];
for (int i = 0; i < numReturns; i++) {
ret[i] = IdUtil.computeReturnId(taskId, i + 1);
}
return ret;
}
public static <T extends BaseId> byte[][] getIdBytes(List<T> objectIds) {
int size = objectIds.size();
@@ -81,79 +23,6 @@ public class IdUtil {
return ids;
}
public static byte[][] getByteListFromByteBuffer(ByteBuffer byteBufferOfIds, int length) {
Preconditions.checkArgument(byteBufferOfIds != null);
byte[] bytesOfIds = new byte[byteBufferOfIds.remaining()];
byteBufferOfIds.get(bytesOfIds, 0, byteBufferOfIds.remaining());
int count = bytesOfIds.length / length;
byte[][] idBytes = new byte[count][];
for (int i = 0; i < count; ++i) {
byte[] id = new byte[length];
System.arraycopy(bytesOfIds, i * length, id, 0, length);
idBytes[i] = id;
}
return idBytes;
}
/**
* Get unique IDs from concatenated ByteBuffer.
*
* @param byteBufferOfIds The ByteBuffer concatenated from IDs.
* @return The array of unique IDs.
*/
public static UniqueId[] getUniqueIdsFromByteBuffer(ByteBuffer byteBufferOfIds) {
byte[][]idBytes = getByteListFromByteBuffer(byteBufferOfIds, UniqueId.LENGTH);
UniqueId[] uniqueIds = new UniqueId[idBytes.length];
for (int i = 0; i < idBytes.length; ++i) {
uniqueIds[i] = UniqueId.fromByteBuffer(ByteBuffer.wrap(idBytes[i]));
}
return uniqueIds;
}
/**
* Get object IDs from concatenated ByteBuffer.
*
* @param byteBufferOfIds The ByteBuffer concatenated from IDs.
* @return The array of object IDs.
*/
public static ObjectId[] getObjectIdsFromByteBuffer(ByteBuffer byteBufferOfIds) {
byte[][]idBytes = getByteListFromByteBuffer(byteBufferOfIds, UniqueId.LENGTH);
ObjectId[] objectIds = new ObjectId[idBytes.length];
for (int i = 0; i < idBytes.length; ++i) {
objectIds[i] = ObjectId.fromByteBuffer(ByteBuffer.wrap(idBytes[i]));
}
return objectIds;
}
/**
* Concatenate IDs to a ByteBuffer.
*
* @param ids The array of IDs that will be concatenated.
* @return A ByteBuffer that contains bytes of concatenated IDs.
*/
public static <T extends BaseId> ByteBuffer concatIds(T[] ids) {
int length = 0;
if (ids != null && ids.length != 0) {
length = ids[0].size() * ids.length;
}
byte[] bytesOfIds = new byte[length];
for (int i = 0; i < ids.length; ++i) {
System.arraycopy(ids[i].getBytes(), 0, bytesOfIds,
i * ids[i].size(), ids[i].size());
}
return ByteBuffer.wrap(bytesOfIds);
}
/**
* Compute the murmur hash code of this ID.
*/
@@ -221,4 +90,16 @@ public class IdUtil {
return h;
}
/*
* A helper function to compute actor creation dummy object id according
* the given actor id.
*/
public static ObjectId computeActorCreationDummyObjectId(ActorId actorId) {
byte[] bytes = new byte[ObjectId.LENGTH];
System.arraycopy(actorId.getBytes(), 0, bytes, 0, ActorId.LENGTH);
Arrays.fill(bytes, ActorId.LENGTH, bytes.length, (byte) 0xFF);
return ObjectId.fromByteBuffer(ByteBuffer.wrap(bytes));
}
}
@@ -11,6 +11,7 @@ import org.ray.api.RayActor;
import org.ray.api.TestUtils;
import org.ray.api.annotation.RayRemote;
import org.ray.api.exception.RayActorException;
import org.ray.api.id.ActorId;
import org.ray.api.id.UniqueId;
import org.ray.api.options.ActorCreationOptions;
import org.testng.Assert;
@@ -106,13 +107,13 @@ public class ActorReconstructionTest extends BaseTest {
}
@Override
public void saveCheckpoint(UniqueId actorId, UniqueId checkpointId) {
public void saveCheckpoint(ActorId actorId, UniqueId checkpointId) {
// In practice, user should save the checkpoint id and data to a persistent store.
// But for simplicity, we don't do that in this unit test.
}
@Override
public UniqueId loadCheckpoint(UniqueId actorId, List<Checkpoint> availableCheckpoints) {
public UniqueId loadCheckpoint(ActorId actorId, List<Checkpoint> availableCheckpoints) {
// Restore previous value and return checkpoint id.
this.value = 3;
this.resumedFromCheckpoint = true;
@@ -120,7 +121,7 @@ public class ActorReconstructionTest extends BaseTest {
}
@Override
public void checkpointExpired(UniqueId actorId, UniqueId checkpointId) {
public void checkpointExpired(ActorId actorId, UniqueId checkpointId) {
}
}
@@ -20,7 +20,7 @@ public class ClientExceptionTest extends BaseTest {
@Test
public void testWaitAndCrash() {
TestUtils.skipTestUnderSingleProcess();
ObjectId randomId = ObjectId.randomId();
ObjectId randomId = ObjectId.fromRandom();
RayObject<String> notExisting = new RayObjectImpl(randomId);
Thread thread = new Thread(() -> {
@@ -13,7 +13,7 @@ public class PlasmaStoreTest extends BaseTest {
@Test
public void testPutWithDuplicateId() {
TestUtils.skipTestUnderSingleProcess();
ObjectId objectId = ObjectId.randomId();
ObjectId objectId = ObjectId.fromRandom();
AbstractRayRuntime runtime = (AbstractRayRuntime) Ray.internal();
ObjectStoreProxy objectInterface = runtime.getObjectStoreProxy();
objectInterface.put(objectId, 1);
@@ -1,6 +1,8 @@
package org.ray.api.test;
import org.ray.api.RayPyActor;
import org.ray.api.id.ActorId;
import org.ray.api.id.JobId;
import org.ray.api.id.UniqueId;
import org.ray.runtime.RayPyActorImpl;
import org.ray.runtime.util.Serializer;
@@ -11,7 +13,7 @@ public class RaySerializerTest {
@Test
public void testSerializePyActor() {
final UniqueId pyActorId = UniqueId.randomId();
final ActorId pyActorId = ActorId.generateActorId(JobId.fromInt(1));
RayPyActor pyActor = new RayPyActorImpl(pyActorId, "test", "RaySerializerTest");
byte[] bytes = Serializer.encode(pyActor);
RayPyActor result = Serializer.decode(bytes);
@@ -3,6 +3,7 @@ package org.ray.api.test;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.annotation.RayRemote;
import org.ray.api.id.ActorId;
import org.ray.api.id.JobId;
import org.ray.api.id.UniqueId;
import org.testng.Assert;
@@ -41,7 +42,7 @@ public class RuntimeContextTest extends BaseTest {
@RayRemote
public static class RuntimeContextTester {
public String testRuntimeContext(UniqueId actorId) {
public String testRuntimeContext(ActorId actorId) {
Assert.assertEquals(JOB_ID, Ray.getRuntimeContext().getCurrentJobId());
Assert.assertEquals(actorId, Ray.getRuntimeContext().getCurrentActorId());
Assert.assertEquals(RAYLET_SOCKET_NAME, Ray.getRuntimeContext().getRayletSocketName());
@@ -3,6 +3,7 @@ package org.ray.api.test;
import java.nio.ByteBuffer;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
import org.ray.api.id.ObjectId;
import org.ray.api.id.TaskId;
import org.ray.api.id.UniqueId;
@@ -52,49 +53,26 @@ public class UniqueIdTest {
@Test
public void testComputeReturnId() {
// Mock a taskId, and the lowest 4 bytes should be 0.
TaskId taskId = TaskId.fromHexString("123456789ABCDEF123456789ABCDEF00");
TaskId taskId = TaskId.fromHexString("123456789ABCDE123456789ABCDE");
ObjectId returnId = IdUtil.computeReturnId(taskId, 1);
Assert.assertEquals("123456789abcdef123456789abcdef0001000000", returnId.toString());
ObjectId returnId = ObjectId.forReturn(taskId, 1);
Assert.assertEquals("123456789abcde123456789abcde00c001000000", returnId.toString());
Assert.assertEquals(returnId.getTaskId(), taskId);
returnId = IdUtil.computeReturnId(taskId, 0x01020304);
Assert.assertEquals("123456789abcdef123456789abcdef0004030201", returnId.toString());
}
@Test
public void testComputeTaskId() {
ObjectId objId = ObjectId.fromHexString("123456789ABCDEF123456789ABCDEF0034421980");
TaskId taskId = objId.getTaskId();
Assert.assertEquals("123456789abcdef123456789abcdef00", taskId.toString());
returnId = ObjectId.forReturn(taskId, 0x01020304);
Assert.assertEquals("123456789abcde123456789abcde00c004030201", returnId.toString());
}
@Test
public void testComputePutId() {
// Mock a taskId, the lowest 4 bytes should be 0.
TaskId taskId = TaskId.fromHexString("123456789ABCDEF123456789ABCDEF00");
TaskId taskId = TaskId.fromHexString("123456789ABCDE123456789ABCDE");
ObjectId putId = IdUtil.computePutId(taskId, 1);
Assert.assertEquals("123456789ABCDEF123456789ABCDEF00FFFFFFFF".toLowerCase(), putId.toString());
ObjectId putId = ObjectId.forPut(taskId, 1);
Assert.assertEquals("123456789abcde123456789abcde008001000000".toLowerCase(), putId.toString());
putId = IdUtil.computePutId(taskId, 0x01020304);
Assert.assertEquals("123456789ABCDEF123456789ABCDEF00FCFCFDFE".toLowerCase(), putId.toString());
}
@Test
public void testUniqueIdsAndByteBufferInterConversion() {
final int len = 5;
UniqueId[] ids = new UniqueId[len];
for (int i = 0; i < len; ++i) {
ids[i] = UniqueId.randomId();
}
ByteBuffer temp = IdUtil.concatIds(ids);
UniqueId[] res = IdUtil.getUniqueIdsFromByteBuffer(temp);
for (int i = 0; i < len; ++i) {
Assert.assertEquals(ids[i], res[i]);
}
putId = ObjectId.forPut(taskId, 0x01020304);
Assert.assertEquals("123456789abcde123456789abcde008004030201".toLowerCase(), putId.toString());
}
@Test
@@ -104,24 +82,4 @@ public class UniqueIdTest {
Assert.assertEquals(remainder, 787616861);
}
@Test
void testConcateIds() {
String taskHexStr = "123456789ABCDEF123456789ABCDEF00";
String objectHexStr = taskHexStr + "01020304";
ObjectId objectId1 = ObjectId.fromHexString(objectHexStr);
ObjectId objectId2 = ObjectId.fromHexString(objectHexStr);
TaskId[] taskIds = new TaskId[2];
taskIds[0] = objectId1.getTaskId();
taskIds[1] = objectId2.getTaskId();
ObjectId[] objectIds = new ObjectId[2];
objectIds[0] = objectId1;
objectIds[1] = objectId2;
String taskHexCompareStr = taskHexStr + taskHexStr;
String objectHexCompareStr = objectHexStr + objectHexStr;
Assert.assertEquals(DatatypeConverter.printHexBinary(
IdUtil.concatIds(taskIds).array()), taskHexCompareStr);
Assert.assertEquals(DatatypeConverter.printHexBinary(
IdUtil.concatIds(objectIds).array()), objectHexCompareStr);
}
}