CLOVER🍀

That was when it all began.

Apache Spark(スタンドアロンモード)のマスターノード、ワーカーノードをフォアグラウンドで起動する

これは、なにをしたくて書いたもの?

スタンドアロンモードのApache Sparkのマスターノード、ワーカーノードをふつうに起動するとバックグラウンドに
行ってしまうのですが。

これをフォアグラウンドで実行する方法はないのかな?と思いまして。

結果からいくと、SPARK_NO_DAEMONIZEという環境変数で制御できるようです。

環境

今回の環境は、こちらです。

$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)


$ python3 -V
Python 3.8.10

Apache Sparkは、3.1.2を使用します。

$ bin/pyspark --version
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:/opt/spark/jars/spark-unsafe_2.12-3.1.2.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of org.apache.spark.unsafe.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 3.1.2
      /_/
                        
Using Scala version 2.12.10, OpenJDK 64-Bit Server VM, 11.0.11
Branch HEAD
Compiled by user centos on 2021-05-24T04:27:48Z
Revision de351e30a90dd988b133b3d00fa6218bfcaba8b8
Url https://github.com/apache/spark
Type --help for more information.

SPARK_NO_DAEMONIZE環境変数

SPARK_NO_DAEMONIZEという環境変数を使うことで、今回のお題をクリアすることができます。

ドキュメントには特に記載がないようですが、sbin/start-master.shsbin/start-worker.shが内部で使用している
sbin/spark-daemon.shに記述があります。

https://github.com/apache/spark/blob/v3.1.2/sbin/spark-daemon.sh#L31

# Runs a Spark command as a daemon.
#
# Environment Variables
#
#   SPARK_CONF_DIR  Alternate conf dir. Default is ${SPARK_HOME}/conf.
#   SPARK_LOG_DIR   Where log files are stored. ${SPARK_HOME}/logs by default.
#   SPARK_LOG_MAX_FILES Max log files of Spark daemons can rotate to. Default is 5.
#   SPARK_MASTER    host:path where spark code should be rsync'd from
#   SPARK_PID_DIR   The pid files are stored. /tmp by default.
#   SPARK_IDENT_STRING   A string representing this instance of spark. $USER by default
#   SPARK_NICENESS The scheduling priority for daemons. Defaults to 0.
#   SPARK_NO_DAEMONIZE   If set, will run the proposed command in the foreground. It will not output a PID file.
##

こちらですね。値を設定しておくと、フォアグラウンドで起動してくれるようです。

SPARK_NO_DAEMONIZE If set, will run the proposed command in the foreground. It will not output a PID file.

試してみましょう。

まずはSPARK_NO_DAEMONIZE環境変数を設定して、マスターノードを起動。

$ SPARK_NO_DAEMONIZE=1 sbin/start-master.sh

すると、フォアグラウンドでマスターノードが起動しました。

starting org.apache.spark.deploy.master.Master, logging to /opt/spark/logs/spark--org.apache.spark.deploy.master.Master-1-master.out
Spark Command: /opt/java/openjdk/bin/java -cp /opt/spark/conf/:/opt/spark/jars/* -Xmx1g org.apache.spark.deploy.master.Master --host master --port 7077 --webui-port 8080
========================================
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
21/09/11 15:38:46 INFO Master: Started daemon with process name: 37@master
21/09/11 15:38:46 INFO SignalUtils: Registering signal handler for TERM
21/09/11 15:38:46 INFO SignalUtils: Registering signal handler for HUP
21/09/11 15:38:46 INFO SignalUtils: Registering signal handler for INT

〜省略〜

ワーカーノードも起動してみます。こちらも、SPARK_NO_DAEMONIZE環境変数を設定。

$ SPARK_NO_DAEMONIZE=1 sbin/start-worker.sh spark://[マスターノードのIPアドレス]:7077

こちらも、フォアグラウンドで起動しました。

starting org.apache.spark.deploy.worker.Worker, logging to /opt/spark/logs/spark--org.apache.spark.deploy.worker.Worker-1-worker1.out
Spark Command: /opt/java/openjdk/bin/java -cp /opt/spark/conf/:/opt/spark/jars/* -Xmx1g org.apache.spark.deploy.worker.Worker --webui-port 8081 spark://172.17.0.2:7077
========================================
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
21/09/11 15:40:23 INFO Worker: Started daemon with process name: 19@worker1
21/09/11 15:40:23 INFO SignalUtils: Registering signal handler for TERM
21/09/11 15:40:23 INFO SignalUtils: Registering signal handler for HUP
21/09/11 15:40:23 INFO SignalUtils: Registering signal handler for INT

〜省略〜

知っておくと、Dockerコンテナ化する時などに便利かなと思います。