一、这是$SPARK_HOME/sbin/start-all.sh脚本
#!/usr/bin/envbash
# Start all spark daemons. 后台启动所有spark节点,将master在这个节点中启动,worker节点是通过conf/slaver来决定的
#Starts the master on this node.
#Starts a worker on each node specified in conf/slaves
##############################################################
#判断spark_home这个环境变量串的长度等于0,则shell自己赋值
if [-z "${SPARK_HOME}" ]; then
export SPARK_HOME="$(cd "`dirname"$0"`"/..; pwd)"
fi
TACHYON_STR=""
#- $#表示参数的shell后面参数的个数,shift:像js数组一样,每调用一次会将第一个元素去掉
#如果找到--with-tachyon这样的参数,赋给变量TACHYON_STR
while(( "$#" )); do
case$1 in
--with-tachyon)
TACHYON_STR="--with-tachyon"
;;
esac
shift
done
#Load the Spark configuration
#这个spark-config.sh作用就是将$SPARK_CONF_DIR环境变量的值取出来.即:${SPARK_HOME}/conf
."${SPARK_HOME}/sbin/spark-config.sh"
# Start Master 在这个节点中启动start-master.sh
"${SPARK_HOME}/sbin"/start-master.sh$TACHYON_STR
#Start Workers
"${SPARK_HOME}/sbin"/start-slaves.sh$TACHYON_STR
二、这是$SPARK_HOME/sbin/start-master.sh脚本
#!/usr/bin/envbash
#Starts the master on the machine this script is executed on.
# 使用这个脚本启动master
if [-z "${SPARK_HOME}" ]; then
export SPARK_HOME="$(cd "`dirname"$0"`"/..; pwd)"
fi
#NOTE: This exact class name is matched downstream by SparkSubmit.
#Any changes need to be reflected there.
#注意:该确切的类名与SparkSubmit的下游匹配。
CLASS="org.apache.spark.deploy.master.Master"
#判断语句里面可以用正则表达式,只能匹配以--help或-h结束的串
if[[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then
echo "Usage: ./sbin/start-master.sh[options]"
#环境变量可以使用+=进行拼串, 串中的\|表示或的意思,如果不想加\,又有或的意思,需要让grep -E
pattern="Usage:"
pattern+="\|Using Spark's default log4jprofile:"
pattern+="\|Registered signal handlersfor"
#请求spark-class将Master类传进去,然后调用--help进行帮助
#grep -v 表示过滤掉含有$pattern变量的串,然后退出
"${SPARK_HOME}"/bin/spark-class$CLASS --help 2>&1 | grep -v "$pattern" 1>&2
exit 1
fi
#将所有调用start-master.sh的参数给变量ORIGINAL_ARGS
ORIGINAL_ARGS="$@"
START_TACHYON=false
#这个while语句就是判断如果使用tachyon时,需在"${SPARK_HOME}"/tachyon/bin/tachyon有目录才可以
#$#表示参数个数 ,shift:像js数组一样,每调用一次会将第一个元素去掉
while(( "$#" )); do
case$1 in
--with-tachyon)
if [ ! -e"${SPARK_HOME}"/tachyon/bin/tachyon ]; then
echo "Error: --with-tachyon specified,but tachyon not found."
exit -1
fi
START_TACHYON=true
;;
esac
shift
done
#这个spark-config.sh作用就是将$SPARK_CONF_DIR环境变量的值取出来.即:${SPARK_HOME}/conf
."${SPARK_HOME}/sbin/spark-config.sh"
#该脚本会加载spark-env.sh加载一次。并设置环境变量SPARK_SCALA_VERSION=2.10及SPARK_ENV_LOADED=1
."${SPARK_HOME}/bin/load-spark-env.sh"
#如果SPARK_MASTER_PORT是空串则master的端口设置为7077,SPARK_MASTER_IP就是当前脚本的主机ip,webui的端口是8080
if ["$SPARK_MASTER_PORT" = "" ]; then
SPARK_MASTER_PORT=7077
fi
if ["$SPARK_MASTER_IP" = "" ]; then
SPARK_MASTER_IP=`hostname`
fi
if ["$SPARK_MASTER_WEBUI_PORT" = "" ]; then
SPARK_MASTER_WEBUI_PORT=8080
fi
#会调用sbin/spark-daemon.sh start org.apache.spark.deploy.master.Master 1luyl152 --port 7077 --webui-port 8080
"${SPARK_HOME}/sbin"/spark-daemon.shstart $CLASS 1 \
--ip $SPARK_MASTER_IP --port$SPARK_MASTER_PORT --webui-port $SPARK_MASTER_WEBUI_PORT \
$ORIGINAL_ARGS
if ["$START_TACHYON" == "true" ]; then
"${SPARK_HOME}"/tachyon/bin/tachyonbootstrap-conf $SPARK_MASTER_IP
"${SPARK_HOME}"/tachyon/bin/tachyonformat -s
"${SPARK_HOME}"/tachyon/bin/tachyon-start.sh master
fi
接下来分析一下$SPARK_HOME/sbin/spark-daemon.sh,看一下spark-daemon.sh是如何启动master