概述
ResourceManager#serviceInit()方法
1、判断是否启动HA。如果yarn.resourcemanager.ha.enabled配置参数为true,则为启动HA。
2、如果启动HA,判断是否启用自动失败重启。如果yarn.resourcemanager.ha.automatic-failover.enabled配置参数为true,则为启动自动失败重启。如果启用自动失败重启,创建EmbeddedElector。
EmbeddedElector有2种类型:
- CuratorBasedElectorService
- ActiveStandbyElectorBasedElectorService。
如果yarn.resourcemanager.ha.curator-leader-elector.enabled参数为true,则EmbeddedElector类型为CuratorBasedElectorService,该参数默认为false。
CuratorBasedElectorService实现了curator框架的LeaderLatchListener接口。
curator框架的LeaderLatch封装了zk的主从选举,在当前进程当选为leader节点时,LeaderLatch会回调LeaderLatchListener的isLeader方法。
isLeader方法是RM在当选为leader节点后的执行逻辑。在RM当选为leader节点后,将会启动RMActiveServices。
// Set HA configuration should be done before login
this.rmContext.setHAEnabled(HAUtil.isHAEnabled(this.conf));
if (this.rmContext.isHAEnabled()) {
HAUtil.verifyAndSetConfiguration(this.conf);
}
// elector must be added post adminservice
if (this.rmContext.isHAEnabled()) {
// If the RM is configured to use an embedded leader elector,
// initialize the leader elector.
if (HAUtil.isAutomaticFailoverEnabled(conf)
&& HAUtil.isAutomaticFailoverEmbedded(conf)) {
EmbeddedElector elector = createEmbeddedElector();
addIfService(elector);
rmContext.setLeaderElectorService(elector);
}
}
RM基于zk的主从选举过程
CuratorBasedElectorService启动leaderLatch
private void initAndStartLeaderLatch() throws Exception {
leaderLatch = new LeaderLatch(curator, latchPath, rmId);
leaderLatch.addListener(this);
leaderLatch.start();
}
leaderLatch竞选leader节点,并当选为leader节点时回调LeaderLatchListener的isLeader方法
略,请参阅