WangGuangxin commented on a change in pull request #28780:
URL: https://github.com/apache/spark/pull/28780#discussion_r443445290
##########
File path:
core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java
##########
@@ -104,11 +104,13 @@ public static UnsafeExternalSorter
createWithExistingInMemorySorter(
int initialSize,
long pageSizeBytes,
int numElementsForSpillThreshold,
- UnsafeInMemorySorter inMemorySorter) throws IOException {
+ UnsafeInMemorySorter inMemorySorter,
+ long existingMemoryConsumption) throws IOException {
UnsafeExternalSorter sorter = new UnsafeExternalSorter(taskMemoryManager,
blockManager,
serializerManager, taskContext, recordComparatorSupplier,
prefixComparator, initialSize,
pageSizeBytes, numElementsForSpillThreshold, inMemorySorter, false /*
ignored */);
sorter.spill(Long.MAX_VALUE, sorter);
+ taskContext.taskMetrics().incMemoryBytesSpilled(existingMemoryConsumption);
Review comment:
> Is it the same to use `inMemorySorter.getMemoryUsage`?
>
> Also, shall we update `sorter.totalSpillBytes`?
1. It's not the same with `inMemorySorter.getMemoryUsage`.
When we insert a record into `UnsafeExternalSorter`, the record itself is
copied to sorter's `allocatedPages`, and stores the memory address into
`InMemorySorter`. So when we spill it, the memory size is the sum of
`inMemorySorter.getMemoryUsage`
and `allocatePages`, that's what it does in `UnsafeExternalSorter.spill`.
But when we do `sorter.spill` in
`UnsafeExternalSorter.createWithExistingInMemorySorter`, the sorter's
`allocatedPages` is empty since we didn't copy any records to it. The memory
address in `inMemorySorter` points to the memory pages in `BytesToBytesMap`.
The passed parameter denotes the size of memory pages in `BytesToBytesMap`.
2. yes, seems we also need to update `sorter.totalSpillBytes`
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]