Search in sources :

Example 11 with SimpleStateMachine4Testing

use of org.apache.ratis.statemachine.SimpleStateMachine4Testing in project incubator-ratis by apache.

the class TestRetryCacheWithGrpc method testRetryOnResourceUnavailableException.

@Test(timeout = 10000)
public void testRetryOnResourceUnavailableException() throws InterruptedException, IOException {
    RaftProperties properties = new RaftProperties();
    properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    RaftServerConfigKeys.Write.setElementLimit(properties, 1);
    MiniRaftClusterWithGrpc cluster = getFactory().newCluster(NUM_SERVERS, properties);
    cluster.start();
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftServer leaderProxy = leader.getRaftServer();
    for (RaftServer.Division follower : cluster.getFollowers()) {
        // block followers to trigger ResourceUnavailableException
        ((SimpleStateMachine4Testing) follower.getStateMachine()).blockWriteStateMachineData();
    }
    AtomicBoolean failure = new AtomicBoolean(false);
    long callId = 1;
    ClientId clientId = ClientId.randomId();
    RaftClientRequest r = null;
    while (!failure.get()) {
        long cid = callId;
        r = cluster.newRaftClientRequest(clientId, leaderProxy.getId(), callId++, new RaftTestUtil.SimpleMessage("message"));
        CompletableFuture<RaftClientReply> f = leaderProxy.submitClientRequestAsync(r);
        f.exceptionally(e -> {
            if (e.getCause() instanceof ResourceUnavailableException) {
                RetryCacheTestUtil.isFailed(RetryCacheTestUtil.get(leader, clientId, cid));
                failure.set(true);
            }
            return null;
        });
    }
    for (RaftServer.Division follower : cluster.getFollowers()) {
        // unblock followers
        ((SimpleStateMachine4Testing) follower.getStateMachine()).unblockWriteStateMachineData();
    }
    while (failure.get()) {
        try {
            // retry until the request failed with ResourceUnavailableException succeeds.
            RaftClientReply reply = leaderProxy.submitClientRequestAsync(r).get();
            if (reply.isSuccess()) {
                failure.set(false);
            }
        } catch (Exception e) {
        // Ignore the exception
        }
    }
    cluster.shutdown();
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftServer(org.apache.ratis.server.RaftServer) RaftProperties(org.apache.ratis.conf.RaftProperties) ResourceUnavailableException(org.apache.ratis.protocol.exceptions.ResourceUnavailableException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ResourceUnavailableException(org.apache.ratis.protocol.exceptions.ResourceUnavailableException) ClientId(org.apache.ratis.protocol.ClientId) Test(org.junit.Test)

Aggregations

SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)11 RaftClient (org.apache.ratis.client.RaftClient)8 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)6 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)6 CompletableFuture (java.util.concurrent.CompletableFuture)4 IOException (java.io.IOException)3 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)3 RaftLog (org.apache.ratis.server.raftlog.RaftLog)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)2 RaftServer (org.apache.ratis.server.RaftServer)2 TermIndex (org.apache.ratis.server.protocol.TermIndex)2 TestSegmentedRaftLog (org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog)2 TimeDuration (org.apache.ratis.util.TimeDuration)2 Gauge (com.codahale.metrics.Gauge)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1