-
Notifications
You must be signed in to change notification settings - Fork 10
feat(spider-tdl): Add wire format to serialize/deserialize task inputs and task outputs. #308
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
Open
LinZhihao-723
wants to merge
6
commits into
y-scope:main
Choose a base branch
from
LinZhihao-723:wire-format
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f44d7f7
tdl-foundation implemented.
LinZhihao-723 0bbb8dd
Compare the error itself in a round trip (not the display).
LinZhihao-723 f2727d7
Merge branch 'main' into tdl-foundation
sitaowang1998 c9712fa
Address code review comments.
LinZhihao-723 a2e75fb
Implementation done.
LinZhihao-723 842cb8a
Use first_chunk
LinZhihao-723 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,17 @@ | ||
| [package] | ||
| name = "spider-tdl" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [lib] | ||
| name = "spider_tdl" | ||
| path = "src/lib.rs" | ||
|
|
||
| [dependencies] | ||
| rmp-serde = "1.3.1" | ||
| serde = { version = "1.0.228", features = ["derive"] } | ||
| spider-core = { path = "../spider-core" } | ||
| thiserror = "2.0.18" | ||
|
|
||
| [dev-dependencies] | ||
| anyhow = "1.0.98" |
This file contains hidden or 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,47 @@ | ||
| //! Error type returned from user-authored TDL tasks. | ||
| //! | ||
| //! [`TdlError`] crosses the C-FFI boundary as a msgpack-encoded payload inside | ||
| //! [`ExecutionResult::Error`], so it derives both [`serde::Serialize`] and [`serde::Deserialize`]. | ||
|
|
||
| /// All possible errors produced while executing a user-defined task inside the task executor. | ||
| /// This type can be serialized across the C-FFI boundary as a msgpack-encoded payload through | ||
| /// [`serde`]. | ||
| #[derive(Debug, Eq, PartialEq, thiserror::Error, serde::Serialize, serde::Deserialize)] | ||
| pub enum TdlError { | ||
| #[error("task not found: {0}")] | ||
| TaskNotFound(String), | ||
|
|
||
| #[error("deserialization error: {0}")] | ||
| DeserializationError(String), | ||
|
|
||
| #[error("serialization error: {0}")] | ||
| SerializationError(String), | ||
|
|
||
| #[error("execution error: {0}")] | ||
| ExecutionError(String), | ||
|
|
||
| #[error("{0}")] | ||
| Custom(String), | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::TdlError; | ||
|
|
||
| #[test] | ||
| fn round_trip_all_variants() -> anyhow::Result<()> { | ||
| let errors_to_test = [ | ||
| TdlError::TaskNotFound("task_not_found".to_owned()), | ||
| TdlError::DeserializationError("deserialization_error".to_owned()), | ||
| TdlError::SerializationError("serialization_error".to_owned()), | ||
| TdlError::ExecutionError("execution_error".to_owned()), | ||
| TdlError::Custom("custom".to_owned()), | ||
| ]; | ||
| for error in errors_to_test { | ||
| let encoded = rmp_serde::to_vec(&error)?; | ||
| let decoded: TdlError = rmp_serde::from_slice(&encoded)?; | ||
| assert_eq!(decoded, error); | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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,307 @@ | ||
| //! `#[repr(C)]` types shared across the TDL package / task executor C-FFI boundary. | ||
| //! | ||
| //! Both sides of the boundary live in the same process and share the same Rust global allocator, | ||
| //! so buffers allocated on one side can be reclaimed on the other via [`Box::into_raw`] / | ||
| //! [`Box::from_raw`]. | ||
|
|
||
| use std::{ffi::c_char, fmt, marker::PhantomData, mem::ManuallyDrop, ops::Deref, str::Utf8Error}; | ||
|
|
||
| /// Borrowed, C-ABI-compatible view of a contiguous slice `&'borrow_lifetime [ElementType]`. | ||
| /// | ||
| /// [`CArray`] implements [`Deref<Target = [ElementType]>`][Deref] so that all slice methods | ||
| /// are available directly, without requiring a conversion call. | ||
| /// | ||
| /// # Type Parameters | ||
| /// | ||
| /// * `'borrow_lifetime` - The lifetime of the borrowed slice. | ||
| /// * `ElementType` - The type of the element inside the slice. | ||
| #[repr(C)] | ||
| pub struct CArray<'borrow_lifetime, ElementType> { | ||
| pointer: *const ElementType, | ||
| length: usize, | ||
| _lifetime: PhantomData<&'borrow_lifetime [ElementType]>, | ||
| } | ||
|
|
||
| // Manual `Copy`/`Clone` impls avoid the auto-derived `ElementType: Copy` / `ElementType: Clone` | ||
| // bounds: a borrowed pointer/length pair is always trivially copyable regardless of the element | ||
| // type. | ||
| impl<ElementType> Copy for CArray<'_, ElementType> {} | ||
|
|
||
| impl<ElementType> Clone for CArray<'_, ElementType> { | ||
| fn clone(&self) -> Self { | ||
| *self | ||
| } | ||
| } | ||
|
|
||
| impl<ElementType> fmt::Debug for CArray<'_, ElementType> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("CArray") | ||
| .field("pointer", &self.pointer) | ||
| .field("length", &self.length) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl<'borrow_lifetime, ElementType> CArray<'borrow_lifetime, ElementType> { | ||
| /// Borrows `slice` as a C-ABI array view. | ||
| /// | ||
| /// The returned [`CArray`] is tied to the lifetime of `slice`; the pointer remains valid as | ||
| /// long as the original slice is not moved or dropped. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The constructed C array from the given slice. | ||
| #[must_use] | ||
| pub const fn from_slice(slice: &'borrow_lifetime [ElementType]) -> Self { | ||
| Self { | ||
| pointer: slice.as_ptr(), | ||
| length: slice.len(), | ||
| _lifetime: PhantomData, | ||
| } | ||
| } | ||
|
|
||
| /// Reconstructs a Rust slice from the raw pointer and length. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A slice of `length` elements starting at `pointer`. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must guarantee that: | ||
| /// | ||
| /// * `pointer` points to a single, contiguous allocation of at least `length` elements of | ||
| /// `ElementType`, properly initialized. | ||
| /// * The memory remains valid and immutable for the returned lifetime. | ||
| #[must_use] | ||
| pub const fn as_slice(&self) -> &'borrow_lifetime [ElementType] { | ||
| unsafe { std::slice::from_raw_parts(self.pointer, self.length) } | ||
| } | ||
|
LinZhihao-723 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl<ElementType> Deref for CArray<'_, ElementType> { | ||
| type Target = [ElementType]; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.as_slice() | ||
| } | ||
| } | ||
|
|
||
| /// Borrowed view of a UTF-8 string as a `char`-typed C array. | ||
| pub type CCharArray<'borrow_lifetime> = CArray<'borrow_lifetime, c_char>; | ||
|
|
||
| impl<'borrow_lifetime> CCharArray<'borrow_lifetime> { | ||
| /// Borrows UTF8-encoded string `s` as a C-ABI char array view. | ||
| /// | ||
| /// The returned view is **not** NUL-terminated; both sides of the FFI boundary rely on the | ||
| /// explicit `length` field rather than a terminator. | ||
| /// | ||
| /// Equivalent to `std::string_view` in C++. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The constructed C char array from the given `&str`. | ||
| #[must_use] | ||
| pub const fn from_utf8(s: &'borrow_lifetime str) -> Self { | ||
| Self { | ||
| pointer: s.as_ptr().cast::<c_char>(), | ||
| length: s.len(), | ||
| _lifetime: PhantomData, | ||
| } | ||
| } | ||
|
|
||
| /// Reconstructs a UTF8-encoded Rust `&str` from the raw pointer and length. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `&str` view of the underlying C char array on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * Forwards [`str::from_utf8`]'s return values on failure. | ||
| pub const fn as_utf8(&self) -> Result<&'borrow_lifetime str, Utf8Error> { | ||
| let bytes: &[u8] = | ||
| unsafe { std::slice::from_raw_parts(self.pointer.cast::<u8>(), self.length) }; | ||
| str::from_utf8(bytes) | ||
| } | ||
| } | ||
|
|
||
| /// Borrowed view of a raw byte buffer. | ||
| pub type CByteArray<'borrow_lifetime> = CArray<'borrow_lifetime, u8>; | ||
|
|
||
| /// Owned, C-ABI-compatible result buffer returned from a TDL task execution. | ||
| /// | ||
| /// The buffer is allocated on the TDL-package side by leaking a `Box<[u8]>` via [`Box::into_raw`] | ||
| /// and reclaimed on the executor side via [`Box::from_raw`]. This only works because both sides | ||
| /// share the same global allocator, which is true when the package is loaded via `dlopen` into | ||
| /// the executor process. | ||
| #[repr(C)] | ||
| pub struct TaskExecutionResult { | ||
| is_error: bool, | ||
| pointer: *mut u8, | ||
| length: usize, | ||
| } | ||
|
|
||
| impl TaskExecutionResult { | ||
| /// Constructs a successful result wrapping wire-format-encoded output bytes. | ||
| #[must_use] | ||
| pub fn from_outputs(bytes: Vec<u8>) -> Self { | ||
| Self::from_buffer(false, bytes) | ||
| } | ||
|
|
||
| /// Constructs a failing result wrapping msgpack-encoded [`TdlError`](crate::TdlError) bytes. | ||
| #[must_use] | ||
| pub fn from_error(bytes: Vec<u8>) -> Self { | ||
| Self::from_buffer(true, bytes) | ||
| } | ||
|
|
||
| /// Reclaims ownership of the leaked buffer and returns it. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// `Ok(bytes)` on success, where `bytes` is the wire-format output payload produced by the | ||
| /// user task. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns `Err(bytes)` if the result represented failure, where `bytes` is a msgpack-encoded | ||
| /// [`crate::TdlError`] produced inside the TDL package. The caller is responsible for decoding | ||
| /// it via [`rmp_serde::from_slice`]. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must guarantee that `self.pointer` / `self.length` originated from a prior call | ||
| /// to [`Self::from_outputs`] or [`Self::from_error`] in a component that shares this process's | ||
| /// global allocator. | ||
| pub fn into_result(self) -> Result<Vec<u8>, Vec<u8>> { | ||
| // Prevent the destructor from running after we reconstruct the `Box`. | ||
| let this = ManuallyDrop::new(self); | ||
| let boxed: Box<[u8]> = unsafe { | ||
| Box::from_raw(std::ptr::slice_from_raw_parts_mut( | ||
| this.pointer, | ||
| this.length, | ||
| )) | ||
| }; | ||
| let vec = boxed.into_vec(); | ||
| if this.is_error { Err(vec) } else { Ok(vec) } | ||
| } | ||
|
LinZhihao-723 marked this conversation as resolved.
|
||
|
|
||
| fn from_buffer(is_error: bool, buffer: Vec<u8>) -> Self { | ||
| let boxed: Box<[u8]> = buffer.into_boxed_slice(); | ||
| let length = boxed.len(); | ||
| let pointer = Box::into_raw(boxed).cast::<u8>(); | ||
| Self { | ||
| is_error, | ||
| pointer, | ||
| length, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::TdlError; | ||
|
|
||
| #[test] | ||
| fn c_byte_array_round_trip() { | ||
| let data: [u8; 5] = [1, 2, 3, 4, 5]; | ||
| let view = CByteArray::from_slice(&data); | ||
| assert_eq!(view.len(), 5); | ||
| assert!(!view.is_empty()); | ||
| let reconstructed = view.as_slice(); | ||
| assert_eq!(reconstructed, &data[..]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn c_byte_array_deref_to_slice() { | ||
| let data: [u8; 4] = [10, 20, 30, 40]; | ||
| let view = CByteArray::from_slice(&data); | ||
| // Slice methods are available directly via `Deref`. | ||
| assert_eq!(view.iter().sum::<u8>(), 100); | ||
| assert_eq!(view[1], 20); | ||
| assert_eq!(&view[..2], &[10, 20]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn c_byte_array_deserializes_msgpack_via_deref() -> anyhow::Result<()> { | ||
| #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)] | ||
| struct Point { | ||
| x: i32, | ||
| y: i32, | ||
| } | ||
| let original = Point { x: -7, y: 42 }; | ||
| let encoded = rmp_serde::to_vec(&original)?; | ||
|
|
||
| let view = CByteArray::from_slice(&encoded); | ||
| // `&*view` triggers `Deref`, yielding `&[u8]` directly — no `as_slice()` call. | ||
| let decoded: Point = rmp_serde::from_slice(&view)?; | ||
|
|
||
| assert_eq!(decoded, original); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn c_byte_array_empty() { | ||
| let data: [u8; 0] = []; | ||
| let view = CByteArray::from_slice(&data); | ||
| assert_eq!(view.len(), 0); | ||
| assert!(view.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn c_char_array_round_trip() -> anyhow::Result<()> { | ||
| let original = "hello, TDL"; | ||
| let view = CCharArray::from_utf8(original); | ||
| assert_eq!(view.len(), original.len()); | ||
| let reconstructed = view.as_utf8()?; | ||
| assert_eq!(reconstructed, original); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn c_char_array_invalid_utf8() { | ||
| // 0xFF is never valid in any position of a UTF-8 sequence. | ||
| let invalid_bytes: &[u8] = &[0x68, 0x65, 0xff, 0x6c, 0x6f]; | ||
| // Reinterpret as `&[c_char]` to go through `CArray::from_slice` rather than `from_utf8`. | ||
| let c_chars: &[std::ffi::c_char] = unsafe { | ||
| std::slice::from_raw_parts(invalid_bytes.as_ptr().cast(), invalid_bytes.len()) | ||
| }; | ||
| let view = CCharArray::from_slice(c_chars); | ||
| assert_eq!(view.len(), 5); | ||
| assert!(view.as_utf8().is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn task_execution_result_success_round_trip() { | ||
| let payload = vec![10u8, 20, 30, 40]; | ||
| let expected = payload.clone(); | ||
| let result = TaskExecutionResult::from_outputs(payload); | ||
| let reclaimed = result.into_result(); | ||
| assert_eq!(reclaimed, Ok(expected)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn task_execution_result_error_round_trip() -> anyhow::Result<()> { | ||
| let error = TdlError::Custom("custom task execution error".to_owned()); | ||
| let payload = rmp_serde::to_vec(&error)?; | ||
| let result = TaskExecutionResult::from_error(payload); | ||
| let reclaimed = result.into_result(); | ||
| if let Err(payload) = reclaimed { | ||
| let decoded: TdlError = rmp_serde::from_slice(&payload)?; | ||
| assert_eq!(decoded.to_string(), error.to_string()); | ||
| } else { | ||
| panic!("reclaimed payload did not match original"); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn task_execution_result_empty_buffer() { | ||
| let result = TaskExecutionResult::from_outputs(Vec::new()); | ||
| let reclaimed = result.into_result(); | ||
| assert_eq!(reclaimed, Ok(Vec::new())); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.