Skip to content

Commit eea9558

Browse files
cloud-fankai-chi
authored andcommitted
[SPARK-27065][CORE] avoid more than one active task set managers for a stage
## What changes were proposed in this pull request? This is another attempt to fix the more-than-one-active-task-set-managers bug. apache#17208 is the first attempt. It marks the TSM as zombie before sending a task completion event to DAGScheduler. This is necessary, because when the DAGScheduler gets the task completion event, and it's for the last partition, then the stage is finished. However, if it's a shuffle stage and it has missing map outputs, DAGScheduler will resubmit it(see the [code](https://github.com/apache/spark/blob/v2.4.0/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala#L1416-L1422)) and create a new TSM for this stage. This leads to more than one active TSM of a stage and fail. This fix has a hole: Let's say a stage has 10 partitions and 2 task set managers: TSM1(zombie) and TSM2(active). TSM1 has a running task for partition 10 and it completes. TSM2 finishes tasks for partitions 1-9, and thinks he is still active because he hasn't finished partition 10 yet. However, DAGScheduler gets task completion events for all the 10 partitions and thinks the stage is finished. Then the same problem occurs: DAGScheduler may resubmit the stage and cause more than one actice TSM error. apache#21131 fixed this hole by notifying all the task set managers when a task finishes. For the above case, TSM2 will know that partition 10 is already completed, so he can mark himself as zombie after partitions 1-9 are completed. However, apache#21131 still has a hole: TSM2 may be created after the task from TSM1 is completed. Then TSM2 can't get notified about the task completion, and leads to the more than one active TSM error. apache#22806 and apache#23871 are created to fix this hole. However the fix is complicated and there are still ongoing discussions. This PR proposes a simple fix, which can be easy to backport: mark all existing task set managers as zombie when trying to create a new task set manager. After this PR, apache#21131 is still necessary, to avoid launching unnecessary tasks and fix [SPARK-25250](https://issues.apache.org/jira/browse/SPARK-25250 ). apache#22806 and apache#23871 are its followups to fix the hole. ## How was this patch tested? existing tests. Closes apache#23927 from cloud-fan/scheduler. Authored-by: Wenchen Fan <[email protected]> Signed-off-by: Imran Rashid <[email protected]> (cherry picked from commit cb20fbc) Signed-off-by: Imran Rashid <[email protected]>
1 parent 3346f5f commit eea9558

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,20 @@ private[spark] class TaskSchedulerImpl(
207207
val stage = taskSet.stageId
208208
val stageTaskSets =
209209
taskSetsByStageIdAndAttempt.getOrElseUpdate(stage, new HashMap[Int, TaskSetManager])
210-
stageTaskSets(taskSet.stageAttemptId) = manager
211-
val conflictingTaskSet = stageTaskSets.exists { case (_, ts) =>
212-
ts.taskSet != taskSet && !ts.isZombie
213-
}
214-
if (conflictingTaskSet) {
215-
throw new IllegalStateException(s"more than one active taskSet for stage $stage:" +
216-
s" ${stageTaskSets.toSeq.map{_._2.taskSet.id}.mkString(",")}")
210+
211+
// Mark all the existing TaskSetManagers of this stage as zombie, as we are adding a new one.
212+
// This is necessary to handle a corner case. Let's say a stage has 10 partitions and has 2
213+
// TaskSetManagers: TSM1(zombie) and TSM2(active). TSM1 has a running task for partition 10
214+
// and it completes. TSM2 finishes tasks for partition 1-9, and thinks he is still active
215+
// because partition 10 is not completed yet. However, DAGScheduler gets task completion
216+
// events for all the 10 partitions and thinks the stage is finished. If it's a shuffle stage
217+
// and somehow it has missing map outputs, then DAGScheduler will resubmit it and create a
218+
// TSM3 for it. As a stage can't have more than one active task set managers, we must mark
219+
// TSM2 as zombie (it actually is).
220+
stageTaskSets.foreach { case (_, ts) =>
221+
ts.isZombie = true
217222
}
223+
stageTaskSets(taskSet.stageAttemptId) = manager
218224
schedulableBuilder.addTaskSetManager(manager, manager.taskSet.properties)
219225

220226
if (!isLocal && !hasReceivedTask) {

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,28 +201,39 @@ class TaskSchedulerImplSuite extends SparkFunSuite with LocalSparkContext with B
201201
// Even if one of the task sets has not-serializable tasks, the other task set should
202202
// still be processed without error
203203
taskScheduler.submitTasks(FakeTask.createTaskSet(1))
204-
taskScheduler.submitTasks(taskSet)
204+
val taskSet2 = new TaskSet(
205+
Array(new NotSerializableFakeTask(1, 0), new NotSerializableFakeTask(0, 1)), 1, 0, 0, null)
206+
taskScheduler.submitTasks(taskSet2)
205207
taskDescriptions = taskScheduler.resourceOffers(multiCoreWorkerOffers).flatten
206208
assert(taskDescriptions.map(_.executorId) === Seq("executor0"))
207209
}
208210

209-
test("refuse to schedule concurrent attempts for the same stage (SPARK-8103)") {
211+
test("concurrent attempts for the same stage only have one active taskset") {
210212
val taskScheduler = setupScheduler()
213+
def isTasksetZombie(taskset: TaskSet): Boolean = {
214+
taskScheduler.taskSetManagerForAttempt(taskset.stageId, taskset.stageAttemptId).get.isZombie
215+
}
216+
211217
val attempt1 = FakeTask.createTaskSet(1, 0)
212-
val attempt2 = FakeTask.createTaskSet(1, 1)
213218
taskScheduler.submitTasks(attempt1)
214-
intercept[IllegalStateException] { taskScheduler.submitTasks(attempt2) }
219+
// The first submitted taskset is active
220+
assert(!isTasksetZombie(attempt1))
215221

216-
// OK to submit multiple if previous attempts are all zombie
217-
taskScheduler.taskSetManagerForAttempt(attempt1.stageId, attempt1.stageAttemptId)
218-
.get.isZombie = true
222+
val attempt2 = FakeTask.createTaskSet(1, 1)
219223
taskScheduler.submitTasks(attempt2)
224+
// The first submitted taskset is zombie now
225+
assert(isTasksetZombie(attempt1))
226+
// The newly submitted taskset is active
227+
assert(!isTasksetZombie(attempt2))
228+
220229
val attempt3 = FakeTask.createTaskSet(1, 2)
221-
intercept[IllegalStateException] { taskScheduler.submitTasks(attempt3) }
222-
taskScheduler.taskSetManagerForAttempt(attempt2.stageId, attempt2.stageAttemptId)
223-
.get.isZombie = true
224230
taskScheduler.submitTasks(attempt3)
225-
assert(!failedTaskSet)
231+
// The first submitted taskset remains zombie
232+
assert(isTasksetZombie(attempt1))
233+
// The second submitted taskset is zombie now
234+
assert(isTasksetZombie(attempt2))
235+
// The newly submitted taskset is active
236+
assert(!isTasksetZombie(attempt3))
226237
}
227238

228239
test("don't schedule more tasks after a taskset is zombie") {

0 commit comments

Comments
 (0)