Bowei Xu created SPARK-62336:
--------------------------------

             Summary: TaskSetManager does not clear a task's pending 
speculation when the task succeeds
                 Key: SPARK-62336
                 URL: https://issues.apache.org/jira/browse/SPARK-62336
             Project: Spark
          Issue Type: Bug
          Components: Scheduler, Spark Core
    Affects Versions: 4.3.0, 3.5.9, 5.0.0
            Reporter: Bowei Xu


[TaskSetManager.speculatableTasks|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L205]
 holds the task indices that have a requested speculative copy that has not 
launched yet. The copy itself is filed in the pending speculative lists, which 
are [cleaned up 
lazily|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L332-L363].
 An index leaves {{speculatableTasks}} only when its speculative copy is 
[dequeued|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L405],
 and 
[checkAndSubmitSpeculatableTasks|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L1319]
 never speculates an index that is still in it.

Neither 
[handleSuccessfulTask|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L888-L899]
 nor 
[markPartitionCompleted|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L984-L996]
 clears this state when the task succeeds. So if a task is marked speculatable 
and its regular attempt succeeds before the speculative copy launches, its 
index stays in {{speculatableTasks}} and its entry stays in the pending lists.

This becomes a problem once the task is 
[resubmitted|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L1274-L1281],
 for example because the executor holding its shuffle output is lost (no 
external shuffle service, or decommission without migration). The following 
sequence is possible:
# Tasks 0 and 1 of a shuffle map stage are running, and both are marked 
speculatable.
# Task 0's regular attempt succeeds before its speculative copy launches.
# The executor that ran task 0 is lost, so task 0 is resubmitted and its rerun 
starts.
# Then one of two things happens:
## If task 0's stale pending entry is still in a list, the next offer launches 
a speculative copy of the rerun right away, because the dequeue [only checks 
that the task has not succeeded and has one running 
copy|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L353-L358].
 Nobody requested a copy for the rerun.
## If the stale entry was already dropped, the rerun is never speculated, 
however slow it is, because its index is still in {{speculatableTasks}}.

Notes:
* Before SPARK-26755 (3.0.0), {{dequeueSpeculativeTask}} started with [a retain 
call that dropped succeeded 
tasks|https://github.com/apache/spark/blob/2656c9d304b59584c331b923e8536e4093d83f81/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L342]
 from {{speculatableTasks}}. SPARK-26755 replaced it with removal on dequeue 
only, so this has been possible since 3.0.0. The logic is unchanged in 3.5.9, 
4.0.5, 4.1.4, 4.2.1, 4.3.0 and master.
* SPARK-41192 already drops the pending speculative task in 
{{ExecutorAllocationManager}} when the task finishes, but {{TaskSetManager}} 
keeps it.
* Once {{speculatableTasks}} is empty, speculative dequeue [returns 
early|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L398-L400]
 and never scans the pending lists, yet 
[isSchedulable|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L1206-L1207]
 and the [delay-scheduling reject 
check|https://github.com/apache/spark/blob/0bcfe02d3cad22a9fd4c22a48cf0ae346498dffe/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala#L526]
 still count their entries. A fix that clears {{speculatableTasks}} on success 
has to account for that.

----
h2. Repro

The two tests below can be pasted into {{TaskSetManagerSuite}}. Both fail on 
master (0bcfe02d3ca):
* REPRO 1 fails with {{0 did not equal 1}}: the offer launches a speculative 
copy of task 0's rerun instead of the copy requested for task 1.
* REPRO 2 fails with {{manager.checkSpeculatableTasks(0L) was false}}: the slow 
rerun is never speculated.

{code:scala}
  private def staleSpeculationSetup(): (TaskSetManager, ManualClock, Int, Int) 
= {
    val conf = new SparkConf().set(config.SPECULATION_ENABLED, true)
      .set(config.SPECULATION_MULTIPLIER, 0.0).set(config.SPECULATION_QUANTILE, 
0.5)
    sc = new SparkContext("local", "test", conf)
    sched = new FakeTaskScheduler(sc, (1 to 5).map(i => (s"exec$i", 
s"host$i")): _*)
    val clock = new ManualClock()
    val manager = new TaskSetManager(sched,
      FakeTask.createShuffleMapTaskSet(2, 0, 0, Seq(), Seq()), 
MAX_TASK_FAILURES, clock = clock)
    val task0 = manager.resourceOffer("exec1", "host1", ANY)._1.get
    val task1 = manager.resourceOffer("exec2", "host2", ANY)._1.get
    // Both tasks are speculatable; task 0's regular attempt succeeds before 
its copy launches.
    Seq(task1, task0).foreach { t =>
      manager.speculatableTasks += t.index
      manager.addPendingTask(t.index, speculatable = true)
    }
    clock.advance(1)
    manager.handleSuccessfulTask(task0.taskId, createTaskResult(task0.index))
    (manager, clock, task0.index, task1.index)
  }

  test("REPRO 1: no speculative copy of the rerun unless one is requested") {
    val (manager, _, task0, task1) = staleSpeculationSetup()
    manager.executorLost("exec1", "host1", ExecutorProcessLost())
    assert(manager.resourceOffer("exec3", "host3", ANY)._1.get.index === task0) 
// the rerun
    // The next offer should launch the copy requested for task 1.
    assert(manager.resourceOffer("exec4", "host4", ANY)._1.get.index === task1)
  }

  test("REPRO 2: a slow rerun can be speculated") {
    val (manager, clock, _, _) = staleSpeculationSetup()
    manager.resourceOffer("exec3", "host3", ANY) // launches task 1's requested 
copy
    manager.resourceOffer("exec4", "host4", ANY) // drops task 0's stale 
pending entry
    manager.executorLost("exec1", "host1", ExecutorProcessLost())
    manager.resourceOffer("exec5", "host5", ANY) // the rerun of task 0
    clock.advance(100000)
    assert(manager.checkSpeculatableTasks(0))
  }
{code}

h2. Proposed fix

Remove the index from {{speculatableTasks}} when the task succeeds, in both 
{{handleSuccessfulTask}} and {{markPartitionCompleted}}. Only launch a 
speculative copy while its index is still in {{speculatableTasks}}, and use 
{{speculatableTasks}} instead of the pending lists to decide whether 
speculative work is pending.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to