Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -124,7 +124,7 @@ Iterator<InternalRow> sort() throws IOException {
return new AbstractScalaRowIterator() {

private final int numFields = schema.length();
private final UnsafeRow row = new UnsafeRow();
private UnsafeRow row = new UnsafeRow();

@Override
public boolean hasNext() {
Expand All @@ -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 = null; // so that we don't keep references to the base object
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