use of org.apache.ratis.protocol.exceptions.RaftRetryFailureException in project incubator-ratis by apache.
the class TestExceptionDependentRetry method runTestExceptionRetryAttempts.
void runTestExceptionRetryAttempts(MiniRaftClusterWithGrpc cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final ExceptionDependentRetry policy = ExceptionDependentRetry.newBuilder().setExceptionToPolicy(TimeoutIOException.class, MultipleLinearRandomRetry.parseCommaSeparated("1ms, 5")).setDefaultPolicy(RetryPolicies.retryForeverNoSleep()).build();
// create a client with the exception dependent policy
try (final RaftClient client = cluster.createClient(policy)) {
client.async().send(new RaftTestUtil.SimpleMessage("1")).get();
}
try (final RaftClient client = cluster.createClient(policy)) {
SimpleStateMachine4Testing.get(leader).blockWriteStateMachineData();
client.async().send(new RaftTestUtil.SimpleMessage("2")).get();
Assert.fail("Test should have failed.");
} catch (ExecutionException e) {
RaftRetryFailureException rrfe = (RaftRetryFailureException) e.getCause();
Assert.assertEquals(16, rrfe.getAttemptCount());
} finally {
SimpleStateMachine4Testing.get(leader).unblockWriteStateMachineData();
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.exceptions.RaftRetryFailureException in project incubator-ratis by apache.
the class RaftAsyncTests method runTestRequestAsyncWithRetryFailure.
void runTestRequestAsyncWithRetryFailure(boolean initialMessages, CLUSTER cluster) throws Exception {
final TimeDuration sleepTime = HUNDRED_MILLIS;
final RetryLimited retryPolicy = RetryPolicies.retryUpToMaximumCountWithFixedSleep(10, sleepTime);
try (final RaftClient client = cluster.createClient(null, retryPolicy)) {
RaftPeerId leader = null;
if (initialMessages) {
// cluster is already started, send a few success messages
leader = RaftTestUtil.waitForLeader(cluster).getId();
final SimpleMessage[] messages = SimpleMessage.create(10, "initial-");
final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
for (int i = 0; i < messages.length; i++) {
replies.add(client.async().send(messages[i]));
}
for (int i = 0; i < messages.length; i++) {
RaftTestUtil.assertSuccessReply(replies.get(i));
}
// kill the only server
cluster.killServer(leader);
}
// now, either the cluster is not yet started or the server is killed.
final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
{
final SimpleMessage[] messages = SimpleMessage.create(10);
int i = 0;
// send half of the calls without starting the cluster
for (; i < messages.length / 2; i++) {
replies.add(client.async().send(messages[i]));
}
// sleep most of the retry time
sleepTime.apply(t -> t * (retryPolicy.getMaxAttempts() - 1)).sleep();
// send another half of the calls without starting the cluster
for (; i < messages.length; i++) {
replies.add(client.async().send(messages[i]));
}
Assert.assertEquals(messages.length, replies.size());
}
// sleep again so that the first half calls will fail retries.
// the second half still have retry time remaining.
sleepTime.apply(t -> t * 2).sleep();
if (leader != null) {
cluster.restartServer(leader, false);
} else {
cluster.start();
}
// all the calls should fail for ordering guarantee
for (int i = 0; i < replies.size(); i++) {
final CheckedRunnable<Exception> getReply = replies.get(i)::get;
final String name = "retry-failure-" + i;
if (i == 0) {
final Throwable t = testFailureCase(name, getReply, ExecutionException.class, RaftRetryFailureException.class);
assertRaftRetryFailureException((RaftRetryFailureException) t.getCause(), retryPolicy, name);
} else {
testFailureCase(name, getReply, ExecutionException.class, AlreadyClosedException.class, RaftRetryFailureException.class);
}
}
testFailureCaseAsync("last-request", () -> client.async().send(new SimpleMessage("last")), AlreadyClosedException.class, RaftRetryFailureException.class);
}
}
Aggregations