-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-6650] [core] Stop ExecutorAllocationManager when context stops. #5311
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,12 @@ | |
|
||
package org.apache.spark | ||
|
||
import java.util.concurrent.{TimeUnit, Executors} | ||
|
||
import scala.collection.mutable | ||
|
||
import org.apache.spark.scheduler._ | ||
import org.apache.spark.util.{SystemClock, Clock} | ||
import org.apache.spark.util.{Clock, SystemClock, Utils} | ||
|
||
/** | ||
* An agent that dynamically allocates and removes executors based on the workload. | ||
|
@@ -129,6 +131,10 @@ private[spark] class ExecutorAllocationManager( | |
// Listener for Spark events that impact the allocation policy | ||
private val listener = new ExecutorAllocationListener | ||
|
||
// Executor that handles the scheduling task. | ||
private val executor = Executors.newSingleThreadScheduledExecutor( | ||
Utils.namedThreadFactory("spark-dynamic-executor-allocation")) | ||
|
||
/** | ||
* Verify that the settings specified through the config are valid. | ||
* If not, throw an appropriate exception. | ||
|
@@ -173,32 +179,24 @@ private[spark] class ExecutorAllocationManager( | |
} | ||
|
||
/** | ||
* Register for scheduler callbacks to decide when to add and remove executors. | ||
* Register for scheduler callbacks to decide when to add and remove executors, and start | ||
* the scheduling task. | ||
*/ | ||
def start(): Unit = { | ||
listenerBus.addListener(listener) | ||
startPolling() | ||
|
||
val scheduleTask = new Runnable() { | ||
override def run(): Unit = Utils.logUncaughtExceptions(schedule()) | ||
} | ||
executor.scheduleAtFixedRate(scheduleTask, 0, intervalMillis, TimeUnit.MILLISECONDS) | ||
} | ||
|
||
/** | ||
* Start the main polling thread that keeps track of when to add and remove executors. | ||
* Stop the allocation manager. | ||
*/ | ||
private def startPolling(): Unit = { | ||
val t = new Thread { | ||
override def run(): Unit = { | ||
while (true) { | ||
try { | ||
schedule() | ||
} catch { | ||
case e: Exception => logError("Exception in dynamic executor allocation thread!", e) | ||
} | ||
Thread.sleep(intervalMillis) | ||
} | ||
} | ||
} | ||
t.setName("spark-dynamic-executor-allocation") | ||
t.setDaemon(true) | ||
t.start() | ||
def stop(): Unit = { | ||
executor.shutdown() | ||
executor.awaitTermination(10, TimeUnit.SECONDS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking aloud here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to follow the order of initialization during shutdown (the alloc manager is started after the event logger so should be stopped right before it - yeah, I know my code doesn't do exactly that). But we could move the stop before the scheduler's stop. |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ package org.apache.spark | |
|
||
import scala.collection.mutable | ||
|
||
import org.scalatest.{FunSuite, PrivateMethodTester} | ||
import org.scalatest.{BeforeAndAfter, FunSuite, PrivateMethodTester} | ||
import org.apache.spark.executor.TaskMetrics | ||
import org.apache.spark.scheduler._ | ||
import org.apache.spark.scheduler.cluster.ExecutorInfo | ||
|
@@ -28,10 +28,20 @@ import org.apache.spark.util.ManualClock | |
/** | ||
* Test add and remove behavior of ExecutorAllocationManager. | ||
*/ | ||
class ExecutorAllocationManagerSuite extends FunSuite with LocalSparkContext { | ||
class ExecutorAllocationManagerSuite extends FunSuite with LocalSparkContext with BeforeAndAfter { | ||
import ExecutorAllocationManager._ | ||
import ExecutorAllocationManagerSuite._ | ||
|
||
private val contexts = new mutable.ListBuffer[SparkContext]() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this ever have more than a single element? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. See |
||
|
||
before { | ||
contexts.clear() | ||
} | ||
|
||
after { | ||
contexts.foreach(_.stop()) | ||
} | ||
|
||
test("verify min/max executors") { | ||
val conf = new SparkConf() | ||
.setMaster("local") | ||
|
@@ -665,16 +675,6 @@ class ExecutorAllocationManagerSuite extends FunSuite with LocalSparkContext { | |
assert(removeTimes(manager).contains("executor-2")) | ||
assert(!removeTimes(manager).contains("executor-1")) | ||
} | ||
} | ||
|
||
/** | ||
* Helper methods for testing ExecutorAllocationManager. | ||
* This includes methods to access private methods and fields in ExecutorAllocationManager. | ||
*/ | ||
private object ExecutorAllocationManagerSuite extends PrivateMethodTester { | ||
private val schedulerBacklogTimeout = 1L | ||
private val sustainedSchedulerBacklogTimeout = 2L | ||
private val executorIdleTimeout = 3L | ||
|
||
private def createSparkContext(minExecutors: Int = 1, maxExecutors: Int = 5): SparkContext = { | ||
val conf = new SparkConf() | ||
|
@@ -688,9 +688,22 @@ private object ExecutorAllocationManagerSuite extends PrivateMethodTester { | |
sustainedSchedulerBacklogTimeout.toString) | ||
.set("spark.dynamicAllocation.executorIdleTimeout", executorIdleTimeout.toString) | ||
.set("spark.dynamicAllocation.testing", "true") | ||
new SparkContext(conf) | ||
val sc = new SparkContext(conf) | ||
contexts += sc | ||
sc | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Helper methods for testing ExecutorAllocationManager. | ||
* This includes methods to access private methods and fields in ExecutorAllocationManager. | ||
*/ | ||
private object ExecutorAllocationManagerSuite extends PrivateMethodTester { | ||
private val schedulerBacklogTimeout = 1L | ||
private val sustainedSchedulerBacklogTimeout = 2L | ||
private val executorIdleTimeout = 3L | ||
|
||
private def createStageInfo(stageId: Int, numTasks: Int): StageInfo = { | ||
new StageInfo(stageId, 0, "name", numTasks, Seq.empty, "no details") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: alphabetize