Skip to content

Commit 5ec645d

Browse files
committed
In memory shuffle (cherry-picked from amplab/graphx#135)
1 parent f36e576 commit 5ec645d

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

core/src/main/scala/org/apache/spark/scheduler/ShuffleMapTask.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.apache.spark.rdd.RDDCheckpointData
2929
import org.apache.spark.serializer.Serializer
3030
import org.apache.spark.storage._
3131
import org.apache.spark.util.{MetadataCleaner, MetadataCleanerType, TimeStampedHashMap}
32+
import java.nio.ByteBuffer
3233

3334
private[spark] object ShuffleMapTask {
3435

@@ -168,7 +169,11 @@ private[spark] class ShuffleMapTask(
168169
var totalBytes = 0L
169170
var totalTime = 0L
170171
val compressedSizes: Array[Byte] = shuffle.writers.map { writer: BlockObjectWriter =>
171-
writer.commit()
172+
// writer.commit()
173+
val bytes = writer.commit()
174+
if (bytes != null) {
175+
blockManager.putBytes(writer.blockId, ByteBuffer.wrap(bytes), StorageLevel.MEMORY_ONLY_SER, tellMaster = false)
176+
}
172177
writer.close()
173178
val size = writer.fileSegment().length
174179
totalBytes += size

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private[spark] class BlockManager(
5757

5858
private val blockInfo = new TimeStampedHashMap[BlockId, BlockInfo]
5959

60-
private[storage] val memoryStore: BlockStore = new MemoryStore(this, maxMemory)
60+
private[storage] val memoryStore = new MemoryStore(this, maxMemory)
6161
private[storage] val diskStore = new DiskStore(this, diskBlockManager)
6262

6363
// If we use Netty for shuffle, start a new Netty-based shuffle sender service.
@@ -293,7 +293,7 @@ private[spark] class BlockManager(
293293
* never deletes (recent) items.
294294
*/
295295
def getLocalFromDisk(blockId: BlockId, serializer: Serializer): Option[Iterator[Any]] = {
296-
diskStore.getValues(blockId, serializer).orElse(
296+
memoryStore.getValues(blockId, serializer).orElse(
297297
sys.error("Block " + blockId + " not found on disk, though it should be"))
298298
}
299299

@@ -313,7 +313,7 @@ private[spark] class BlockManager(
313313
// As an optimization for map output fetches, if the block is for a shuffle, return it
314314
// without acquiring a lock; the disk store never deletes (recent) items so this should work
315315
if (blockId.isShuffle) {
316-
diskStore.getBytes(blockId) match {
316+
memoryStore.getBytes(blockId) match {
317317
case Some(bytes) =>
318318
Some(bytes)
319319
case None =>
@@ -831,7 +831,7 @@ private[spark] class BlockManager(
831831
if (info != null) info.synchronized {
832832
// Removals are idempotent in disk store and memory store. At worst, we get a warning.
833833
val removedFromMemory = memoryStore.remove(blockId)
834-
val removedFromDisk = diskStore.remove(blockId)
834+
val removedFromDisk = false //diskStore.remove(blockId)
835835
if (!removedFromMemory && !removedFromDisk) {
836836
logWarning("Block " + blockId + " could not be removed as it was not found in either " +
837837
"the disk or memory store")

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.spark.storage
1919

20-
import java.io.{FileOutputStream, File, OutputStream}
20+
import java.io.{ByteArrayOutputStream, FileOutputStream, File, OutputStream}
2121
import java.nio.channels.FileChannel
2222

2323
import it.unimi.dsi.fastutil.io.FastBufferedOutputStream
@@ -44,7 +44,7 @@ private[spark] abstract class BlockObjectWriter(val blockId: BlockId) {
4444
* Flush the partial writes and commit them as a single atomic block. Return the
4545
* number of bytes written for this commit.
4646
*/
47-
def commit(): Long
47+
def commit(): Array[Byte]
4848

4949
/**
5050
* Reverts writes that haven't been flushed yet. Callers should invoke this function
@@ -106,7 +106,7 @@ private[spark] class DiskBlockObjectWriter(
106106
/** The file channel, used for repositioning / truncating the file. */
107107
private var channel: FileChannel = null
108108
private var bs: OutputStream = null
109-
private var fos: FileOutputStream = null
109+
private var fos: ByteArrayOutputStream = null
110110
private var ts: TimeTrackingOutputStream = null
111111
private var objOut: SerializationStream = null
112112
private val initialPosition = file.length()
@@ -115,9 +115,8 @@ private[spark] class DiskBlockObjectWriter(
115115
private var _timeWriting = 0L
116116

117117
override def open(): BlockObjectWriter = {
118-
fos = new FileOutputStream(file, true)
118+
fos = new ByteArrayOutputStream()
119119
ts = new TimeTrackingOutputStream(fos)
120-
channel = fos.getChannel()
121120
lastValidPosition = initialPosition
122121
bs = compressStream(new FastBufferedOutputStream(ts, bufferSize))
123122
objOut = serializer.newInstance().serializeStream(bs)
@@ -130,9 +129,6 @@ private[spark] class DiskBlockObjectWriter(
130129
if (syncWrites) {
131130
// Force outstanding writes to disk and track how long it takes
132131
objOut.flush()
133-
val start = System.nanoTime()
134-
fos.getFD.sync()
135-
_timeWriting += System.nanoTime() - start
136132
}
137133
objOut.close()
138134

@@ -149,18 +145,18 @@ private[spark] class DiskBlockObjectWriter(
149145

150146
override def isOpen: Boolean = objOut != null
151147

152-
override def commit(): Long = {
148+
override def commit(): Array[Byte] = {
153149
if (initialized) {
154150
// NOTE: Because Kryo doesn't flush the underlying stream we explicitly flush both the
155151
// serializer stream and the lower level stream.
156152
objOut.flush()
157153
bs.flush()
158154
val prevPos = lastValidPosition
159-
lastValidPosition = channel.position()
160-
lastValidPosition - prevPos
155+
lastValidPosition = fos.size()
156+
fos.toByteArray
161157
} else {
162158
// lastValidPosition is zero if stream is uninitialized
163-
lastValidPosition
159+
null
164160
}
165161
}
166162

@@ -170,7 +166,7 @@ private[spark] class DiskBlockObjectWriter(
170166
// truncate the file to the last valid position.
171167
objOut.flush()
172168
bs.flush()
173-
channel.truncate(lastValidPosition)
169+
throw new UnsupportedOperationException("Revert temporarily broken due to in memory shuffle code changes.")
174170
}
175171
}
176172

@@ -182,7 +178,7 @@ private[spark] class DiskBlockObjectWriter(
182178
}
183179

184180
override def fileSegment(): FileSegment = {
185-
new FileSegment(file, initialPosition, bytesWritten)
181+
new FileSegment(null, initialPosition, bytesWritten)
186182
}
187183

188184
// Only valid if called after close()

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import java.util.LinkedHashMap
2323
import scala.collection.mutable.ArrayBuffer
2424

2525
import org.apache.spark.util.{SizeEstimator, Utils}
26+
import org.apache.spark.serializer.Serializer
2627

2728
/**
2829
* Stores blocks in memory, either as ArrayBuffers of deserialized Java objects or as
@@ -119,6 +120,14 @@ private class MemoryStore(blockManager: BlockManager, maxMemory: Long)
119120
}
120121
}
121122

123+
/**
124+
* A version of getValues that allows a custom serializer. This is used as part of the
125+
* shuffle short-circuit code.
126+
*/
127+
def getValues(blockId: BlockId, serializer: Serializer): Option[Iterator[Any]] = {
128+
getBytes(blockId).map(bytes => blockManager.dataDeserialize(blockId, bytes, serializer))
129+
}
130+
122131
override def remove(blockId: BlockId): Boolean = {
123132
entries.synchronized {
124133
val entry = entries.remove(blockId)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ class ShuffleBlockManager(blockManager: BlockManager) extends Logging {
187187
}
188188
})
189189
}
190+
191+
def removeAllShuffleStuff() {
192+
for (state <- shuffleStates.values;
193+
group <- state.allFileGroups;
194+
(mapId, _) <- group.mapIdToIndex.iterator;
195+
reducerId <- 0 until group.files.length) {
196+
val blockId = new ShuffleBlockId(group.shuffleId, mapId, reducerId)
197+
blockManager.removeBlock(blockId, tellMaster = false)
198+
}
199+
shuffleStates.clear()
200+
}
190201
}
191202

192203
private[spark]
@@ -200,7 +211,7 @@ object ShuffleBlockManager {
200211
* Stores the absolute index of each mapId in the files of this group. For instance,
201212
* if mapId 5 is the first block in each file, mapIdToIndex(5) = 0.
202213
*/
203-
private val mapIdToIndex = new PrimitiveKeyOpenHashMap[Int, Int]()
214+
val mapIdToIndex = new PrimitiveKeyOpenHashMap[Int, Int]()
204215

205216
/**
206217
* Stores consecutive offsets of blocks into each reducer file, ordered by position in the file.

graphx/src/main/scala/org/apache/spark/graphx/Pregel.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.apache.spark.graphx
1919

2020
import scala.reflect.ClassTag
2121
import org.apache.spark.Logging
22+
import org.apache.spark.SparkEnv
2223

2324

2425
/**
@@ -143,6 +144,12 @@ object Pregel extends Logging {
143144
// hides oldMessages (depended on by newVerts), newVerts (depended on by messages), and the
144145
// vertices of prevG (depended on by newVerts, oldMessages, and the vertices of g).
145146
activeMessages = messages.count()
147+
148+
// Very ugly code to clear the in-memory shuffle data
149+
messages.foreachPartition { iter =>
150+
SparkEnv.get.blockManager.shuffleBlockManager.removeAllShuffleStuff()
151+
}
152+
146153
// Unpersist the RDDs hidden by newly-materialized RDDs
147154
oldMessages.unpersist(blocking=false)
148155
newVerts.unpersist(blocking=false)

0 commit comments

Comments
 (0)