Skip to content

Commit 007a733

Browse files
committed
SPARK-1286: Make usage of spark-env.sh idempotent
Various spark scripts load spark-env.sh. This can cause growth of any variables that may be appended to (SPARK_CLASSPATH, SPARK_REPL_OPTS) and it makes the precedence order for options specified in spark-env.sh less clear. One use-case for the latter is that we want to set options from the command-line of spark-shell, but these options will be overridden by subsequent loading of spark-env.sh. If we were to load the spark-env.sh first and then set our command-line options, we could guarantee correct precedence order. Note that we use SPARK_CONF_DIR if available to support the sbin/ scripts, which always set this variable from sbin/spark-config.sh. Otherwise, we default to the ../conf/ as usual. Author: Aaron Davidson <[email protected]> Closes #184 from aarondav/idem and squashes the following commits: e291f91 [Aaron Davidson] Use "private" variables in load-spark-env.sh 8da8360 [Aaron Davidson] Add .sh extension to load-spark-env.sh 93a2471 [Aaron Davidson] SPARK-1286: Make usage of spark-env.sh idempotent
1 parent b637f2d commit 007a733

File tree

11 files changed

+45
-34
lines changed

11 files changed

+45
-34
lines changed

bin/compute-classpath.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ SCALA_VERSION=2.10
2525
# Figure out where Spark is installed
2626
FWDIR="$(cd `dirname $0`/..; pwd)"
2727

28-
# Load environment variables from conf/spark-env.sh, if it exists
29-
if [ -e "$FWDIR/conf/spark-env.sh" ] ; then
30-
. $FWDIR/conf/spark-env.sh
31-
fi
28+
. $FWDIR/bin/load-spark-env.sh
3229

3330
# Build up classpath
3431
CLASSPATH="$SPARK_CLASSPATH:$FWDIR/conf"

bin/load-spark-env.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership.
7+
# The ASF licenses this file to You under the Apache License, Version 2.0
8+
# (the "License"); you may not use this file except in compliance with
9+
# the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
# This script loads spark-env.sh if it exists, and ensures it is only loaded once.
21+
# spark-env.sh is loaded from SPARK_CONF_DIR if set, or within the current directory's
22+
# conf/ subdirectory.
23+
24+
if [ -z "$SPARK_ENV_LOADED" ]; then
25+
export SPARK_ENV_LOADED=1
26+
27+
# Returns the parent of the directory this script lives in.
28+
parent_dir="$(cd `dirname $0`/..; pwd)"
29+
30+
use_conf_dir=${SPARK_CONF_DIR:-"$parent_dir/conf"}
31+
32+
if [ -f "${use_conf_dir}/spark-env.sh" ]; then
33+
. "${use_conf_dir}/spark-env.sh"
34+
fi
35+
fi

bin/pyspark

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ if [ ! -f "$FWDIR/RELEASE" ]; then
3636
fi
3737
fi
3838

39-
# Load environment variables from conf/spark-env.sh, if it exists
40-
if [ -e "$FWDIR/conf/spark-env.sh" ] ; then
41-
. $FWDIR/conf/spark-env.sh
42-
fi
39+
. $FWDIR/bin/load-spark-env.sh
4340

4441
# Figure out which Python executable to use
4542
if [ -z "$PYSPARK_PYTHON" ] ; then

bin/run-example

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ FWDIR="$(cd `dirname $0`/..; pwd)"
3030
# Export this as SPARK_HOME
3131
export SPARK_HOME="$FWDIR"
3232

33-
# Load environment variables from conf/spark-env.sh, if it exists
34-
if [ -e "$FWDIR/conf/spark-env.sh" ] ; then
35-
. $FWDIR/conf/spark-env.sh
36-
fi
33+
. $FWDIR/bin/load-spark-env.sh
3734

3835
if [ -z "$1" ]; then
3936
echo "Usage: run-example <example-class> [<args>]" >&2

bin/spark-class

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ FWDIR="$(cd `dirname $0`/..; pwd)"
3030
# Export this as SPARK_HOME
3131
export SPARK_HOME="$FWDIR"
3232

33-
# Load environment variables from conf/spark-env.sh, if it exists
34-
if [ -e "$FWDIR/conf/spark-env.sh" ] ; then
35-
. $FWDIR/conf/spark-env.sh
36-
fi
33+
. $FWDIR/bin/load-spark-env.sh
3734

3835
if [ -z "$1" ]; then
3936
echo "Usage: spark-class <class> [<args>]" >&2

bin/spark-shell

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ done
8181
# Set MASTER from spark-env if possible
8282
DEFAULT_SPARK_MASTER_PORT=7077
8383
if [ -z "$MASTER" ]; then
84-
if [ -e "$FWDIR/conf/spark-env.sh" ]; then
85-
. "$FWDIR/conf/spark-env.sh"
86-
fi
84+
. $FWDIR/bin/load-spark-env.sh
8785
if [ "x" != "x$SPARK_MASTER_IP" ]; then
8886
if [ "y" != "y$SPARK_MASTER_PORT" ]; then
8987
SPARK_MASTER_PORT="${SPARK_MASTER_PORT}"

sbin/slaves.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ then
6363
shift
6464
fi
6565

66-
if [ -f "${SPARK_CONF_DIR}/spark-env.sh" ]; then
67-
. "${SPARK_CONF_DIR}/spark-env.sh"
68-
fi
66+
. "$SPARK_PREFIX/bin/load-spark-env.sh"
6967

7068
if [ "$HOSTLIST" = "" ]; then
7169
if [ "$SPARK_SLAVES" = "" ]; then

sbin/spark-daemon.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ spark_rotate_log ()
8686
fi
8787
}
8888

89-
if [ -f "${SPARK_CONF_DIR}/spark-env.sh" ]; then
90-
. "${SPARK_CONF_DIR}/spark-env.sh"
91-
fi
89+
. "$SPARK_PREFIX/bin/load-spark-env.sh"
9290

9391
if [ "$SPARK_IDENT_STRING" = "" ]; then
9492
export SPARK_IDENT_STRING="$USER"

sbin/start-master.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ done
3939

4040
. "$sbin/spark-config.sh"
4141

42-
if [ -f "${SPARK_CONF_DIR}/spark-env.sh" ]; then
43-
. "${SPARK_CONF_DIR}/spark-env.sh"
44-
fi
42+
. "$SPARK_PREFIX/bin/load-spark-env.sh"
4543

4644
if [ "$SPARK_MASTER_PORT" = "" ]; then
4745
SPARK_MASTER_PORT=7077

sbin/start-slaves.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ done
3838

3939
. "$sbin/spark-config.sh"
4040

41-
if [ -f "${SPARK_CONF_DIR}/spark-env.sh" ]; then
42-
. "${SPARK_CONF_DIR}/spark-env.sh"
43-
fi
41+
. "$SPARK_PREFIX/bin/load-spark-env.sh"
4442

4543
# Find the port number for the master
4644
if [ "$SPARK_MASTER_PORT" = "" ]; then

0 commit comments

Comments
 (0)