runzhiwang commented on a change in pull request #184:
URL: https://github.com/apache/incubator-ratis/pull/184#discussion_r479712633



##########
File path: 
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderState.java
##########
@@ -794,12 +795,58 @@ private void replicateNewConf() {
     return lists;
   }
 
+  private void yieldLeaderToHigherPriorityPeer() {
+    if (!server.getRole().isLeader()) {
+      return;
+    }
+
+    final RaftConfiguration conf = server.getRaftConf();
+    int leaderPriority = conf.getPeer(server.getId()).getPriority();
+
+    TermIndex leaderLastEntry = server.getState().getLastEntry();
+
+    for (LogAppender logAppender : senders.getSenders()) {
+      FollowerInfo followerInfo = logAppender.getFollower();
+      RaftPeerId followerID = followerInfo.getPeer().getId();
+      int followerPriority = conf.getPeer(followerID).getPriority();
+
+      if (followerPriority <= leaderPriority) {
+        continue;
+      }
+
+      if (leaderLastEntry == null) {
+        LOG.info("{} stepDown leadership on term:{} because follower's 
priority:{} is higher than leader's:{} " +
+                "and leader's lastEntry is null",
+            this, currentTerm, followerPriority, leaderPriority);
+
+        // step down as follower
+        stepDown(currentTerm);
+        return;
+      }
+
+      if (followerInfo.getMatchIndex() >= leaderLastEntry.getIndex()) {
+        LOG.info("{} stepDown leadership on term:{} because follower's 
priority:{} is higher than leader's:{} " +
+                "and follower's lastEntry index:{} catch up with leader's:{}",
+            this, currentTerm, followerPriority, leaderPriority, 
followerInfo.getMatchIndex(),
+            leaderLastEntry.getIndex());
+
+        // step down as follower
+        stepDown(currentTerm);

Review comment:
       @szetszwo Thanks for the advice, I have updated the PR.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to