-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support metadata for passing by value task arguments #5527
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fd8c082
Enable metadata for passing by value
kfstorm 2a39d58
Fix core worker test
kfstorm 487c13e
Merge remote-tracking branch 'upstream/master' into pass_by_value_met…
kfstorm ae75b5e
Fix typo
kfstorm be4eb1e
lint
kfstorm d3c7c90
clean
kfstorm e7d4753
Simplify code
kfstorm 06661ad
Fix build
kfstorm f99dae6
Add todo
kfstorm 9f492de
Address comments
kfstorm 0bd5697
Merge remote-tracking branch 'upstream/master' into pass_by_value_met…
kfstorm c8597dd
Merge remote-tracking branch 'upstream/master' into pass_by_value_met…
kfstorm 6ce3429
Address comments
kfstorm b62f4c3
Avoid empty buffer in RayObject
kfstorm bcb6fb1
Merge remote-tracking branch 'upstream/master' into pass_by_value_met…
kfstorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 16 additions & 1 deletion
17
java/runtime/src/main/java/org/ray/runtime/object/NativeRayObject.java
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 |
---|---|---|
@@ -1,16 +1,31 @@ | ||
package org.ray.runtime.object; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
/** | ||
* Binary representation of ray object. | ||
* Binary representation of a ray object. See `RayObject` class in C++ for details. | ||
*/ | ||
public class NativeRayObject { | ||
|
||
public byte[] data; | ||
public byte[] metadata; | ||
|
||
public NativeRayObject(byte[] data, byte[] metadata) { | ||
Preconditions.checkState(bufferLength(data) > 0 || bufferLength(metadata) > 0); | ||
this.data = data; | ||
this.metadata = metadata; | ||
} | ||
|
||
private static int bufferLength(byte[] buffer) { | ||
if (buffer == null) { | ||
return 0; | ||
} | ||
return buffer.length; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "<data>: " + bufferLength(data) + ", <metadata>: " + bufferLength(metadata); | ||
} | ||
} | ||
|
83 changes: 83 additions & 0 deletions
83
java/runtime/src/main/java/org/ray/runtime/object/ObjectSerializer.java
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,83 @@ | ||
package org.ray.runtime.object; | ||
|
||
import java.util.Arrays; | ||
import org.ray.api.exception.RayActorException; | ||
import org.ray.api.exception.RayTaskException; | ||
import org.ray.api.exception.RayWorkerException; | ||
import org.ray.api.exception.UnreconstructableException; | ||
import org.ray.api.id.ObjectId; | ||
import org.ray.runtime.generated.Gcs.ErrorType; | ||
import org.ray.runtime.util.Serializer; | ||
|
||
/** | ||
* Serialize to and deserialize from {@link NativeRayObject}. Metadata is generated during | ||
* serialization and respected during deserialization. | ||
*/ | ||
public class ObjectSerializer { | ||
|
||
private static final byte[] WORKER_EXCEPTION_META = String | ||
.valueOf(ErrorType.WORKER_DIED.getNumber()).getBytes(); | ||
private static final byte[] ACTOR_EXCEPTION_META = String | ||
.valueOf(ErrorType.ACTOR_DIED.getNumber()).getBytes(); | ||
private static final byte[] UNRECONSTRUCTABLE_EXCEPTION_META = String | ||
.valueOf(ErrorType.OBJECT_UNRECONSTRUCTABLE.getNumber()).getBytes(); | ||
|
||
private static final byte[] TASK_EXECUTION_EXCEPTION_META = String | ||
.valueOf(ErrorType.TASK_EXECUTION_EXCEPTION.getNumber()).getBytes(); | ||
|
||
private static final byte[] RAW_TYPE_META = "RAW".getBytes(); | ||
|
||
/** | ||
* Deserialize an object from an {@link NativeRayObject} instance. | ||
* | ||
* @param nativeRayObject The object to deserialize. | ||
* @param objectId The associated object ID of the object. | ||
* @param classLoader The classLoader of the object. | ||
* @return The deserialized object. | ||
*/ | ||
public static Object deserialize(NativeRayObject nativeRayObject, ObjectId objectId, | ||
ClassLoader classLoader) { | ||
byte[] meta = nativeRayObject.metadata; | ||
byte[] data = nativeRayObject.data; | ||
|
||
if (meta != null && meta.length > 0) { | ||
// If meta is not null, deserialize the object from meta. | ||
if (Arrays.equals(meta, RAW_TYPE_META)) { | ||
return data; | ||
} else if (Arrays.equals(meta, WORKER_EXCEPTION_META)) { | ||
return RayWorkerException.INSTANCE; | ||
} else if (Arrays.equals(meta, ACTOR_EXCEPTION_META)) { | ||
return RayActorException.INSTANCE; | ||
} else if (Arrays.equals(meta, UNRECONSTRUCTABLE_EXCEPTION_META)) { | ||
return new UnreconstructableException(objectId); | ||
} else if (Arrays.equals(meta, TASK_EXECUTION_EXCEPTION_META)) { | ||
return Serializer.decode(data, classLoader); | ||
} | ||
throw new IllegalArgumentException("Unrecognized metadata " + Arrays.toString(meta)); | ||
} else { | ||
// If data is not null, deserialize the Java object. | ||
return Serializer.decode(data, classLoader); | ||
} | ||
} | ||
|
||
/** | ||
* Serialize an Java object to an {@link NativeRayObject} instance. | ||
* | ||
* @param object The object to serialize. | ||
* @return The serialized object. | ||
*/ | ||
public static NativeRayObject serialize(Object object) { | ||
if (object instanceof NativeRayObject) { | ||
return (NativeRayObject) object; | ||
} else if (object instanceof byte[]) { | ||
// If the object is a byte array, skip serializing it and use a special metadata to | ||
// indicate it's raw binary. So that this object can also be read by Python. | ||
return new NativeRayObject((byte[]) object, RAW_TYPE_META); | ||
} else if (object instanceof RayTaskException) { | ||
return new NativeRayObject(Serializer.encode(object), | ||
TASK_EXECUTION_EXCEPTION_META); | ||
} else { | ||
return new NativeRayObject(Serializer.encode(object), null); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
meta != null
isn't needed now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I'll make null pointer valid, it is needed.