-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
57 changed files
with
1,076 additions
and
644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.ray.api.id; | ||
|
||
import java.io.Serializable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import javax.xml.bind.DatatypeConverter; | ||
|
||
public abstract class BaseId implements Serializable { | ||
private static final long serialVersionUID = 8588849129675565761L; | ||
private final byte[] id; | ||
private int hashCodeCache = 0; | ||
private Boolean isNilCache = null; | ||
|
||
/** | ||
* Create a BaseId instance according to the input byte array. | ||
*/ | ||
public BaseId(byte[] id) { | ||
if (id.length != size()) { | ||
throw new IllegalArgumentException("Failed to construct BaseId, expect " + size() | ||
+ " bytes, but got " + id.length + " bytes."); | ||
} | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Get the byte data of this id. | ||
*/ | ||
public byte[] getBytes() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Convert the byte data to a ByteBuffer. | ||
*/ | ||
public ByteBuffer toByteBuffer() { | ||
return ByteBuffer.wrap(id); | ||
} | ||
|
||
/** | ||
* @return True if this id is nil. | ||
*/ | ||
public boolean isNil() { | ||
if (isNilCache == null) { | ||
isNilCache = true; | ||
for (int i = 0; i < size(); ++i) { | ||
if (id[i] != (byte) 0xff) { | ||
isNilCache = false; | ||
break; | ||
} | ||
} | ||
} | ||
return isNilCache; | ||
} | ||
|
||
/** | ||
* Derived class should implement this function. | ||
* @return The length of this id in bytes. | ||
*/ | ||
public abstract int size(); | ||
|
||
@Override | ||
public int hashCode() { | ||
// Lazy evaluation. | ||
if (hashCodeCache == 0) { | ||
hashCodeCache = Arrays.hashCode(id); | ||
} | ||
return hashCodeCache; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
|
||
if (!this.getClass().equals(obj.getClass())) { | ||
return false; | ||
} | ||
|
||
BaseId r = (BaseId) obj; | ||
return Arrays.equals(id, r.id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return DatatypeConverter.printHexBinary(id).toLowerCase(); | ||
} | ||
|
||
protected static byte[] hexString2Bytes(String hex) { | ||
return DatatypeConverter.parseHexBinary(hex); | ||
} | ||
|
||
protected static byte[] byteBuffer2Bytes(ByteBuffer bb) { | ||
byte[] id = new byte[bb.remaining()]; | ||
bb.get(id); | ||
return id; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.ray.api.id; | ||
|
||
import java.io.Serializable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Random; | ||
|
||
/** | ||
* Represents the id of a Ray object. | ||
*/ | ||
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)); | ||
} | ||
|
||
/** | ||
* Create an ObjectId from a ByteBuffer. | ||
*/ | ||
public static ObjectId fromByteBuffer(ByteBuffer bb) { | ||
return new ObjectId(byteBuffer2Bytes(bb)); | ||
} | ||
|
||
/** | ||
* Generate a nil ObjectId. | ||
*/ | ||
private static ObjectId genNil() { | ||
byte[] b = new byte[LENGTH]; | ||
Arrays.fill(b, (byte) 0xFF); | ||
return new ObjectId(b); | ||
} | ||
|
||
/** | ||
* Generate an ObjectId with random value. | ||
*/ | ||
public static ObjectId randomId() { | ||
byte[] b = new byte[LENGTH]; | ||
new Random().nextBytes(b); | ||
return new ObjectId(b); | ||
} | ||
|
||
public ObjectId(byte[] id) { | ||
super(id); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return LENGTH; | ||
} | ||
|
||
public TaskId getTaskId() { | ||
byte[] taskIdBytes = Arrays.copyOf(getBytes(), TaskId.LENGTH); | ||
return new TaskId(taskIdBytes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.ray.api.id; | ||
|
||
import java.io.Serializable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Random; | ||
|
||
/** | ||
* Represents the id of a Ray task. | ||
*/ | ||
public class TaskId extends BaseId implements Serializable { | ||
|
||
public static final int LENGTH = 16; | ||
public static final TaskId NIL = genNil(); | ||
|
||
/** | ||
* Create a TaskId from a hex string. | ||
*/ | ||
public static TaskId fromHexString(String hex) { | ||
return new TaskId(hexString2Bytes(hex)); | ||
} | ||
|
||
/** | ||
* Creates a TaskId from a ByteBuffer. | ||
*/ | ||
public static TaskId fromByteBuffer(ByteBuffer bb) { | ||
return new TaskId(byteBuffer2Bytes(bb)); | ||
} | ||
|
||
/** | ||
* Generate a nil TaskId. | ||
*/ | ||
private static TaskId genNil() { | ||
byte[] b = new byte[LENGTH]; | ||
Arrays.fill(b, (byte) 0xFF); | ||
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) { | ||
super(id); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return LENGTH; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.