⚡ Bolt: reduce lock contention in RepartitionExec#264
Conversation
### 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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reduced lock contention in
RepartitionExecby refactoring state management to useOnceLockand 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