LebronAl commented on a change in pull request #1112:
URL: https://github.com/apache/incubator-iotdb/pull/1112#discussion_r417285003



##########
File path: 
cluster/src/main/java/org/apache/iotdb/cluster/server/member/RaftMember.java
##########
@@ -384,7 +385,89 @@ public void appendEntry(AppendEntryRequest request, 
AsyncMethodCallback resultHa
 
   @Override
   public void appendEntries(AppendEntriesRequest request, AsyncMethodCallback 
resultHandler) {
-    //TODO-Cluster#354: implement
+    logger.debug("{} received an AppendEntriesRequest", name);
+
+    // the term checked here is that of the leader, not that of the log
+    if (!checkRequestTerm(request, resultHandler)) {
+      return;
+    }
+
+    try {
+      long response = 0;
+      List<Log> logs = new ArrayList<>();
+      for (ByteBuffer buffer : request.getEntries()) {
+        Log log = LogParser.getINSTANCE().parse(buffer);
+        logs.add(log);
+      }
+
+      response = appendEntries(logs);
+      resultHandler.onComplete(response);
+      logger.debug("{} AppendEntriesRequest of log size {} completed", name,
+          request.getEntries().size());
+    } catch (UnknownLogTypeException e) {
+      resultHandler.onError(e);
+    }
+  }
+
+  /**
+   * Find the local previous log of "log". If such log is found, discard all 
local logs behind it
+   * and append "log" to it. Otherwise report a log mismatch.
+   *
+   * @param logs
+   * @return Response.RESPONSE_AGREE when the log is successfully appended or 
Response
+   * .RESPONSE_LOG_MISMATCH if the previous log of "log" is not found.
+   */
+  private long appendEntries(List<Log> logs) {

Review comment:
       Actually,I used to think that the interface raftLogManager exposed to 
followers to append logs was `maybeAppend`, and in that function we can handle 
this check, but now appendEntry's RPC doesn't have lastLogTerm, lastLogIndex, 
leaderCommit. So i have to use `append `.
   So now we can either add some fields to the appendEntry rpc to support the 
use of maybeAppend, or we can do this check inside `append`. What's your 
opinion?




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