Skip to content
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-26632][Core] Separate Thread Configurations of Driver and Executor #23560

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4c16bf8
separate thread configurations of driver and executor for serverThrea…
jiafuzha Jan 16, 2019
81b4bf0
Revert "separate thread configurations of driver and executor for ser…
jiafuzha Jan 25, 2019
d5ec0a0
separate thread configurations of driver and executor for serverThrea…
jiafuzha Jan 16, 2019
dd8405d
correct some style issue
jiafuzha Jan 16, 2019
ab9b965
correct style issue
jiafuzha Jan 16, 2019
5427eb8
correct style issue
jiafuzha Jan 16, 2019
ecab1cc
add check for driver's legacy identifier
jiafuzha Jan 17, 2019
7519af3
override io threads with specific values for driver and executor
jiafuzha Jan 29, 2019
64e9536
correct indent
jiafuzha Jan 30, 2019
d0b319d
Merge remote-tracking branch 'upstream/master'
jiafuzha Feb 1, 2019
1f813ec
Merge branch 'master' into thread_conf_separation
jiafuzha Feb 1, 2019
0fafe21
use role instead of boolean to differentiate driver and executor
jiafuzha Feb 13, 2019
11233d2
more accurate code to look for driver and executor thread configuration
jiafuzha Mar 13, 2019
59f9922
remove SparkContext.LEGACY_DRIVER_IDENTIFIER
jiafuzha Mar 13, 2019
bedf0fe
fix binary incompatibility issue
jiafuzha Mar 13, 2019
d73cfb5
refined code
jiafuzha Mar 13, 2019
cb4e5aa
restructure code and fix executor id missing issue
jiafuzha Mar 14, 2019
8f82381
Merge branch 'master' of https://github.com/apache/spark
jiafuzha Mar 14, 2019
e4b1023
merged from master to resolve conflict
jiafuzha Mar 14, 2019
05164b9
OMerge branch 'master' of https://github.com/apache/spark
jiafuzha Apr 30, 2019
5b9d86e
Merge branch 'master' into thread_conf_separation
jiafuzha Apr 30, 2019
cb27a2c
rewrite some code and add documentation
jiafuzha Apr 30, 2019
51f78ed
miscellaneous changes
jiafuzha May 6, 2019
6bdfe6d
miscellaneous udpdate
jiafuzha May 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.apache.spark.TaskState.TaskState
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.deploy.worker.WorkerWatcher
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config.EXECUTOR_ID
import org.apache.spark.rpc._
import org.apache.spark.scheduler.{ExecutorLossReason, TaskDescription}
import org.apache.spark.scheduler.cluster.CoarseGrainedClusterMessages._
Expand Down Expand Up @@ -239,6 +240,7 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
SparkHadoopUtil.get.addDelegationTokens(tokens, driverConf)
}

driverConf.set(EXECUTOR_ID, arguments.executorId)
val env = SparkEnv.createExecutorEnv(driverConf, arguments.executorId, arguments.hostname,
arguments.cores, cfg.ioEncryptionKey, isLocal = false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,26 @@ object SparkTransportConf {
* @param numUsableCores if nonzero, this will restrict the server and client threads to only
* use the given number of cores, rather than all of the machine's cores.
* This restriction will only occur if these properties are not already set.
* @param role optional role, could be driver, executor, worker and master. Default is
* [[None]], means no role specific configurations.
*/
def fromSparkConf(_conf: SparkConf, module: String, numUsableCores: Int = 0): TransportConf = {
def fromSparkConf(
_conf: SparkConf,
module: String,
numUsableCores: Int = 0,
role: Option[String] = None): TransportConf = {
val conf = _conf.clone

// Specify thread configuration based on our JVM's allocation of cores (rather than necessarily
// assuming we have all the machine's cores).
// NB: Only set if serverThreads/clientThreads not already set.
// specify default thread configuration based on our JVM's allocation of cores (rather than
// necessarily assuming we have all the machine's cores).
val numThreads = NettyUtils.defaultNumThreads(numUsableCores)
conf.setIfMissing(s"spark.$module.io.serverThreads", numThreads.toString)
conf.setIfMissing(s"spark.$module.io.clientThreads", numThreads.toString)
// override threads configurations with role specific values if specified
// config order is role > module > default
Seq("serverThreads", "clientThreads").foreach { suffix =>
val value = role.flatMap { r => conf.getOption(s"spark.$r.$module.io.$suffix") }
.getOrElse(
conf.get(s"spark.$module.io.$suffix", numThreads.toString))
conf.set(s"spark.$module.io.$suffix", value)
}

new TransportConf(module, new ConfigProvider {
override def get(name: String): String = conf.get(name)
Expand Down
19 changes: 15 additions & 4 deletions core/src/main/scala/org/apache/spark/rpc/netty/Dispatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import scala.collection.JavaConverters._
import scala.concurrent.Promise
import scala.util.control.NonFatal

import org.apache.spark.SparkException
import org.apache.spark.{SparkConf, SparkContext, SparkException}
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config.EXECUTOR_ID
import org.apache.spark.internal.config.Network.RPC_NETTY_DISPATCHER_NUM_THREADS
import org.apache.spark.network.client.RpcResponseCallback
import org.apache.spark.rpc._
Expand Down Expand Up @@ -194,12 +195,22 @@ private[netty] class Dispatcher(nettyEnv: NettyRpcEnv, numUsableCores: Int) exte
endpoints.containsKey(name)
}

/** Thread pool used for dispatching messages. */
private val threadpool: ThreadPoolExecutor = {
private def getNumOfThreads(conf: SparkConf): Int = {
val availableCores =
if (numUsableCores > 0) numUsableCores else Runtime.getRuntime.availableProcessors()
val numThreads = nettyEnv.conf.get(RPC_NETTY_DISPATCHER_NUM_THREADS)

val modNumThreads = conf.get(RPC_NETTY_DISPATCHER_NUM_THREADS)
.getOrElse(math.max(2, availableCores))

conf.get(EXECUTOR_ID).map { id =>
val role = if (id == SparkContext.DRIVER_IDENTIFIER) "driver" else "executor"
conf.getInt(s"spark.$role.rpc.netty.dispatcher.numThreads", modNumThreads)
}.getOrElse(modNumThreads)
}

/** Thread pool used for dispatching messages. */
private val threadpool: ThreadPoolExecutor = {
val numThreads = getNumOfThreads(nettyEnv.conf)
val pool = ThreadUtils.newDaemonFixedThreadPool(numThreads, "dispatcher-event-loop")
for (i <- 0 until numThreads) {
pool.execute(new MessageLoop)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import scala.reflect.ClassTag
import scala.util.{DynamicVariable, Failure, Success, Try}
import scala.util.control.NonFatal

import org.apache.spark.{SecurityManager, SparkConf}
import org.apache.spark.{SecurityManager, SparkConf, SparkContext}
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config.EXECUTOR_ID
import org.apache.spark.internal.config.Network._
import org.apache.spark.network.TransportContext
import org.apache.spark.network.client._
Expand All @@ -47,11 +48,15 @@ private[netty] class NettyRpcEnv(
host: String,
securityManager: SecurityManager,
numUsableCores: Int) extends RpcEnv(conf) with Logging {
val role = conf.get(EXECUTOR_ID).map { id =>
if (id == SparkContext.DRIVER_IDENTIFIER) Some("driver") else Some("executor")
}.getOrElse(None)
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved

private[netty] val transportConf = SparkTransportConf.fromSparkConf(
conf.clone.set(RPC_IO_NUM_CONNECTIONS_PER_PEER, 1),
"rpc",
conf.get(RPC_IO_THREADS).getOrElse(numUsableCores))
conf.get(RPC_IO_THREADS).getOrElse(numUsableCores),
role)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not necessary to separate into another line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine (one arg per line).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerryshao , The guide says,

For method declarations, use 4 space indentation for their parameters and put each in each line when the parameters don't fit in two lines.

I'll keep this code.


private val dispatcher: Dispatcher = new Dispatcher(this, numUsableCores)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.network.netty

import org.scalatest.Matchers
import org.scalatest.mockito.MockitoSugar

import org.apache.spark.{SparkConf, SparkFunSuite}
import org.apache.spark.network.util.NettyUtils

class SparkTransportConfSuite extends SparkFunSuite with MockitoSugar with Matchers{
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved
val module = "rpc"
val serThreads = "serverThreads"
val cliThreads = "clientThreads"

test("default value is get when neither role nor module is set") {
val numUsableCores = 4
val conf = new SparkConf()
val sparkTransportConf = SparkTransportConf.fromSparkConf(conf, module, numUsableCores, None)
val expected = NettyUtils.defaultNumThreads(numUsableCores)
val serActual = sparkTransportConf.get(s"spark.$module.io.$serThreads", "")
val cliActual = sparkTransportConf.get(s"spark.$module.io.$cliThreads", "")
serActual should equal(expected.toString)
cliActual should equal(expected.toString)
}

test("module value is get when role is not set") {
val numUsableCores = 3
val serExpected = "7"
val cliExpected = "5"
val conf = new SparkConf()
.set(s"spark.$module.io.$serThreads", serExpected)
.set(s"spark.$module.io.$cliThreads", cliExpected)
val sparkTransportConf = SparkTransportConf.fromSparkConf(conf, module, numUsableCores, None)
val serActual = sparkTransportConf.get(s"spark.$module.io.$serThreads", "")
val cliActual = sparkTransportConf.get(s"spark.$module.io.$cliThreads", "")
serActual should equal(serExpected)
cliActual should equal(cliExpected)
}

test("role value is get when role is set") {
val role = Some("driver")
val numUsableCores = 10
val serModule = "7"
val cliModule = "5"
val serExpected = "8"
val cliExpected = "6"
val conf = new SparkConf()
.set(s"spark.$module.io.$serThreads", serModule)
.set(s"spark.$module.io.$cliThreads", cliModule)
.set(s"spark.${role.get}.$module.io.$serThreads", serExpected)
.set(s"spark.${role.get}.$module.io.$cliThreads", cliExpected)
val sparkTransportConf = SparkTransportConf.fromSparkConf(conf, module, numUsableCores, role)
val serActual = sparkTransportConf.get(s"spark.$module.io.$serThreads", "")
val cliActual = sparkTransportConf.get(s"spark.$module.io.$cliThreads", "")
serActual should equal(serExpected)
cliActual should equal(cliExpected)
}

test("module value is get when role other than mine is set") {
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved
val role = Some("driver")
val otherRole = "executor"
val numUsableCores = 10
val serExpected = "7"
val cliExpected = "5"
val serRole = "8"
val cliRole = "6"
val conf = new SparkConf()
.set(s"spark.$module.io.$serThreads", serExpected)
.set(s"spark.$module.io.$cliThreads", cliExpected)
.set(s"spark.$otherRole.$module.io.$serThreads", serRole)
.set(s"spark.$otherRole.$module.io.$cliThreads", cliRole)
val sparkTransportConf = SparkTransportConf.fromSparkConf(conf, module, numUsableCores, role)
val serActual = sparkTransportConf.get(s"spark.$module.io.$serThreads", "")
val cliActual = sparkTransportConf.get(s"spark.$module.io.$cliThreads", "")
serActual should equal(serExpected)
cliActual should equal(cliExpected)
}
}
40 changes: 40 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,46 @@ Apart from these, the following properties are also available, and may be useful
</tr>
</table>

### Thread Configurations

Depending on jobs and cluster configurations, we can set number of threads in several places in Spark to utilize
available resources efficiently to get better performance. Prior to Spark 3.0, these thread configurations apply
to all roles of Spark, such as driver, executor, worker and master. From Spark 3.0, we can configure threads in
finer granularity starting from driver and executor. Take RPC module as example in below table. For other modules,
like shuffle, just replace "rpc" with "shuffle" in the property names except
spark.{driver|executor}.rpc.netty.dispatcher.numThreads, which is only for RPC module.
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved

<table class="table">
<tr><th>Property Name</th><th>Default</th><th>Meaning</th></tr>
<tr>
<td>spark.{driver|executor}.rpc.io.serverThreads</td>
<td>
Fall back on spark.rpc.io.serverThreads
</td>
<td>Number of threads used in the server thread pool</td>
</tr>
<tr>
<td>spark.{driver|executor}.rpc.io.clientThreads</td>
<td>
Fall back on spark.rpc.io.clientThreads
</td>
<td>Number of threads used in the client thread pool</td>
</tr>
<tr>
<td>spark.{driver|executor}.rpc.netty.dispatcher.numThreads</td>
<td>
Fall back on spark.rpc.netty.dispatcher.numThreads
</td>
<td>Number of threads used in RPC message dispatcher thread pool</td>
</tr>
</table>

The default values of spark.rpc.io.serverThreads, spark.rpc.io.clientThreads and spark.rpc.netty.dispatcher.numThreads
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved
are same. It's <br>
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved
number of CPU cores if specified. Otherwise, the available processors to the JVM. In either cases, the default value
jiafuzha marked this conversation as resolved.
Show resolved Hide resolved
is limited to <code>org.apache.spark.network.util.NettyUtils.MAX_DEFAULT_NETTY_THREADS</code>.


### Security

Please refer to the [Security](security.html) page for available options on how to secure different
Expand Down
3 changes: 3 additions & 0 deletions project/MimaExcludes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ object MimaExcludes {
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.scheduler.SparkListenerApplicationStart.copy"),
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.scheduler.SparkListenerApplicationStart.this"),
ProblemFilters.exclude[MissingTypesProblem]("org.apache.spark.scheduler.SparkListenerApplicationStart$"),

// [SPARK-26632][Core] Separate Thread Configurations of Driver and Executor
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.network.netty.SparkTransportConf.fromSparkConf"),

// [SPARK-25765][ML] Add training cost to BisectingKMeans summary
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.mllib.clustering.BisectingKMeansModel.this"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.apache.spark.{SparkConf, SparkEnv, TaskState}
import org.apache.spark.TaskState
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config.EXECUTOR_ID
import org.apache.spark.scheduler.TaskDescription
import org.apache.spark.scheduler.cluster.mesos.MesosSchedulerUtils
import org.apache.spark.util.Utils
Expand Down Expand Up @@ -74,6 +75,7 @@ private[spark] class MesosExecutorBackend
val properties = Utils.deserialize[Array[(String, String)]](executorInfo.getData.toByteArray) ++
Seq[(String, String)](("spark.app.id", frameworkInfo.getId.getValue))
val conf = new SparkConf(loadDefaults = true).setAll(properties)
conf.set(EXECUTOR_ID, executorId)
val env = SparkEnv.createExecutorEnv(
conf, executorId, slaveInfo.getHostname, cpusPerTask, None, isLocal = false)

Expand Down