Skip to content

Commit 2d1aab3

Browse files
authored
fix: size JVM shuffle pointer array growth from the array, not the data pages (apache#5907)
CometShuffleExternalSorter.growPointerArrayIfNecessary sized the replacement pointer array from SpillSorter.getMemoryUsage(), which includes every data page, so the first growth requested an array proportional to the page size instead of doubling the array as Spark's ShuffleExternalSorter does. Add SpillSorter.getPointerArrayMemoryUsage() and use it for growth.
1 parent 9b63e7d commit 2d1aab3

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎spark/src/main/java/org/apache/spark/shuffle/sort/CometShuffleExternalSorter.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ public void cleanupResources() {
254254
private void growPointerArrayIfNecessary() throws IOException {
255255
assert (activeSpillSorter != null);
256256
if (!activeSpillSorter.hasSpaceForAnotherRecord()) {
257-
long used = activeSpillSorter.getMemoryUsage();
257+
// Size the new array from the pointer array alone, as Spark's ShuffleExternalSorter does.
258+
// SpillSorter.getMemoryUsage() also counts the data pages, which would make the array grow
259+
// in proportion to the pages instead of doubling.
260+
long used = activeSpillSorter.getPointerArrayMemoryUsage();
258261
LongArray array;
259262
try {
260263
// could trigger spilling

‎spark/src/main/java/org/apache/spark/shuffle/sort/SpillSorter.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@ public long getMemoryUsage() {
180180
}
181181
}
182182

183+
/**
184+
* Memory held by the in-memory sorter's pointer array alone, excluding the data pages. This is
185+
* what the pointer array growth in {@link CometShuffleExternalSorter} must be sized from: sizing
186+
* it from {@link #getMemoryUsage()} would grow the array in proportion to the data pages instead
187+
* of doubling it.
188+
*/
189+
public long getPointerArrayMemoryUsage() {
190+
synchronized (this) {
191+
if (freed || inMemSorter == null) {
192+
return 0;
193+
}
194+
return inMemSorter.getMemoryUsage();
195+
}
196+
}
197+
183198
@Override
184199
protected void spill(int required) throws IOException {
185200
spillCallback.onSpillRequired();

‎spark/src/test/scala/org/apache/spark/shuffle/sort/SpillSorterSuite.scala‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,51 @@ class SpillSorterSuite extends AnyFunSuite with BeforeAndAfterEach {
259259
}
260260
}
261261

262+
test("pointer array growth is sized from the array, not from the data pages") {
263+
// Use the unified (off-heap) allocator so that every allocation made by this sorter is
264+
// visible through `getUsed` on an allocator that is private to this test.
265+
val offHeapConf = new SparkConf(false)
266+
.set("spark.memory.offHeap.enabled", "true")
267+
.set("spark.memory.offHeap.size", "64m")
268+
val offHeapMemoryManager = new TestMemoryManager(offHeapConf)
269+
offHeapMemoryManager.limit(64L * 1024 * 1024)
270+
val offHeapTaskMemoryManager = new TaskMemoryManager(offHeapMemoryManager, 0)
271+
val allocator =
272+
CometShuffleMemoryAllocator.getInstance(offHeapConf, offHeapTaskMemoryManager, PAGE_SIZE)
273+
// The block manager is only touched when spilling, which this test never does.
274+
val sorter = new CometShuffleExternalSorter(
275+
allocator,
276+
null,
277+
TaskContext.empty(),
278+
INITIAL_SIZE,
279+
2,
280+
offHeapConf,
281+
new ShuffleWriteMetrics(),
282+
createTestSchema())
283+
284+
try {
285+
val recordData = new Array[Byte](16)
286+
def insert(i: Int): Unit =
287+
sorter.insertRecord(recordData, Platform.BYTE_ARRAY_OFFSET, recordData.length, i % 2)
288+
289+
val initialArrayBytes = INITIAL_SIZE * 8L
290+
assert(allocator.getUsed === initialArrayBytes)
291+
292+
insert(0)
293+
val pageBytes = allocator.getUsed - initialArrayBytes
294+
assert(pageBytes >= PAGE_SIZE)
295+
296+
// With radix sort enabled the in-memory sorter uses half the array for records, so the
297+
// pointer array is grown when the (INITIAL_SIZE / 2 + 1)th record arrives. Growth must
298+
// double the pointer array, not request an array sized from the data pages as well.
299+
val recordsToTriggerGrowth = INITIAL_SIZE / 2 + 1
300+
(1 until recordsToTriggerGrowth).foreach(insert)
301+
assert(sorter.getPeakMemoryUsedBytes === pageBytes + 2 * initialArrayBytes)
302+
assert(allocator.getUsed === pageBytes + 2 * initialArrayBytes)
303+
} finally {
304+
sorter.cleanupResources()
305+
assert(offHeapTaskMemoryManager.cleanUpAllAllocatedMemory() === 0L)
306+
}
307+
}
308+
262309
}

0 commit comments

Comments
 (0)