-
Notifications
You must be signed in to change notification settings - Fork 354
feat: add RSS partition writer and task-owned JNI callback (1/n) #5473
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
pingzh
wants to merge
1
commit into
apache:main
Choose a base branch
from
pingzh:pingzh/rss-partition-writer-jni-callback
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 all commits
Commits
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 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,147 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use crate::{check_exception, errors::CometError, JVMClasses}; | ||
| use datafusion::common::{DataFusionError, Result}; | ||
| use jni::objects::{Global, JMethodID, JObject, JValue}; | ||
| use jni::signature::{Primitive, ReturnType}; | ||
| use jni::Env; | ||
|
|
||
| /// Receives a complete encoded shuffle block for one output partition. | ||
| /// | ||
| /// Implementations must remain safe when invoked from native execution | ||
| /// threads that do not inherit Spark's task-local JVM state. | ||
| pub trait ShufflePartitionPusher: Send + Sync { | ||
| /// Sends one complete, length-prefixed Arrow IPC shuffle block. | ||
| fn push_partition_data(&self, partition_id: i32, data: &[u8]) -> Result<()>; | ||
| } | ||
|
|
||
| /// Invokes a task-owned JVM shuffle callback from any native execution thread. | ||
| /// | ||
| /// The global callback reference keeps both the Java object and its class alive | ||
| /// for the lifetime of the cached method ID. No thread-local JNI environment | ||
| /// or Spark task context is retained between invocations. | ||
| pub struct JavaShufflePartitionPusher { | ||
| callback: Global<JObject<'static>>, | ||
| push_method: JMethodID, | ||
| } | ||
|
|
||
| impl JavaShufflePartitionPusher { | ||
| /// Captures the callback while running on an attached JVM thread. | ||
| pub fn try_new(env: &mut Env<'_>, callback: &JObject<'_>) -> Result<Self> { | ||
| if callback.is_null() { | ||
| return Err(DataFusionError::Execution( | ||
| "Remote shuffle callback must not be null".to_string(), | ||
| )); | ||
| } | ||
|
|
||
| let callback_class = env.get_object_class(callback).map_err(CometError::from)?; | ||
| let push_method = env | ||
| .get_method_id( | ||
| &callback_class, | ||
| jni::jni_str!("pushPartitionData"), | ||
| jni::jni_sig!("(I[BI)V"), | ||
| ) | ||
| .map_err(CometError::from)?; | ||
| let callback = env.new_global_ref(callback).map_err(CometError::from)?; | ||
|
|
||
| Ok(Self { | ||
| callback, | ||
| push_method, | ||
| }) | ||
| } | ||
|
|
||
| fn checked_payload_length(partition_id: i32, payload_length: usize) -> Result<i32> { | ||
| if partition_id < 0 { | ||
| return Err(DataFusionError::Execution(format!( | ||
| "Remote shuffle partition must be nonnegative, got {partition_id}" | ||
| ))); | ||
| } | ||
|
|
||
| i32::try_from(payload_length).map_err(|_| { | ||
| DataFusionError::Execution(format!( | ||
| "Remote shuffle payload size {payload_length} exceeds the JVM array limit" | ||
| )) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ShufflePartitionPusher for JavaShufflePartitionPusher { | ||
| fn push_partition_data(&self, partition_id: i32, data: &[u8]) -> Result<()> { | ||
| let payload_length = Self::checked_payload_length(partition_id, data.len())?; | ||
|
|
||
| JVMClasses::with_env(|env| { | ||
| let payload = env.byte_array_from_slice(data).map_err(CometError::from)?; | ||
|
|
||
| // SAFETY: `push_method` was resolved against the callback object's | ||
| // class with this exact argument list and void return type. The | ||
| // global object reference keeps its defining class alive. | ||
| let result = unsafe { | ||
| env.call_method_unchecked( | ||
| self.callback.as_obj(), | ||
| self.push_method, | ||
| ReturnType::Primitive(Primitive::Void), | ||
| &[ | ||
| JValue::Int(partition_id).as_jni(), | ||
| JValue::Object(&payload).as_jni(), | ||
| JValue::Int(payload_length).as_jni(), | ||
| ], | ||
| ) | ||
| }; | ||
|
|
||
| // Inspect the pending exception before consuming the JNI result so | ||
| // its original throwable survives the DataFusion error boundary. | ||
| if let Some(exception) = check_exception(env)? { | ||
| return Err(exception.into()); | ||
| } | ||
|
|
||
| result.map_err(CometError::from)?; | ||
| Ok(()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::JavaShufflePartitionPusher; | ||
|
|
||
| #[test] | ||
| fn accepts_payload_lengths_representable_by_jvm_arrays() { | ||
| assert_eq!( | ||
| JavaShufflePartitionPusher::checked_payload_length(0, 0).unwrap(), | ||
| 0 | ||
| ); | ||
| assert_eq!( | ||
| JavaShufflePartitionPusher::checked_payload_length(7, i32::MAX as usize).unwrap(), | ||
| i32::MAX | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_negative_partition_ids() { | ||
| let error = JavaShufflePartitionPusher::checked_payload_length(-1, 1).unwrap_err(); | ||
| assert!(error.to_string().contains("partition must be nonnegative")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_payload_lengths_larger_than_a_jvm_array() { | ||
| let oversized_length = i32::MAX as usize + 1; | ||
| let error = | ||
| JavaShufflePartitionPusher::checked_payload_length(0, oversized_length).unwrap_err(); | ||
| assert!(error.to_string().contains("exceeds the JVM array limit")); | ||
| } | ||
| } | ||
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
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.
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.
in specification it Integer.MAX_VALUE, but HotSpot implementation details Integer.MAX_VALUE-8
so maybe make sense to test it as well
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/util/ArraysSupport.java#L854-L866