szetszwo commented on code in PR #958:
URL: https://github.com/apache/ratis/pull/958#discussion_r1378988763
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/ReadRequests.java:
##########
@@ -95,6 +95,13 @@ CompletableFuture<Long> waitToAdvance(long readIndex) {
if (stateMachine.getLastAppliedTermIndex().getIndex() >= readIndex) {
return CompletableFuture.completedFuture(readIndex);
}
- return readIndexQueue.add(readIndex);
+ final CompletableFuture<Long> f = readIndexQueue.add(readIndex);
+
+ // double check here to avoid potential data race conditions on
LastAppliedTermIndex
+ if (stateMachine.getLastAppliedTermIndex().getIndex() >= readIndex) {
+ return CompletableFuture.completedFuture(readIndex);
+ } else {
+ return f;
+ }
Review Comment:
- Let's call `readIndexQueue.complete(..)`?
- BTW, I found that it is more correct to complete the future in line 96
with `lastAppliedIndex` instead of `readIndex`, although it is not used.
Then, the code becomes:
```java
CompletableFuture<Long> waitToAdvance(long readIndex) {
final long lastApplied =
stateMachine.getLastAppliedTermIndex().getIndex();
if (lastApplied >= readIndex) {
return CompletableFuture.completedFuture(lastApplied);
}
final CompletableFuture<Long> f = readIndexQueue.add(readIndex);
final long current = stateMachine.getLastAppliedTermIndex().getIndex();
if (current > lastApplied) {
readIndexQueue.complete(current);
}
return f;
}
```
--
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]