You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by ru...@apache.org on 2020/11/04 05:28:01 UTC

[incubator-ratis] branch master updated: RATIS-1120. Terminate election in advance if majority peers reject (#241)

This is an automated email from the ASF dual-hosted git repository.

runzhiwang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f5fd11  RATIS-1120. Terminate election in advance if majority peers reject (#241)
9f5fd11 is described below

commit 9f5fd11aa6ed33a7f439e92a21fc0a78b4254b23
Author: dengziming <de...@gmail.com>
AuthorDate: Wed Nov 4 13:27:52 2020 +0800

    RATIS-1120. Terminate election in advance if majority peers reject (#241)
    
    Co-authored-by: dengziming <de...@growingio.com>
---
 .../apache/ratis/server/impl/LeaderElection.java   |  6 ++
 .../ratis/server/impl/PeerConfiguration.java       | 10 ++++
 .../ratis/server/impl/RaftConfiguration.java       |  6 ++
 .../ratis/server/impl/PeerConfigurationTest.java   | 68 ++++++++++++++++++++++
 4 files changed, 90 insertions(+)

diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
index 4796a75..5c023c6 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java
@@ -289,6 +289,7 @@ class LeaderElection implements Runnable {
     final List<Exception> exceptions = new ArrayList<>();
     int waitForNum = submitted;
     Collection<RaftPeerId> votedPeers = new ArrayList<>();
+    Collection<RaftPeerId> rejectedPeers = new ArrayList<>();
     Set<RaftPeerId> higherPriorityPeers = getHigherPriorityPeers(conf);
 
     while (waitForNum > 0 && shouldRun(electionTerm)) {
@@ -344,6 +345,11 @@ class LeaderElection implements Runnable {
           if (higherPriorityPeers.size() == 0 && conf.hasMajority(votedPeers, server.getId())) {
             return logAndReturn(Result.PASSED, responses, exceptions, -1);
           }
+        } else {
+          rejectedPeers.add(replierId);
+          if (conf.majorityRejectVotes(rejectedPeers)) {
+            return logAndReturn(Result.REJECTED, responses, exceptions, -1);
+          }
         }
       } catch(ExecutionException e) {
         LogUtils.infoOrTrace(LOG, () -> this + " got exception when requesting votes", e);
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 7215665..60cd33d 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
@@ -94,6 +94,16 @@ class PeerConfiguration {
     return num > size() / 2;
   }
 
+  boolean majorityRejectVotes(Collection<RaftPeerId> rejected) {
+    int num = size();
+    for (RaftPeerId other : rejected) {
+      if (contains(other)) {
+        num --;
+      }
+    }
+    return num <= size() / 2;
+  }
+
   @Override
   public boolean equals(Object obj) {
     if (this == obj) {
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
index a56bd5a..44080da 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
@@ -206,6 +206,12 @@ public final class RaftConfiguration {
         (oldConf == null || oldConf.hasMajority(others, selfId));
   }
 
+  /** @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) ||
+            (oldConf != null && oldConf.majorityRejectVotes(rejects));
+  }
+
   @Override
   public String toString() {
     return logEntryIndex + ": " + conf + ", old=" + oldConf;
diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/PeerConfigurationTest.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/PeerConfigurationTest.java
new file mode 100644
index 0000000..e40ad71
--- /dev/null
+++ b/ratis-server/src/test/java/org/apache/ratis/server/impl/PeerConfigurationTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.server.impl;
+
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.protocol.RaftPeerId;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class PeerConfigurationTest {
+
+  @Test
+  public void testOddNodesQuorum() {
+    String localId = "0";
+    String node1 = "1";
+    String node2 = "2";
+    PeerConfiguration conf = new PeerConfiguration(raftPeers(localId, node1, node2));
+    // in odd quorum, half + self is majority
+    assertTrue(conf.hasMajority(raftPeerIds(node1), RaftPeerId.valueOf(localId)));
+    // in odd quorum, majority is impossible after half rejected
+    assertFalse(conf.majorityRejectVotes(raftPeerIds(node1)));
+  }
+
+  @Test
+  public void testEvenNodeQuorum() {
+    String localId = "0";
+    String node1 = "1";
+    String node2 = "2";
+    String node3 = "3";
+    PeerConfiguration conf = new PeerConfiguration(raftPeers(localId, node1, node2, node3));
+    // in even quorum, half + self is majority
+    assertFalse(conf.hasMajority(raftPeerIds(node1), RaftPeerId.valueOf(localId)));
+    assertTrue(conf.hasMajority(raftPeerIds(node1, node2), RaftPeerId.valueOf(localId)));
+    // in even quorum, majority is impossible after half rejected
+    assertFalse(conf.majorityRejectVotes(raftPeerIds(node1)));
+    assertTrue(conf.majorityRejectVotes(raftPeerIds(node1, node2)));
+  }
+
+  private Collection<RaftPeer> raftPeers(String... voters) {
+    return Arrays.stream(voters).map(voter -> new RaftPeer(RaftPeerId.valueOf(voter))).collect(Collectors.toSet());
+  }
+
+  private Collection<RaftPeerId> raftPeerIds(String... voters) {
+    return Arrays.stream(voters).map(RaftPeerId::valueOf).collect(Collectors.toSet());
+  }
+
+}