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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0e38073  RATIS-1420. Improve the appendEntity with inconsistency 
entities case (#523)
0e38073 is described below

commit 0e380739f9eeb240c4ad2cf4ba5264cf03cf4b05
Author: Sammi Chen <[email protected]>
AuthorDate: Wed Nov 3 21:50:25 2021 +0800

    RATIS-1420. Improve the appendEntity with inconsistency entities case (#523)
---
 .../org/apache/ratis/server/impl/RaftServerImpl.java | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
index 62a0676..11d13d5 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
@@ -1297,22 +1297,18 @@ class RaftServerImpl implements RaftServer.Division,
       return state.getNextIndex();
     }
 
-    // Check that the first log entry is greater than the snapshot index in 
the latest snapshot.
-    // If not, reply to the leader the new next index.
+    // Check that the first log entry is greater than the snapshot index in 
the latest snapshot and follower's last
+    // committed index. If not, reply to the leader the new next index.
     if (entries != null && entries.length > 0) {
       final long firstEntryIndex = entries[0].getIndex();
       final long snapshotIndex = state.getSnapshotIndex();
-      if (snapshotIndex > 0 && snapshotIndex >= firstEntryIndex) {
-        LOG.info("{}: Failed appendEntries: the first entry (index {}) is 
already in snapshot (snapshot index: {})",
-            getMemberId(), firstEntryIndex, snapshotIndex);
-        return snapshotIndex + 1;
-      }
-
       final long commitIndex =  state.getLog().getLastCommittedIndex();
-      if (commitIndex > 0 && commitIndex >= firstEntryIndex) {
-        LOG.info("{}: Failed appendEntries: the first entry (index {}) is 
already committed (commit index: {})",
-            getMemberId(), firstEntryIndex, commitIndex);
-        return commitIndex + 1;
+      final long nextIndex = Math.max(snapshotIndex, commitIndex);
+      if (nextIndex > 0 && nextIndex >= firstEntryIndex) {
+        LOG.info("{}: Failed appendEntries as the first entry (index {})" +
+                " already exists (snapshotIndex: {}, commitIndex: {})",
+            getMemberId(), firstEntryIndex, snapshotIndex, commitIndex);
+        return nextIndex + 1;
       }
     }
 

Reply via email to