-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-1103] [WIP] Automatic garbage collection of RDD, shuffle and broadcast data #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1e752f1
80dd977
e427a9e
8512612
a24fefc
cb0a5a6
ae9da88
e61daa0
a7260d3
892b952
e1fba5f
f2881fd
620eca3
a007307
d2f8b97
6c9dcf6
c7ccef1
ba52e00
d0edef3
544ac86
e95479c
f201a8d
c92e4d9
0d17060
34f436f
fbfeec8
88904a3
e442246
8557c12
7edbc98
634a097
7ed72fb
5016375
f0aabb1
762a4d8
a6460d4
c5b1d98
a2cc8bc
ada45f0
cd72d19
b27f8e8
a430f06
104a89a
6222697
41c9ece
2b95b5e
4d05314
cff023c
d25a86e
f489fdc
61b8d6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…p shuffle metadata in MapOutputTrackerWorker.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package org.apache.spark | |
| import scala.collection.mutable.{ArrayBuffer, SynchronizedBuffer} | ||
|
|
||
| import java.util.concurrent.{LinkedBlockingQueue, TimeUnit} | ||
| import org.apache.spark.storage.StorageLevel | ||
|
|
||
| /** Listener class used for testing when any item has been cleaned by the Cleaner class */ | ||
| private[spark] trait CleanerListener { | ||
|
|
@@ -61,19 +62,19 @@ private[spark] class ContextCleaner(sc: SparkContext) extends Logging { | |
| } | ||
|
|
||
| /** | ||
| * Clean RDD data. Do not perform any time or resource intensive | ||
| * Schedule cleanup of RDD data. Do not perform any time or resource intensive | ||
| * computation in this function as this is called from a finalize() function. | ||
| */ | ||
| def cleanRDD(rddId: Int) { | ||
| def scheduleRDDCleanup(rddId: Int) { | ||
| enqueue(CleanRDD(rddId)) | ||
| logDebug("Enqueued RDD " + rddId + " for cleaning up") | ||
| } | ||
|
|
||
| /** | ||
| * Clean shuffle data. Do not perform any time or resource intensive | ||
| * Schedule cleanup of shuffle data. Do not perform any time or resource intensive | ||
| * computation in this function as this is called from a finalize() function. | ||
| */ | ||
| def cleanShuffle(shuffleId: Int) { | ||
| def scheduleShuffleCleanup(shuffleId: Int) { | ||
| enqueue(CleanShuffle(shuffleId)) | ||
| logDebug("Enqueued shuffle " + shuffleId + " for cleaning up") | ||
| } | ||
|
|
@@ -83,6 +84,13 @@ private[spark] class ContextCleaner(sc: SparkContext) extends Logging { | |
| listeners += listener | ||
| } | ||
|
|
||
| /** Unpersists RDD and remove all blocks for it from memory and disk. */ | ||
| def unpersistRDD(rddId: Int, blocking: Boolean) { | ||
| logDebug("Unpersisted RDD " + rddId) | ||
| sc.env.blockManager.master.removeRdd(rddId, blocking) | ||
| sc.persistentRdds.remove(rddId) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once you rebase against master, sc.unpersist pretty much does exactly the same thing. (You might have to change the argument from an RDD to an Int though). |
||
|
|
||
| /** | ||
| * Enqueue a cleaning task. Do not perform any time or resource intensive | ||
| * computation in this function as this is called from a finalize() function. | ||
|
|
@@ -115,8 +123,7 @@ private[spark] class ContextCleaner(sc: SparkContext) extends Logging { | |
| private def doCleanRDD(rddId: Int) { | ||
| try { | ||
| logDebug("Cleaning RDD " + rddId) | ||
| blockManagerMaster.removeRdd(rddId, false) | ||
| sc.persistentRdds.remove(rddId) | ||
| unpersistRDD(rddId, false) | ||
| listeners.foreach(_.rddCleaned(rddId)) | ||
| logInfo("Cleaned RDD " + rddId) | ||
| } catch { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,15 +56,15 @@ class ShuffleDependency[K, V]( | |
| override def finalize() { | ||
| try { | ||
| if (rdd != null) { | ||
| rdd.sparkContext.cleaner.cleanShuffle(shuffleId) | ||
| rdd.sparkContext.cleaner.scheduleShuffleCleanup(shuffleId) | ||
| } | ||
| } catch { | ||
| case t: Throwable => | ||
| // Paranoia - If logError throws error as well, report to stderr. | ||
| try { | ||
| logError("Error in finalize", t) | ||
|
||
| } catch { | ||
| case _ => | ||
| case _ : Throwable => | ||
| System.err.println("Error in finalize (and could not write to logError): " + t) | ||
| } | ||
| } finally { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ package org.apache.spark | |
| import java.io._ | ||
| import java.util.zip.{GZIPInputStream, GZIPOutputStream} | ||
|
|
||
| import scala.collection.mutable.{HashSet, Map} | ||
| import scala.collection.mutable.{HashSet, HashMap, Map} | ||
| import scala.concurrent.Await | ||
|
|
||
| import akka.actor._ | ||
|
|
@@ -34,6 +34,7 @@ private[spark] case class GetMapOutputStatuses(shuffleId: Int) | |
| extends MapOutputTrackerMessage | ||
| private[spark] case object StopMapOutputTracker extends MapOutputTrackerMessage | ||
|
|
||
| /** Actor class for MapOutputTrackerMaster */ | ||
| private[spark] class MapOutputTrackerMasterActor(tracker: MapOutputTrackerMaster) | ||
| extends Actor with Logging { | ||
| def receive = { | ||
|
|
@@ -50,28 +51,35 @@ private[spark] class MapOutputTrackerMasterActor(tracker: MapOutputTrackerMaster | |
| } | ||
|
|
||
| /** | ||
| * Class that keeps track of the location of the location of the map output of | ||
| * Class that keeps track of the location of the map output of | ||
| * a stage. This is abstract because different versions of MapOutputTracker | ||
| * (driver and worker) use different HashMap to store its metadata. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "... keeps track of the location of the location of the mapt output..."
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| */ | ||
| private[spark] abstract class MapOutputTracker(conf: SparkConf) extends Logging { | ||
|
|
||
| private val timeout = AkkaUtils.askTimeout(conf) | ||
|
|
||
| // Set to the MapOutputTrackerActor living on the driver | ||
| /** Set to the MapOutputTrackerActor living on the driver */ | ||
| var trackerActor: ActorRef = _ | ||
|
|
||
| /** This HashMap needs to have different storage behavior for driver and worker */ | ||
| protected val mapStatuses: Map[Int, Array[MapStatus]] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is a bit vague - could you elaborate on what is different? |
||
|
|
||
| // Incremented every time a fetch fails so that client nodes know to clear | ||
| // their cache of map output locations if this happens. | ||
| /** | ||
| * Incremented every time a fetch fails so that client nodes know to clear | ||
| * their cache of map output locations if this happens. | ||
| */ | ||
| protected var epoch: Long = 0 | ||
| protected val epochLock = new java.lang.Object | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: not part of your patch, but can this just be AnyRef?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, does the exact same thing. Changed. |
||
|
|
||
| // Send a message to the trackerActor and get its result within a default timeout, or | ||
| // throw a SparkException if this fails. | ||
| private def askTracker(message: Any): Any = { | ||
| /** Remembers which map output locations are currently being fetched on a worker */ | ||
| private val fetching = new HashSet[Int] | ||
|
|
||
| /** | ||
| * Send a message to the trackerActor and get its result within a default timeout, or | ||
| * throw a SparkException if this fails. | ||
| */ | ||
| protected def askTracker(message: Any): Any = { | ||
| try { | ||
| val future = trackerActor.ask(message)(timeout) | ||
| Await.result(future, timeout) | ||
|
|
@@ -81,17 +89,17 @@ private[spark] abstract class MapOutputTracker(conf: SparkConf) extends Logging | |
| } | ||
| } | ||
|
|
||
| // Send a one-way message to the trackerActor, to which we expect it to reply with true. | ||
| private def communicate(message: Any) { | ||
| /** Send a one-way message to the trackerActor, to which we expect it to reply with true. */ | ||
| protected def sendTracker(message: Any) { | ||
| if (askTracker(message) != true) { | ||
| throw new SparkException("Error reply received from MapOutputTracker") | ||
| } | ||
| } | ||
|
|
||
| // Remembers which map output locations are currently being fetched on a worker | ||
| private val fetching = new HashSet[Int] | ||
|
|
||
| // Called on possibly remote nodes to get the server URIs and output sizes for a given shuffle | ||
| /** | ||
| * Called from executors to get the server URIs and | ||
| * output sizes of the map outputs of a given shuffle | ||
| */ | ||
| def getServerStatuses(shuffleId: Int, reduceId: Int): Array[(BlockManagerId, Long)] = { | ||
| val statuses = mapStatuses.get(shuffleId).orNull | ||
| if (statuses == null) { | ||
|
|
@@ -150,22 +158,18 @@ private[spark] abstract class MapOutputTracker(conf: SparkConf) extends Logging | |
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not exactly related to your patch, but does the MOTMaster ever call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Will do.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I played around with this and actually realized that the these are used in the local mode. So they kind of have to be in the MapOutputTracker as both MapOutputTrackerWorker and MapOutputTrackerMaster (in local mode), needs them. |
||
|
|
||
| def stop() { | ||
| communicate(StopMapOutputTracker) | ||
| mapStatuses.clear() | ||
| trackerActor = null | ||
| } | ||
|
|
||
| // Called to get current epoch number | ||
| /** Called to get current epoch number */ | ||
| def getEpoch: Long = { | ||
| epochLock.synchronized { | ||
| return epoch | ||
| } | ||
| } | ||
|
|
||
| // Called on workers to update the epoch number, potentially clearing old outputs | ||
| // because of a fetch failure. (Each worker task calls this with the latest epoch | ||
| // number on the master at the time it was created.) | ||
| /** | ||
| * Called from executors to update the epoch number, potentially clearing old outputs | ||
| * because of a fetch failure. Each worker task calls this with the latest epoch | ||
| * number on the master at the time it was created. | ||
| */ | ||
| def updateEpoch(newEpoch: Long) { | ||
| epochLock.synchronized { | ||
| if (newEpoch > epoch) { | ||
|
|
@@ -175,24 +179,17 @@ private[spark] abstract class MapOutputTracker(conf: SparkConf) extends Logging | |
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * MapOutputTracker for the workers. This uses BoundedHashMap to keep track of | ||
| * a limited number of most recently used map output information. | ||
| */ | ||
| private[spark] class MapOutputTrackerWorker(conf: SparkConf) extends MapOutputTracker(conf) { | ||
| /** Unregister shuffle data */ | ||
| def unregisterShuffle(shuffleId: Int) { | ||
| mapStatuses.remove(shuffleId) | ||
| } | ||
|
|
||
| /** | ||
| * Bounded HashMap for storing serialized statuses in the worker. This allows | ||
| * the HashMap stay bounded in memory-usage. Things dropped from this HashMap will be | ||
| * automatically repopulated by fetching them again from the driver. Its okay to | ||
| * keep the cache size small as it unlikely that there will be a very large number of | ||
| * stages active simultaneously in the worker. | ||
| */ | ||
| protected val mapStatuses = new BoundedHashMap[Int, Array[MapStatus]]( | ||
| conf.getInt("spark.mapOutputTracker.cacheSize", 100), true | ||
| ) | ||
| def stop() { | ||
| sendTracker(StopMapOutputTracker) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this method get called on all of the executors (in addition to the master)? If so, does the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is only called when env.stop() is called, which is only called on the driver when we stop the SparkContext. Like many other things in |
||
| mapStatuses.clear() | ||
| trackerActor = null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -202,7 +199,7 @@ private[spark] class MapOutputTrackerWorker(conf: SparkConf) extends MapOutputTr | |
| private[spark] class MapOutputTrackerMaster(conf: SparkConf) | ||
| extends MapOutputTracker(conf) { | ||
|
|
||
| // Cache a serialized version of the output statuses for each shuffle to send them out faster | ||
| /** Cache a serialized version of the output statuses for each shuffle to send them out faster */ | ||
| private var cacheEpoch = epoch | ||
|
|
||
| /** | ||
|
|
@@ -211,7 +208,6 @@ private[spark] class MapOutputTrackerMaster(conf: SparkConf) | |
| * by TTL-based cleaning (if set). Other than these two | ||
| * scenarios, nothing should be dropped from this HashMap. | ||
| */ | ||
|
|
||
| protected val mapStatuses = new TimeStampedHashMap[Int, Array[MapStatus]]() | ||
| private val cachedSerializedStatuses = new TimeStampedHashMap[Int, Array[Byte]]() | ||
|
|
||
|
|
@@ -232,13 +228,15 @@ private[spark] class MapOutputTrackerMaster(conf: SparkConf) | |
| } | ||
| } | ||
|
|
||
| /** Register multiple map output information for the given shuffle */ | ||
| def registerMapOutputs(shuffleId: Int, statuses: Array[MapStatus], changeEpoch: Boolean = false) { | ||
| mapStatuses.put(shuffleId, Array[MapStatus]() ++ statuses) | ||
| if (changeEpoch) { | ||
| incrementEpoch() | ||
| } | ||
| } | ||
|
|
||
| /** Unregister map output information of the given shuffle, mapper and block manager */ | ||
| def unregisterMapOutput(shuffleId: Int, mapId: Int, bmAddress: BlockManagerId) { | ||
| val arrayOpt = mapStatuses.get(shuffleId) | ||
| if (arrayOpt.isDefined && arrayOpt.get != null) { | ||
|
|
@@ -254,11 +252,17 @@ private[spark] class MapOutputTrackerMaster(conf: SparkConf) | |
| } | ||
| } | ||
|
|
||
| def unregisterShuffle(shuffleId: Int) { | ||
| /** Unregister shuffle data */ | ||
| override def unregisterShuffle(shuffleId: Int) { | ||
| mapStatuses.remove(shuffleId) | ||
| cachedSerializedStatuses.remove(shuffleId) | ||
| } | ||
|
|
||
| /** Check if the given shuffle is being tracked */ | ||
| def containsShuffle(shuffleId: Int): Boolean = { | ||
| cachedSerializedStatuses.contains(shuffleId) || mapStatuses.contains(shuffleId) | ||
| } | ||
|
|
||
| def incrementEpoch() { | ||
| epochLock.synchronized { | ||
| epoch += 1 | ||
|
|
@@ -295,26 +299,26 @@ private[spark] class MapOutputTrackerMaster(conf: SparkConf) | |
| bytes | ||
| } | ||
|
|
||
| def contains(shuffleId: Int): Boolean = { | ||
| cachedSerializedStatuses.contains(shuffleId) || mapStatuses.contains(shuffleId) | ||
| } | ||
|
|
||
| override def stop() { | ||
| super.stop() | ||
| metadataCleaner.cancel() | ||
| cachedSerializedStatuses.clear() | ||
| } | ||
|
|
||
| override def updateEpoch(newEpoch: Long) { | ||
| // This might be called on the MapOutputTrackerMaster if we're running in local mode. | ||
| } | ||
|
|
||
| protected def cleanup(cleanupTime: Long) { | ||
| mapStatuses.clearOldValues(cleanupTime) | ||
| cachedSerializedStatuses.clearOldValues(cleanupTime) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be private |
||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to your patch, but looks like we over-indented in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| /** | ||
| * MapOutputTracker for the workers, which fetches map output information from the driver's | ||
| * MapOutputTrackerMaster. | ||
| */ | ||
| private[spark] class MapOutputTrackerWorker(conf: SparkConf) extends MapOutputTracker(conf) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pwendell Given that MapOutputTrackerWorker only change the type of HashMap used over MapOutputTracker, does it make sense to define two separate MapOutputTrackerMaster and MapOutputTrackerWorker? Or keep it as before, MapOutputTracker (for workers) and MapOutputTrackerMaster (for driver)? I think since we instantiate different classes in the driver and the worker, its best to name them accordingly. |
||
| protected val mapStatuses = new HashMap[Int, Array[MapStatus]] | ||
| } | ||
|
|
||
| private[spark] object MapOutputTracker { | ||
| private val LOG_BASE = 1.1 | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: not used