vanzin commented on a change in pull request #24704: [SPARK-20286][core] Improve logic for timing out executors in dynamic allocation. URL: https://github.com/apache/spark/pull/24704#discussion_r289506925
########## File path: core/src/test/scala/org/apache/spark/scheduler/dynalloc/ExecutorMonitorSuite.scala ########## @@ -0,0 +1,253 @@ +/* + * 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.dynalloc + +import java.util.concurrent.TimeUnit + +import scala.collection.mutable + +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.{mock, when} + +import org.apache.spark._ +import org.apache.spark.internal.config._ +import org.apache.spark.scheduler._ +import org.apache.spark.storage._ +import org.apache.spark.util.ManualClock + +class ExecutorMonitorSuite extends SparkFunSuite { + + private val idleTimeoutMs = TimeUnit.SECONDS.toMillis(60L) + private val storageTimeoutMs = TimeUnit.SECONDS.toMillis(120L) + + private val conf = new SparkConf() + .set(DYN_ALLOCATION_EXECUTOR_IDLE_TIMEOUT.key, "60s") + .set(DYN_ALLOCATION_CACHED_EXECUTOR_IDLE_TIMEOUT.key, "120s") + + private var monitor: ExecutorMonitor = _ + private var client: ExecutorAllocationClient = _ + private var clock: ManualClock = _ + + // List of known executors. Allows easily mocking which executors are alive without + // having to use mockito APIs directly in each test. + private val knownExecs = mutable.HashSet[String]() + + override def beforeEach(): Unit = { + super.beforeEach() + knownExecs.clear() + clock = new ManualClock() + client = mock(classOf[ExecutorAllocationClient]) + when(client.isExecutorActive(any())).thenAnswer { invocation => + knownExecs.contains(invocation.getArguments()(0).asInstanceOf[String]) + } + monitor = new ExecutorMonitor(conf, client, clock) + } + + test("basic executor timeout") { + knownExecs += "1" + monitor.onExecutorAdded(SparkListenerExecutorAdded(clock.getTimeMillis(), "1", null)) + assert(monitor.executorCount === 1) + assert(monitor.isExecutorIdle("1")) + assert(monitor.timedOutExecutors(idleDeadline) === Seq("1")) + } + + test("SPARK-4951, SPARK-26927: handle out of order task start events") { + knownExecs ++= Set("1", "2") + + monitor.onTaskStart(SparkListenerTaskStart(1, 1, taskInfo("1", 1))) + assert(monitor.executorCount === 1) + + monitor.onExecutorAdded(SparkListenerExecutorAdded(clock.getTimeMillis(), "1", null)) + assert(monitor.executorCount === 1) + + monitor.onExecutorAdded(SparkListenerExecutorAdded(clock.getTimeMillis(), "2", null)) + assert(monitor.executorCount === 2) + + monitor.onExecutorRemoved(SparkListenerExecutorRemoved(clock.getTimeMillis(), "2", null)) + assert(monitor.executorCount === 1) + + knownExecs -= "2" + + monitor.onTaskStart(SparkListenerTaskStart(1, 1, taskInfo("2", 2))) + assert(monitor.executorCount === 1) + } + + test("track tasks running on executor") { + knownExecs += "1" + + monitor.onExecutorAdded(SparkListenerExecutorAdded(clock.getTimeMillis(), "1", null)) + monitor.onTaskStart(SparkListenerTaskStart(1, 1, taskInfo("1", 1))) + assert(!monitor.isExecutorIdle("1")) + + // Start/end a few tasks and make sure the executor does not go idle. + (2 to 10).foreach { i => + monitor.onTaskStart(SparkListenerTaskStart(i, 1, taskInfo("1", 1))) + assert(!monitor.isExecutorIdle("1")) + + monitor.onTaskEnd(SparkListenerTaskEnd(i, 1, "foo", Success, taskInfo("1", 1), null)) + assert(!monitor.isExecutorIdle("1")) + } + + monitor.onTaskEnd(SparkListenerTaskEnd(1, 1, "foo", Success, taskInfo("1", 1), null)) + assert(monitor.isExecutorIdle("1")) + assert(monitor.timedOutExecutors(clock.getTimeMillis()).isEmpty) + assert(monitor.timedOutExecutors(clock.getTimeMillis() + idleTimeoutMs + 1) === Seq("1")) + } + + test("use appropriate time out depending on whether blocks are stored") { + knownExecs += "1" + monitor.onExecutorAdded(SparkListenerExecutorAdded(clock.getTimeMillis(), "1", null)) + assert(monitor.isExecutorIdle("1")) + assert(monitor.timedOutExecutors(idleDeadline) === Seq("1")) + + monitor.onBlockUpdated(rddUpdate(1, 0, "1")) + assert(monitor.isExecutorIdle("1")) + assert(monitor.timedOutExecutors(idleDeadline).isEmpty) + assert(monitor.timedOutExecutors(storageDeadline) === Seq("1")) + + monitor.onBlockUpdated(rddUpdate(1, 0, "1", level = StorageLevel.NONE)) + assert(monitor.isExecutorIdle("1")) + assert(monitor.timedOutExecutors(idleDeadline) === Seq("1")) + + monitor.onTaskStart(SparkListenerTaskStart(1, 1, taskInfo("1", 1))) + assert(!monitor.isExecutorIdle("1")) + monitor.onBlockUpdated(rddUpdate(1, 0, "1")) + assert(!monitor.isExecutorIdle("1")) + monitor.onBlockUpdated(rddUpdate(1, 0, "1", level = StorageLevel.NONE)) + assert(!monitor.isExecutorIdle("1")) + } + + test("keeps track of stored blocks for each rdd and split") { + monitor.onExecutorAdded(SparkListenerExecutorAdded(clock.getTimeMillis(), "1", null)) + + monitor.onBlockUpdated(rddUpdate(1, 0, "1")) + assert(monitor.timedOutExecutors(idleDeadline).isEmpty) + assert(monitor.timedOutExecutors(storageDeadline) === Seq("1")) + + monitor.onBlockUpdated(rddUpdate(1, 1, "1")) + assert(monitor.timedOutExecutors(idleDeadline).isEmpty) + assert(monitor.timedOutExecutors(storageDeadline) === Seq("1")) + + monitor.onBlockUpdated(rddUpdate(2, 0, "1")) + assert(monitor.timedOutExecutors(idleDeadline).isEmpty) + assert(monitor.timedOutExecutors(storageDeadline) === Seq("1")) + + monitor.onBlockUpdated(rddUpdate(1, 1, "1", level = StorageLevel.NONE)) + assert(monitor.timedOutExecutors(idleDeadline).isEmpty) + assert(monitor.timedOutExecutors(storageDeadline) === Seq("1")) + + monitor.onUnpersistRDD(SparkListenerUnpersistRDD(1)) + assert(monitor.timedOutExecutors(idleDeadline).isEmpty) + assert(monitor.timedOutExecutors(storageDeadline) === Seq("1")) + + monitor.onUnpersistRDD(SparkListenerUnpersistRDD(2)) Review comment: Actually the `clock.setTime()` is not needed, because the `timedOutExecutors` call here is a test-specific one that takes the time as a parameter. But I'll add the comment. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
