安装spark的流程就不说了。本篇讲述spark-shell
log level
在$SPARK_HOME/conf目录中,有log4j.properties文件(如果没有 执行cp log4j.properties.template log4j.properties
). spark-shell默认日志级别为WARN,修改log4j.properties如下,以打印更详细的信息
1 | # Set the default spark-shell log level to WARN. When running the spark-shell, the |
run workCount on spark-shell
执行$SPARK_HOME/bin/spark-shell,结果如下
1 | (base) joker:spark xmly$ bin/spark-shell |
从启动日志,可看到SparkContext、SecurityManager、SparkEnv、BlockManagerMasterEndpoint、DiskBlockManager、MemoryStore、SparkUI、Executor、NettyBlockTransferService、BlockManager、等
以下执行workCount
1 | scala> :paste |
可以看到reducyByKey算子将MapPartitionsRDD
转变成ShuffledRDD
通过toDebugString可以看到更详细的RDD转换过程
1 | counts.toDebugString |
执行action操作
1 | counts.collect() |
SparkContext开启job,id是0。
DAGScheduler划分、提交两个stage。第一个stage为ShuffleMapStage,id为0;第二个stage为ResultStage,id为1
每个stage都有两个task,因为2 output partitions
(line 4)
TaskSchedulerImpl添加task到task set,Executor执行task
analyse spark-shell
part of spark-shell
1 | function main() { |
spark-shell执行了spark-submit
脚本
spark-submit
1 | if [ -z "${SPARK_HOME}" ]; then |
执行spark-class
part of spark-class
1 | # Find the java binary |
spark启动以SparkSubmit
为主类的JVM进程
jmx监控
在spark-shell中找到如下配置:
1 | SPARK-4161: scala does not assume use of the java classpath, |
修改为
1 | SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Dscala.usejavacp=true -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false" |
启动jvisualvm
,打开本地或全程JMX的org.apache.spark.deploy.SparkSubmit
进程,点击”线程”item,点击”main”线程,然后点击”线程Dump“,会dump线程,拖到最下面
1 | "main" #1 prio=5 os_prio=31 tid=0x00007f9202000800 nid=0x2503 runnable [0x0000700002997000] |
可以看出函数调用顺序:
SparkSubmit.main —> Main.main —> ILoop.process