spark 源代码分析 (二)spark启动过程

下载spark后,调用shell脚本启动:
./bin/spark-shell --master local[2]
接下来将分析这个一个过程。spark-shell
会启动 -- class org . apache . spark . repl . Main,
参数 -- name "Spark shell" "$@" ,然后会调用 spark - submit
   
   
  1. cygwin=false
  2. case "$(uname)" in
  3. CYGWIN*) cygwin=true;;
  4. esac
  5. # Enter posix mode for bash
  6. set -o posix
  7. if [ -z "${SPARK_HOME}" ]; then
  8. source "$(dirname "$0")"/find-spark-home
  9. fi
  10. export _SPARK_CMD_USAGE="Usage: ./bin/spark-shell [options]"
  11. # SPARK-4161: scala does not assume use of the java classpath,
  12. # so we need to add the "-Dscala.usejavacp=true" flag manually. We
  13. # do this specifically for the Spark shell because the scala REPL
  14. # has its own class loader, and any additional classpath specified
  15. # through spark.driver.extraClassPath is not automatically propagated.
  16. SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Dscala.usejavacp=true"
  17. function main() {
  18. if $cygwin; then
  19. # Workaround for issue involving JLine and Cygwin
  20. # (see http://sourceforge.net/p/jline/bugs/40/).
  21. # If you're using the Mintty terminal emulator in Cygwin, may need to set the
  22. # "Backspace sends ^H" setting in "Keys" section of the Mintty options
  23. # (see https://github.com/sbt/sbt/issues/562).
  24. stty -icanon min 1 -echo > /dev/null 2>&1
  25. export SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Djline.terminal=unix"
  26. ## 这里自定了启动
  27. "${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
  28. stty icanon echo > /dev/null 2>&1
  29. else
  30. export SPARK_SUBMIT_OPTS
  31. "${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
  32. fi
  33. }
  34. # Copy restore-TTY-on-exit functions from Scala script so spark-shell exits properly even in
  35. # binary distribution of Spark where Scala is not installed
  36. exit_status=127
  37. saved_stty=""
  38. # restore stty settings (echo in particular)
  39. function restoreSttySettings() {
  40. stty $saved_stty
  41. saved_stty=""
  42. }
  43. function onExit() {
  44. if [[ "$saved_stty" != "" ]]; then
  45. restoreSttySettings
  46. fi
  47. exit $exit_status
  48. }
  49. # to reenable echo if we are interrupted before completing.
  50. trap onExit INT
  51. # save terminal settings
  52. saved_stty=$(stty -g 2>/dev/null)
  53. # clear on error so we don't later try to restore them
  54. if [[ ! $? ]]; then
  55. saved_stty=""
  56. fi
  57. main "$@"
  58. # record the exit status lest it be overwritten:
  59. # then reenable echo and propagate the code.
  60. exit_status=$?
  61. onExit

spark-submit,这个简单,调用了 spark - class 参数 :org . apache . spark . deploy . SparkSubmit
   
   
  1. if [ -z "${SPARK_HOME}" ]; then
  2. source "$(dirname "$0")"/find-spark-home
  3. fi
  4. # disable randomized hash for string in Python 3.3+
  5. export PYTHONHASHSEED=0
  6. exec "${SPARK_HOME}"/bin/spark-class org.apache.spark.deploy.SparkSubmit "$@"

spark-class如下:这里主要加载java class 和jar包
   
   
  1. if [ -z "${SPARK_HOME}" ]; then
  2. source "$(dirname "$0")"/find-spark-home
  3. fi
  4. . "${SPARK_HOME}"/bin/load-spark-env.sh
  5. # Find the java binary
  6. if [ -n "${JAVA_HOME}" ]; then
  7. RUNNER="${JAVA_HOME}/bin/java"
  8. else
  9. if [ "$(command -v java)" ]; then
  10. RUNNER="java"
  11. else
  12. echo "JAVA_HOME is not set" >&2
  13. exit 1
  14. fi
  15. fi
  16. # Find Spark jars.
  17. if [ -d "${SPARK_HOME}/jars" ]; then
  18. SPARK_JARS_DIR="${SPARK_HOME}/jars"
  19. else
  20. SPARK_JARS_DIR="${SPARK_HOME}/assembly/target/scala-$SPARK_SCALA_VERSION/jars"
  21. fi
  22. if [ ! -d "$SPARK_JARS_DIR" ] && [ -z "$SPARK_TESTING$SPARK_SQL_TESTING" ]; then
  23. echo "Failed to find Spark jars directory ($SPARK_JARS_DIR)." 1>&2
  24. echo "You need to build Spark with the target \"package\" before running this program." 1>&2
  25. exit 1
  26. else
  27. LAUNCH_CLASSPATH="$SPARK_JARS_DIR/*"
  28. fi
  29. # Add the launcher build dir to the classpath if requested.
  30. if [ -n "$SPARK_PREPEND_CLASSES" ]; then
  31. LAUNCH_CLASSPATH="${SPARK_HOME}/launcher/target/scala-$SPARK_SCALA_VERSION/classes:$LAUNCH_CLASSPATH"
  32. fi
  33. # For tests
  34. if [[ -n "$SPARK_TESTING" ]]; then
  35. unset YARN_CONF_DIR
  36. unset HADOOP_CONF_DIR
  37. fi
  38. # The launcher library will print arguments separated by a NULL character, to allow arguments with
  39. # characters that would be otherwise interpreted by the shell. Read that in a while loop, populating
  40. # an array that will be used to exec the final command.
  41. #
  42. # The exit code of the launcher is appended to the output, so the parent shell removes it from the
  43. # command array and checks the value to see if the launcher succeeded.
  44. build_command() {
  45. "$RUNNER" -Xmx128m -cp "$LAUNCH_CLASSPATH" org.apache.spark.launcher.Main "$@"
  46. printf "%d\0" $?
  47. }
  48. # Turn off posix mode since it does not allow process substitution
  49. set +o posix
  50. CMD=()
  51. while IFS= read -d '' -r ARG; do
  52. CMD+=("$ARG")
  53. done < <(build_command "$@")
  54. COUNT=${#CMD[@]}
  55. LAST=$((COUNT - 1))
  56. LAUNCHER_EXIT_CODE=${CMD[$LAST]}
  57. # Certain JVM failures result in errors being printed to stdout (instead of stderr), which causes
  58. # the code that parses the output of the launcher to get confused. In those cases, check if the
  59. # exit code is an integer, and if it's not, handle it as a special error case.
  60. if ! [[ $LAUNCHER_EXIT_CODE =~ ^[0-9]+$ ]]; then
  61. echo "${CMD[@]}" | head -n-1 1>&2
  62. exit 1
  63. fi
  64. if [ $LAUNCHER_EXIT_CODE != 0 ]; then
  65. exit $LAUNCHER_EXIT_CODE
  66. fi
  67. CMD=("${CMD[@]:0:$LAST}")
  68. exec "${CMD[@]}"

所以最终启动了
org . apache . spark . repl . Main
org.apache.spark.deploy.SparkSubmit
org . apache . spark . launcher . Main


















  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值