Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,47 @@ private[spark] class CoarseGrainedExecutorBackend(

private[spark] object CoarseGrainedExecutorBackend extends Logging {

private def run(
case class Arguments(
driverUrl: String,
executorId: String,
hostname: String,
cores: Int,
appId: String,
workerUrl: Option[String],
userClassPath: Seq[URL]) {
userClassPath: mutable.ListBuffer[URL])

def main(args: Array[String]): Unit = {
val createFn: (RpcEnv, Arguments, SparkEnv) =>
CoarseGrainedExecutorBackend = { case (rpcEnv, arguments, env) =>
new CoarseGrainedExecutorBackend(rpcEnv, arguments.driverUrl, arguments.executorId,
arguments.hostname, arguments.cores, arguments.userClassPath, env)
}
run(parseArguments(args, this.getClass.getCanonicalName.stripSuffix("$")), createFn)
System.exit(0)
}

def run(
arguments: Arguments,
backendCreateFn: (RpcEnv, Arguments, SparkEnv) => CoarseGrainedExecutorBackend): Unit = {

Utils.initDaemon(log)

SparkHadoopUtil.get.runAsSparkUser { () =>
// Debug code
Utils.checkHost(hostname)
Utils.checkHost(arguments.hostname)

// Bootstrap to fetch the driver's Spark properties.
val executorConf = new SparkConf
val fetcher = RpcEnv.create(
"driverPropsFetcher",
hostname,
arguments.hostname,
-1,
executorConf,
new SecurityManager(executorConf),
clientMode = true)
val driver = fetcher.setupEndpointRefByURI(driverUrl)
val driver = fetcher.setupEndpointRefByURI(arguments.driverUrl)
val cfg = driver.askSync[SparkAppConfig](RetrieveSparkAppConfig)
val props = cfg.sparkProperties ++ Seq[(String, String)](("spark.app.id", appId))
val props = cfg.sparkProperties ++ Seq[(String, String)](("spark.app.id", arguments.appId))
fetcher.shutdown()

// Create SparkEnv using properties we fetched from the driver.
Expand All @@ -225,19 +239,18 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
SparkHadoopUtil.get.addDelegationTokens(tokens, driverConf)
}

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

env.rpcEnv.setupEndpoint("Executor", new CoarseGrainedExecutorBackend(
env.rpcEnv, driverUrl, executorId, hostname, cores, userClassPath, env))
workerUrl.foreach { url =>
env.rpcEnv.setupEndpoint("Executor", backendCreateFn(env.rpcEnv, arguments, env))
arguments.workerUrl.foreach { url =>
env.rpcEnv.setupEndpoint("WorkerWatcher", new WorkerWatcher(env.rpcEnv, url))
}
env.rpcEnv.awaitTermination()
}
}

def main(args: Array[String]) {
def parseArguments(args: Array[String], classNameForEntry: String): Arguments = {
var driverUrl: String = null
var executorId: String = null
var hostname: String = null
Expand Down Expand Up @@ -276,24 +289,24 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
// scalastyle:off println
System.err.println(s"Unrecognized options: ${tail.mkString(" ")}")
// scalastyle:on println
printUsageAndExit()
printUsageAndExit(classNameForEntry)
Copy link
Contributor

Choose a reason for hiding this comment

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

You want classNameForEntry.stripSuffix("$"), either here or in the caller, or the help message will be wrong.

Copy link
Contributor Author

@HeartSaVioR HeartSaVioR Feb 14, 2019

Choose a reason for hiding this comment

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

Nice finding! Maybe I'm still thinking many things in Java way. Will address.

}
}

if (driverUrl == null || executorId == null || hostname == null || cores <= 0 ||
appId == null) {
printUsageAndExit()
printUsageAndExit(classNameForEntry)
}

run(driverUrl, executorId, hostname, cores, appId, workerUrl, userClassPath)
System.exit(0)
Arguments(driverUrl, executorId, hostname, cores, appId, workerUrl,
userClassPath)
}

private def printUsageAndExit() = {
private def printUsageAndExit(classNameForEntry: String): Unit = {
// scalastyle:off println
System.err.println(
"""
|Usage: CoarseGrainedExecutorBackend [options]
s"""
|Usage: $classNameForEntry [options]
|
| Options are:
| --driver-url <driverUrl>
Expand All @@ -307,5 +320,4 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
// scalastyle:on println
System.exit(1)
}

}
18 changes: 18 additions & 0 deletions docs/running-on-yarn.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,18 @@ To use a custom metrics.properties for the application master and executors, upd
<td>{{HTTP_SCHEME}}</td>
<td>`http://` or `https://` according to YARN HTTP policy. (Configured via `yarn.http.policy`)</td>
</tr>
<tr>
<td>{{NM_HOST}}</td>
<td>The "host" of node where container was run.</td>
</tr>
<tr>
<td>{{NM_PORT}}</td>
<td>The "port" of node manager where container was run.</td>
</tr>
<tr>
<td>{{NM_HTTP_PORT}}</td>
<td>The "port" of node manager's http server where container was run.</td>
</tr>
<tr>
<td>{{NM_HTTP_ADDRESS}}</td>
<td>Http URI of the node on which the container is allocated.</td>
Expand All @@ -502,6 +514,12 @@ To use a custom metrics.properties for the application master and executors, upd
</tr>
</table>

For example, suppose you would like to point log url link to Job History Server directly instead of let NodeManager http server redirects it, you can configure `spark.history.custom.executor.log.url` as below:

`{{HTTP_SCHEME}}<JHS_HOST>:<JHS_PORT>/jobhistory/logs/{{NM_HOST}}:{{NM_PORT}}/{{CONTAINER_ID}}/{{CONTAINER_ID}}/{{USER}}/{{FILE_NAME}}?start=-4096`

NOTE: you need to replace `<JHS_POST>` and `<JHS_PORT>` with actual value.

# Important notes

- Whether core requests are honored in scheduling decisions depends on which scheduler is in use and how it is configured.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private[yarn] class ExecutorRunnable(
val commands = prefixEnv ++
Seq(Environment.JAVA_HOME.$$() + "/bin/java", "-server") ++
javaOpts ++
Seq("org.apache.spark.executor.CoarseGrainedExecutorBackend",
Seq("org.apache.spark.executor.YarnCoarseGrainedExecutorBackend",
"--driver-url", masterAddress,
"--executor-id", executorId,
"--hostname", hostname,
Expand Down Expand Up @@ -235,21 +235,6 @@ private[yarn] class ExecutorRunnable(
}
}

// Add log urls, as well as executor attributes
container.foreach { c =>
YarnContainerInfoHelper.getLogUrls(conf, Some(c)).foreach { m =>
m.foreach { case (fileName, url) =>
env("SPARK_LOG_URL_" + fileName.toUpperCase(Locale.ROOT)) = url
}
}

YarnContainerInfoHelper.getAttributes(conf, Some(c)).foreach { m =>
m.foreach { case (attr, value) =>
env("SPARK_EXECUTOR_ATTRIBUTE_" + attr.toUpperCase(Locale.ROOT)) = value
}
}
}

env
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.executor

import java.net.URL

import org.apache.spark.SparkEnv
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.internal.Logging
import org.apache.spark.rpc.RpcEnv
import org.apache.spark.util.YarnContainerInfoHelper

/**
* Custom implementation of CoarseGrainedExecutorBackend for YARN resource manager.
* This class extracts executor log URLs and executor attributes from system environment which
* properties are available for container being set via YARN.
*/
private[spark] class YarnCoarseGrainedExecutorBackend(

Choose a reason for hiding this comment

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

Can we have some comments here as to what does this class do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry missed this. Just addressed.

rpcEnv: RpcEnv,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: indent args and extends one more level.

Copy link
Contributor Author

@HeartSaVioR HeartSaVioR Feb 13, 2019

Choose a reason for hiding this comment

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

I'm sorry I'm not sure I understand correctly. All args are 4 spaces and extends line is 2 spaces which doesn't seem to violate the style. Could you please guide your suggestion via actual code change?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I swear I saw 2 spaces here when I looked at this code before. Ignore me.

driverUrl: String,
executorId: String,
hostname: String,
cores: Int,
userClassPath: Seq[URL],
env: SparkEnv)
extends CoarseGrainedExecutorBackend(
rpcEnv,
driverUrl,
executorId,
hostname,
cores,
userClassPath,
env) with Logging {

private lazy val hadoopConfiguration = SparkHadoopUtil.get.newConfiguration(env.conf)

override def extractLogUrls: Map[String, String] = {
YarnContainerInfoHelper.getLogUrls(hadoopConfiguration, container = None)
.getOrElse(Map())
}

override def extractAttributes: Map[String, String] = {
YarnContainerInfoHelper.getAttributes(hadoopConfiguration, container = None)
.getOrElse(Map())
}
}

private[spark] object YarnCoarseGrainedExecutorBackend extends Logging {

def main(args: Array[String]): Unit = {
val createFn: (RpcEnv, CoarseGrainedExecutorBackend.Arguments, SparkEnv) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I'm using CoarseGrainedExecutorBackend.Arguments instead of Arguments for clarity. Please let me know once we would want to use Arguments directly.

CoarseGrainedExecutorBackend = { case (rpcEnv, arguments, env) =>
new YarnCoarseGrainedExecutorBackend(rpcEnv, arguments.driverUrl, arguments.executorId,
arguments.hostname, arguments.cores, arguments.userClassPath, env)
}
val backendArgs = CoarseGrainedExecutorBackend.parseArguments(args,
this.getClass.getCanonicalName.stripSuffix("$"))
CoarseGrainedExecutorBackend.run(backendArgs, createFn)
System.exit(0)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ private[spark] object YarnContainerInfoHelper extends Logging {
val yarnConf = new YarnConfiguration(conf)
Some(Map(
"HTTP_SCHEME" -> getYarnHttpScheme(yarnConf),
"NM_HOST" -> getNodeManagerHost(container),
"NM_PORT" -> getNodeManagerPort(container),
"NM_HTTP_PORT" -> getNodeManagerHttpPort(container),
"NM_HTTP_ADDRESS" -> getNodeManagerHttpAddress(container),
"CLUSTER_ID" -> getClusterId(yarnConf).getOrElse(""),
"CONTAINER_ID" -> ConverterUtils.toString(getContainerId(container)),
Expand Down Expand Up @@ -97,7 +100,22 @@ private[spark] object YarnContainerInfoHelper extends Logging {

def getNodeManagerHttpAddress(container: Option[Container]): String = container match {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same applies to all methods: now we don't have an actual usage to pass Container as parameter, but it's still safer given that it's independent on context of process. Would we want to leave this as it is, or remove this and add a note to class (or methods) javadoc?

case Some(c) => c.getNodeHttpAddress
case None => System.getenv(Environment.NM_HOST.name()) + ":" +
System.getenv(Environment.NM_HTTP_PORT.name())
case None => getNodeManagerHost(None) + ":" + getNodeManagerHttpPort(None)
}

def getNodeManagerHost(container: Option[Container]): String = container match {
case Some(c) => c.getNodeHttpAddress.split(":")(0)
case None => System.getenv(Environment.NM_HOST.name())
}

def getNodeManagerHttpPort(container: Option[Container]): String = container match {
case Some(c) => c.getNodeHttpAddress.split(":")(1)
case None => System.getenv(Environment.NM_HTTP_PORT.name())
}

def getNodeManagerPort(container: Option[Container]): String = container match {
case Some(_) => "-1" // Just return invalid port given we cannot retrieve the value
case None => System.getenv(Environment.NM_PORT.name())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ private object YarnClusterDriver extends Logging with Matchers {
val driverAttributes = listener.driverAttributes.get
val expectationAttributes = Map(
"HTTP_SCHEME" -> YarnContainerInfoHelper.getYarnHttpScheme(yarnConf),
"NM_HOST" -> YarnContainerInfoHelper.getNodeManagerHost(container = None),
"NM_PORT" -> YarnContainerInfoHelper.getNodeManagerPort(container = None),
"NM_HTTP_PORT" -> YarnContainerInfoHelper.getNodeManagerHttpPort(container = None),
"NM_HTTP_ADDRESS" -> YarnContainerInfoHelper.getNodeManagerHttpAddress(container = None),
"CLUSTER_ID" -> YarnContainerInfoHelper.getClusterId(yarnConf).getOrElse(""),
"CONTAINER_ID" -> ConverterUtils.toString(containerId),
Expand Down