mirror of
https://github.com/wassname/ray.git
synced 2026-07-06 05:16:30 +08:00
Refactor ID Serial 1: Separate ObjectID and TaskID from UniqueID (#4776)
* Enable BaseId. * Change TaskID and make python test pass * Remove unnecessary functions and fix test failure and change TaskID to 16 bytes. * Java code change draft * Refine * Lint * 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/BaseId.java Co-Authored-By: Hao Chen <chenh1024@gmail.com> * Update java/api/src/main/java/org/ray/api/id/BaseId.java Co-Authored-By: Hao Chen <chenh1024@gmail.com> * Update java/api/src/main/java/org/ray/api/id/ObjectId.java Co-Authored-By: Hao Chen <chenh1024@gmail.com> * Address comment * Lint * Fix SINGLE_PROCESS * Fix comments * Refine code * Refine test * Resolve conflict
This commit is contained in:
@@ -6,7 +6,7 @@ import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.TestUtils;
|
||||
import org.ray.api.exception.RayException;
|
||||
import org.ray.api.id.UniqueId;
|
||||
import org.ray.api.id.ObjectId;
|
||||
import org.ray.runtime.RayObjectImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -20,7 +20,7 @@ public class ClientExceptionTest extends BaseTest {
|
||||
@Test
|
||||
public void testWaitAndCrash() {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
UniqueId randomId = UniqueId.randomId();
|
||||
ObjectId randomId = ObjectId.randomId();
|
||||
RayObject<String> notExisting = new RayObjectImpl(randomId);
|
||||
|
||||
Thread thread = new Thread(() -> {
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.id.UniqueId;
|
||||
import org.ray.api.id.ObjectId;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ObjectStoreTest extends BaseTest {
|
||||
@Test
|
||||
public void testGetMultipleObjects() {
|
||||
List<Integer> ints = ImmutableList.of(1, 2, 3, 4, 5);
|
||||
List<UniqueId> ids = ints.stream().map(obj -> Ray.put(obj).getId())
|
||||
List<ObjectId> ids = ints.stream().map(obj -> Ray.put(obj).getId())
|
||||
.collect(Collectors.toList());
|
||||
Assert.assertEquals(ints, Ray.get(ids));
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.ray.api.RayObject;
|
||||
import org.ray.api.TestUtils;
|
||||
import org.ray.api.annotation.RayRemote;
|
||||
import org.ray.runtime.AbstractRayRuntime;
|
||||
import org.ray.runtime.util.UniqueIdUtil;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -38,7 +37,7 @@ public class PlasmaFreeTest extends BaseTest {
|
||||
|
||||
final boolean result = TestUtils.waitForCondition(
|
||||
() -> !(((AbstractRayRuntime)Ray.internal()).getGcsClient())
|
||||
.rayletTaskExistsInGcs(UniqueIdUtil.computeTaskId(helloId.getId())), 50);
|
||||
.rayletTaskExistsInGcs(helloId.getId().getTaskId()), 50);
|
||||
Assert.assertTrue(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.ray.api.Ray;
|
||||
import org.ray.api.RayActor;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.TestUtils;
|
||||
import org.ray.api.id.UniqueId;
|
||||
import org.ray.api.id.ObjectId;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -23,7 +23,7 @@ public class StressTest extends BaseTest {
|
||||
for (int numIterations : ImmutableList.of(1, 10, 100, 1000)) {
|
||||
int numTasks = 1000 / numIterations;
|
||||
for (int i = 0; i < numIterations; i++) {
|
||||
List<UniqueId> resultIds = new ArrayList<>();
|
||||
List<ObjectId> resultIds = new ArrayList<>();
|
||||
for (int j = 0; j < numTasks; j++) {
|
||||
resultIds.add(Ray.call(StressTest::echo, 1).getId());
|
||||
}
|
||||
@@ -60,7 +60,7 @@ public class StressTest extends BaseTest {
|
||||
}
|
||||
|
||||
public int ping(int n) {
|
||||
List<UniqueId> objectIds = new ArrayList<>();
|
||||
List<ObjectId> objectIds = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
objectIds.add(Ray.call(Actor::ping, actor).getId());
|
||||
}
|
||||
@@ -76,7 +76,7 @@ public class StressTest extends BaseTest {
|
||||
public void testSubmittingManyTasksToOneActor() {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
RayActor<Actor> actor = Ray.createActor(Actor::new);
|
||||
List<UniqueId> objectIds = new ArrayList<>();
|
||||
List<ObjectId> objectIds = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
RayActor<Worker> worker = Ray.createActor(Worker::new, actor);
|
||||
objectIds.add(Ray.call(Worker::ping, worker, 100).getId());
|
||||
|
||||
@@ -3,8 +3,10 @@ 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;
|
||||
import org.ray.runtime.util.UniqueIdUtil;
|
||||
import org.ray.runtime.util.IdUtil;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -42,7 +44,7 @@ public class UniqueIdTest {
|
||||
|
||||
|
||||
// Test `genNil()`
|
||||
UniqueId id6 = UniqueId.genNil();
|
||||
UniqueId id6 = UniqueId.NIL;
|
||||
Assert.assertEquals("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".toLowerCase(), id6.toString());
|
||||
Assert.assertTrue(id6.isNil());
|
||||
}
|
||||
@@ -50,33 +52,33 @@ public class UniqueIdTest {
|
||||
@Test
|
||||
public void testComputeReturnId() {
|
||||
// Mock a taskId, and the lowest 4 bytes should be 0.
|
||||
UniqueId taskId = UniqueId.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
|
||||
TaskId taskId = TaskId.fromHexString("123456789ABCDEF123456789ABCDEF00");
|
||||
|
||||
UniqueId returnId = UniqueIdUtil.computeReturnId(taskId, 1);
|
||||
Assert.assertEquals("01000000123456789abcdef123456789abcdef00", returnId.toString());
|
||||
ObjectId returnId = IdUtil.computeReturnId(taskId, 1);
|
||||
Assert.assertEquals("123456789abcdef123456789abcdef0001000000", returnId.toString());
|
||||
|
||||
returnId = UniqueIdUtil.computeReturnId(taskId, 0x01020304);
|
||||
Assert.assertEquals("04030201123456789abcdef123456789abcdef00", returnId.toString());
|
||||
returnId = IdUtil.computeReturnId(taskId, 0x01020304);
|
||||
Assert.assertEquals("123456789abcdef123456789abcdef0004030201", returnId.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputeTaskId() {
|
||||
UniqueId objId = UniqueId.fromHexString("34421980123456789ABCDEF123456789ABCDEF00");
|
||||
UniqueId taskId = UniqueIdUtil.computeTaskId(objId);
|
||||
ObjectId objId = ObjectId.fromHexString("123456789ABCDEF123456789ABCDEF0034421980");
|
||||
TaskId taskId = objId.getTaskId();
|
||||
|
||||
Assert.assertEquals("00000000123456789abcdef123456789abcdef00", taskId.toString());
|
||||
Assert.assertEquals("123456789abcdef123456789abcdef00", taskId.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComputePutId() {
|
||||
// Mock a taskId, the lowest 4 bytes should be 0.
|
||||
UniqueId taskId = UniqueId.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
|
||||
TaskId taskId = TaskId.fromHexString("123456789ABCDEF123456789ABCDEF00");
|
||||
|
||||
UniqueId putId = UniqueIdUtil.computePutId(taskId, 1);
|
||||
Assert.assertEquals("FFFFFFFF123456789ABCDEF123456789ABCDEF00".toLowerCase(), putId.toString());
|
||||
ObjectId putId = IdUtil.computePutId(taskId, 1);
|
||||
Assert.assertEquals("123456789ABCDEF123456789ABCDEF00FFFFFFFF".toLowerCase(), putId.toString());
|
||||
|
||||
putId = UniqueIdUtil.computePutId(taskId, 0x01020304);
|
||||
Assert.assertEquals("FCFCFDFE123456789ABCDEF123456789ABCDEF00".toLowerCase(), putId.toString());
|
||||
putId = IdUtil.computePutId(taskId, 0x01020304);
|
||||
Assert.assertEquals("123456789ABCDEF123456789ABCDEF00FCFCFDFE".toLowerCase(), putId.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -87,8 +89,8 @@ public class UniqueIdTest {
|
||||
ids[i] = UniqueId.randomId();
|
||||
}
|
||||
|
||||
ByteBuffer temp = UniqueIdUtil.concatUniqueIds(ids);
|
||||
UniqueId[] res = UniqueIdUtil.getUniqueIdsFromByteBuffer(temp);
|
||||
ByteBuffer temp = IdUtil.concatIds(ids);
|
||||
UniqueId[] res = IdUtil.getUniqueIdsFromByteBuffer(temp);
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Assert.assertEquals(ids[i], res[i]);
|
||||
@@ -98,8 +100,28 @@ public class UniqueIdTest {
|
||||
@Test
|
||||
void testMurmurHash() {
|
||||
UniqueId id = UniqueId.fromHexString("3131313131313131313132323232323232323232");
|
||||
long remainder = Long.remainderUnsigned(UniqueIdUtil.murmurHashCode(id), 1000000000);
|
||||
long remainder = Long.remainderUnsigned(IdUtil.murmurHashCode(id), 1000000000);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user