0001 #!/usr/bin/env bash
0002
0003 #
0004 # Licensed to the Apache Software Foundation (ASF) under one or more
0005 # contributor license agreements. See the NOTICE file distributed with
0006 # this work for additional information regarding copyright ownership.
0007 # The ASF licenses this file to You under the Apache License, Version 2.0
0008 # (the "License"); you may not use this file except in compliance with
0009 # the License. You may obtain a copy of the License at
0010 #
0011 # http://www.apache.org/licenses/LICENSE-2.0
0012 #
0013 # Unless required by applicable law or agreed to in writing, software
0014 # distributed under the License is distributed on an "AS IS" BASIS,
0015 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 # See the License for the specific language governing permissions and
0017 # limitations under the License.
0018 #
0019
0020 if [ -z "${SPARK_HOME}" ]; then
0021 source "$(dirname "$0")"/find-spark-home
0022 fi
0023
0024 . "${SPARK_HOME}"/bin/load-spark-env.sh
0025
0026 # Find the java binary
0027 if [ -n "${JAVA_HOME}" ]; then
0028 RUNNER="${JAVA_HOME}/bin/java"
0029 else
0030 if [ "$(command -v java)" ]; then
0031 RUNNER="java"
0032 else
0033 echo "JAVA_HOME is not set" >&2
0034 exit 1
0035 fi
0036 fi
0037
0038 # Find Spark jars.
0039 if [ -d "${SPARK_HOME}/jars" ]; then
0040 SPARK_JARS_DIR="${SPARK_HOME}/jars"
0041 else
0042 SPARK_JARS_DIR="${SPARK_HOME}/assembly/target/scala-$SPARK_SCALA_VERSION/jars"
0043 fi
0044
0045 if [ ! -d "$SPARK_JARS_DIR" ] && [ -z "$SPARK_TESTING$SPARK_SQL_TESTING" ]; then
0046 echo "Failed to find Spark jars directory ($SPARK_JARS_DIR)." 1>&2
0047 echo "You need to build Spark with the target \"package\" before running this program." 1>&2
0048 exit 1
0049 else
0050 LAUNCH_CLASSPATH="$SPARK_JARS_DIR/*"
0051 fi
0052
0053 # Add the launcher build dir to the classpath if requested.
0054 if [ -n "$SPARK_PREPEND_CLASSES" ]; then
0055 LAUNCH_CLASSPATH="${SPARK_HOME}/launcher/target/scala-$SPARK_SCALA_VERSION/classes:$LAUNCH_CLASSPATH"
0056 fi
0057
0058 # For tests
0059 if [[ -n "$SPARK_TESTING" ]]; then
0060 unset YARN_CONF_DIR
0061 unset HADOOP_CONF_DIR
0062 fi
0063
0064 # The launcher library will print arguments separated by a NULL character, to allow arguments with
0065 # characters that would be otherwise interpreted by the shell. Read that in a while loop, populating
0066 # an array that will be used to exec the final command.
0067 #
0068 # The exit code of the launcher is appended to the output, so the parent shell removes it from the
0069 # command array and checks the value to see if the launcher succeeded.
0070 build_command() {
0071 "$RUNNER" -Xmx128m $SPARK_LAUNCHER_OPTS -cp "$LAUNCH_CLASSPATH" org.apache.spark.launcher.Main "$@"
0072 printf "%d\0" $?
0073 }
0074
0075 # Turn off posix mode since it does not allow process substitution
0076 set +o posix
0077 CMD=()
0078 DELIM=$'\n'
0079 CMD_START_FLAG="false"
0080 while IFS= read -d "$DELIM" -r ARG; do
0081 if [ "$CMD_START_FLAG" == "true" ]; then
0082 CMD+=("$ARG")
0083 else
0084 if [ "$ARG" == $'\0' ]; then
0085 # After NULL character is consumed, change the delimiter and consume command string.
0086 DELIM=''
0087 CMD_START_FLAG="true"
0088 elif [ "$ARG" != "" ]; then
0089 echo "$ARG"
0090 fi
0091 fi
0092 done < <(build_command "$@")
0093
0094 COUNT=${#CMD[@]}
0095 LAST=$((COUNT - 1))
0096 LAUNCHER_EXIT_CODE=${CMD[$LAST]}
0097
0098 # Certain JVM failures result in errors being printed to stdout (instead of stderr), which causes
0099 # the code that parses the output of the launcher to get confused. In those cases, check if the
0100 # exit code is an integer, and if it's not, handle it as a special error case.
0101 if ! [[ $LAUNCHER_EXIT_CODE =~ ^[0-9]+$ ]]; then
0102 echo "${CMD[@]}" | head -n-1 1>&2
0103 exit 1
0104 fi
0105
0106 if [ $LAUNCHER_EXIT_CODE != 0 ]; then
0107 exit $LAUNCHER_EXIT_CODE
0108 fi
0109
0110 CMD=("${CMD[@]:0:$LAST}")
0111 exec "${CMD[@]}"