Skip to content

Commit

Permalink
Merge pull request alibaba#361 from alibaba/hotfix_decide_leader_bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Fury Zhu authored Nov 28, 2018
2 parents ccea92e + 44cb206 commit 7974578
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,22 @@ public RaftPeer decideLeader(RaftPeer candidate) {
peers.put(candidate.ip, candidate);

SortedBag ips = new TreeBag();
int maxApproveCount = 0;
String maxApprovePeer = null;
for (RaftPeer peer : peers.values()) {
if (StringUtils.isEmpty(peer.voteFor)) {
continue;
}

ips.add(peer.voteFor);
if (ips.getCount(peer.voteFor) > maxApproveCount) {
maxApproveCount = ips.getCount(peer.voteFor);
maxApprovePeer = peer.voteFor;
}
}

String first = (String) ips.last();
if (ips.getCount(first) >= majorityCount()) {
RaftPeer peer = peers.get(first);
if (maxApproveCount >= majorityCount()) {
RaftPeer peer = peers.get(maxApprovePeer);
peer.state = RaftPeer.State.LEADER;

if (!Objects.equals(leader, peer)) {
Expand Down

0 comments on commit 7974578

Please sign in to comment.