Fix memory reservation starvation in sort-merge#20642
Open
xudong963 wants to merge 4 commits intoapache:mainfrom
Open
Fix memory reservation starvation in sort-merge#20642xudong963 wants to merge 4 commits intoapache:mainfrom
xudong963 wants to merge 4 commits intoapache:mainfrom
Conversation
Member
Author
|
Update: I added an end-to-end test which fails on main |
0f2140c to
651e9c9
Compare
rluvaton
reviewed
Mar 3, 2026
Comment on lines
+358
to
+364
| // Transfer the pre-reserved merge memory to the streaming merge | ||
| // using `take()` instead of `new_empty()`. This ensures the merge | ||
| // stream starts with `sort_spill_reservation_bytes` already | ||
| // allocated, preventing starvation when concurrent sort partitions | ||
| // compete for pool memory. `take()` moves the bytes atomically | ||
| // without releasing them back to the pool, so other partitions | ||
| // cannot race to consume the freed memory. |
Member
There was a problem hiding this comment.
The pre reserved merge memory should be used as part of the sort merge stream.
I mean that if x pre reserved merge memory was reserved the sort merge stream should know about that so it wont think it starting from 0, otherwise this just reserve for unaccounted memory
coracuity
added a commit
to acuitymd/silk-chiffon
that referenced
this pull request
Mar 4, 2026
DataFusion 52.1.0 has a TOCTOU race in ExternalSorter where merge reservations are freed and re-created empty, letting other partitions steal the memory (apache/datafusion#20642). Until the upstream fix lands, compute a data-aware sort_spill_reservation_bytes by sampling actual Arrow row sizes from the input, estimating spill file count, and reserving enough for the merge phase.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue does this PR close?
Rationale for this change
This PR fixes memory reservation starvation in sort-merge when multiple sort partitions share a GreedyMemoryPool.
When multiple
ExternalSorterinstances run concurrently and share a single memory pool, the merge phase starves:What changes are included in this PR?
Are these changes tested?
I can't find a deterministic way to reproduce the bug, but it occurs in our production.Add an end-to-end test to verify the fixAre there any user-facing changes?