Skip to content

Commit c51c772

Browse files
JoshRosenandrewor14
authored andcommitted
[SPARK-18640] Add synchronization to TaskScheduler.runningTasksByExecutors
## What changes were proposed in this pull request? The method `TaskSchedulerImpl.runningTasksByExecutors()` accesses the mutable `executorIdToRunningTaskIds` map without proper synchronization. In addition, as markhamstra pointed out in #15986, the signature's use of parentheses is a little odd given that this is a pure getter method. This patch fixes both issues. ## How was this patch tested? Covered by existing tests. Author: Josh Rosen <[email protected]> Closes #16073 from JoshRosen/runningTasksByExecutors-thread-safety.
1 parent bc95ea0 commit c51c772

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

core/src/main/scala/org/apache/spark/SparkStatusTracker.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class SparkStatusTracker private[spark] (sc: SparkContext) {
112112
*/
113113
def getExecutorInfos: Array[SparkExecutorInfo] = {
114114
val executorIdToRunningTasks: Map[String, Int] =
115-
sc.taskScheduler.asInstanceOf[TaskSchedulerImpl].runningTasksByExecutors()
115+
sc.taskScheduler.asInstanceOf[TaskSchedulerImpl].runningTasksByExecutors
116116

117117
sc.getExecutorStorageStatus.map { status =>
118118
val bmId = status.blockManagerId

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private[spark] class TaskSchedulerImpl(
9696
// IDs of the tasks running on each executor
9797
private val executorIdToRunningTaskIds = new HashMap[String, HashSet[Long]]
9898

99-
def runningTasksByExecutors(): Map[String, Int] = {
99+
def runningTasksByExecutors: Map[String, Int] = synchronized {
100100
executorIdToRunningTaskIds.toMap.mapValues(_.size)
101101
}
102102

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ class TaskSchedulerImplSuite extends SparkFunSuite with LocalSparkContext with B
678678
// Check that state associated with the lost task attempt is cleaned up:
679679
assert(taskScheduler.taskIdToExecutorId.isEmpty)
680680
assert(taskScheduler.taskIdToTaskSetManager.isEmpty)
681-
assert(taskScheduler.runningTasksByExecutors().get("executor0").isEmpty)
681+
assert(taskScheduler.runningTasksByExecutors.get("executor0").isEmpty)
682682
}
683683

684684
test("if a task finishes with TaskState.LOST its executor is marked as dead") {
@@ -709,7 +709,7 @@ class TaskSchedulerImplSuite extends SparkFunSuite with LocalSparkContext with B
709709
// Check that state associated with the lost task attempt is cleaned up:
710710
assert(taskScheduler.taskIdToExecutorId.isEmpty)
711711
assert(taskScheduler.taskIdToTaskSetManager.isEmpty)
712-
assert(taskScheduler.runningTasksByExecutors().get("executor0").isEmpty)
712+
assert(taskScheduler.runningTasksByExecutors.get("executor0").isEmpty)
713713

714714
// Check that the executor has been marked as dead
715715
assert(!taskScheduler.isExecutorAlive("executor0"))

0 commit comments

Comments
 (0)