Skip to content

Commit 745dc42

Browse files
committed
Merge pull request alteryx#118 from JoshRosen/blockinfo-memory-usage
Reduce the memory footprint of BlockInfo objects This pull request reduces the memory footprint of all BlockInfo objects and makes additional optimizations for shuffle blocks. For all BlockInfo objects, these changes remove two boolean fields and one Object field. For shuffle blocks, we additionally remove an Object field and a boolean field. When storing tens of thousands of these objects, this may add up to significant memory savings. A ShuffleBlockInfo now only needs to wrap a single long. This was motivated by a [report of high blockInfo memory usage during shuffles](https://mail-archives.apache.org/mod_mbox/incubator-spark-user/201310.mbox/%3C20131026134353.202b2b9b%40sh9%3E). I haven't run benchmarks to measure the exact memory savings. /cc @aarondav
2 parents f0e23a0 + cb9c8a9 commit 745dc42

File tree

2 files changed

+99
-57
lines changed

2 files changed

+99
-57
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.storage
19+
20+
import java.util.concurrent.ConcurrentHashMap
21+
22+
private[storage] trait BlockInfo {
23+
def level: StorageLevel
24+
def tellMaster: Boolean
25+
// To save space, 'pending' and 'failed' are encoded as special sizes:
26+
@volatile var size: Long = BlockInfo.BLOCK_PENDING
27+
private def pending: Boolean = size == BlockInfo.BLOCK_PENDING
28+
private def failed: Boolean = size == BlockInfo.BLOCK_FAILED
29+
private def initThread: Thread = BlockInfo.blockInfoInitThreads.get(this)
30+
31+
setInitThread()
32+
33+
private def setInitThread() {
34+
// Set current thread as init thread - waitForReady will not block this thread
35+
// (in case there is non trivial initialization which ends up calling waitForReady as part of
36+
// initialization itself)
37+
BlockInfo.blockInfoInitThreads.put(this, Thread.currentThread())
38+
}
39+
40+
/**
41+
* Wait for this BlockInfo to be marked as ready (i.e. block is finished writing).
42+
* Return true if the block is available, false otherwise.
43+
*/
44+
def waitForReady(): Boolean = {
45+
if (pending && initThread != Thread.currentThread()) {
46+
synchronized {
47+
while (pending) this.wait()
48+
}
49+
}
50+
!failed
51+
}
52+
53+
/** Mark this BlockInfo as ready (i.e. block is finished writing) */
54+
def markReady(sizeInBytes: Long) {
55+
require (sizeInBytes >= 0, "sizeInBytes was negative: " + sizeInBytes)
56+
assert (pending)
57+
size = sizeInBytes
58+
BlockInfo.blockInfoInitThreads.remove(this)
59+
synchronized {
60+
this.notifyAll()
61+
}
62+
}
63+
64+
/** Mark this BlockInfo as ready but failed */
65+
def markFailure() {
66+
assert (pending)
67+
size = BlockInfo.BLOCK_FAILED
68+
BlockInfo.blockInfoInitThreads.remove(this)
69+
synchronized {
70+
this.notifyAll()
71+
}
72+
}
73+
}
74+
75+
private object BlockInfo {
76+
// initThread is logically a BlockInfo field, but we store it here because
77+
// it's only needed while this block is in the 'pending' state and we want
78+
// to minimize BlockInfo's memory footprint.
79+
private val blockInfoInitThreads = new ConcurrentHashMap[BlockInfo, Thread]
80+
81+
private val BLOCK_PENDING: Long = -1L
82+
private val BLOCK_FAILED: Long = -2L
83+
}
84+
85+
// All shuffle blocks have the same `level` and `tellMaster` properties,
86+
// so we can save space by not storing them in each instance:
87+
private[storage] class ShuffleBlockInfo extends BlockInfo {
88+
// These need to be defined using 'def' instead of 'val' in order for
89+
// the compiler to eliminate the fields:
90+
def level: StorageLevel = StorageLevel.DISK_ONLY
91+
def tellMaster: Boolean = false
92+
}
93+
94+
private[storage] class BlockInfoImpl(val level: StorageLevel, val tellMaster: Boolean)
95+
extends BlockInfo {
96+
// Intentionally left blank
97+
}

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

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,61 +46,6 @@ private[spark] class BlockManager(
4646
maxMemory: Long)
4747
extends Logging {
4848

49-
private class BlockInfo(val level: StorageLevel, val tellMaster: Boolean) {
50-
@volatile var pending: Boolean = true
51-
@volatile var size: Long = -1L
52-
@volatile var initThread: Thread = null
53-
@volatile var failed = false
54-
55-
setInitThread()
56-
57-
private def setInitThread() {
58-
// Set current thread as init thread - waitForReady will not block this thread
59-
// (in case there is non trivial initialization which ends up calling waitForReady as part of
60-
// initialization itself)
61-
this.initThread = Thread.currentThread()
62-
}
63-
64-
/**
65-
* Wait for this BlockInfo to be marked as ready (i.e. block is finished writing).
66-
* Return true if the block is available, false otherwise.
67-
*/
68-
def waitForReady(): Boolean = {
69-
if (initThread != Thread.currentThread() && pending) {
70-
synchronized {
71-
while (pending) this.wait()
72-
}
73-
}
74-
!failed
75-
}
76-
77-
/** Mark this BlockInfo as ready (i.e. block is finished writing) */
78-
def markReady(sizeInBytes: Long) {
79-
assert (pending)
80-
size = sizeInBytes
81-
initThread = null
82-
failed = false
83-
initThread = null
84-
pending = false
85-
synchronized {
86-
this.notifyAll()
87-
}
88-
}
89-
90-
/** Mark this BlockInfo as ready but failed */
91-
def markFailure() {
92-
assert (pending)
93-
size = 0
94-
initThread = null
95-
failed = true
96-
initThread = null
97-
pending = false
98-
synchronized {
99-
this.notifyAll()
100-
}
101-
}
102-
}
103-
10449
val shuffleBlockManager = new ShuffleBlockManager(this)
10550
val diskBlockManager = new DiskBlockManager(
10651
System.getProperty("spark.local.dir", System.getProperty("java.io.tmpdir")))
@@ -526,7 +471,7 @@ private[spark] class BlockManager(
526471
if (shuffleBlockManager.consolidateShuffleFiles) {
527472
diskBlockManager.mapBlockToFileSegment(blockId, writer.fileSegment())
528473
}
529-
val myInfo = new BlockInfo(StorageLevel.DISK_ONLY, false)
474+
val myInfo = new ShuffleBlockInfo()
530475
blockInfo.put(blockId, myInfo)
531476
myInfo.markReady(writer.fileSegment().length)
532477
})
@@ -560,7 +505,7 @@ private[spark] class BlockManager(
560505
// to be dropped right after it got put into memory. Note, however, that other threads will
561506
// not be able to get() this block until we call markReady on its BlockInfo.
562507
val myInfo = {
563-
val tinfo = new BlockInfo(level, tellMaster)
508+
val tinfo = new BlockInfoImpl(level, tellMaster)
564509
// Do atomically !
565510
val oldBlockOpt = blockInfo.putIfAbsent(blockId, tinfo)
566511

0 commit comments

Comments
 (0)