Skip to content

⚡ Bolt: reduce lock contention in RepartitionExec#264

Open
Dandandan wants to merge 1 commit intomainfrom
bolt/fix-repartition-lock-contention-17973953021454908054
Open

⚡ Bolt: reduce lock contention in RepartitionExec#264
Dandandan wants to merge 1 commit intomainfrom
bolt/fix-repartition-lock-contention-17973953021454908054

Conversation

@Dandandan
Copy link
Copy Markdown
Owner

Reduced lock contention in RepartitionExec by refactoring state management to use OnceLock and granular locks, and by switching to an RAII-based memory tracking approach for record batches.


PR created automatically by Jules for task 17973953021454908054 started by @Dandandan

### What
This patch refactors `RepartitionExec`'s internal state management and memory tracking to eliminate global lock contention.

1.  **State Management**: Replaced the single `Arc<Mutex<RepartitionExecState>>` with a `OnceLock`-based approach for lazy initialization of input streams and output channels. Used granular, per-partition `Mutex`es for individual channel state, allowing concurrent polling of output streams.
2.  **Memory Tracking**: Transitioned from a shared `Arc<Mutex<MemoryReservation>>` to an RAII-style approach. Each `RepartitionBatch::Memory` now carries an owned `MemoryReservation` (split from the producer's local reservation), which is automatically released when dropped by the consumer.

### Why
The global `Mutex` on `RepartitionExecState` was a bottleneck when executing with many partitions, as every output stream contended for the lock during initialization and channel retrieval. Similarly, the shared memory reservation lock caused contention between multiple input tasks and the output task.

### Impact
*   Reduces global lock contention during query execution.
*   Enables better scaling with higher partition counts.
*   Improves memory management efficiency by utilizing RAII.

### Measurement
Verified with existing unit tests in `datafusion-physical-plan` to ensure no regressions in repartitioning logic or memory accounting. Reduced `Mutex` lock/unlock operations significantly during execution.

Co-authored-by: Dandandan <163737+Dandandan@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant