From 44cb206794b635985513bc4111f4172e27cbfb9d Mon Sep 17 00:00:00 2001 From: nkorange Date: Wed, 28 Nov 2018 15:47:21 +0800 Subject: [PATCH] Fix #357 --- .../java/com/alibaba/nacos/naming/raft/PeerSet.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java b/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java index b1b67a5c3..d9a36e7ff 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/raft/PeerSet.java @@ -120,17 +120,22 @@ public class PeerSet { 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)) {