This is an automated email from the ASF dual-hosted git repository. szetszwo pushed a commit to branch branch-2_tmp in repository https://gitbox.apache.org/repos/asf/ratis.git
commit ceab6c6d6b264f53630e9e4df1aebe51dd7d806e Author: Yaolong Liu <[email protected]> AuthorDate: Fri Sep 9 16:55:54 2022 +0800 RATIS-1698. Add unit-test of listener related to leaderElection (#737) (cherry picked from commit 1f13165a64291f983c52447ae8a045c952808e36) --- .../ratis/server/impl/PeerConfiguration.java | 4 +++ .../ratis/server/impl/RaftConfigurationImpl.java | 4 +++ .../ratis/server/impl/LeaderElectionTests.java | 42 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java index 172081a6f..6730b6181 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java @@ -157,6 +157,10 @@ class PeerConfiguration { return num > size() / 2; } + int getMajorityCount() { + return size() / 2 + 1; + } + boolean majorityRejectVotes(Collection<RaftPeerId> rejected) { int num = size(); for (RaftPeerId other : rejected) { diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java index ee01f275c..43818395a 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfigurationImpl.java @@ -231,6 +231,10 @@ final class RaftConfigurationImpl implements RaftConfiguration { (oldConf == null || oldConf.hasMajority(others, selfId)); } + int getMajorityCount() { + return conf.getMajorityCount(); + } + /** @return true if the rejects are in the majority(maybe half is enough in some cases). */ boolean majorityRejectVotes(Collection<RaftPeerId> rejects) { return conf.majorityRejectVotes(rejects) || diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java index 9de568344..befcb95b1 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java @@ -129,6 +129,48 @@ public abstract class LeaderElectionTests<CLUSTER extends MiniRaftCluster> } } + @Test + public void testLeaderNotCountListenerForMajority() throws Exception { + runWithNewCluster(3, 2, this::runTestLeaderNotCountListenerForMajority); + } + + void runTestLeaderNotCountListenerForMajority(CLUSTER cluster) throws Exception { + final RaftServer.Division leader = waitForLeader(cluster); + Assert.assertEquals(2, ((RaftConfigurationImpl)cluster.getLeader().getRaftConf()).getMajorityCount()); + try (RaftClient client = cluster.createClient(leader.getId())) { + client.io().send(new RaftTestUtil.SimpleMessage("message")); + List<RaftPeer> listeners = cluster.getListeners() + .stream().map(RaftServer.Division::getPeer).collect(Collectors.toList()); + Assert.assertEquals(2, listeners.size()); + RaftClientReply reply = client.admin().setConfiguration(cluster.getPeers()); + Assert.assertTrue(reply.isSuccess()); + Collection<RaftPeer> peer = leader.getRaftConf().getAllPeers(RaftProtos.RaftPeerRole.LISTENER); + Assert.assertEquals(0, peer.size()); + } + Assert.assertEquals(3, ((RaftConfigurationImpl)cluster.getLeader().getRaftConf()).getMajorityCount()); + } + + @Test + public void testListenerNotStartLeaderElection() throws Exception { + runWithNewCluster(3, 2, this::runTestListenerNotStartLeaderElection); + } + + void runTestListenerNotStartLeaderElection(CLUSTER cluster) throws Exception { + final RaftServer.Division leader = waitForLeader(cluster); + final TimeDuration maxTimeout = RaftServerConfigKeys.Rpc.timeoutMax(getProperties()); + + final RaftServer.Division listener = cluster.getListeners().get(0); + final RaftPeerId listenerId = listener.getId(); + try { + isolate(cluster, listenerId); + maxTimeout.sleep(); + maxTimeout.sleep(); + Assert.assertEquals(RaftProtos.RaftPeerRole.LISTENER, listener.getInfo().getCurrentRole()); + } finally { + deIsolate(cluster, listener.getId()); + } + } + @Test public void testTransferLeader() throws Exception { try(final MiniRaftCluster cluster = newCluster(3)) {
