-
Notifications
You must be signed in to change notification settings - Fork 299
perf: avoid FFI import/export when passing batches between native plans #3930
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
andygrove
wants to merge
21
commits into
apache:main
Choose a base branch
from
andygrove:batch-stash-shuffle-optimization
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.
+736
−240
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
86a4989
docs: add design spec for batch stash shuffle optimization
andygrove 488522c
docs: add implementation plan for batch stash shuffle optimization
andygrove ca8ee6a
feat: add BatchStash registry for native batch handle passing
andygrove e68d777
feat: add CometHandleBatchIterator Java class and JNI bridge
andygrove 07c86c7
feat: add executePlanBatchHandle JNI function for stash-mode output
andygrove d2f87ab
feat: add stash mode to CometExecIterator for batch handle output
andygrove 0a49ca6
feat: add handle-mode input path to ScanExec for batch stash retrieval
andygrove 84862ef
feat: detect CometHandleBatchIterator in planner and enable handle mode
andygrove 3588543
feat: preserve CometExecIterator reference through shuffle dependency…
andygrove db137ad
feat: integrate batch stash mode in CometNativeShuffleWriter
andygrove ed95e83
style: apply formatting fixes
andygrove a827437
fix: use scan source name for handle mode detection instead of JNI is…
andygrove 3af0e9a
chore: remove JVM crash log files
andygrove e6c3a42
refactor: deduplicate executePlan logic and address code review findings
andygrove 6cc0e78
refactor: use protobuf field for batch stash handle mode instead of s…
andygrove c42be90
chore: remove design docs from PR
andygrove f70119a
fix: add batch_stash_handle field to Scan struct literals in tests
andygrove c64d482
fix: use InputBatch::Complete for stashed batches to bypass schema re…
andygrove d37752a
feat: add spark.comet.exec.shuffle.batchStash.enabled config (default…
andygrove 38af7d9
fix: apply schema reconciliation for stashed batches with type mismat…
andygrove ba07bf7
fix: address PR review feedback for batch stash
andygrove 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // 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. | ||
|
|
||
| //! Global registry for passing RecordBatch values between native execution contexts | ||
| //! via opaque u64 handles, without Arrow FFI serialization. | ||
|
|
||
| use arrow::record_batch::RecordBatch; | ||
| use once_cell::sync::Lazy; | ||
| use std::collections::HashMap; | ||
| use std::sync::atomic::{AtomicU64, Ordering}; | ||
| use std::sync::Mutex; | ||
|
|
||
| /// Counter for generating unique handles. | ||
| static NEXT_HANDLE: AtomicU64 = AtomicU64::new(1); | ||
|
|
||
| /// Global stash mapping handles to RecordBatch values. | ||
| /// Entries are removed by `take()` when the downstream ScanExec consumes them, | ||
| /// so there is no leak under normal operation. The stash lives for the process | ||
| /// lifetime but is effectively empty between query executions. | ||
| static STASH: Lazy<Mutex<HashMap<u64, RecordBatch>>> = Lazy::new(|| Mutex::new(HashMap::new())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this must be a global one? Any leak risk? Do we need some cleanup to remove the content? |
||
|
|
||
| /// Store a RecordBatch in the global stash and return a unique handle. | ||
| pub(crate) fn stash(batch: RecordBatch) -> u64 { | ||
| let handle = NEXT_HANDLE.fetch_add(1, Ordering::Relaxed); | ||
| STASH | ||
| .lock() | ||
| .unwrap_or_else(|e| e.into_inner()) | ||
| .insert(handle, batch); | ||
| handle | ||
| } | ||
|
|
||
| /// Remove and return the RecordBatch associated with the given handle. | ||
| /// | ||
| /// Returns `None` if the handle does not exist in the stash. | ||
| pub(crate) fn take(handle: u64) -> Option<RecordBatch> { | ||
| STASH | ||
| .lock() | ||
| .unwrap_or_else(|e| e.into_inner()) | ||
| .remove(&handle) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use arrow::array::Int32Array; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use std::sync::Arc; | ||
|
|
||
| fn make_batch(values: Vec<i32>) -> RecordBatch { | ||
| let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); | ||
| let array = Arc::new(Int32Array::from(values)); | ||
| RecordBatch::try_new(schema, vec![array]).unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_stash_and_take() { | ||
| let batch = make_batch(vec![1, 2, 3]); | ||
| let num_rows = batch.num_rows(); | ||
|
|
||
| let handle = stash(batch); | ||
| let retrieved = take(handle).expect("expected batch to be present"); | ||
|
|
||
| assert_eq!(retrieved.num_rows(), num_rows); | ||
| let col = retrieved | ||
| .column(0) | ||
| .as_any() | ||
| .downcast_ref::<Int32Array>() | ||
| .unwrap(); | ||
| assert_eq!(col.values(), &[1, 2, 3]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_take_removes_entry() { | ||
| let batch = make_batch(vec![10, 20]); | ||
| let handle = stash(batch); | ||
|
|
||
| // First take returns the batch. | ||
| assert!(take(handle).is_some()); | ||
| // Second take finds nothing. | ||
| assert!(take(handle).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_take_unknown_handle() { | ||
| // Handle 0 is never issued (counter starts at 1). | ||
| assert!(take(0).is_none()); | ||
| // A large handle that was never issued. | ||
| assert!(take(u64::MAX).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_handles_are_unique() { | ||
| let batch1 = make_batch(vec![1]); | ||
| let batch2 = make_batch(vec![2]); | ||
| let batch3 = make_batch(vec![3]); | ||
|
|
||
| let h1 = stash(batch1); | ||
| let h2 = stash(batch2); | ||
| let h3 = stash(batch3); | ||
|
|
||
| assert_ne!(h1, h2); | ||
| assert_ne!(h2, h3); | ||
| assert_ne!(h1, h3); | ||
|
|
||
| // Clean up. | ||
| take(h1); | ||
| take(h2); | ||
| take(h3); | ||
| } | ||
| } | ||
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.
Added config for now just in case we discover bugs, but I plan on removing this config in the future.