Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1561,9 +1561,9 @@ private CompletableFuture<Message> replyPendingRequest(
if (getInfo().isLeader() && cacheEntry.isCompletedNormally()) {
LOG.warn("{} retry cache entry of leader should be pending: {}", this, cacheEntry);
}
if (cacheEntry.isFailed()) {
retryCache.refreshEntry(new CacheEntry(cacheEntry.getKey()));
}
final CacheEntry entryToComplete = cacheEntry.isFailed()
? retryCache.refreshEntry(new CacheEntry(cacheEntry.getKey()))
: cacheEntry;

return stateMachineFuture.whenComplete((reply, exception) -> {
getTransactionManager().remove(termIndex);
Expand All @@ -1581,9 +1581,9 @@ private CompletableFuture<Message> replyPendingRequest(
// update pending request
final LeaderStateImpl leader = role.getLeaderState().orElse(null);
if (leader != null) {
leader.replyPendingRequest(termIndex, r, cacheEntry);
leader.replyPendingRequest(termIndex, r, entryToComplete);
} else {
cacheEntry.updateResult(r);
entryToComplete.updateResult(r);
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions ratis-server/src/test/java/org/apache/ratis/RetryCacheTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.ratis;

import org.apache.ratis.server.RetryCache;
import org.apache.ratis.server.impl.MiniRaftCluster;
import org.apache.ratis.server.impl.PeerChanges;
import org.apache.ratis.RaftTestUtil.SimpleMessage;
Expand Down Expand Up @@ -165,4 +166,34 @@ void runTestRetryOnNewLeader(CLUSTER cluster) throws Exception {
Assertions.assertEquals(0, count(cluster.getLeader().getRaftLog(), oldLastApplied + 1));
}
}

@Test
public void testApplyCompletesReplacementOfFailedEntry() throws Exception {
runWithNewCluster(NUM_SERVERS, this::runTestApplyCompletesReplacementOfFailedEntry);
}

void runTestApplyCompletesReplacementOfFailedEntry(CLUSTER cluster) throws Exception {
final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
final RaftServer.Division follower = cluster.getFollowers().get(0);

try (final RaftClient client = cluster.createClient(leaderId)) {
final long callId = 999;
final RetryCache.Entry failed = RetryCacheTestUtil.createFailedEntry(follower, client.getId(),
callId, new IllegalStateException("Injected failure"));

final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId,
callId, new SimpleMessage("message"));
assertReply(client.getClientRpc().sendRequest(r), client, callId);

JavaUtils.attempt(() -> {
final RetryCache.Entry entry = RetryCacheTestUtil.get(follower, client.getId(), callId);
Assertions.assertNotNull(entry, "No retry cache entry on the follower");
Assertions.assertNotSame(failed, entry);

final RaftClientReply reply = entry.getReplyFuture().getNow(null);
Assertions.assertNotNull(reply, "The replacement entry is not completed");
assertReply(reply, client, callId);
}, 20, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS), "wait for the follower to apply", LOG);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public static boolean isFailed(RetryCache.Entry entry) {
return ((RetryCacheImpl.CacheEntry)entry).isFailed();
}

public static RetryCache.Entry createFailedEntry(RaftServer.Division server, ClientId clientId,
long callId, Throwable t) {
final RetryCacheImpl cache = (RetryCacheImpl) server.getRetryCache();
final RetryCacheImpl.CacheEntry entry =
cache.getOrCreateEntry(ClientInvocationId.valueOf(clientId, callId));
entry.failWithException(t);
return entry;
}

public static void assertFailure(RetryCache cache, LogEntryProto logEntry, boolean isFailed) {
if(logEntry.hasStateMachineLogEntry()) {
final ClientInvocationId invocationId = ClientInvocationId.valueOf(logEntry.getStateMachineLogEntry());
Expand Down
Loading