This is an automated email from the ASF dual-hosted git repository.

dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 95287e1d6a55 [SPARK-60998][SQL] Do not round LongToUnsafeRowMap pages 
up to the page size
95287e1d6a55 is described below

commit 95287e1d6a55b26d9b27f4635835c864610f1e81
Author: Liang-Chi Hsieh <[email protected]>
AuthorDate: Fri Nov 6 19:02:45 2026 -0800

    [SPARK-60998][SQL] Do not round LongToUnsafeRowMap pages up to the page size
    
    ### What changes were proposed in this pull request?
    
    This PR passes a page size floor of one word (8 bytes) to `MemoryConsumer` 
in `LongToUnsafeRowMap`, instead of the default page size of the 
`TaskMemoryManager`, and requests the 1 MiB initial page explicitly in `init()`.
    
    `MemoryConsumer.allocatePage` rounds every request up to `max(pageSize, 
required)`, and `pageSize` is only used there. With this change, as before 
SPARK-54116:
    
    - the initial page is 1 MiB,
    - the page grows by doubling (2 MiB, 4 MiB, ...) instead of jumping to the 
page size,
    - a deserialized map allocates exactly what it reads.
    
    The floor is not 0 because `TaskMemoryManager` does not allocate a page of 
0 bytes, which an empty deserialized map would ask for. 
`LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES` and 
`LongToUnsafeRowMap.MIN_PAGE_SIZE_BYTES` are added for the two sizes. The 
on-heap and off-heap support added by SPARK-54116 does not change.
    
    ### Why are the changes needed?
    
    Since SPARK-54116, `LongToUnsafeRowMap` allocates its page through 
`MemoryConsumer.allocatePage`. Before it, the map used a 1 MiB `long[]` and 
acquired execution memory for exactly that size. Now every map reserves at 
least a full page, which is `spark.buffer.pageSize`, or a value derived from 
the executor memory and cores of up to 64 MiB. That happens even when the map 
holds only a few rows.
    
    When one task keeps many maps alive at once, for example a stage with a 
long chain of shuffled hash joins, the task runs out of execution memory long 
before the maps hold that much data:
    
    ```
    [UNABLE_TO_ACQUIRE_MEMORY] Unable to acquire 512 bytes of memory, got 0.
    Memory used by task 0 grouped by consumer:
      org.apache.spark.sql.execution.joins.LongToUnsafeRowMap47d52df1: 64.0 MiB
      org.apache.spark.sql.execution.joins.LongToUnsafeRowMap454d5d4a: 64.0 MiB
      org.apache.spark.sql.execution.joins.LongToUnsafeRowMapc17cf36: 64.0 MiB
      org.apache.spark.sql.execution.joins.LongToUnsafeRowMap239e2389: 64.0 MiB
    ```
    
    This is the output of the new test `SPARK-60998: many LongToUnsafeRowMaps 
fit in one task when each holds little data` without this fix: 4 maps of 10 
rows each take the whole 256 MiB.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. It fixes a regression in Spark 4.2.0: queries that keep many 
`LongToUnsafeRowMap`s in one task, such as a chain of shuffled hash joins, 
could fail with `UNABLE_TO_ACQUIRE_MEMORY`. Each map now reserves memory close 
to what it stores, as in Spark 4.1 and earlier.
    
    ### How was this patch tested?
    
    New tests in `HashedRelationSuite`, which run in both 
`HashedRelationOnHeapSuite` and `HashedRelationOffHeapSuite`. The maps are 
built on a memory manager with a 64 MiB page size:
    
    - `SPARK-60998: LongToUnsafeRowMap does not round its page up to the page 
size`: the map reserves the array plus 1 MiB, and 2 MiB after a 1.5 MiB value 
is appended.
    - `SPARK-60998: many LongToUnsafeRowMaps fit in one task when each holds 
little data`: 16 small maps in 256 MiB of execution memory.
    - `SPARK-60998: deserialized LongToUnsafeRowMap allocates exactly what it 
reads`: a non-empty map and an empty map, with both Java and Kryo 
serialization. The deserialized map reserves exactly its array and page, and 
releases them on `free()`.
    
    All three tests fail without the fix, in both memory modes.
    
    ```
    build/sbt "sql/testOnly 
org.apache.spark.sql.execution.joins.HashedRelationOnHeapSuite 
org.apache.spark.sql.execution.joins.HashedRelationOffHeapSuite"
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5.5)
    
    This pull request and its description were written by Isaac.
    
    Closes #60167 from viirya/fix-long-hashed-relation-page-size.
    
    Authored-by: Liang-Chi Hsieh <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    (cherry picked from commit 0d4e2402992ac6954b401e137ec1257b95a373ce)
    Signed-off-by: Dongjoon Hyun <[email protected]>
---
 .../spark/sql/execution/joins/HashedRelation.scala |  33 ++++--
 .../sql/execution/joins/HashedRelationSuite.scala  | 113 ++++++++++++++++++++-
 2 files changed, 137 insertions(+), 9 deletions(-)

diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
index 7712fdc9f6cc..801243fc9be0 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
@@ -537,7 +537,8 @@ private[execution] final class LongToUnsafeRowMap(
     val mm: TaskMemoryManager,
     capacity: Int,
     ignoresDuplicatedKey: Boolean = false)
-  extends MemoryConsumer(mm, mm.getTungstenMemoryMode) with Externalizable 
with KryoSerializable {
+  extends MemoryConsumer(mm, LongToUnsafeRowMap.MIN_PAGE_SIZE_BYTES, 
mm.getTungstenMemoryMode)
+  with Externalizable with KryoSerializable {
 
   // Whether the keys are stored in dense mode or not.
   private var isDense = false
@@ -593,7 +594,7 @@ private[execution] final class LongToUnsafeRowMap(
       array = allocateArray(n * 2)
       array.zeroOut()
       mask = n * 2 - 2
-      page = allocatePage(1 << 20) // 1M bytes
+      page = allocatePage(LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
       cursor = page.getBaseOffset
     }
   }
@@ -827,10 +828,10 @@ private[execution] final class LongToUnsafeRowMap(
       val newNumWords = math.max(neededNumWords, math.min(page.size() / 8 * 2, 
1 << 30))
       // newNumWords is a Long up to 1 << 30. Multiplying by 8 must stay in 
Long
       // arithmetic; `newNumWords.toInt * 8` (Int * Int) overflows to 0 at the
-      // upper bound, causing `allocatePage(0)` to fall back to the default 
page
-      // size while subsequent writes still advance `cursor` past the new 
page's
-      // end (heap corruption observed as a `forward_copy_longs` SEGV during
-      // BHJ build on aarch64).
+      // upper bound, causing `allocatePage(0)` to return a page of only
+      // `MIN_PAGE_SIZE_BYTES` while subsequent writes still advance `cursor`
+      // past the new page's end (heap corruption observed as a
+      // `forward_copy_longs` SEGV during BHJ build on aarch64).
       val newPage = allocatePage(newNumWords * 8L)
       Platform.copyMemory(page.getBaseObject, page.getBaseOffset, 
newPage.getBaseObject,
         newPage.getBaseOffset, usedBytes)
@@ -990,6 +991,26 @@ private[execution] final class LongToUnsafeRowMap(
   }
 }
 
+private[execution] object LongToUnsafeRowMap {
+  /**
+   * The size of the page that a new map starts with. The page grows by 
doubling from here.
+   */
+  val INITIAL_PAGE_SIZE_BYTES: Long = 1L << 20
+
+  /**
+   * The page size passed to `MemoryConsumer`, which rounds every 
`allocatePage` request up to it.
+   *
+   * With the default page size (`spark.buffer.pageSize`, up to 64 MiB), even 
an empty map would
+   * reserve 64 MiB of execution memory. A task that keeps many maps alive at 
once, e.g. a stage
+   * with a chain of shuffled hash joins, then runs out of execution memory 
long before the maps
+   * hold that much data. With a floor of one word, a new map reserves 
`INITIAL_PAGE_SIZE_BYTES`,
+   * the page grows by doubling, and a deserialized map allocates exactly what 
it reads. The floor
+   * is not 0 because `TaskMemoryManager` does not allocate a page of 0 bytes, 
which an empty
+   * deserialized map would ask for.
+   */
+  val MIN_PAGE_SIZE_BYTES: Long = 8L
+}
+
 class LongHashedRelation(
     private var nFields: Int,
     private var map: LongToUnsafeRowMap) extends HashedRelation with 
Externalizable {
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
index f8fa2f5fe35f..50fa9aa899d5 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
@@ -25,11 +25,11 @@ import scala.util.Random
 
 import org.apache.spark.SparkConf
 import org.apache.spark.SparkException
-import org.apache.spark.internal.config.{MEMORY_OFFHEAP_ENABLED, 
MEMORY_OFFHEAP_SIZE}
+import org.apache.spark.internal.config.{BUFFER_PAGESIZE, 
MEMORY_OFFHEAP_ENABLED, MEMORY_OFFHEAP_SIZE}
 import org.apache.spark.internal.config.Kryo._
-import org.apache.spark.memory.{TaskMemoryManager, UnifiedMemoryManager}
+import org.apache.spark.memory.{SparkOutOfMemoryError, TaskMemoryManager, 
UnifiedMemoryManager}
 import org.apache.spark.network.util.ByteUnit
-import org.apache.spark.serializer.KryoSerializer
+import org.apache.spark.serializer.{JavaSerializer, KryoSerializer}
 import org.apache.spark.sql.catalyst.InternalRow
 import org.apache.spark.sql.catalyst.expressions._
 import org.apache.spark.sql.internal.SQLConf
@@ -318,6 +318,113 @@ abstract class HashedRelationSuite extends 
SharedSparkSession {
     secondTimeSerialized.free()
   }
 
+  /**
+   * Creates a memory manager whose page size is set explicitly, so that the 
maps built on it do
+   * not depend on the page size derived from the JVM heap and the number of 
cores.
+   */
+  private def newMemoryManager(pageSize: Long, memorySize: Long): 
UnifiedMemoryManager = {
+    new UnifiedMemoryManager(
+      new SparkConf()
+        .set(MEMORY_OFFHEAP_ENABLED, useOffHeapMemoryMode)
+        .set(MEMORY_OFFHEAP_SIZE, memorySize)
+        .set(BUFFER_PAGESIZE, pageSize),
+      memorySize,
+      memorySize / 2,
+      1)
+  }
+
+  test("SPARK-60998: LongToUnsafeRowMap does not round its page up to the page 
size") {
+    val largeUmm = newMemoryManager(ByteUnit.MiB.toBytes(64), 
ByteUnit.GiB.toBytes(1))
+    val largeMm = new TaskMemoryManager(largeUmm, 0)
+    assert(largeMm.pageSizeBytes() === ByteUnit.MiB.toBytes(64))
+
+    // Capacity 16 gives an array of 32 longs, which does not grow for a 
single key.
+    val arrayBytes = 32 * 8L
+    val map = new LongToUnsafeRowMap(largeMm, 16)
+    assert(map.getMode === largeMm.getTungstenMemoryMode)
+    assert(map.getUsed === arrayBytes + 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
+
+    // A value larger than the initial page doubles the page to 2 MiB, not 64 
MiB.
+    val unsafeProj = UnsafeProjection.create(Array[DataType](StringType))
+    val bigStr = UTF8String.fromString("x".repeat(3 << 19))
+    map.append(0L, unsafeProj(InternalRow(bigStr)))
+    assert(map.getUsed === arrayBytes + 2 * 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
+
+    val resultRow = new UnsafeRow(1)
+    assert(map.getValue(0L, resultRow).getUTF8String(0) === bigStr)
+    map.free()
+    assert(largeUmm.executionMemoryUsed === 0)
+  }
+
+  test("SPARK-60998: many LongToUnsafeRowMaps fit in one task when each holds 
little data") {
+    // 16 maps rounded up to a 64 MiB page each would need 1 GiB, 4x the 
memory available.
+    val smallUmm = newMemoryManager(ByteUnit.MiB.toBytes(64), 
ByteUnit.MiB.toBytes(256))
+    val smallMm = new TaskMemoryManager(smallUmm, 0)
+    val unsafeProj = UnsafeProjection.create(Seq(BoundReference(0, LongType, 
false)))
+    val maps = ArrayBuffer.empty[LongToUnsafeRowMap]
+    try {
+      (0 until 16).foreach { i =>
+        val map = new LongToUnsafeRowMap(smallMm, 16)
+        maps += map
+        (0 until 10).foreach(k => map.append(k, 
unsafeProj(InternalRow(k.toLong + i))))
+        map.optimize()
+      }
+      maps.zipWithIndex.foreach { case (map, i) =>
+        assert(map.getUsed < 2 * LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
+        val resultRow = new UnsafeRow(1)
+        (0 until 10).foreach(k => assert(map.getValue(k, resultRow).getLong(0) 
=== k + i))
+      }
+    } catch {
+      // SparkOutOfMemoryError is fatal to ScalaTest and would abort the whole 
suite.
+      case e: SparkOutOfMemoryError =>
+        fail("Could not build 16 small LongToUnsafeRowMaps in one task", e)
+    } finally {
+      maps.foreach(_.free())
+    }
+    assert(smallUmm.executionMemoryUsed === 0)
+  }
+
+  test("SPARK-60998: deserialized LongToUnsafeRowMap allocates exactly what it 
reads") {
+    val largeUmm = newMemoryManager(ByteUnit.MiB.toBytes(64), 
ByteUnit.GiB.toBytes(1))
+    val largeMm = new TaskMemoryManager(largeUmm, 0)
+    val unsafeProj = UnsafeProjection.create(Array[DataType](LongType))
+    val serializers = Seq(
+      new JavaSerializer(new SparkConf()).newInstance(),
+      new KryoSerializer(new SparkConf().set(KRYO_REFERENCE_TRACKING, 
false)).newInstance())
+
+    // Both a non-empty map and an empty map, whose serialized page has length 
0.
+    for (ser <- serializers; numRows <- Seq(10, 0)) {
+      val originalMap = new LongToUnsafeRowMap(largeMm, 16)
+      (0 until numRows).foreach(k => originalMap.append(k, 
unsafeProj(InternalRow(k * 2L))))
+      if (numRows > 0) {
+        originalMap.optimize()
+      }
+      val deserializedMap = 
ser.deserialize[LongToUnsafeRowMap](ser.serialize(originalMap))
+
+      // The deserialized map does not use `largeMm`: it is created by the 
no-arg constructor,
+      // which builds its own memory manager. It allocates the array and the 
page it reads. After
+      // `optimize()`, the keys 0 until 10 use a dense array of 10 longs, and 
each value takes a
+      // 16-byte UnsafeRow plus an 8-byte pointer. The empty map keeps its 
sparse array of 32
+      // longs, and its empty page is rounded up to `MIN_PAGE_SIZE_BYTES`.
+      val expectedUsed = if (numRows > 0) {
+        numRows * 8L + numRows * 24L
+      } else {
+        32 * 8L + LongToUnsafeRowMap.MIN_PAGE_SIZE_BYTES
+      }
+      assert(originalMap.getUsed < 2 * 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
+      assert(deserializedMap.getUsed === expectedUsed)
+      val resultRow = new UnsafeRow(1)
+      (0 until numRows).foreach { k =>
+        assert(deserializedMap.getValue(k, resultRow).getLong(0) === k * 2L)
+      }
+      assert(deserializedMap.getValue(numRows, resultRow) === null)
+      originalMap.free()
+      deserializedMap.free()
+      assert(deserializedMap.getUsed === 0)
+    }
+    assert(largeUmm.executionMemoryUsed === 0)
+  }
+
   test("Spark-14521") {
     val ser = new KryoSerializer(
       (new SparkConf).set(KRYO_REFERENCE_TRACKING, false)).newInstance()


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to