Well,不要刷屏了

ActiveMasters的疑问

Elasticsearch | 作者 neko_z | 发布于2021年05月21日 | 阅读数:1127

/*
* 找到当前集群的 master
*/
List<DiscoveryNode> activeMasters = new ArrayList<>();
for (ZenPing.PingResponse pingResponse : pingResponses) {
// We can't include the local node in pingMasters list, otherwise we may up electing ourselves without
// any check / verifications from other nodes in ZenDiscover#innerJoinCluster()
if (pingResponse.master() != null && !localNode.equals(pingResponse.master())) {
activeMasters.add(pingResponse.master());
}
}

/*
* 当前集群没有 master
*/
if (activeMasters.isEmpty()) {
/*
* 当前节点与超过 minimum candidate 个节点保持联系
* 从候选节点中选出 master
*/
if (electMaster.hasEnoughCandidates(masterCandidates)) {
final ElectMasterService.MasterCandidate winner = electMaster.electMaster(masterCandidates);
logger.trace("candidate {} won election", winner);
return winner.getNode();
} else {

/*
* 无法选出 master 返回 null
*/
// if we don't have enough master nodes, we bail, because there are not enough master to elect from
logger.warn("not enough master nodes discovered during pinging (found [{}], but needed [{}]), pinging again",
masterCandidates, electMaster.minimumMasterNodes());
return null;
}
} else {
assert !activeMasters.contains(localNode) : "local node should never be elected as master when other nodes indicate an active master";
// lets tie break between discovered nodes
/*
* 集群存在多个 master 节点, 选择 id 较小的当做 master
*/
return electMaster.tieBreakActiveMasters(activeMasters);
}

正常情况下,activeMasters 这个属性要么是0个,要么是1个,可是这里为什么这里定义的是个List,它在哪些情况下可能出现多个?
已邀请:

Charele - Cisco4321

赞同来自:

 List<DiscoveryNode> activeMasters = new ArrayList<>();
它是一个List(不是Set),有多个节点报告它们各自的master的话,
这里面有多个。
 
虽然正常情况下,这些master应该是同一个,但比一下也不会出错的。
这是个人看法,可能还有其它原因。
 
另外,这种ZenDiscovery的选举方式在ES7后已经弃用了

要回复问题请先登录注册