kaijchen commented on code in PR #811:
URL: https://github.com/apache/ratis/pull/811#discussion_r1080855106


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -1024,49 +1057,15 @@ private long[] getSorted(List<FollowerInfo> 
followerInfos, boolean includeSelf,
     return indices;
   }
 
-  private void yieldLeaderToHigherPriorityPeer() {
-    if (!server.getInfo().isLeader()) {
-      return;
-    }
-
+  private void checkPeersForYieldingLeader() {
     final RaftConfigurationImpl conf = server.getRaftConf();
     final RaftPeer leader = conf.getPeer(server.getId());
     if (leader == null) {
       LOG.error("{} the leader {} is not in the conf {}", this, 
server.getId(), conf);
       return;
     }
-    int leaderPriority = leader.getPriority();
-
     for (LogAppender logAppender : senders.getSenders()) {
-      final FollowerInfo followerInfo = logAppender.getFollower();
-      final RaftPeerId followerID = followerInfo.getPeer().getId();
-      final RaftPeer follower = conf.getPeer(followerID);
-      if (follower == null) {
-        if (conf.getPeer(followerID, RaftPeerRole.LISTENER) == null) {
-          LOG.error("{} the follower {} is not in the conf {}", this, 
followerID, conf);
-        }
-        continue;
-      }
-      final int followerPriority = follower.getPriority();
-      if (followerPriority <= leaderPriority) {
-        continue;
-      }
-      final TermIndex leaderLastEntry = server.getState().getLastEntry();
-      if (leaderLastEntry == null) {
-        LOG.info("{} send StartLeaderElectionRequest to follower:{} on term:{} 
because follower's priority:{} " +
-                "is higher than leader's:{} and leader's lastEntry is null",
-            this, followerID, currentTerm, followerPriority, leaderPriority);
-
-        sendStartLeaderElectionToHigherPriorityPeer(followerID, null);
-        return;
-      }
-
-      if (followerInfo.getMatchIndex() >= leaderLastEntry.getIndex()) {
-        LOG.info("{} send StartLeaderElectionRequest to follower:{} on term:{} 
because follower's priority:{} " +
-                "is higher than leader's:{} and follower's lastEntry index:{} 
catch up with leader's:{}",
-            this, followerID, currentTerm, followerPriority, leaderPriority, 
followerInfo.getMatchIndex(),
-            leaderLastEntry.getIndex());
-        sendStartLeaderElectionToHigherPriorityPeer(followerID, 
leaderLastEntry);
+      if (sendStartLeaderElection(logAppender.getFollower(), 
leader.getPriority())) {

Review Comment:
   How about find the peer with max priority first, and then send 
StartLeaderElection.
   Current code will find the first peer whose priority is higher than the 
leader, which may leads to unnecessary transfers.
   
   For example:
   
   | Node | Priority |
   | --- | --- |
   | Leader | 0 |
   | Follower 1 | 1 |
   | Follower 2 | 2 |
   
   Leader should skip follower 1, and send StartLeaderElection to follower 2.



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -685,6 +685,40 @@ private synchronized void 
sendStartLeaderElectionToHigherPriorityPeer(RaftPeerId
     });
   }
 
+  boolean sendStartLeaderElection(FollowerInfo followerInfo, int 
leaderPriority) {

Review Comment:
   This abstraction is quite hard to be reused in TransferLeadership.
   Because TransferLeadershipRequest contains `RaftPeerId transferee` instead 
of `FollowerInfo`. And `leaderPriority` sounds like an internal state of 
`LeaderStateImpl`.
   
   Also, I think it's probably better to use another name instead of overload 
`sendStartLeaderElection`, because it's doing something else. And it may not 
send StartLeaderElection if conditions are not met.



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -720,9 +754,8 @@ public void run() {
               event.execute();
             } else if (inStagingState()) {
               checkStaging();
-            } else {
-              yieldLeaderToHigherPriorityPeer();
-              checkLeadership();
+            } else if (checkLeadership()) {
+              checkPeersForYieldingLeader();

Review Comment:
   Good job.



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to