cuddly-jelly-27016
03/03/2025, 7:05 PMflyteidl-rust as a Python binding. The changes address issues raised in issues and serve as a foundation for the PR.
What changes were proposed in this pull request?
Key Modifications:
1. Custom Code Generator:
Added a custom tonic_builder gRPC code generator. The code for this generator can be found in src/gen.rs. Will try to refactor into buf generator in follow-up PRs.
2. Compile Well-Known Types:
Background:
1. Utilize the #[pyclass] attribute macro on our Rust structure. This macro implements the PyClass trait for the underlying structure "under the hood".
2. Implementing the PyClass trait for a structure from an external crate (like prost_types) can lead to orphan rule violations in our local crate. The Rust compiler will not permit implementation of traits for types outside of the crate where the type is defined. (This is known as the "orphan rule".)
Avoiding Orphan Rules:
In some protobuf builders, like tonic_builder, there's an option to enable "compile well-known types". This forces the builder to compile well-known types (WKTs) directly using Rust primitive types instead of relying on external structures compiled from the prost_types crate.
Alternatives Considered:
One alternative is to create local wrappers around each external structure, as suggested by the PyO3 community. However, this solution becomes impractical when dealing with nested member fields within these structures. Because we still have to add PyClass trait all the way down to the nesting hierarchy. We'll elaborate more on gRPC client cases.
3. implement PyClass Traits for `Box<T>`:
T already has PyClass Traits. Box<T> needs but does not have PyClass Traits.
For example, in flyteidl.core.rs, `Box<T>`: LiteralType, Literal, Union, Scalar, ConjunctionExpression, BooleanExpression, IfBlock, BranchNode, ArrayNode, a total of 9 types will be on the heap.
What situations will be converted to boxes?
• Recursive types, such as ArrayNode, can hold references to other Nodes and are converted to ArrayNode {node: Option<Box<Node>>, ...}.
• This poses a challenge for PyO3, as it requires a PyClass to hold a reference to another PyClass. This is a Rust Non-lifetime-free issue.
• While this PR supports Cell<T>, but didn't support Box<T>, it also highlights the difficulties of supporting Box<T>.
• The PyO3 roadmap on addressing these issue seems like this way.
Solution:
Introduce conversion implementations and lifetime markers for the 9 types that use Box to prevent refs from outliving. For example, for Box, implement these two conversion traits instead of directly adding PyClass traits to Box:
impl pyo3:conversionIntoPy<pyo3:PyObject> for Box<Node> {
fn into_py(self, py: pyo3:marker:Python<'_>) -> pyo3::PyObject {
self.as_ref().clone().into_py(py)
}
}
impl<'py> pyo3:conversion:FromPyObject<'py> for Box<Node> {
fn extract_bound(obj: & pyo3::Bound<'py, pyo3:types: PyAny>) -> pyo3::PyResult<Self> {
Ok(Box::new(Node::extract_bound(obj).unwrap()))
}
}
Prevents references from outliving their intended lifetime, ensuring memory safety and preventing potential memory leaks.
4. IntoRequest Trait:
For cases like following example,
let request = tonic:Request:new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
Based on Types implementing this trait can be used as arguments to client RPC
We can directly pass them to gRPC services, like:
client.get_feature(Request::new(Point {})); // can work.
client.get_feature(Point {}); // can work, too.
5. Tuple Variants in Complex Enum: PyO3 supports tuple enum variants after version v0.22.0
6. Python submodules: Use latest feature Declarative modules introduced in version v0.22.0.
---
1. Optional: Concretize Generic Types
Challenges:
1. Each instantiation, for example: GenericType<i32>, GenericType<f64>, would have to have a different Python type object. This is an infinite set, and code would need to be generated for each instantiation. It isn't trivia for PyO3 to support.
2. In our case, for any gRPC client service stubs may accepts arbitrary Channel, ChannelInterceptor, etc. We must specify the underlying concrete type.
The Macro Solution
1. Define Concrete Structures: We can create new structures (AdminStub in the example) with predefined concrete types for each gRPC stub. This allows specifying the exact types needed for PyO3 interaction.
2. Using Macros for Automation: The following example leverages a Rust macro named concrete_generic_structure! to automate the creation of these concrete structures. It takes arguments for the structure name, the generic type it wraps, and the concrete type used. Additionally, it allows defining methods associated with the stub, specifying the request and response types.
• The provided code demonstrates how the macro is used to create an AdminStub structure. This structure wraps the AdminServiceClient generic type, and specifies tonic::transport::Channel as the concrete type for the channel. Additionally, it defines two methods (create_task and get_task) with their specific request and response types.
macro_rules! concrete_generic_structure {
($name:ident, $generic:ident, $type:ty, $( ($method_name:ident, $request_type:ty, $response_type:ty) ),* ) => {
#[pyo3::pyclass]
#[derive(Debug)]
pub struct $name {
pub inner: $generic<$type>,
pub rt: Runtime,
}
}
concrete_generic_structure!(
AdminStub,
AdminServiceClient,
tonic:transport:Channel,
(
create_task,
flyteidl:admin:TaskCreateRequest,
flyteidl:admin:TaskCreateResponse
),
(
get_task,
flyteidl:admin:ObjectGetRequest,
flyteidl:admin:Task
)
)
Trade-offs:
• Predefined Stub Support: This approach requires defining the stubs you want to support beforehand (e.g., defining Interceptor for channels).
How was this patch tested?
Setup process
Screenshots
Check all the applicable boxes
• I updated the documentation accordingly.
• All new and existing tests passed.
• All commits are signed-off.
Related PRs
flyteorg/flytekit#2536
Docs link
flyteorg/flytecuddly-jelly-27016
03/03/2025, 7:05 PM