Skip to content

Commit 8f25cb2

Browse files
committed
[SPARK-18553][CORE][BRANCH-1.6] Fix leak of TaskSetManager following executor loss
## What changes were proposed in this pull request? _This is the master branch-1.6 version of #15986; the original description follows:_ This patch fixes a critical resource leak in the TaskScheduler which could cause RDDs and ShuffleDependencies to be kept alive indefinitely if an executor with running tasks is permanently lost and the associated stage fails. This problem was originally identified by analyzing the heap dump of a driver belonging to a cluster that had run out of shuffle space. This dump contained several `ShuffleDependency` instances that were retained by `TaskSetManager`s inside the scheduler but were not otherwise referenced. Each of these `TaskSetManager`s was considered a "zombie" but had no running tasks and therefore should have been cleaned up. However, these zombie task sets were still referenced by the `TaskSchedulerImpl.taskIdToTaskSetManager` map. Entries are added to the `taskIdToTaskSetManager` map when tasks are launched and are removed inside of `TaskScheduler.statusUpdate()`, which is invoked by the scheduler backend while processing `StatusUpdate` messages from executors. The problem with this design is that a completely dead executor will never send a `StatusUpdate`. There is [some code](https://github.com/apache/spark/blob/072f4c518cdc57d705beec6bcc3113d9a6740819/core/src/main/scala/org/apache/spark/scheduler/TaskSchedulerImpl.scala#L338) in `statusUpdate` which handles tasks that exit with the `TaskState.LOST` state (which is supposed to correspond to a task failure triggered by total executor loss), but this state only seems to be used in Mesos fine-grained mode. There doesn't seem to be any code which performs per-task state cleanup for tasks that were running on an executor that completely disappears without sending any sort of final death message. The `executorLost` and [`removeExecutor`](https://github.com/apache/spark/blob/072f4c518cdc57d705beec6bcc3113d9a6740819/core/src/main/scala/org/apache/spark/scheduler/TaskSchedulerImpl.scala#L527) methods don't appear to perform any cleanup of the `taskId -> *` mappings, causing the leaks observed here. This patch's fix is to maintain a `executorId -> running task id` mapping so that these `taskId -> *` maps can be properly cleaned up following an executor loss. There are some potential corner-case interactions that I'm concerned about here, especially some details in [the comment](https://github.com/apache/spark/blob/072f4c518cdc57d705beec6bcc3113d9a6740819/core/src/main/scala/org/apache/spark/scheduler/TaskSchedulerImpl.scala#L523) in `removeExecutor`, so I'd appreciate a very careful review of these changes. ## How was this patch tested? I added a new unit test to `TaskSchedulerImplSuite`. /cc kayousterhout and markhamstra, who reviewed #15986. Author: Josh Rosen <[email protected]> Closes #16070 from JoshRosen/fix-leak-following-total-executor-loss-1.6.
1 parent 9136e26 commit 8f25cb2

File tree

3 files changed

+115
-33
lines changed

3 files changed

+115
-33
lines changed

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

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ private[spark] class TaskSchedulerImpl(
8787
// Incrementing task IDs
8888
val nextTaskId = new AtomicLong(0)
8989

90-
// Number of tasks running on each executor
91-
private val executorIdToTaskCount = new HashMap[String, Int]
90+
// IDs of the tasks running on each executor
91+
private val executorIdToRunningTaskIds = new HashMap[String, HashSet[Long]]
9292

9393
// The set of executors we have on each host; this is used to compute hostsAlive, which
9494
// in turn is used to decide when we can attain data locality on a given host
@@ -254,7 +254,7 @@ private[spark] class TaskSchedulerImpl(
254254
val tid = task.taskId
255255
taskIdToTaskSetManager(tid) = taskSet
256256
taskIdToExecutorId(tid) = execId
257-
executorIdToTaskCount(execId) += 1
257+
executorIdToRunningTaskIds(execId).add(tid)
258258
executorsByHost(host) += execId
259259
availableCpus(i) -= CPUS_PER_TASK
260260
assert(availableCpus(i) >= 0)
@@ -283,7 +283,7 @@ private[spark] class TaskSchedulerImpl(
283283
var newExecAvail = false
284284
for (o <- offers) {
285285
executorIdToHost(o.executorId) = o.host
286-
executorIdToTaskCount.getOrElseUpdate(o.executorId, 0)
286+
executorIdToRunningTaskIds.getOrElseUpdate(o.executorId, HashSet[Long]())
287287
if (!executorsByHost.contains(o.host)) {
288288
executorsByHost(o.host) = new HashSet[String]()
289289
executorAdded(o.executorId, o.host)
@@ -329,37 +329,34 @@ private[spark] class TaskSchedulerImpl(
329329
var failedExecutor: Option[String] = None
330330
synchronized {
331331
try {
332-
if (state == TaskState.LOST && taskIdToExecutorId.contains(tid)) {
333-
// We lost this entire executor, so remember that it's gone
334-
val execId = taskIdToExecutorId(tid)
335-
336-
if (executorIdToTaskCount.contains(execId)) {
337-
removeExecutor(execId,
338-
SlaveLost(s"Task $tid was lost, so marking the executor as lost as well."))
339-
failedExecutor = Some(execId)
340-
}
341-
}
342332
taskIdToTaskSetManager.get(tid) match {
343333
case Some(taskSet) =>
344-
if (TaskState.isFinished(state)) {
345-
taskIdToTaskSetManager.remove(tid)
346-
taskIdToExecutorId.remove(tid).foreach { execId =>
347-
if (executorIdToTaskCount.contains(execId)) {
348-
executorIdToTaskCount(execId) -= 1
349-
}
334+
if (state == TaskState.LOST) {
335+
// TaskState.LOST is only used by the Mesos fine-grained scheduling mode,
336+
// where each executor corresponds to a single task, so mark the executor as failed.
337+
val execId = taskIdToExecutorId.getOrElse(tid, throw new IllegalStateException(
338+
"taskIdToTaskSetManager.contains(tid) <=> taskIdToExecutorId.contains(tid)"))
339+
if (executorIdToRunningTaskIds.contains(execId)) {
340+
val reason =
341+
SlaveLost(s"Task $tid was lost, so marking the executor as lost as well.")
342+
removeExecutor(execId, reason)
343+
failedExecutor = Some(execId)
350344
}
351345
}
352-
if (state == TaskState.FINISHED) {
353-
taskSet.removeRunningTask(tid)
354-
taskResultGetter.enqueueSuccessfulTask(taskSet, tid, serializedData)
355-
} else if (Set(TaskState.FAILED, TaskState.KILLED, TaskState.LOST).contains(state)) {
346+
if (TaskState.isFinished(state)) {
347+
cleanupTaskState(tid)
356348
taskSet.removeRunningTask(tid)
357-
taskResultGetter.enqueueFailedTask(taskSet, tid, state, serializedData)
349+
if (state == TaskState.FINISHED) {
350+
taskResultGetter.enqueueSuccessfulTask(taskSet, tid, serializedData)
351+
} else if (Set(TaskState.FAILED, TaskState.KILLED, TaskState.LOST).contains(state)) {
352+
taskResultGetter.enqueueFailedTask(taskSet, tid, state, serializedData)
353+
}
358354
}
359355
case None =>
360356
logError(
361357
("Ignoring update with state %s for TID %s because its task set is gone (this is " +
362-
"likely the result of receiving duplicate task finished status updates)")
358+
"likely the result of receiving duplicate task finished status updates) or its " +
359+
"executor has been marked as failed.")
363360
.format(state, tid))
364361
}
365362
} catch {
@@ -468,7 +465,7 @@ private[spark] class TaskSchedulerImpl(
468465
var failedExecutor: Option[String] = None
469466

470467
synchronized {
471-
if (executorIdToTaskCount.contains(executorId)) {
468+
if (executorIdToRunningTaskIds.contains(executorId)) {
472469
val hostPort = executorIdToHost(executorId)
473470
logExecutorLoss(executorId, hostPort, reason)
474471
removeExecutor(executorId, reason)
@@ -510,13 +507,31 @@ private[spark] class TaskSchedulerImpl(
510507
logError(s"Lost executor $executorId on $hostPort: $reason")
511508
}
512509

510+
/**
511+
* Cleans up the TaskScheduler's state for tracking the given task.
512+
*/
513+
private def cleanupTaskState(tid: Long): Unit = {
514+
taskIdToTaskSetManager.remove(tid)
515+
taskIdToExecutorId.remove(tid).foreach { executorId =>
516+
executorIdToRunningTaskIds.get(executorId).foreach { _.remove(tid) }
517+
}
518+
}
519+
513520
/**
514521
* Remove an executor from all our data structures and mark it as lost. If the executor's loss
515522
* reason is not yet known, do not yet remove its association with its host nor update the status
516523
* of any running tasks, since the loss reason defines whether we'll fail those tasks.
517524
*/
518525
private def removeExecutor(executorId: String, reason: ExecutorLossReason) {
519-
executorIdToTaskCount -= executorId
526+
// The tasks on the lost executor may not send any more status updates (because the executor
527+
// has been lost), so they should be cleaned up here.
528+
executorIdToRunningTaskIds.remove(executorId).foreach { taskIds =>
529+
logDebug("Cleaning up TaskScheduler state for tasks " +
530+
s"${taskIds.mkString("[", ",", "]")} on failed executor $executorId")
531+
// We do not notify the TaskSetManager of the task failures because that will
532+
// happen below in the rootPool.executorLost() call.
533+
taskIds.foreach(cleanupTaskState)
534+
}
520535

521536
val host = executorIdToHost(executorId)
522537
val execs = executorsByHost.getOrElse(host, new HashSet)
@@ -554,11 +569,11 @@ private[spark] class TaskSchedulerImpl(
554569
}
555570

556571
def isExecutorAlive(execId: String): Boolean = synchronized {
557-
executorIdToTaskCount.contains(execId)
572+
executorIdToRunningTaskIds.contains(execId)
558573
}
559574

560575
def isExecutorBusy(execId: String): Boolean = synchronized {
561-
executorIdToTaskCount.getOrElse(execId, -1) > 0
576+
executorIdToRunningTaskIds.get(execId).exists(_.nonEmpty)
562577
}
563578

564579
// By default, rack is unknown

core/src/test/scala/org/apache/spark/deploy/StandaloneDynamicAllocationSuite.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,11 @@ class StandaloneDynamicAllocationSuite
425425
assert(executors.size === 2)
426426

427427
// simulate running a task on the executor
428-
val getMap = PrivateMethod[mutable.HashMap[String, Int]]('executorIdToTaskCount)
428+
val getMap =
429+
PrivateMethod[mutable.HashMap[String, mutable.HashSet[Long]]]('executorIdToRunningTaskIds)
429430
val taskScheduler = sc.taskScheduler.asInstanceOf[TaskSchedulerImpl]
430-
val executorIdToTaskCount = taskScheduler invokePrivate getMap()
431-
executorIdToTaskCount(executors.head) = 1
431+
val executorIdToRunningTaskIds = taskScheduler invokePrivate getMap()
432+
executorIdToRunningTaskIds(executors.head) = mutable.HashSet(1L)
432433
// kill the busy executor without force; this should fail
433434
assert(killExecutor(sc, executors.head, force = false))
434435
apps = getApplications()

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.spark.scheduler
1919

20+
import java.nio.ByteBuffer
21+
2022
import org.apache.spark._
2123

2224
class FakeSchedulerBackend extends SchedulerBackend {
@@ -273,4 +275,68 @@ class TaskSchedulerImplSuite extends SparkFunSuite with LocalSparkContext with L
273275
assert("executor1" === taskDescriptions3(0).executorId)
274276
}
275277

278+
test("if an executor is lost then the state for its running tasks is cleaned up (SPARK-18553)") {
279+
sc = new SparkContext("local", "TaskSchedulerImplSuite")
280+
val taskScheduler = new TaskSchedulerImpl(sc)
281+
taskScheduler.initialize(new FakeSchedulerBackend)
282+
// Need to initialize a DAGScheduler for the taskScheduler to use for callbacks.
283+
new DAGScheduler(sc, taskScheduler) {
284+
override def taskStarted(task: Task[_], taskInfo: TaskInfo) {}
285+
override def executorAdded(execId: String, host: String) {}
286+
}
287+
288+
val e0Offers = Seq(WorkerOffer("executor0", "host0", 1))
289+
val attempt1 = FakeTask.createTaskSet(1)
290+
291+
// submit attempt 1, offer resources, task gets scheduled
292+
taskScheduler.submitTasks(attempt1)
293+
val taskDescriptions = taskScheduler.resourceOffers(e0Offers).flatten
294+
assert(1 === taskDescriptions.length)
295+
296+
// mark executor0 as dead
297+
taskScheduler.executorLost("executor0", SlaveLost())
298+
assert(!taskScheduler.isExecutorAlive("executor0"))
299+
assert(!taskScheduler.hasExecutorsAliveOnHost("host0"))
300+
assert(taskScheduler.getExecutorsAliveOnHost("host0").isEmpty)
301+
302+
303+
// Check that state associated with the lost task attempt is cleaned up:
304+
assert(taskScheduler.taskIdToExecutorId.isEmpty)
305+
assert(taskScheduler.taskIdToTaskSetManager.isEmpty)
306+
}
307+
308+
test("if a task finishes with TaskState.LOST its executor is marked as dead") {
309+
sc = new SparkContext("local", "TaskSchedulerImplSuite")
310+
val taskScheduler = new TaskSchedulerImpl(sc)
311+
taskScheduler.initialize(new FakeSchedulerBackend)
312+
// Need to initialize a DAGScheduler for the taskScheduler to use for callbacks.
313+
new DAGScheduler(sc, taskScheduler) {
314+
override def taskStarted(task: Task[_], taskInfo: TaskInfo) {}
315+
override def executorAdded(execId: String, host: String) {}
316+
}
317+
318+
val e0Offers = Seq(WorkerOffer("executor0", "host0", 1))
319+
val attempt1 = FakeTask.createTaskSet(1)
320+
321+
// submit attempt 1, offer resources, task gets scheduled
322+
taskScheduler.submitTasks(attempt1)
323+
val taskDescriptions = taskScheduler.resourceOffers(e0Offers).flatten
324+
assert(1 === taskDescriptions.length)
325+
326+
// Report the task as failed with TaskState.LOST
327+
taskScheduler.statusUpdate(
328+
tid = taskDescriptions.head.taskId,
329+
state = TaskState.LOST,
330+
serializedData = ByteBuffer.allocate(0)
331+
)
332+
333+
// Check that state associated with the lost task attempt is cleaned up:
334+
assert(taskScheduler.taskIdToExecutorId.isEmpty)
335+
assert(taskScheduler.taskIdToTaskSetManager.isEmpty)
336+
337+
// Check that the executor has been marked as dead
338+
assert(!taskScheduler.isExecutorAlive("executor0"))
339+
assert(!taskScheduler.hasExecutorsAliveOnHost("host0"))
340+
assert(taskScheduler.getExecutorsAliveOnHost("host0").isEmpty)
341+
}
276342
}

0 commit comments

Comments
 (0)