spark-core_05: $SPARK_HOME/sbin/start-all.sh、start-master.sh腳本分析:

一、這是$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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章