mirror of
https://github.com/wassname/ray.git
synced 2026-06-27 21:38:18 +08:00
[Java worker] Migrate task execution and submission on top of core worker (#5370)
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
package org.ray.api;
|
||||
|
||||
public enum ObjectType {
|
||||
PUT_OBJECT,
|
||||
RETURN_OBJECT,
|
||||
}
|
||||
@@ -2,14 +2,11 @@ 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 int LENGTH = 8;
|
||||
|
||||
public static final ActorId NIL = nil();
|
||||
|
||||
@@ -25,19 +22,6 @@ public class ActorId extends BaseId implements Serializable {
|
||||
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.
|
||||
*/
|
||||
@@ -47,6 +31,15 @@ public class ActorId extends BaseId implements Serializable {
|
||||
return new ActorId(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an ActorId with random value. Used for local mode and test only.
|
||||
*/
|
||||
public static ActorId fromRandom() {
|
||||
byte[] b = new byte[LENGTH];
|
||||
new Random().nextBytes(b);
|
||||
return new ActorId(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return LENGTH;
|
||||
|
||||
@@ -2,10 +2,8 @@ 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.
|
||||
@@ -16,20 +14,6 @@ public class ObjectId extends BaseId implements Serializable {
|
||||
|
||||
public static final ObjectId NIL = genNil();
|
||||
|
||||
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.
|
||||
*/
|
||||
@@ -55,48 +39,6 @@ public class ObjectId extends BaseId implements Serializable {
|
||||
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);
|
||||
}
|
||||
@@ -106,25 +48,4 @@ public class ObjectId extends BaseId implements Serializable {
|
||||
return LENGTH;
|
||||
}
|
||||
|
||||
public TaskId getTaskId() {
|
||||
byte[] taskIdBytes = Arrays.copyOf(getBytes(), TaskId.LENGTH);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,9 +11,7 @@ import java.util.Random;
|
||||
*/
|
||||
public class TaskId extends BaseId implements Serializable {
|
||||
|
||||
private static final int UNIQUE_BYTES_LENGTH = 6;
|
||||
|
||||
public static final int LENGTH = UNIQUE_BYTES_LENGTH + ActorId.LENGTH;
|
||||
public static final int LENGTH = 14;
|
||||
|
||||
public static final TaskId NIL = genNil();
|
||||
|
||||
@@ -38,15 +36,6 @@ public class TaskId extends BaseId implements Serializable {
|
||||
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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user