Skip to content

Commit 65055d1

Browse files
kayousterhoutpdeyhim
authored andcommitted
[SPARK-1396] Properly cleanup DAGScheduler on job cancellation.
Previously, when jobs were cancelled, not all of the state in the DAGScheduler was cleaned up, leading to a slow memory leak in the DAGScheduler. As we expose easier ways to cancel jobs, it's more important to fix these issues. This commit also fixes a second and less serious problem, which is that previously, when a stage failed, not all of the appropriate stages were cancelled. See the "failure of stage used by two jobs" test for an example of this. This just meant that extra work was done, and is not a correctness problem. This commit adds 3 tests. “run shuffle with map stage failure” is a new test to more thoroughly test this functionality, and passes on both the old and new versions of the code. “trivial job cancellation” fails on the old code because all state wasn’t cleaned up correctly when jobs were cancelled (we didn’t remove the job from resultStageToJob). “failure of stage used by two jobs” fails on the old code because taskScheduler.cancelTasks wasn’t called for one of the stages (see test comments). This should be checked in before apache#246, which makes it easier to cancel stages / jobs. Author: Kay Ousterhout <[email protected]> Closes apache#305 from kayousterhout/incremental_abort_fix and squashes the following commits: f33d844 [Kay Ousterhout] Mark review comments 9217080 [Kay Ousterhout] Properly cleanup DAGScheduler on job cancellation.
1 parent 015a4bc commit 65055d1

File tree

2 files changed

+115
-21
lines changed

2 files changed

+115
-21
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -982,15 +982,7 @@ class DAGScheduler(
982982
if (!jobIdToStageIds.contains(jobId)) {
983983
logDebug("Trying to cancel unregistered job " + jobId)
984984
} else {
985-
val independentStages = removeJobAndIndependentStages(jobId)
986-
independentStages.foreach(taskScheduler.cancelTasks)
987-
val error = new SparkException("Job %d cancelled".format(jobId))
988-
val job = jobIdToActiveJob(jobId)
989-
job.listener.jobFailed(error)
990-
jobIdToStageIds -= jobId
991-
activeJobs -= job
992-
jobIdToActiveJob -= jobId
993-
listenerBus.post(SparkListenerJobEnd(job.jobId, JobFailed(error, job.finalStage.id)))
985+
failJobAndIndependentStages(jobIdToActiveJob(jobId), s"Job $jobId cancelled")
994986
}
995987
}
996988

@@ -1007,19 +999,39 @@ class DAGScheduler(
1007999
stageToInfos(failedStage).completionTime = Some(System.currentTimeMillis())
10081000
for (resultStage <- dependentStages) {
10091001
val job = resultStageToJob(resultStage)
1010-
val error = new SparkException("Job aborted: " + reason)
1011-
job.listener.jobFailed(error)
1012-
jobIdToStageIdsRemove(job.jobId)
1013-
jobIdToActiveJob -= resultStage.jobId
1014-
activeJobs -= job
1015-
resultStageToJob -= resultStage
1016-
listenerBus.post(SparkListenerJobEnd(job.jobId, JobFailed(error, failedStage.id)))
1002+
failJobAndIndependentStages(job, s"Job aborted due to stage failure: $reason")
10171003
}
10181004
if (dependentStages.isEmpty) {
10191005
logInfo("Ignoring failure of " + failedStage + " because all jobs depending on it are done")
10201006
}
10211007
}
10221008

1009+
/**
1010+
* Fails a job and all stages that are only used by that job, and cleans up relevant state.
1011+
*/
1012+
private def failJobAndIndependentStages(job: ActiveJob, failureReason: String) {
1013+
val error = new SparkException(failureReason)
1014+
job.listener.jobFailed(error)
1015+
1016+
// Cancel all tasks in independent stages.
1017+
val independentStages = removeJobAndIndependentStages(job.jobId)
1018+
independentStages.foreach(taskScheduler.cancelTasks)
1019+
1020+
// Clean up remaining state we store for the job.
1021+
jobIdToActiveJob -= job.jobId
1022+
activeJobs -= job
1023+
jobIdToStageIds -= job.jobId
1024+
val resultStagesForJob = resultStageToJob.keySet.filter(
1025+
stage => resultStageToJob(stage).jobId == job.jobId)
1026+
if (resultStagesForJob.size != 1) {
1027+
logWarning(
1028+
s"${resultStagesForJob.size} result stages for job ${job.jobId} (expect exactly 1)")
1029+
}
1030+
resultStageToJob --= resultStagesForJob
1031+
1032+
listenerBus.post(SparkListenerJobEnd(job.jobId, JobFailed(error, job.finalStage.id)))
1033+
}
1034+
10231035
/**
10241036
* Return true if one of stage's ancestors is target.
10251037
*/

core/src/test/scala/org/apache/spark/scheduler/DAGSchedulerSuite.scala

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.spark.scheduler
1919

2020
import scala.Tuple2
21-
import scala.collection.mutable.{HashMap, Map}
21+
import scala.collection.mutable.{HashSet, HashMap, Map}
2222

2323
import org.scalatest.{BeforeAndAfter, FunSuite}
2424

@@ -43,6 +43,10 @@ class DAGSchedulerSuite extends FunSuite with BeforeAndAfter with LocalSparkCont
4343
val conf = new SparkConf
4444
/** Set of TaskSets the DAGScheduler has requested executed. */
4545
val taskSets = scala.collection.mutable.Buffer[TaskSet]()
46+
47+
/** Stages for which the DAGScheduler has called TaskScheduler.cancelTasks(). */
48+
val cancelledStages = new HashSet[Int]()
49+
4650
val taskScheduler = new TaskScheduler() {
4751
override def rootPool: Pool = null
4852
override def schedulingMode: SchedulingMode = SchedulingMode.NONE
@@ -53,7 +57,9 @@ class DAGSchedulerSuite extends FunSuite with BeforeAndAfter with LocalSparkCont
5357
taskSet.tasks.foreach(_.epoch = mapOutputTracker.getEpoch)
5458
taskSets += taskSet
5559
}
56-
override def cancelTasks(stageId: Int) {}
60+
override def cancelTasks(stageId: Int) {
61+
cancelledStages += stageId
62+
}
5763
override def setDAGScheduler(dagScheduler: DAGScheduler) = {}
5864
override def defaultParallelism() = 2
5965
}
@@ -91,6 +97,7 @@ class DAGSchedulerSuite extends FunSuite with BeforeAndAfter with LocalSparkCont
9197
before {
9298
sc = new SparkContext("local", "DAGSchedulerSuite")
9399
taskSets.clear()
100+
cancelledStages.clear()
94101
cacheLocations.clear()
95102
results.clear()
96103
mapOutputTracker = new MapOutputTrackerMaster(conf)
@@ -174,22 +181,28 @@ class DAGSchedulerSuite extends FunSuite with BeforeAndAfter with LocalSparkCont
174181
}
175182
}
176183

177-
/** Sends the rdd to the scheduler for scheduling. */
184+
/** Sends the rdd to the scheduler for scheduling and returns the job id. */
178185
private def submit(
179186
rdd: RDD[_],
180187
partitions: Array[Int],
181188
func: (TaskContext, Iterator[_]) => _ = jobComputeFunc,
182189
allowLocal: Boolean = false,
183-
listener: JobListener = listener) {
190+
listener: JobListener = listener): Int = {
184191
val jobId = scheduler.nextJobId.getAndIncrement()
185192
runEvent(JobSubmitted(jobId, rdd, func, partitions, allowLocal, null, listener))
193+
return jobId
186194
}
187195

188196
/** Sends TaskSetFailed to the scheduler. */
189197
private def failed(taskSet: TaskSet, message: String) {
190198
runEvent(TaskSetFailed(taskSet, message))
191199
}
192200

201+
/** Sends JobCancelled to the DAG scheduler. */
202+
private def cancel(jobId: Int) {
203+
runEvent(JobCancelled(jobId))
204+
}
205+
193206
test("zero split job") {
194207
val rdd = makeRdd(0, Nil)
195208
var numResults = 0
@@ -248,7 +261,15 @@ class DAGSchedulerSuite extends FunSuite with BeforeAndAfter with LocalSparkCont
248261
test("trivial job failure") {
249262
submit(makeRdd(1, Nil), Array(0))
250263
failed(taskSets(0), "some failure")
251-
assert(failure.getMessage === "Job aborted: some failure")
264+
assert(failure.getMessage === "Job aborted due to stage failure: some failure")
265+
assertDataStructuresEmpty
266+
}
267+
268+
test("trivial job cancellation") {
269+
val rdd = makeRdd(1, Nil)
270+
val jobId = submit(rdd, Array(0))
271+
cancel(jobId)
272+
assert(failure.getMessage === s"Job $jobId cancelled")
252273
assertDataStructuresEmpty
253274
}
254275

@@ -323,6 +344,67 @@ class DAGSchedulerSuite extends FunSuite with BeforeAndAfter with LocalSparkCont
323344
assertDataStructuresEmpty
324345
}
325346

347+
test("run shuffle with map stage failure") {
348+
val shuffleMapRdd = makeRdd(2, Nil)
349+
val shuffleDep = new ShuffleDependency(shuffleMapRdd, null)
350+
val reduceRdd = makeRdd(2, List(shuffleDep))
351+
submit(reduceRdd, Array(0, 1))
352+
353+
// Fail the map stage. This should cause the entire job to fail.
354+
val stageFailureMessage = "Exception failure in map stage"
355+
failed(taskSets(0), stageFailureMessage)
356+
assert(failure.getMessage === s"Job aborted due to stage failure: $stageFailureMessage")
357+
assertDataStructuresEmpty
358+
}
359+
360+
/**
361+
* Makes sure that failures of stage used by multiple jobs are correctly handled.
362+
*
363+
* This test creates the following dependency graph:
364+
*
365+
* shuffleMapRdd1 shuffleMapRDD2
366+
* | \ |
367+
* | \ |
368+
* | \ |
369+
* | \ |
370+
* reduceRdd1 reduceRdd2
371+
*
372+
* We start both shuffleMapRdds and then fail shuffleMapRdd1. As a result, the job listeners for
373+
* reduceRdd1 and reduceRdd2 should both be informed that the job failed. shuffleMapRDD2 should
374+
* also be cancelled, because it is only used by reduceRdd2 and reduceRdd2 cannot complete
375+
* without shuffleMapRdd1.
376+
*/
377+
test("failure of stage used by two jobs") {
378+
val shuffleMapRdd1 = makeRdd(2, Nil)
379+
val shuffleDep1 = new ShuffleDependency(shuffleMapRdd1, null)
380+
val shuffleMapRdd2 = makeRdd(2, Nil)
381+
val shuffleDep2 = new ShuffleDependency(shuffleMapRdd2, null)
382+
383+
val reduceRdd1 = makeRdd(2, List(shuffleDep1))
384+
val reduceRdd2 = makeRdd(2, List(shuffleDep1, shuffleDep2))
385+
386+
// We need to make our own listeners for this test, since by default submit uses the same
387+
// listener for all jobs, and here we want to capture the failure for each job separately.
388+
class FailureRecordingJobListener() extends JobListener {
389+
var failureMessage: String = _
390+
override def taskSucceeded(index: Int, result: Any) {}
391+
override def jobFailed(exception: Exception) = { failureMessage = exception.getMessage }
392+
}
393+
val listener1 = new FailureRecordingJobListener()
394+
val listener2 = new FailureRecordingJobListener()
395+
396+
submit(reduceRdd1, Array(0, 1), listener=listener1)
397+
submit(reduceRdd2, Array(0, 1), listener=listener2)
398+
399+
val stageFailureMessage = "Exception failure in map stage"
400+
failed(taskSets(0), stageFailureMessage)
401+
402+
assert(cancelledStages.contains(1))
403+
assert(listener1.failureMessage === s"Job aborted due to stage failure: $stageFailureMessage")
404+
assert(listener2.failureMessage === s"Job aborted due to stage failure: $stageFailureMessage")
405+
assertDataStructuresEmpty
406+
}
407+
326408
test("run trivial shuffle with out-of-band failure and retry") {
327409
val shuffleMapRdd = makeRdd(2, Nil)
328410
val shuffleDep = new ShuffleDependency(shuffleMapRdd, null)

0 commit comments

Comments
 (0)