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


##########
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:
   I think we can maintain a `lastVerifiedReadindexTime` here and if current 
time within defined time windows then we can return `readIndex` directly.
   This would reduce follower read overhead as leader won't need to verify its 
leadership for every read request.
   (recommend to set same value as election timeout)



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