Skip to content

RATIS-2831. Replace readOnlyAsync API with readIndexAsync - #1707

Open
ivandika3 wants to merge 4 commits into
apache:masterfrom
ivandika3:RATIS-2831
Open

ivandika3 wants to merge 4 commits into
apache:masterfrom
ivandika3:RATIS-2831

Conversation

@ivandika3

@ivandika3 ivandika3 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Generated-by: GPT 6.1 Sol

Please see https://issues.apache.org/jira/browse/RATIS-2831

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/RATIS-2831

How was this patch tested?

UT. Did a benchmark on the follower read for the ReadIndex batching implementation (RATIS-2524, WIP) and the regression due to the coupling between the ReadIndex and the actual query disappeared and the ReadIndex batching performance is now considerably improved than without batching.

@ivandika3 ivandika3 self-assigned this Nov 24, 2026
@amaliujia

amaliujia commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @ivandika3 for all the context in:
https://issues.apache.org/jira/browse/RATIS-2509
https://issues.apache.org/jira/browse/RATIS-2831
https://issues.apache.org/jira/browse/RATIS-2524

I'm concerned that readIndexAsync idea in this PR moves the linearizability contract from Ratis to every embedded caller: applications must query after the barrier, on the same division, against live state (not cache), and must handle a snapshot install happens in between.

Ratis can no longer enforce any of these. The returned Long also exposes a log index that callers might misuse (as it is just a number not a proto).

The root cause in RATIS-2831 is that thenCompose runs the query on the thread who completes the ReadIndex future. Could we fix that while keeping the read inside Ratis?

For example, replace the API to readOnlyAsync(clientId, type, query, Executor), which then uses thenComposeAsync? Per my understanding, now the gRPC thread bottleneck you mentioned is migrated off: the gRPC threads only need to do a lightweight submit, and the real query happens in the provided executor which is designed by the applications?

@amaliujia

Copy link
Copy Markdown
Contributor

@ivandika3 also part of the reason that we had the public API discussion is because the context is spread over different JIRA without a clear grouping:

https://issues.apache.org/jira/browse/RATIS-2509
https://issues.apache.org/jira/browse/RATIS-2831
https://issues.apache.org/jira/browse/RATIS-2524

It would be better if you can build a parent JIRA to state the ultimate goal and convert the JIRAs to sub-tasks.

@ivandika3

ivandika3 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @amaliujia for further reviews.

I'm concerned that readIndexAsync idea in this PR moves the linearizability contract from Ratis to every embedded caller: applications must query after the barrier, on the same division

Yes, this is a valid concern since now the job of doing linearizable read is in the application and therefore can be misused by the application. However, this seems to be the way other systems like etcd implemented it so there is a precedence. The javadoc already specifies the guarantees of this API.

I understand that this issue breaks the Raft encapsulation (althought internal logic like the actual readindex propagation and waiting for application is still handled by Ratis), but I believe this is the current best way to be flexible and improves the performance profile.

against live state (not cache), and must handle a snapshot install happens in between.

The current OM read already checks the cache which will be invalidated if it's already been applied to RocksDB. There should not be any extra logic needed to handle this. For snapshot install, even the previous readOnlyAsync does not handle cases where there is a snapshot install in StateMachine#query so this can be addressed in another ticket to synchronize the reads and the snapshot install (e.g. drain the reads first before starting installSnapshot).

Ratis can no longer enforce any of these. The returned Long also exposes a log index that callers might misuse (as it is just a number not a proto).

Yes, misuse is possible, but changing it to protobuf does not really enforces anything and can introduce additional serde overhead.

The root cause in RATIS-2831 is that thenCompose runs the query on the thread who completes the ReadIndex future. Could we fix that while keeping the read inside Ratis?

For example, replace the API to readOnlyAsync(clientId, type, query, Executor), which then uses thenComposeAsync? Per my understanding, now the gRPC thread bottleneck you mentioned is migrated off: the gRPC threads only need to do a lightweight submit, and the real query happens in the provided executor which is designed by the applications?

I have thought about passing an executor and this would technically work. However, this requires us to create and manage another ReadIndex executor (or we can reuse one of the Ratis executors). This has an overhead of handing off to another set of executors instead of actually running it in the RPC threads, such as the handoff overhead, Java async scheduling, low level things like cache locality. Another issue is that this executor needs to be sized accordingly (e.g. we have to size it to the number RPC threads). Finally, we need some kind of special thread local propagation between the caller thread and the executor threads (like apache/ozone#11257 in Ozone).

In Ozone OM, the OM leader simply reads from the local read without handing off anything and this makes the read performance of leader read superior than anything that has to go through Ratis. I wanted to close this gap as much as possible.

Hope this clarifies.

@ivandika3

Copy link
Copy Markdown
Contributor Author

also part of the reason that we had the public API discussion is because the context is spread over different JIRA without a clear grouping:

@amaliujia Thanks, I have raised RATIS-2836 to include all the linearizable read performance works.

@amaliujia

Copy link
Copy Markdown
Contributor

Thanks for the clarification! IMO the right direction is still having the Ratis deal with the tough work directly when possible.

Looping @szetszwo to see if there are other solutions or which technical direction we should go.

@szetszwo

Copy link
Copy Markdown
Contributor

... However, this requires us to create and manage another ReadIndex executor (or we can reuse one of the Ratis executors). ...

@ivandika3 , Ratis use serverExecutor for handling client requests. We may use it when application does not pass it. Please see if below would work?

diff --git a/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServer.java b/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServer.java
index 7d4cba962..8beb73e90 100644
--- a/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServer.java
+++ b/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServer.java
@@ -25,6 +25,8 @@ import java.util.Collection;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+
 import org.apache.ratis.conf.Parameters;
 import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.proto.RaftProtos.CommitInfoProto;
@@ -156,8 +158,8 @@ public interface RaftServer extends Closeable, RpcType.Get,
      * application request bytes.  Remote clients should continue to use
      * {@link org.apache.ratis.client.api.AsyncApi#sendReadOnly(Message)}.
      */
-    default CompletableFuture<Message> readOnlyAsync(Message queryMessage) throws IOException {
-      return readOnlyAsync(ClientId.randomId(), ReadRequestTypeProto.getDefaultInstance(), queryMessage);
+    default CompletableFuture<Message> readOnlyAsync(Message queryMessage, Executor executor) throws IOException {
+      return readOnlyAsync(ClientId.randomId(), ReadRequestTypeProto.getDefaultInstance(), queryMessage, executor);
     }
 
     /**
@@ -169,7 +171,7 @@ public interface RaftServer extends Closeable, RpcType.Get,
      * consistency must use the same client ID as their prior writes.
      */
     default CompletableFuture<Message> readOnlyAsync(ClientId clientId, ReadRequestTypeProto readRequestType,
-        Message queryMessage) throws IOException {
+        Message queryMessage, Executor executor) throws IOException {
       throw new UnsupportedOperationException("readOnlyAsync is not supported");
     }
 
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 cb9818e45..9752b8a90 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
@@ -125,6 +125,7 @@ import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
@@ -1175,8 +1176,9 @@ class RaftServerImpl implements RaftServer.Division,
 
   @Override
   public CompletableFuture<Message> readOnlyAsync(ClientId clientId, ReadRequestTypeProto readRequestType,
-      Message queryMessage) throws IOException {
-    return readRequestHandler.readOnlyAsync(clientId, readRequestType, queryMessage);
+      Message queryMessage, Executor executor) throws IOException {
+    return readRequestHandler.readOnlyAsync(clientId, readRequestType, queryMessage,
+        executor != null ? executor : serverExecutor);
   }
 
   @Override
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/ReadRequestHandler.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/ReadRequestHandler.java
index aaaa92b26..c3b025ec0 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ReadRequestHandler.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ReadRequestHandler.java
@@ -47,6 +47,7 @@ import java.io.IOException;
 import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
+import java.util.concurrent.Executor;
 
 import static org.apache.ratis.server.impl.ServerProtoUtils.toReadIndexReplyProto;
 import static org.apache.ratis.server.impl.ServerProtoUtils.toReadIndexRequestProto;
@@ -142,7 +143,7 @@ class ReadRequestHandler {
   }
 
   CompletableFuture<Message> readOnlyAsync(ClientId clientId, ReadRequestTypeProto readRequestType,
-      Message queryMessage) throws IOException {
+      Message queryMessage, Executor executor) throws IOException {
     Objects.requireNonNull(clientId, "clientId == null");
     Objects.requireNonNull(readRequestType, "readRequestType == null");
     Objects.requireNonNull(queryMessage, "queryMessage == null");
@@ -160,7 +161,7 @@ class ReadRequestHandler {
       return getReadIndex(clientId, readRequestType, leader)
           .thenCompose(readIndex -> state.getReadRequests().waitToAdvance(readIndex,
               () -> getReadExceptionIfInstallingSnapshot("local read")))
-          .thenCompose(readIndex -> stateMachine.query(queryMessage));
+          .thenComposeAsync(readIndex -> stateMachine.query(queryMessage), executor);
     } else {
       throw new IllegalStateException("Unexpected read option: " + readOption);
     }

@ivandika3

ivandika3 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @szetszwo for the suggestion. Using serverExecutor makes sense as a default, I'll add back the readOnlyAsync with executor integration.

The concern is still whether the performance of using readOnlyAsync will be on-par with using readIndexAsync due to the executor handoff and if using serverExecutor whether sharing the executor with appendEntries and transferLeadership will affect performance. I'm still benchmarking on OM with readIndexAsync so the performance comparison with readOnlyAsync will be done later.

I also added Javadoc to indicate that these APIs are experimental and subject for removal.

@szetszwo szetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivandika3 , thanks for the update!

In RaftServer, there are 4 overloaded readOnlyAsync and 2 readIndexAsync methods now. It seems too many. If we cannot decide the right API, how about we add them later? Otherwise, the bad API is very hard to be removed later.

We may just add public methods in RaftServerImpl and use them for testing. Once we have decided the right API, we can add them to RaftServer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants