一、Master 配置 recoveryMode
Master 作为 Spark Standalone 模式中的核心,如果 Master 出现异常,则整个集群的运行情况和资源将无法进行管理,整个集群将处于“群龙无首”的状况。
Spark 在设计时考虑了这种情况,在集群运行的时候,可以启动一个或多个 Standby Master,当 Master 出现异常的时候,Standby Master 将根据一定规则确定其中一个接管 Master。
Standalone 模式中,当 Master 出现异常,Spark 有四种恢复策略,可以在配置文件 Spark-env.sh 配置项 spark.deploy.recoveryMode 进行设置,默认为 NONE。
在 Master 中,也存在如下代码:
private val RECOVERY_MODE = conf.get("spark.deploy.recoveryMode", "NONE")
根据 spark.deploy.recoveryMode 的配置,Master 在启动后会初始化不同的 PersistenceEngine 和 LeaderElectionAgent 用于选举新的 Master 并恢复集群状态。
Master # onStart:
val (persistenceEngine_, leaderElectionAgent_) = RECOVERY_MODE match {
case "ZOOKEEPER" =>
logInfo("Persisting recovery state to ZooKeeper")
val zkFactory =
new ZooKeeperRecoveryModeFactory(conf, serializer)
(zkFactory.createPersistenceEngine(), zkFactory.createLeaderElectionAgent(this))
case "FILESYSTEM" =>
val fsFactory =
new FileSystemRecoveryModeFactory(conf, serializer)
(fsFactory.createPersistenceEngine(), fsFactory.createLeaderElectionAgent(this))
case "CUSTOM" =>
val clazz = Utils.classForName(conf.get("spark.deploy.recoveryMode.factory"))
val factory = clazz.getConstructor(classOf[SparkConf], classOf[Serializer])
.newInstance(conf, serializer)
.asInstanceOf[StandaloneRecoveryModeFactory]
(factory.createPersistenceEngine(), factory.