szetszwo commented on code in PR #730:
URL: https://github.com/apache/ratis/pull/730#discussion_r2083250927


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -1028,6 +1064,56 @@ public boolean checkLeadership() {
     return false;
   }
 
+  /**
+   * Obtain the current readIndex for read only requests. See Raft paper 
section 6.4.
+   * 1. Leader makes sure at least one log from current term is committed.
+   * 2. Leader record last committed index as readIndex.
+   * 3. Leader broadcast heartbeats to followers and waits for 
acknowledgements.
+   * 4. If majority respond success, returns readIndex.
+   * @return current readIndex.
+   */
+  CompletableFuture<Long> getReadIndex() {
+    final long readIndex = server.getRaftLog().getLastCommittedIndex();
+
+    // if group contains only one member, fast path
+    if (server.getRaftConf().getCurrentPeers().size() == 1) {
+      return CompletableFuture.completedFuture(readIndex);
+    }
+
+    // leader has not committed any entries in this term, reject
+    if (server.getRaftLog().getTermIndex(readIndex).getTerm() != 
server.getState().getCurrentTerm()) {
+      return JavaUtils.completeExceptionally(new 
LeaderNotReadyException(server.getMemberId()));
+    }
+

Review Comment:
   This is the leader lease approach; see #928 .



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