szetszwo commented on a change in pull request #184:
URL: https://github.com/apache/incubator-ratis/pull/184#discussion_r479615443
##########
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:
There may be a race condition -- when stepping down, the lastEntry may
change since the code is not synchronized. However, "synchronized" is not a
good idea since it will slow down the leader.
How about we add a new method, say stepDown(lastEntry), which is
synchronized and also check if the given lastEntry matched the log's last entry?
----------------------------------------------------------------
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]