Skip to content

Commit 4e7f066

Browse files
author
Marcelo Vanzin
committed
Correctly propagate SPARK_JAVA_OPTS to driver/executor.
Users expected it to be possible to set spark.* config options using SPARK_JAVA_OPTS, but that's not possible when trying to propagate the env variable using spark.*.extraJavaOptions. So instead, in Yarn mode, propagate the env variable itself. Also make sure that, in cluster mode, the warning about SPARK_JAVA_OPTS being deprecated is printed to the logs.
1 parent 6a454ea commit 4e7f066

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

yarn/alpha/src/main/scala/org/apache/spark/deploy/yarn/Client.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ object Client {
178178
System.setProperty("SPARK_YARN_MODE", "true")
179179

180180
val sparkConf = new SparkConf
181-
sparkConf.validateSettings()
182181

183182
try {
184183
val args = new ClientArguments(argStrings, sparkConf)

yarn/common/src/main/scala/org/apache/spark/deploy/yarn/ClientBase.scala

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ trait ClientBase extends Logging {
294294
// Pass SPARK_YARN_USER_ENV itself to the AM so it can use it to set up executor environments.
295295
env("SPARK_YARN_USER_ENV") = userEnvs
296296
}
297-
298-
logInfo(s"ApplicationMaster environment: $env")
299297
env
300298
}
301299

@@ -320,6 +318,37 @@ trait ClientBase extends Logging {
320318
logInfo("Setting up container launch context")
321319
val amContainer = Records.newRecord(classOf[ContainerLaunchContext])
322320
amContainer.setLocalResources(localResources)
321+
322+
// In cluster mode, if the deprecated SPARK_JAVA_OPTS is set, we need to propagate it to
323+
// executors. But we can't just set spark.executor.extraJavaOptions, because the driver's
324+
// SparkContext will not let that set spark* system properties, which is expected behavior for
325+
// Yarn clients. So propagate it through the environment.
326+
//
327+
// Note that to warn the user about the deprecation in cluster mode, some code from
328+
// SparkConf#validateSettings() is duplicated here (to avoid triggering the condition
329+
// described above).
330+
if (args.amClass == classOf[ApplicationMaster].getName) {
331+
sys.env.get("SPARK_JAVA_OPTS").foreach { value =>
332+
val warning =
333+
s"""
334+
|SPARK_JAVA_OPTS was detected (set to '$value').
335+
|This is deprecated in Spark 1.0+.
336+
|
337+
|Please instead use:
338+
| - ./spark-submit with conf/spark-defaults.conf to set defaults for an application
339+
| - ./spark-submit with --driver-java-options to set -X options for a driver
340+
| - spark.executor.extraJavaOptions to set -X options for executors
341+
""".stripMargin
342+
logWarning(warning)
343+
for (proc <- Seq("driver", "executor")) {
344+
val key = s"spark.$proc.extraJavaOptions"
345+
if (sparkConf.contains(key)) {
346+
throw new SparkException(s"Found both $key and SPARK_JAVA_OPTS. Use only the former.")
347+
}
348+
}
349+
env("SPARK_JAVA_OPTS") = value
350+
}
351+
}
323352
amContainer.setEnvironment(env)
324353

325354
val amMemory = calculateAMMemory(newApp)
@@ -357,8 +386,11 @@ trait ClientBase extends Logging {
357386
for ((k, v) <- sparkConf.getAll) {
358387
javaOpts += "-D" + k + "=" + "\\\"" + v + "\\\""
359388
}
389+
360390
if (args.amClass == classOf[ApplicationMaster].getName) {
361-
sparkConf.getOption("spark.driver.extraJavaOptions").foreach(opts => javaOpts += opts)
391+
sparkConf.getOption("spark.driver.extraJavaOptions")
392+
.orElse(sys.env.get("SPARK_JAVA_OPTS"))
393+
.foreach(opts => javaOpts += opts)
362394
sparkConf.getOption("spark.driver.libraryPath")
363395
.foreach(p => javaOpts += s"-Djava.library.path=$p")
364396
}
@@ -374,7 +406,10 @@ trait ClientBase extends Logging {
374406
"1>", ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stdout",
375407
"2>", ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stderr")
376408

377-
logInfo("Command for starting the Spark ApplicationMaster: " + commands)
409+
logInfo("Yarn AM launch context:")
410+
logInfo(s" class: ${args.amClass}")
411+
logInfo(s" env: $env")
412+
logInfo(s" command: ${commands.mkString(" ")}")
378413

379414
// TODO: it would be nicer to just make sure there are no null commands here
380415
val printableCommands = commands.map(s => if (s == null) "null" else s).toList

yarn/common/src/main/scala/org/apache/spark/deploy/yarn/ExecutorRunnableUtil.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ trait ExecutorRunnableUtil extends Logging {
5555
sys.props.get("spark.executor.extraJavaOptions").foreach { opts =>
5656
javaOpts += opts
5757
}
58+
sys.env.get("SPARK_JAVA_OPTS").foreach { opts =>
59+
javaOpts += opts
60+
}
5861

5962
javaOpts += "-Djava.io.tmpdir=" +
6063
new Path(Environment.PWD.$(), YarnConfiguration.DEFAULT_CONTAINER_TEMP_DIR)

yarn/stable/src/main/scala/org/apache/spark/deploy/yarn/Client.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ object Client {
185185
// see Client#setupLaunchEnv().
186186
System.setProperty("SPARK_YARN_MODE", "true")
187187
val sparkConf = new SparkConf()
188-
sparkConf.validateSettings()
189188

190189
try {
191190
val args = new ClientArguments(argStrings, sparkConf)

0 commit comments

Comments
 (0)