Skip to content

Commit 13ba7ad

Browse files
committed
Put fail-fast for non-fitting blocks earlier in call chain.
1 parent 5384117 commit 13ba7ad

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

core/src/main/scala/org/apache/spark/memory/StaticMemoryManager.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ private[spark] class StaticMemoryManager(
5757
blockId: BlockId,
5858
numBytes: Long,
5959
evictedBlocks: mutable.Buffer[(BlockId, BlockStatus)]): Boolean = synchronized {
60-
storageMemoryPool.acquireMemory(blockId, numBytes, evictedBlocks)
60+
if (numBytes > storageMemoryPool.poolSize) {
61+
// Fail fast if the block simply won't fit
62+
logInfo(s"Will not store $blockId as the required space ($numBytes bytes) exceeds our " +
63+
s"memory limit (${storageMemoryPool.poolSize} bytes)")
64+
false
65+
} else {
66+
storageMemoryPool.acquireMemory(blockId, numBytes, evictedBlocks)
67+
}
6168
}
6269

6370
override def acquireUnrollMemory(

core/src/main/scala/org/apache/spark/memory/UnifiedMemoryManager.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ private[spark] class UnifiedMemoryManager private[memory] (
110110
evictedBlocks: mutable.Buffer[(BlockId, BlockStatus)]): Boolean = synchronized {
111111
assert(onHeapExecutionMemoryPool.poolSize + storageMemoryPool.poolSize == maxMemory)
112112
assert(numBytes >= 0)
113+
if (numBytes > maxMemory) {
114+
// Fail fast if the block simply won't fit
115+
logInfo(s"Will not store $blockId as the required space ($numBytes bytes) exceeds our " +
116+
s"memory limit ($maxMemory bytes)")
117+
return false
118+
}
113119
if (numBytes > storageMemoryPool.memoryFree) {
114120
// There is not enough free memory in the storage pool, so try to borrow free memory from
115121
// the execution pool.

core/src/main/scala/org/apache/spark/storage/MemoryStore.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,9 @@ private[spark] class MemoryStore(blockManager: BlockManager, memoryManager: Memo
454454
blockId.map { id => s"for block $id" }.getOrElse("") +
455455
s"(free: $freeMemory, max: $maxMemory)")
456456

457-
if (space > maxMemory) {
458-
// Fail fast if the block simply won't fit
459-
logInfo("Will not " + blockId.map { id => s"store $id" }.getOrElse("free memory") +
460-
s" as the required space ($space bytes) exceeds our memory limit ($maxMemory bytes)")
461-
false
462-
} else if (freeMemory >= space) {
457+
assert(space <= maxMemory)
458+
459+
if (freeMemory >= space) {
463460
// No need to evict anything if there is already enough free space
464461
true
465462
} else {

core/src/test/scala/org/apache/spark/memory/StaticMemoryManagerSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class StaticMemoryManagerSuite extends MemoryManagerSuite {
9090
assert(mm.storageMemoryUsed === 110L)
9191
// Acquire more than the max, not granted
9292
assert(!mm.acquireStorageMemory(dummyBlock, maxStorageMem + 1L, evictedBlocks))
93-
assertEnsureFreeSpaceCalled(ms, maxStorageMem + 1L)
93+
assertEnsureFreeSpaceNotCalled(ms)
9494
assert(mm.storageMemoryUsed === 110L)
9595
// Acquire up to the max, requests after this are still granted due to LRU eviction
9696
assert(mm.acquireStorageMemory(dummyBlock, maxStorageMem, evictedBlocks))

core/src/test/scala/org/apache/spark/memory/UnifiedMemoryManagerSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class UnifiedMemoryManagerSuite extends MemoryManagerSuite with PrivateMethodTes
8484
assert(evictedBlocks.isEmpty)
8585
// Acquire more than the max, not granted
8686
assert(!mm.acquireStorageMemory(dummyBlock, maxMemory + 1L, evictedBlocks))
87-
assertEnsureFreeSpaceCalled(ms, maxMemory + 1L)
87+
assertEnsureFreeSpaceNotCalled(ms)
8888
assert(mm.storageMemoryUsed === 110L)
8989
assert(evictedBlocks.isEmpty)
9090
// Acquire up to the max, requests after this are still granted due to LRU eviction

0 commit comments

Comments
 (0)