SzyWilliam commented on code in PR #735:
URL: https://github.com/apache/ratis/pull/735#discussion_r963015242
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/ReadRequests.java:
##########
@@ -129,28 +142,75 @@ synchronized AppendEntriesListener add(long commitIndex,
Function<Long, AppendEn
synchronized void onAppendEntriesReply(AppendEntriesReplyProto reply,
Predicate<Predicate<RaftPeerId>>
hasMajority) {
final long callId = reply.getServerReply().getCallId();
- for (;;) {
- final Map.Entry<Long, AppendEntriesListener> first =
sorted.firstEntry();
- if (first == null || first.getKey() > callId) {
+ for (Map.Entry<Long, AppendEntriesListener> entry : sorted.entrySet()) {
+ if (entry.getKey() > callId) {
return;
}
- final AppendEntriesListener listener = first.getValue();
+ final AppendEntriesListener listener = entry.getValue();
if (listener == null) {
continue;
}
if (listener.receive(reply, hasMajority)) {
- final AppendEntriesListener removed = sorted.remove(callId);
+ final AppendEntriesListener removed = sorted.remove(entry.getKey());
Preconditions.assertSame(listener, removed, "AppendEntriesListener");
ackedCommitIndex.updateToMax(listener.commitIndex, s ->
LOG.debug("{}: {}", ReadRequests.this, s));
}
}
}
}
+ static class ReadIndexQueue {
+ private final TimeoutScheduler scheduler = TimeoutScheduler.getInstance();
+ private final NavigableMap<Long, CompletableFuture<Long>> sorted = new
ConcurrentSkipListMap<>();
+ private final TimeDuration readTimeout;
+
+ ReadIndexQueue(TimeDuration readTimeout) {
+ this.readTimeout = readTimeout;
+ }
+
+ CompletableFuture<Long> add(long readIndex) {
+ final MemoizedSupplier<CompletableFuture<Long>> supplier =
MemoizedSupplier.valueOf(CompletableFuture::new);
+ final CompletableFuture<Long> f = sorted.computeIfAbsent(readIndex, i ->
supplier.get());
+
+ if (supplier.isInitialized()) {
+ scheduler.onTimeout(readTimeout, () -> handleTimeout(readIndex),
+ LOG, () -> "Failed to handle read timeout for index " + readIndex);
+ }
+ return f;
+ }
+
+ private void handleTimeout(long readIndex) {
+ Optional.ofNullable(sorted.remove(readIndex)).ifPresent(consumer -> {
+ consumer.completeExceptionally(
+ new ReadOnlyException(new TimeoutIOException("Read timeout for index
" + readIndex)));
+ });
+ }
+
+ synchronized void complete(Long appliedIndex) {
Review Comment:
done
--
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]