|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.deploy |
| 19 | + |
| 20 | +import java.io.File |
| 21 | + |
| 22 | +import scala.collection.JavaConversions._ |
| 23 | + |
| 24 | +import org.apache.spark.util.{RedirectThread, Utils} |
| 25 | + |
| 26 | +/** |
| 27 | + * Launch an application through Spark submit in client mode with the appropriate classpath, |
| 28 | + * library paths, java options and memory. These properties of the JVM must be set before the |
| 29 | + * driver JVM is launched. The sole purpose of this class is to avoid handling the complexity |
| 30 | + * of parsing the properties file for such relevant configs in Bash. |
| 31 | + * |
| 32 | + * Usage: org.apache.spark.deploy.SparkSubmitDriverBootstrapper <submit args> |
| 33 | + */ |
| 34 | +private[spark] object SparkSubmitDriverBootstrapper { |
| 35 | + |
| 36 | + // Note: This class depends on the behavior of `bin/spark-class` and `bin/spark-submit`. |
| 37 | + // Any changes made there must be reflected in this file. |
| 38 | + |
| 39 | + def main(args: Array[String]): Unit = { |
| 40 | + |
| 41 | + // This should be called only from `bin/spark-class` |
| 42 | + if (!sys.env.contains("SPARK_CLASS")) { |
| 43 | + System.err.println("SparkSubmitDriverBootstrapper must be called from `bin/spark-class`!") |
| 44 | + System.exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + val submitArgs = args |
| 48 | + val runner = sys.env("RUNNER") |
| 49 | + val classpath = sys.env("CLASSPATH") |
| 50 | + val javaOpts = sys.env("JAVA_OPTS") |
| 51 | + val defaultDriverMemory = sys.env("OUR_JAVA_MEM") |
| 52 | + |
| 53 | + // Spark submit specific environment variables |
| 54 | + val deployMode = sys.env("SPARK_SUBMIT_DEPLOY_MODE") |
| 55 | + val propertiesFile = sys.env("SPARK_SUBMIT_PROPERTIES_FILE") |
| 56 | + val bootstrapDriver = sys.env("SPARK_SUBMIT_BOOTSTRAP_DRIVER") |
| 57 | + val submitDriverMemory = sys.env.get("SPARK_SUBMIT_DRIVER_MEMORY") |
| 58 | + val submitLibraryPath = sys.env.get("SPARK_SUBMIT_LIBRARY_PATH") |
| 59 | + val submitClasspath = sys.env.get("SPARK_SUBMIT_CLASSPATH") |
| 60 | + val submitJavaOpts = sys.env.get("SPARK_SUBMIT_OPTS") |
| 61 | + |
| 62 | + assume(runner != null, "RUNNER must be set") |
| 63 | + assume(classpath != null, "CLASSPATH must be set") |
| 64 | + assume(javaOpts != null, "JAVA_OPTS must be set") |
| 65 | + assume(defaultDriverMemory != null, "OUR_JAVA_MEM must be set") |
| 66 | + assume(deployMode == "client", "SPARK_SUBMIT_DEPLOY_MODE must be \"client\"!") |
| 67 | + assume(propertiesFile != null, "SPARK_SUBMIT_PROPERTIES_FILE must be set") |
| 68 | + assume(bootstrapDriver != null, "SPARK_SUBMIT_BOOTSTRAP_DRIVER must be set") |
| 69 | + |
| 70 | + // Parse the properties file for the equivalent spark.driver.* configs |
| 71 | + val properties = SparkSubmitArguments.getPropertiesFromFile(new File(propertiesFile)).toMap |
| 72 | + val confDriverMemory = properties.get("spark.driver.memory") |
| 73 | + val confLibraryPath = properties.get("spark.driver.extraLibraryPath") |
| 74 | + val confClasspath = properties.get("spark.driver.extraClassPath") |
| 75 | + val confJavaOpts = properties.get("spark.driver.extraJavaOptions") |
| 76 | + |
| 77 | + // Favor Spark submit arguments over the equivalent configs in the properties file. |
| 78 | + // Note that we do not actually use the Spark submit values for library path, classpath, |
| 79 | + // and Java opts here, because we have already captured them in Bash. |
| 80 | + |
| 81 | + val newDriverMemory = submitDriverMemory |
| 82 | + .orElse(confDriverMemory) |
| 83 | + .getOrElse(defaultDriverMemory) |
| 84 | + |
| 85 | + val newLibraryPath = |
| 86 | + if (submitLibraryPath.isDefined) { |
| 87 | + // SPARK_SUBMIT_LIBRARY_PATH is already captured in JAVA_OPTS |
| 88 | + "" |
| 89 | + } else { |
| 90 | + confLibraryPath.map("-Djava.library.path=" + _).getOrElse("") |
| 91 | + } |
| 92 | + |
| 93 | + val newClasspath = |
| 94 | + if (submitClasspath.isDefined) { |
| 95 | + // SPARK_SUBMIT_CLASSPATH is already captured in CLASSPATH |
| 96 | + classpath |
| 97 | + } else { |
| 98 | + classpath + confClasspath.map(sys.props("path.separator") + _).getOrElse("") |
| 99 | + } |
| 100 | + |
| 101 | + val newJavaOpts = |
| 102 | + if (submitJavaOpts.isDefined) { |
| 103 | + // SPARK_SUBMIT_OPTS is already captured in JAVA_OPTS |
| 104 | + javaOpts |
| 105 | + } else { |
| 106 | + javaOpts + confJavaOpts.map(" " + _).getOrElse("") |
| 107 | + } |
| 108 | + |
| 109 | + val filteredJavaOpts = Utils.splitCommandString(newJavaOpts) |
| 110 | + .filterNot(_.startsWith("-Xms")) |
| 111 | + .filterNot(_.startsWith("-Xmx")) |
| 112 | + |
| 113 | + // Build up command |
| 114 | + val command: Seq[String] = |
| 115 | + Seq(runner) ++ |
| 116 | + Seq("-cp", newClasspath) ++ |
| 117 | + Seq(newLibraryPath) ++ |
| 118 | + filteredJavaOpts ++ |
| 119 | + Seq(s"-Xms$newDriverMemory", s"-Xmx$newDriverMemory") ++ |
| 120 | + Seq("org.apache.spark.deploy.SparkSubmit") ++ |
| 121 | + submitArgs |
| 122 | + |
| 123 | + // Print the launch command. This follows closely the format used in `bin/spark-class`. |
| 124 | + if (sys.env.contains("SPARK_PRINT_LAUNCH_COMMAND")) { |
| 125 | + System.err.print("Spark Command: ") |
| 126 | + System.err.println(command.mkString(" ")) |
| 127 | + System.err.println("========================================\n") |
| 128 | + } |
| 129 | + |
| 130 | + // Start the driver JVM |
| 131 | + val filteredCommand = command.filter(_.nonEmpty) |
| 132 | + val builder = new ProcessBuilder(filteredCommand) |
| 133 | + val process = builder.start() |
| 134 | + |
| 135 | + // Redirect stdin, stdout, and stderr to/from the child JVM |
| 136 | + val stdinThread = new RedirectThread(System.in, process.getOutputStream, "redirect stdin") |
| 137 | + val stdoutThread = new RedirectThread(process.getInputStream, System.out, "redirect stdout") |
| 138 | + val stderrThread = new RedirectThread(process.getErrorStream, System.err, "redirect stderr") |
| 139 | + stdinThread.start() |
| 140 | + stdoutThread.start() |
| 141 | + stderrThread.start() |
| 142 | + |
| 143 | + // Terminate on broken pipe, which signals that the parent process has exited. This is |
| 144 | + // important for the PySpark shell, where Spark submit itself is a python subprocess. |
| 145 | + stdinThread.join() |
| 146 | + process.destroy() |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments