tomvanbussel opened a new pull request #29785:
URL: https://github.com/apache/spark/pull/29785


   ### What changes were proposed in this pull request?
   
   This PR changes `UnsafeExternalSorter` to no no longer allocate any memory 
while spilling. In particular it removes the allocation of a new pointer array 
in `UnsafeInMemorySorter`. Instead the new pointer array is allocated whenever 
the next record is inserted into the sorter.
   
   ### Why are the changes needed?
   
   Without this change the `UnsafeExternalSorter` could throw an OOM while 
spilling. The following sequence of events would have triggered an OOM:
   
   1. `UnsafeExternalSorter` runs out of space in its pointer array and 
attempts to allocate a large array to replace the current one.
   2. `TaskMemoryManager` tries to allocate the memory backing the large array 
using `MemoryManager`, but `MemoryManager` is only willing to return most but 
not all of the memory requested.
   3. `TaskMemoryManager` asks `UnsafeExternalSorter` to spill, which causes 
`UnsafeExternalSorter` to spill the current run to disk, to free its record 
pages and to reset its `UnsafeInMemorySorter`.
   4. `UnsafeInMemorySorter` frees its pointer array, and tries to allocate a 
new small pointer array.
   5. `TaskMemoryManager` tries to allocate the memory backing the small array 
using `MemoryManager`, but `MemoryManager` is unwilling to give it any memory, 
as the `TaskMemoryManager` is still holding on to the memory it got for the 
large array.
   6. `TaskMemoryManager` again asks `UnsafeExternalSorter` to spill, but this 
time there is nothing to spill.
   7. `UnsafeInMemorySorter` receives less memory than it requested, and causes 
a `SparkOutOfMemoryError` to be thrown, which causes the current task to fail.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No
   
   ### How was this patch tested?
   
   Tests were added in `UnsafeExternalSorterSuite` and 
`UnsafeInMemorySorterSuite`.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to