谈elastic search的select master service-betway官网手机版_必威|最新网址

本文首要研究一下ela未删减版sticsearch的ElectMasterService

ElectMasterService

elasticsearch-7.0.1/server/src/main海贼王之冰帝来临/java/org/elasticsearch/discovery/zen/ElectMasterService.java

public class ElectMasterService {

private static final Logger logger = LogManager.getLogger(ElectMasterService.class);

public static final Setting DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING =
Setting.intSetting("discovery.zen.minimum_master_nodes", -1, Property.Dynamic, Property.NodeScope, Property.Deprecated);

private volatile int minimumMasterNodes;

/**
* a class to encapsulate all the informat王冬儿ion about a candidate in a master election
* that is needed to decided which of the candidates should win
*/
public static class MasterCandidate {

public static final long UNRECOVERED_CLUSTER_VERSION = -1;

final DiscoveryNode node;

final long clusterStateVersion;

public MasterCandidate(DiscoveryNode node, long clusterStateVersion) {
Objects.requireNonNull(node);
assert clusterStateVersion >= -1 : "got: " + clusterStateVersion;
ass谈elastic search的select master service-betway官网手机版_必威|最新网址ert node.isMasterNode();
this.node = node;
this.clusterStateVersion = clusterStateVersion;
}

public DiscoveryNode getNode() {
return node;
}

public long getClusterStateVersion() {
return clusterStateVersion;
}

@Override
public String toString() {
return "Candidate{" +
"node=" + node +
", clusterStateVersion=" + clusterStateVersion +
'}';
}

/**
* compares two candidates to indicate which the a better master.
* A higher cluster state version is better
*
* @return -1 if c1 is a batter candidate, 1 if c2.
*/
public static int compare(MasterCandidate c1, MasterCandidate c2) {
// we explicitly swap c1 and c2 here. the code expects "better" is lower in a sorted
// list, so if c烦2 has a higher cluster state version, it needs to come first.
int ret = Long.compare(c2.clusterStateVersion, c1.clusterStateVersion);
if (ret == 0) {
ret = compareNodes(c1.getNode(), c2.getNode());
}
return ret;
}
}

public ElectMasterService(Settings settings) {
this.minimumMasterNodes = DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.get(settings);
logger.debug("using minimum_master_no谈elastic search的select master service-betway官网手机版_必威|最新网址des [{}巴黎贝甜]", minimumMasterNodes);
}

public void minimumMasterNodes(int minimumMasterNodes) {
this.minimumMasterNodes = minimumMasterNodes;
}

public int minimumMasterNodes() {
return minimumMasterNodes;
}

public int countMasterNodes(Iterab夕紫荷le nodes) {
int count = 0;
for (DiscoveryNode node : nodes) {
if (node.isMasterNode()) {
count++;
}
}
return count;
}

public boolean hasEnoughCandidates(Collection candidates) {
if (candidates.isEmpty()) {
return false;
}
if (minimumMasterNodes < 1) {
return true;
}
assert candidates.stream().map(MasterCandidate::getNode).collect(Collectors.toSet()).size() == candidates.size() :
"duplicates ahead: " + candidates;
return candidates.size() >= minimumMasterNodes;
}

/**
* Elects a new master out of the possible nodes, returning it. Returns {@code null}
* if no master has been elected.
*/
public MasterCandidate electMaster(Collection candidates) {
assert hasEnoughCandidates(candidates);
List sortedCandidates = new ArrayList<>(candidates);
sortedCandidates.sort(MasterCandidate::compare);
return sortedCandidates.get(0);
}

/** selects the best active master to join, where multiple are discovered */
public DiscoveryNode tieBreakActiveMasters(Collection activeMasters) {
return activeMasters.stream().min(ElectMasterService::compareNodes).get();readbook注册码
}

public boolean hasEnoughMasterNodes(Iterable nodes) {
final int count = countMasterNodes(nodes);
return count > 0 && (minimumMasterNodes < 0 || count >= minimumMasterNodes);
}

public boolean hasTooManyMasterNodes(Iterable nodes) {
final int count = countMasterNodes(nodes);
return coun二胎方针t > 1 && minimumMasterNodes <= count / 2;
}

public void logMinimumMasterNodesWarningIfNecessary(ClusterState oldState, ClusterState newState) {
// check if min_master_nodes setting is too low and log warning
if (hasTooManyMasterNodes(oldState.nodes()) == false && hasTooManyMasterNodes(newState.nodes())) {
logger.warn("value for setting \"{}\" is toagnoyo low. This can result in data loss! Please set it to at least a quorum of master-" +
"eligible nodes (current value: [{}], tot公主游戏al number of master-eligible nodes used for publishing in this round: [{}])",
ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), minimumMasterNodes(),
newState.getNodes().getMasterNodes().size());
}
}

/**
* Returns the given nodes sorted by likelihood of being elected as master, most likely first.
* Non-master nodes are not removed but are rather put in the end
*/
static List sortByMasterLikelihood(Iterable nodes) {
ArrayList sortedNodes = CollectionUtils.it小狮子erableAsArrayList(nodes);
CollectionUtil.introSort(sortedNodes, ElectMasterService::compareNodes);
return sortedNodes;
}

/**
* Returns a lis深圳区号t of the next possible masters.
问琴完整版*/
public DiscoveryNode[] nextPossibleMasters(ObjectContainer nodes, int numberOfPossibleMasters) {
List sortedNodes = sortedMasterNodes(Arrays.asList(nodes.toArray(DiscoveryNode.class)));
if (sortedNodes == null) {
return new DiscoveryNode[0];
}
List nextPossibleMasters = new ArrayList<>(numberOfPossibleMasters);
int counter = 0;
for (DiscoveryNode nextPossibleMaster : sortedNodes) {
if (++counter >= numberOfPossibleMasters) {
break;
}
nextPossibleMasters.add(nextPossibleMaster);
}
return nextPossibleMasters.toArray(new DiscoveryNode[nextPossibleMasters.size()]);
}

private List sortedMasterNodes(Iter女生啪啪able nodes) {
List possibleNodes = CollectionUtils.iterableAsArrayList(nodes);
if (possibleNodes.isEmpty()) {
return null;
}
// clean non master nodes
possibleNodes.removeIf(node -> !node.isMasterNode());
CollectionUtil.introSort(possibleNodes, ElectMasterService::compareNodes);
return possibleNodes;
}

/** master nodes go before other nodes, with a secondary sort by id **/
private static int compareNodes(DiscoveryNode o1, DiscoveryNode o2) {
if (o1.isMasterNode() && !o2.isMasterNode()) {
return -1;
}
if (!o1.isMasterNode() && o2.isMasterNode()) {
return 1;
}
return o1.getId().c谈elastic search的select master service-betway官网手机版_必威|最新网址ompareTo(o2.g股海泛舟网易博客etId());
}
}
  • ElectMasterService的结构器读取discovery.zen.minimum_master_nodes装备到变量moldinimumMasterNodes
  • ElectMasterService界说了静态类MasterCandidate,它供给了compare静态办法用于比较两个MasterCandidate,它首要比照clusterStateVersion,假如该值相同再进行compareNodes,compareNodes会先判别下是否masterNode,都不是则比照他们各自的node的id
  • e谈elastic search的select master service-betway官网手机版_必威|最新网址lectMaster办法首要经过hasEnoughCandidates来确认是否有满足的candidates,满足的话则对他们进行排序,最终取第一个作为master回来

ZenDiscovery.findMaster

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java

public class ZenDiscovery extends AbstractLifecycleComponent implements Discovery, PingContextProvider, IncomingClusterStateListener {
//......

private DiscoveryNode fi审计ndMaster() {
logger.trace("starting to ping");
List fullPingResponses = pingAndWait(pingTimeout).toList();
if (fullPingResponses == null) {
logger.trace("No full ping responses");
return null;
}
if (logger.isTraceEnabled()) {
StringBuilder sb = new StringBuilder();
if (fullPingResponses.size()抢票 == 0) {
sb.append(" {none}");
} else {
for (ZenPing.PingResponse pingResponse : fullPingResponses) {
sb.append("\n\t--> ").append(pingResponse);
}
}
logger.trace("full ping responses:{}", sb);
}

final DiscoveryNode localNode = transportService.getLocalNode();

// add our selves
assert fullPingResponses.stream().map(ZenPing.PingResponse::node)
.filter(n -> n.equals(localNode)).findAny().isPresent() == false;

fullPingResponses.add(new ZenPing.PingResponse(localNode, null, this.clusterState())爱上姐夫);

// filter responses
final List pingResponses = filterPingResponses(fullPingResponses, masterElectionIgnoreNonMasters, logger);

List 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())) {
ac谈elastic search的select master service-betway官网手机版_必威|最新网址tiveMasters.add(pingResponse.master());
}
}

// nodes discovered during pinging
List masterCandidates = new ArrayList<>();
for (ZenPing.PingResponse pingResponse : pingResponses) {
if (pingResponse.node().isMasterNode()) {
masterCandidates.add(new ElectMasterService.MasterCandidate(pingResponse.node(), pingResponse.getClusterStateVersion()));
}
}

if (activeMasters.isEmpty()) {
if (electMaster.hasEnoughCandidates(masterCandidates)) {
final ElectMasterService.MasterCandidate winner = electMaster.electMaster(masterCandidates);
logger.trace("candidate {} won election", winner);
return winner.getNode();
} else {
// 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谈elastic search的select master service-betway官网手机版_必威|最新网址 [{}]), 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
return electMaster.tieBreakActiveMasters(activeMasters);
}
}

//......
}
  • ZenDiscovery的findMaster办法在activeMasters.isEmpty()时会经过electMaster.electMaster(masterCandidates)来选取winner作为master回来

小结

  • ElectMas谈elastic search的select master service-betway官网手机版_必威|最新网址terService的结构器读取discovery.zen.minimum_master_nodes装备到变量minimumMasterNodes;ElectMasterService界说了静态类MasterCandidate,它供给了compare静态办法用于比较两个MasterCandidate,它首要比照clusterStateVersion,假如该值相同再进行compareNodes,compareNodes会先判别下是否masterNode,都不是则比照他们各自的node的id
  • electMaster办法首要经过hasEnoughCandidates来确认是否有满足的candidates,满足的话则对他们进行排序,最终取第一个作为master回来
  • ZenDiscovery的findMaster办法在activeMasters.isEmpty()时会经过electMaster.electMaster(masterCandidates)来选取winner作为master回来

doc

  • Important discovery and cluster formation settings

评论(0)