Search in sources :

Example 26 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class RaftBasicTests method testRequestTimeout.

public static void testRequestTimeout(boolean async, MiniRaftCluster cluster, Logger LOG) throws Exception {
    waitForLeader(cluster);
    final Timestamp startTime = Timestamp.currentTime();
    try (final RaftClient client = cluster.createClient()) {
        // Get the next callId to be used by the client
        final ClientInvocationId invocationId = RaftClientTestUtil.getClientInvocationId(client);
        // Create an entry corresponding to the callId and clientId
        // in each server's retry cache.
        cluster.getServerAliveStream().forEach(raftServer -> RetryCacheTestUtil.getOrCreateEntry(raftServer, invocationId));
        // The retry is successful when the retry cache entry for the corresponding callId and clientId expires.
        if (async) {
            CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("abc"));
            replyFuture.get();
        } else {
            client.io().send(new SimpleMessage("abc"));
        }
        // Eventually the request would be accepted by the server
        // when the retry cache entry is invalidated.
        // The duration for which the client waits should be more than the retryCacheExpiryDuration.
        final TimeDuration duration = startTime.elapsedTime();
        TimeDuration retryCacheExpiryDuration = RaftServerConfigKeys.RetryCache.expiryTime(cluster.getProperties());
        Assert.assertTrue(duration.compareTo(retryCacheExpiryDuration) >= 0);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) TimeDuration(org.apache.ratis.util.TimeDuration) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) Timestamp(org.apache.ratis.util.Timestamp) RaftClient(org.apache.ratis.client.RaftClient)

Example 27 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class TestRatisServerMetricsBase method runTestClientFailedRequest.

void runTestClientFailedRequest(CLUSTER cluster) throws InterruptedException, IOException, ExecutionException {
    final RaftServer.Division leaderImpl = RaftTestUtil.waitForLeader(cluster);
    ClientId clientId = ClientId.randomId();
    // StaleRead with Long.MAX_VALUE minIndex will fail.
    RaftClientRequest r = RaftClientRequest.newBuilder().setClientId(clientId).setLeaderId(leaderImpl.getId()).setGroupId(cluster.getGroupId()).setCallId(0).setMessage(Message.EMPTY).setType(RaftClientRequest.staleReadRequestType(Long.MAX_VALUE)).build();
    final CompletableFuture<RaftClientReply> f = leaderImpl.getRaftServer().submitClientRequestAsync(r);
    Assert.assertTrue(!f.get().isSuccess());
    assertEquals(1L, ((RaftServerMetricsImpl) leaderImpl.getRaftServerMetrics()).getCounter(RATIS_SERVER_FAILED_CLIENT_STALE_READ_COUNT).getCount());
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) ClientId(org.apache.ratis.protocol.ClientId) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl)

Example 28 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class WatchRequestTests method checkAll.

static void checkAll(List<CompletableFuture<WatchReplies>> watches, Logger LOG) throws Exception {
    for (int i = 0; i < watches.size(); i++) {
        final WatchReplies watchReplies = watches.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
        final long logIndex = watchReplies.logIndex;
        LOG.info("checkAll {}: logIndex={}", i, logIndex);
        final RaftClientReply watchAllReply = watchReplies.getAll();
        Assert.assertTrue(watchAllReply.isSuccess());
        final RaftClientReply watchAllCommittedReply = watchReplies.getAllCommitted();
        Assert.assertTrue(watchAllCommittedReply.isSuccess());
        {
            // check commit infos
            final Collection<CommitInfoProto> commitInfos = watchAllCommittedReply.getCommitInfos();
            final String message = "logIndex=" + logIndex + ", " + ProtoUtils.toString(commitInfos);
            Assert.assertEquals(NUM_SERVERS, commitInfos.size());
            commitInfos.forEach(info -> Assert.assertTrue(message, logIndex <= info.getCommitIndex()));
        }
    }
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) CheckedSupplier(org.apache.ratis.util.function.CheckedSupplier) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicies(org.apache.ratis.retry.RetryPolicies) ArrayList(java.util.ArrayList) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) Log4jUtils(org.apache.ratis.util.Log4jUtils) ReplicationLevel(org.apache.ratis.proto.RaftProtos.ReplicationLevel) ProtoUtils(org.apache.ratis.util.ProtoUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) Logger(org.slf4j.Logger) RetryPolicy(org.apache.ratis.retry.RetryPolicy) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Collection(java.util.Collection)

Example 29 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class WatchRequestTests method runTestWatchRequestClientTimeout.

static void runTestWatchRequestClientTimeout(TestParameters p) throws Exception {
    final Logger LOG = p.log;
    CompletableFuture<RaftClientReply> watchReply;
    // watch 1000 which will never be committed
    // so client can not receive reply, and connection closed, throw TimeoutException.
    // We should not retry, because if retry, RaftClientImpl::handleIOException will random select a leader,
    // then sometimes throw NotLeaderException.
    watchReply = p.sendWatchRequest(1000, RetryPolicies.noRetry());
    try {
        watchReply.get();
        fail("runTestWatchRequestClientTimeout failed");
    } catch (Exception ex) {
        LOG.error("error occurred", ex);
        Assert.assertTrue(ex.getCause().getClass() == AlreadyClosedException.class || ex.getCause().getClass() == RaftRetryFailureException.class);
        if (ex.getCause() != null) {
            if (ex.getCause().getCause() != null) {
                Assert.assertEquals(TimeoutIOException.class, ex.getCause().getCause().getClass());
            }
        }
    }
}
Also used : RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) Logger(org.slf4j.Logger) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) RaftRetryFailureException(org.apache.ratis.protocol.exceptions.RaftRetryFailureException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) ExecutionException(java.util.concurrent.ExecutionException)

Example 30 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class WatchRequestTests method checkTimeout.

static void checkTimeout(List<CompletableFuture<RaftClientReply>> replies, List<CompletableFuture<WatchReplies>> watches, Logger LOG) throws Exception {
    for (int i = 0; i < replies.size(); i++) {
        final RaftClientReply reply = replies.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
        LOG.info("checkTimeout {}: receive {}", i, reply);
        final long logIndex = reply.getLogIndex();
        Assert.assertTrue(reply.isSuccess());
        final WatchReplies watchReplies = watches.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
        Assert.assertEquals(logIndex, watchReplies.logIndex);
        assertNotReplicatedException(logIndex, ReplicationLevel.ALL, watchReplies::getAll);
        assertNotReplicatedException(logIndex, ReplicationLevel.ALL_COMMITTED, watchReplies::getAllCommitted);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11