Merge pull request #361 from alibaba/hotfix_decide_leader_bug

Fix #357
This commit is contained in:
Fury Zhu 2018-11-28 17:38:35 +08:00 committed by GitHub
commit 79745783e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -120,17 +120,22 @@ public class PeerSet {
peers.put(candidate.ip, candidate); peers.put(candidate.ip, candidate);
SortedBag ips = new TreeBag(); SortedBag ips = new TreeBag();
int maxApproveCount = 0;
String maxApprovePeer = null;
for (RaftPeer peer : peers.values()) { for (RaftPeer peer : peers.values()) {
if (StringUtils.isEmpty(peer.voteFor)) { if (StringUtils.isEmpty(peer.voteFor)) {
continue; continue;
} }
ips.add(peer.voteFor); ips.add(peer.voteFor);
if (ips.getCount(peer.voteFor) > maxApproveCount) {
maxApproveCount = ips.getCount(peer.voteFor);
maxApprovePeer = peer.voteFor;
}
} }
String first = (String) ips.last(); if (maxApproveCount >= majorityCount()) {
if (ips.getCount(first) >= majorityCount()) { RaftPeer peer = peers.get(maxApprovePeer);
RaftPeer peer = peers.get(first);
peer.state = RaftPeer.State.LEADER; peer.state = RaftPeer.State.LEADER;
if (!Objects.equals(leader, peer)) { if (!Objects.equals(leader, peer)) {