SzyWilliam commented on code in PR #735:
URL: https://github.com/apache/ratis/pull/735#discussion_r963022576


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -907,6 +910,43 @@ private CompletableFuture<RaftClientReply> 
staleReadAsync(RaftClientRequest requ
     return processQueryFuture(stateMachine.queryStale(request.getMessage(), 
minIndex), request);
   }
 
+  ReadRequests getReadRequests() {
+    return getState().getReadRequests();
+  }
+
+  private CompletableFuture<RaftClientReply> readAsync(RaftClientRequest 
request) {
+    if (readOption == RaftServerConfigKeys.Read.Option.LINEARIZABLE) {
+      /*
+        Linearizable read using ReadIndex. See Raft paper section 6.4.
+        1. First obtain readIndex from Leader.
+        2. Then waits for statemachine to advance at least as far as readIndex.
+        3. Finally, query the statemachine and return the result.
+       */
+      final LeaderStateImpl leader = role.getLeaderState().orElse(null);
+      // TODO support follower linearizable read
+      if (leader == null) {
+        return JavaUtils.completeExceptionally(generateNotLeaderException());
+      }
+      return leader.getReadIndex()
+          .thenCompose(readIndex -> getReadRequests().waitToAdvance(readIndex))
+          .thenCompose(readIndex -> queryStateMachine(request))
+          .exceptionally(e -> readOnlyException2Reply(request, e));
+    }
+    // default
+    return queryStateMachine(request);

Review Comment:
   done



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