Repository navigation
RATIS-2795: fix: gate stale read when snapshots installing - #1676
Merged
Merged
Conversation
amaliujia
reviewed
Nov 3, 2026
Contributor
|
Can we also add a config to guard this improvement and by default turn it off? |
szetszwo
reviewed
Nov 4, 2026
szetszwo
left a comment
Contributor
There was a problem hiding this comment.
@dao-jun , thanks for working on this!
- ReadException currently has readIndex in the message. Let's change it.
- Let's also add an op parameter to getReadExceptionIfInstallingSnapshot. Then we can use it for stale read.
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 9b961c577..7d93d9ce3 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
@@ -81,6 +81,11 @@ class ReadRequestHandler {
}
CompletableFuture<RaftClientReply> staleReadAsync(RaftClientRequest request) {
+ final ReadException ex = getReadExceptionIfInstallingSnapshot("stale read");
+ if (ex != null) {
+ return CompletableFuture.completedFuture(
+ server.newExceptionReply(request, new StateMachineException(getMemberId(), ex)));
+ }
final long minIndex = request.getType().getStaleRead().getMinIndex();
final long commitIndex = state.getLog().getLastCommittedIndex();
LOG.debug("{}: minIndex={}, commitIndex={} from {}", getMemberId(), minIndex, commitIndex, request.getClientId());
@@ -94,19 +99,20 @@ class ReadRequestHandler {
}
ReadException getReadException(String op, long installSnapshot, boolean started) {
- return installSnapshot == RaftLog.INVALID_LOG_INDEX ? null : new ReadException(getMemberId() + ": Failed to " + op
- + " readIndex as snapshot (" + installSnapshot + ") installation is " + (started ? "started" : "in progress"));
+ return installSnapshot == RaftLog.INVALID_LOG_INDEX ? null : new ReadException(getMemberId() + ": Failed " + op
+ + " as snapshot (" + installSnapshot + ") installation is " + (started ? "started" : "in progress"));
}
- private ReadException getReadExceptionIfInstallingSnapshot() {
- return getReadException("add", server.getSnapshotInstallationHandler().getInProgressInstallSnapshotIndex(), false);
+ private ReadException getReadExceptionIfInstallingSnapshot(String op) {
+ final long installSnapshotIndex = server.getSnapshotInstallationHandler().getInProgressInstallSnapshotIndex();
+ return getReadException(op, installSnapshotIndex, false);
}
private CompletableFuture<ReadIndexReplyProto> sendReadIndexAsync(
ClientId clientId, ReadRequestTypeProto readRequestType) {
- final long installSnapshot = server.getSnapshotInstallationHandler().getInProgressInstallSnapshotIndex();
- if (installSnapshot != RaftLog.INVALID_LOG_INDEX) {
- return JavaUtils.completeExceptionally(getReadException("get", installSnapshot, false));
+ final ReadException readException = getReadExceptionIfInstallingSnapshot("to send readIndex request");
+ if (readException != null) {
+ return JavaUtils.completeExceptionally(readException);
}
final RaftPeerId leaderId = server.getInfo().getLeaderId();
if (leaderId == null) {
@@ -154,7 +160,7 @@ class ReadRequestHandler {
final LeaderStateImpl leader = server.getRole().getLeaderState().orElse(null);
return getReadIndex(clientId, readRequestType, leader)
.thenCompose(readIndex -> state.getReadRequests().waitToAdvance(readIndex,
- this::getReadExceptionIfInstallingSnapshot))
+ () -> getReadExceptionIfInstallingSnapshot("local read")))
.thenCompose(readIndex -> stateMachine.query(queryMessage));
} else {
throw new IllegalStateException("Unexpected read option: " + readOption);
@@ -177,7 +183,7 @@ class ReadRequestHandler {
return replyFuture
.thenCompose(readIndex -> state.getReadRequests().waitToAdvance(readIndex,
- this::getReadExceptionIfInstallingSnapshot))
+ () -> getReadExceptionIfInstallingSnapshot("read")))
.thenCompose(readIndex -> isDummyRead(request)
? CompletableFuture.completedFuture(server.newSuccessReply(request)) : queryStateMachine(request))
.exceptionally(e -> readException2Reply(request, e));
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java
index d34e84c3f..4e23d30c7 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java
@@ -43,6 +43,7 @@ import org.apache.ratis.util.BatchLogger;
import org.apache.ratis.util.CodeInjectionForTesting;
import org.apache.ratis.util.LifeCycle;
import org.apache.ratis.util.Preconditions;
+import org.apache.ratis.thirdparty.com.google.common.annotations.VisibleForTesting;
import org.apache.ratis.util.ProtoUtils;
import org.apache.ratis.util.Timestamp;
import org.slf4j.Logger;
@@ -106,6 +107,11 @@ class SnapshotInstallationHandler {
return inProgressInstallSnapshotIndex.get();
}
+ @VisibleForTesting
+ void setInProgressInstallSnapshotIndex(long index) {
+ inProgressInstallSnapshotIndex.set(index);
+ }
+
InstallSnapshotReplyProto installSnapshot(InstallSnapshotRequestProto request) throws IOException {
BatchLogger.print(BatchLogKey.INSTALL_SNAPSHOT_REQUEST, getMemberId(),
suffix -> LOG.info("{}: receive installSnapshot: {} {}",
@@ -289,7 +295,7 @@ class SnapshotInstallationHandler {
return future.thenApply(dummy -> reply);
}
server.getState().getReadRequests().fail(
- server.getReadRequestHandler().getReadException("wait for", firstAvailableLogIndex, true));
+ server.getReadRequestHandler().getReadException("to wait for readIndex", firstAvailableLogIndex, true));
final RaftPeerProto leaderProto;
if (!request.hasLastRaftConfigurationLogEntryProto()) {…chunk-based install - Add op parameter to getReadExceptionIfInstallingSnapshot and drop the hardcoded readIndex from the ReadException message (szetszwo) - Set inProgressInstallSnapshotIndex in checkAndInstallSnapshot around the pause/finalize/reload window so the gate covers both install paths (szetszwo, agreed by amaliujia) - Reply with the ReadException directly instead of wrapping it in a StateMachineException, which must come from the state machine (szetszwo) - Test the withholding semantics: with the gate held, a blocking stale read keeps retrying the retriable ReadException instead of returning data from a paused state machine, and completes after the installation finishes
Member
Author
|
@amaliujia @szetszwo Thanks for your review! comments addressed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
gate stale read when snapshots installing
What is the link to the Apache JIRA
https://issues.apache.org/jira/projects/RATIS/issues/RATIS-2795
How was this patch tested?
unit tests