[WIP][SPARK-56410][SQL][CORE] Add bounded k-way merge support in UnsafeExternalSorter to reduce OOM risk#55275
Draft
ivoson wants to merge 1 commit intoapache:masterfrom
Draft
[WIP][SPARK-56410][SQL][CORE] Add bounded k-way merge support in UnsafeExternalSorter to reduce OOM risk#55275ivoson wants to merge 1 commit intoapache:masterfrom
ivoson wants to merge 1 commit intoapache:masterfrom
Conversation
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.
What changes were proposed in this pull request?
Added bounded multi-round k-way merge to
UnsafeExternalSorterto prevent OOM during sort-merge when there are many spill files. Previously,getSortedIterator()opened all spill readers simultaneously (~3MB per reader), causing OOM with hundreds of spills.UnsafeSorterBoundedSpillMergerclass that merges spill files in rounds of at most K files (configurable merge factor, default 64), writing intermediate results to temp spill files, then merging those until the count fits in a single final round.spark.unsafe.sorter.spill.merge.factor(default 64, set -1 to disable) that controls the maximum number of concurrent spill readers during merge. At 64 readers x 3MB = ~192MB, well within typical executor heap sizes.How It Works
For example with 680 spills:
Why are the changes needed?
When
UnsafeExternalSorteraccumulates a large number of spill files, the merge phase opens all spill readers simultaneously. EachUnsafeSorterSpillReaderallocates ~3MB of buffers (1MBNioBufferedFileInputStream+ 1MBReadAheadInputStream+ 1MB record byte array). With hundreds of spills, this means ~GB+ of merge buffers alone, causing OOM even when the executor has sufficient heap for normal operation.Does this PR introduce any user-facing change?
No
How was this patch tested?
UTs added.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (claude-opus-4-6)