|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.shuffle.sort |
| 19 | + |
| 20 | +import java.lang.{Long => JLong} |
| 21 | + |
| 22 | +import org.mockito.Mockito.when |
| 23 | +import org.scalatest.mock.MockitoSugar |
| 24 | + |
| 25 | +import org.apache.spark._ |
| 26 | +import org.apache.spark.executor.{ShuffleWriteMetrics, TaskMetrics} |
| 27 | +import org.apache.spark.memory._ |
| 28 | +import org.apache.spark.unsafe.Platform |
| 29 | + |
| 30 | +class ShuffleExternalSorterSuite extends SparkFunSuite with LocalSparkContext with MockitoSugar { |
| 31 | + |
| 32 | + test("nested spill should be no-op") { |
| 33 | + val conf = new SparkConf() |
| 34 | + .setMaster("local[1]") |
| 35 | + .setAppName("ShuffleExternalSorterSuite") |
| 36 | + .set("spark.testing", "true") |
| 37 | + .set("spark.testing.memory", "1600") |
| 38 | + .set("spark.memory.fraction", "1") |
| 39 | + sc = new SparkContext(conf) |
| 40 | + |
| 41 | + val memoryManager = UnifiedMemoryManager(conf, 1) |
| 42 | + |
| 43 | + var shouldAllocate = false |
| 44 | + |
| 45 | + // Mock `TaskMemoryManager` to allocate free memory when `shouldAllocate` is true. |
| 46 | + // This will trigger a nested spill and expose issues if we don't handle this case properly. |
| 47 | + val taskMemoryManager = new TaskMemoryManager(memoryManager, 0) { |
| 48 | + override def acquireExecutionMemory(required: Long, consumer: MemoryConsumer): Long = { |
| 49 | + // ExecutionMemoryPool.acquireMemory will wait until there are 400 bytes for a task to use. |
| 50 | + // So we leave 400 bytes for the task. |
| 51 | + if (shouldAllocate && |
| 52 | + memoryManager.maxHeapMemory - memoryManager.executionMemoryUsed > 400) { |
| 53 | + val acquireExecutionMemoryMethod = |
| 54 | + memoryManager.getClass.getMethods.filter(_.getName == "acquireExecutionMemory").head |
| 55 | + acquireExecutionMemoryMethod.invoke( |
| 56 | + memoryManager, |
| 57 | + JLong.valueOf( |
| 58 | + memoryManager.maxHeapMemory - memoryManager.executionMemoryUsed - 400), |
| 59 | + JLong.valueOf(1L), // taskAttemptId |
| 60 | + MemoryMode.ON_HEAP |
| 61 | + ).asInstanceOf[java.lang.Long] |
| 62 | + } |
| 63 | + super.acquireExecutionMemory(required, consumer) |
| 64 | + } |
| 65 | + } |
| 66 | + val taskContext = mock[TaskContext] |
| 67 | + val taskMetrics = new TaskMetrics |
| 68 | + when(taskContext.taskMetrics()).thenReturn(taskMetrics) |
| 69 | + val sorter = new ShuffleExternalSorter( |
| 70 | + taskMemoryManager, |
| 71 | + sc.env.blockManager, |
| 72 | + taskContext, |
| 73 | + 100, // initialSize - This will require ShuffleInMemorySorter to acquire at least 800 bytes |
| 74 | + 1, // numPartitions |
| 75 | + conf, |
| 76 | + new ShuffleWriteMetrics) |
| 77 | + val inMemSorter = { |
| 78 | + val field = sorter.getClass.getDeclaredField("inMemSorter") |
| 79 | + field.setAccessible(true) |
| 80 | + field.get(sorter).asInstanceOf[ShuffleInMemorySorter] |
| 81 | + } |
| 82 | + // Allocate memory to make the next "insertRecord" call triggers a spill. |
| 83 | + val bytes = new Array[Byte](1) |
| 84 | + while (inMemSorter.hasSpaceForAnotherRecord) { |
| 85 | + sorter.insertRecord(bytes, Platform.BYTE_ARRAY_OFFSET, 1, 0) |
| 86 | + } |
| 87 | + |
| 88 | + // This flag will make the mocked TaskMemoryManager acquire free memory released by spill to |
| 89 | + // trigger a nested spill. |
| 90 | + shouldAllocate = true |
| 91 | + |
| 92 | + // Should throw `OutOfMemoryError` as there is no enough memory: `ShuffleInMemorySorter` |
| 93 | + // will try to acquire 800 bytes but there are only 400 bytes available. |
| 94 | + // |
| 95 | + // Before the fix, a nested spill may use a released page and this causes two tasks access the |
| 96 | + // same memory page. When a task reads memory written by another task, many types of failures |
| 97 | + // may happen. Here are some examples we have seen: |
| 98 | + // |
| 99 | + // - JVM crash. (This is easy to reproduce in the unit test as we fill newly allocated and |
| 100 | + // deallocated memory with 0xa5 and 0x5a bytes which usually points to an invalid memory |
| 101 | + // address) |
| 102 | + // - java.lang.IllegalArgumentException: Comparison method violates its general contract! |
| 103 | + // - java.lang.NullPointerException |
| 104 | + // at org.apache.spark.memory.TaskMemoryManager.getPage(TaskMemoryManager.java:384) |
| 105 | + // - java.lang.UnsupportedOperationException: Cannot grow BufferHolder by size -536870912 |
| 106 | + // because the size after growing exceeds size limitation 2147483632 |
| 107 | + intercept[OutOfMemoryError] { |
| 108 | + sorter.insertRecord(bytes, Platform.BYTE_ARRAY_OFFSET, 1, 0) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments