Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14079#discussion_r70728183
  
    --- Diff: 
core/src/test/scala/org/apache/spark/scheduler/BlacklistTrackerSuite.scala ---
    @@ -0,0 +1,282 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.scheduler
    +
    +import org.mockito.Mockito.when
    +import org.scalatest.BeforeAndAfterEach
    +import org.scalatest.mock.MockitoSugar
    +
    +import org.apache.spark._
    +import org.apache.spark.internal.config.{BLACKLIST_EXPIRY_TIMEOUT_CONF, 
BLACKLIST_LEGACY_TIMEOUT_CONF}
    +import org.apache.spark.util.ManualClock
    +
    +class BlacklistTrackerSuite extends SparkFunSuite with BeforeAndAfterEach 
with MockitoSugar
    +    with LocalSparkContext {
    +
    +  val stage1 = 1
    +  val stage2 = 2
    +
    +  val partition1 = 1
    +  val partition2 = 2
    +  val partition3 = 3
    +
    +  // Variable name can indicate basic information of taskInfo
    +  // hostA: executor 1, 2, 4
    +  // hostB: executor 3
    +  // The format is "taskInfo_executorId_hostName"
    +  val taskInfo_1_hostA = new TaskInfo(1L, 1, 1, 0L, "1", "hostA", 
TaskLocality.ANY, false)
    +  val taskInfo_2_hostA = new TaskInfo(2L, 1, 1, 0L, "2", "hostA", 
TaskLocality.ANY, false)
    +  val taskInfo_3_hostB = new TaskInfo(3L, 3, 1, 0L, "3", "hostB", 
TaskLocality.ANY, false)
    +
    +  val clock = new ManualClock(0)
    +
    +  var blacklistTracker: BlacklistTracker = _
    +
    +  override def afterEach(): Unit = {
    +    if (blacklistTracker != null) {
    +      blacklistTracker.stop()
    +      blacklistTracker = null
    +    }
    +    super.afterEach()
    +  }
    +
    +  val allOptions = (('A' to 'Z').map("host" + _) ++ (1 to 
100).map{_.toString}).toSet
    +
    +  /**
    +   * Its easier to write our tests as if we could directly look at the 
sets of nodes & executors in
    +   * the blacklist.  However the api doesn't expose a set (for 
thread-safety), so this is a simple
    +   * way to test something similar, since we know the universe of values 
that might appear in these
    +   * sets.
    +   */
    +  def assertEquivalentToSet(f: String => Boolean, expected: Set[String]): 
Unit = {
    +    allOptions.foreach { opt =>
    +      val actual = f(opt)
    +      val exp = expected.contains(opt)
    +      assert(actual === exp, raw"""for string "$opt" """)
    +    }
    +  }
    +
    +  def mockTaskSchedWithConf(conf: SparkConf): TaskSchedulerImpl = {
    +    sc = new SparkContext(conf)
    +    val scheduler = mock[TaskSchedulerImpl]
    +    when(scheduler.sc).thenReturn(sc)
    +    
when(scheduler.mapOutputTracker).thenReturn(SparkEnv.get.mapOutputTracker)
    +    scheduler
    +  }
    +
    +  test("Blacklisting individual tasks") {
    +    val conf = new SparkConf().setAppName("test").setMaster("local")
    +      .set("spark.ui.enabled", "false")
    +      .set("spark.scheduler.executorTaskBlacklistTime", "1000")
    +    val scheduler = mockTaskSchedWithConf(conf)
    +    
when(scheduler.getExecutorsAliveOnHost("hostA")).thenReturn(Some(Set("1", "2", 
"4")))
    +    Set("1", "2", "4").foreach { execId =>
    +      when(scheduler.getHostForExecutor(execId)).thenReturn("hostA")
    +    }
    +
    +    // Task 1 failed on executor 1
    +    blacklistTracker = new BlacklistTracker(conf, clock)
    +    val taskSet = FakeTask.createTaskSet(10)
    +    val tsm = new TaskSetManager(scheduler, Some(blacklistTracker), 
taskSet, 4, clock)
    +    tsm.updateBlacklistForFailedTask("hostA", "1", 0)
    +    for {
    +      executor <- (1 to 4).map(_.toString)
    +      index <- 0 until 10
    +    } {
    +      val exp = (executor == "1"  && index == 0)
    +      assert(tsm.isExecutorBlacklistedForTask(executor, index) === exp)
    +    }
    +    assert(blacklistTracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(blacklistTracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(tsm.isNodeBlacklistedForTaskSet, Set())
    +    assertEquivalentToSet(tsm.isExecutorBlacklistedForTaskSet, Set())
    +
    +    // Task 1 & 2 failed on both executor 1 & 2, so we blacklist all 
executors on that host,
    +    // for all tasks for the stage.  Note the api expects multiple checks 
for each type of
    +    // blacklist -- this actually fits naturally with its use in the 
scheduler
    +    tsm.updateBlacklistForFailedTask("hostA", "1", 1)
    +    tsm.updateBlacklistForFailedTask("hostA", "2", 0)
    +    tsm.updateBlacklistForFailedTask("hostA", "2", 1)
    +    // we don't explicitly return the executors in hostA here, but that is 
OK
    +    for {
    +      executor <- (1 to 4).map(_.toString)
    +      index <- 0 until 10
    +    } {
    +      withClue(s"exec = $executor; index = $index") {
    +        val badExec = (executor == "1" || executor == "2")
    +        val badPart = (index == 0 || index == 1)
    +        val taskExp = (badExec && badPart)
    +        assert(
    +          tsm.isExecutorBlacklistedForTask(executor, index) === taskExp)
    +        val executorExp = badExec
    +        assert(tsm.isExecutorBlacklistedForTaskSet(executor) === 
executorExp)
    +      }
    +    }
    +    assertEquivalentToSet(tsm.isNodeBlacklistedForTaskSet, Set("hostA"))
    +    // we dont' blacklist the nodes or executors till the stages complete
    +    assert(blacklistTracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(blacklistTracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(blacklistTracker.isExecutorBlacklisted(_), Set())
    +
    +    // when the stage completes successfully, now there is sufficient 
evidence we've got
    +    // bad executors and node
    +    blacklistTracker.taskSetSucceeded(tsm.execToFailures, scheduler)
    +    assert(blacklistTracker.nodeBlacklist() === Set("hostA"))
    +    assertEquivalentToSet(blacklistTracker.isNodeBlacklisted(_), 
Set("hostA"))
    +    assertEquivalentToSet(blacklistTracker.isExecutorBlacklisted(_), 
Set("1", "2"))
    +
    +    clock.advance(blacklistTracker.EXECUTOR_RECOVERY_MILLIS + 1)
    +    blacklistTracker.expireExecutorsInBlacklist()
    +    assert(blacklistTracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(blacklistTracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(blacklistTracker.isExecutorBlacklisted(_), Set())
    +  }
    +
    +  def trackerFixture: (BlacklistTracker, TaskSchedulerImpl) = {
    +     val conf = new SparkConf().setAppName("test").setMaster("local")
    +      .set("spark.ui.enabled", "false")
    +      .set("spark.scheduler.executorTaskBlacklistTime", "1000")
    +    val scheduler = mockTaskSchedWithConf(conf)
    +    
when(scheduler.getExecutorsAliveOnHost("hostA")).thenReturn(Some(Set("1", "2", 
"4")))
    +    Set("1", "2", "4").foreach { execId =>
    +      when(scheduler.getHostForExecutor(execId)).thenReturn("hostA")
    +    }
    +
    +    clock.setTime(0)
    +    blacklistTracker = new BlacklistTracker(conf, clock)
    +    (blacklistTracker, scheduler)
    +  }
    +
    +  test("executors can be blacklisted with only a few failures per stage") {
    +    val (tracker, scheduler) = trackerFixture
    +    // for 4 different stages, executor 1 fails a task, then executor 2 
succeeds the task,
    +    // and then the task set is done.  Not enough failures to blacklist 
the executor *within*
    +    // any particular taskset, but we still blacklist the executor overall 
eventually
    +    (0 until 4).foreach { stage =>
    +      val taskSet = FakeTask.createTaskSet(1)
    +      val tsm = new TaskSetManager(scheduler, Some(tracker), taskSet, 4, 
clock)
    +      tsm.updateBlacklistForFailedTask("hostA", "1", 0)
    +      tracker.taskSetSucceeded(tsm.execToFailures, scheduler)
    +    }
    +    assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set("1"))
    +  }
    +
    +  // if an executor has many task failures, but the task set ends up 
failing, don't count it
    +  // against the executor
    +  test("executors aren't blacklisted if task sets fail") {
    +    val (tracker, scheduler) = trackerFixture
    +    // for 4 different stages, executor 1 fails a task, and then the 
taskSet fails.
    +    (0 until 4).foreach { stage =>
    +      val taskSet = FakeTask.createTaskSet(1)
    +      val tsm = new TaskSetManager(scheduler, Some(tracker), taskSet, 4, 
clock)
    +      tsm.updateBlacklistForFailedTask("hostA", "1", 0)
    +      tracker.taskSetFailed(stage)
    +    }
    +    assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set())
    +  }
    +
    +  Seq(true, false).foreach { succeedTaskSet =>
    +    test(s"stage blacklist updates correctly on stage completion 
($succeedTaskSet)") {
    +      // within one taskset, an executor fails a few times, so its 
blacklisted for the taskset.
    +      // but if the taskset fails, we don't blacklist the executor after 
the stage.
    +      val (tracker, scheduler) = trackerFixture
    +      val stageId = 1 + (if (succeedTaskSet) 1 else 0)
    +      val taskSet = FakeTask.createTaskSet(4, stageId, 0)
    +      val tsm = new TaskSetManager(scheduler, Some(tracker), taskSet, 4, 
clock)
    +      (0 until 4).foreach { partition =>
    +        tsm.updateBlacklistForFailedTask("hostA", "1", partition)
    +      }
    +      assert(tsm.isExecutorBlacklistedForTaskSet("1"))
    +      assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set())
    +      if (succeedTaskSet) {
    +        // the task set succeeded elsewhere, so we count those failures 
against our executor,
    +        // and blacklist it across stages
    +        tracker.taskSetSucceeded(tsm.execToFailures, scheduler)
    +        assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set("1"))
    +      } else {
    +        // the task set failed, so we don't count these failures against 
the executor for other
    +        // stages
    +        tracker.taskSetFailed(stageId)
    +        assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set())
    +      }
    +    }
    +  }
    +
    +  test("blacklisted executors and nodes get recovered with time") {
    +    val (tracker, scheduler) = trackerFixture
    +    val taskSet0 = FakeTask.createTaskSet(4)
    +    val tsm0 = new TaskSetManager(scheduler, Some(tracker), taskSet0, 4, 
clock)
    +    (0 until 4).foreach { partition =>
    +      tsm0.updateBlacklistForFailedTask("hostA", "1", partition)
    +    }
    +    tracker.taskSetSucceeded(tsm0.execToFailures, scheduler)
    +    assert(tracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(tracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set("1"))
    +
    +    val taskSet1 = FakeTask.createTaskSet(4, 1, 0)
    +    val tsm1 = new TaskSetManager(scheduler, Some(tracker), taskSet1, 4, 
clock)
    +    (0 until 4).foreach { partition =>
    +      tsm1.updateBlacklistForFailedTask("hostA", "2", partition)
    +    }
    +    tracker.taskSetSucceeded(tsm1.execToFailures, scheduler)
    +    assert(tracker.nodeBlacklist() === Set("hostA"))
    +    assertEquivalentToSet(tracker.isNodeBlacklisted(_), Set("hostA"))
    +    assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set("1", "2"))
    +
    +    clock.advance(tracker.EXECUTOR_RECOVERY_MILLIS + 1)
    +    tracker.expireExecutorsInBlacklist()
    +    assert(tracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(tracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set())
    +
    +    // fail one more task, but executor isn't put back into blacklist 
since count reset to 0
    +    val taskSet2 = FakeTask.createTaskSet(4, 2, 0)
    +    val tsm2 = new TaskSetManager(scheduler, Some(tracker), taskSet2, 4, 
clock)
    +    tsm2.updateBlacklistForFailedTask("hostA", "1", 0)
    +    tracker.taskSetSucceeded(tsm2.execToFailures, scheduler)
    +    assert(tracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(tracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(tracker.isExecutorBlacklisted(_), Set())
    +  }
    +
    +  test("blacklist still respects legacy configs") {
    +    val legacyKey = BLACKLIST_LEGACY_TIMEOUT_CONF.key
    +
    +    {
    +      val localConf = new SparkConf().setMaster("local")
    +      assert(!BlacklistTracker.isBlacklistEnabled(localConf))
    +      localConf.set(legacyKey, "5000")
    +      assert(BlacklistTracker.isBlacklistEnabled(localConf))
    +      assert(5000 == BlacklistTracker.getBlacklistExpiryTime(localConf))
    --- End diff --
    
    `===` (also double check other asserts, just in case)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to