Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ private long getMemoryUsage() {
return sorter.getMemoryUsage() + (allocatedPages.size() * (long) PAGE_SIZE);
}

@VisibleForTesting
public int getNumberOfAllocatedPages() {
return allocatedPages.size();
}

public long freeMemory() {
long memoryFreed = 0;
for (MemoryBlock block : allocatedPages) {
Expand Down Expand Up @@ -257,7 +262,7 @@ public void insertRecord(
currentPagePosition,
lengthInBytes);
currentPagePosition += lengthInBytes;

freeSpaceInCurrentPage -= totalSpaceRequired;
sorter.insertRecord(recordAddress, prefix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,23 @@ public void testSortingEmptyArrays() throws Exception {
}
}

@Test
public void testFillingPage() throws Exception {
final UnsafeExternalSorter sorter = new UnsafeExternalSorter(
memoryManager,
shuffleMemoryManager,
blockManager,
taskContext,
recordComparator,
prefixComparator,
1024,
new SparkConf());

byte[] record = new byte[16];
while (sorter.getNumberOfAllocatedPages() < 2) {
sorter.insertRecord(record, PlatformDependent.BYTE_ARRAY_OFFSET, record.length, 0);
}
sorter.freeMemory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ public InternalRow next() {
numFields,
sortedIterator.getRecordLength());
if (!hasNext()) {
row.copy(); // so that we don't have dangling pointers to freed page
UnsafeRow copy = row.copy(); // so that we don't have dangling pointers to freed page
row.pointTo(null, 0, 0, 0); // so that we don't keep references to the base object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just

row = null ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

cleanupResources();
return copy;
} else {
return row;
}
return row;
} catch (IOException e) {
cleanupResources();
// Scala iterators don't declare any checked exceptions, so we need to use this hack
Expand Down