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

    https://github.com/apache/spark/pull/14079#discussion_r77297069
  
    --- Diff: 
core/src/test/scala/org/apache/spark/scheduler/BlacklistTrackerSuite.scala ---
    @@ -0,0 +1,503 @@
    +/*
    + * 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
    +import org.apache.spark.util.ManualClock
    +
    +class BlacklistTrackerSuite extends SparkFunSuite with BeforeAndAfterEach 
with MockitoSugar
    +    with LocalSparkContext {
    +
    +  private val clock = new ManualClock(0)
    +
    +  private var blacklistTracker: BlacklistTracker = _
    +
    +  override def afterEach(): Unit = {
    +    if (blacklistTracker != null) {
    +      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(config.BLACKLIST_ENABLED.key, "true")
    +    val scheduler = mockTaskSchedWithConf(conf)
    +    // 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.updateBlacklistForSuccessfulTaskSet(0, 0, 
tsm.execToFailures)
    +    assert(blacklistTracker.nodeBlacklist() === Set("hostA"))
    +    assertEquivalentToSet(blacklistTracker.isNodeBlacklisted(_), 
Set("hostA"))
    +    assertEquivalentToSet(blacklistTracker.isExecutorBlacklisted(_), 
Set("1", "2"))
    +
    +    clock.advance(blacklistTracker.BLACKLIST_TIMEOUT_MILLIS + 1)
    +    blacklistTracker.applyBlacklistTimeout()
    +    assert(blacklistTracker.nodeBlacklist() === Set())
    +    assertEquivalentToSet(blacklistTracker.isNodeBlacklisted(_), Set())
    +    assertEquivalentToSet(blacklistTracker.isExecutorBlacklisted(_), Set())
    +  }
    +
    +  def trackerFixture: (BlacklistTracker, TaskSchedulerImpl) = {
    +    trackerFixture()
    +  }
    +
    +  def trackerFixture(confs: (String, String)*): (BlacklistTracker, 
TaskSchedulerImpl) = {
    +    val conf = new SparkConf().setAppName("test").setMaster("local")
    +      .set(config.BLACKLIST_ENABLED.key, "true")
    +    confs.foreach { case (k, v) => conf.set(k, v) }
    +    val scheduler = mockTaskSchedWithConf(conf)
    +
    +    clock.setTime(0)
    +    blacklistTracker = new BlacklistTracker(conf, clock)
    +    (blacklistTracker, scheduler)
    --- End diff --
    
    would it make sense to have the scheduler also be a class variable, and 
then this function could be something like configureTrackerAndScheduler(), and 
each test could use the class variables directly (rather than creating local 
variables that they use)? The local variables seem prone to errors in future 
PRs, where someone doesn't realize that the blacklist tracker is / isn't 
getting nulled at the end properly by afterEach()


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to