Search in sources :

Example 61 with RaftClientReply

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

the class SetPriorityCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    Map<String, Integer> addressPriorityMap = new HashMap<>();
    for (String optionValue : cl.getOptionValues(PEER_WITH_NEW_PRIORITY_OPTION_NAME)) {
        String[] str = optionValue.split("[|]");
        if (str.length < 2) {
            println("The format of the parameter is wrong");
            return -1;
        }
        addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
    }
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        List<RaftPeer> peers = new ArrayList<>();
        for (RaftPeer peer : getRaftGroup().getPeers()) {
            if (!addressPriorityMap.containsKey(peer.getAddress())) {
                peers.add(RaftPeer.newBuilder(peer).build());
            } else {
                peers.add(RaftPeer.newBuilder(peer).setPriority(addressPriorityMap.get(peer.getAddress())).build());
            }
        }
        RaftClientReply reply = client.admin().setConfiguration(peers);
        processReply(reply, () -> "Failed to set master priorities ");
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 62 with RaftClientReply

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

the class TestRaftServerWithGrpc method testRaftClientRequestMetrics.

void testRaftClientRequestMetrics(MiniRaftClusterWithGrpc cluster) throws IOException, ExecutionException, InterruptedException {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftServerMetricsImpl raftServerMetrics = getRaftServerMetrics(leader);
    try (final RaftClient client = cluster.createClient()) {
        final CompletableFuture<RaftClientReply> f1 = client.async().send(new SimpleMessage("testing"));
        Assert.assertTrue(f1.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(RAFT_CLIENT_WRITE_REQUEST).getCount() > 0);
        final CompletableFuture<RaftClientReply> f2 = client.async().sendReadOnly(new SimpleMessage("testing"));
        Assert.assertTrue(f2.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(RAFT_CLIENT_READ_REQUEST).getCount() > 0);
        final CompletableFuture<RaftClientReply> f3 = client.async().sendStaleRead(new SimpleMessage("testing"), 0, leader.getId());
        Assert.assertTrue(f3.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(RAFT_CLIENT_STALE_READ_REQUEST).getCount() > 0);
        final CompletableFuture<RaftClientReply> f4 = client.async().watch(0, RaftProtos.ReplicationLevel.ALL);
        Assert.assertTrue(f4.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(String.format(RAFT_CLIENT_WATCH_REQUEST, "-ALL")).getCount() > 0);
        final CompletableFuture<RaftClientReply> f5 = client.async().watch(0, RaftProtos.ReplicationLevel.MAJORITY);
        Assert.assertTrue(f5.get().isSuccess());
        Assert.assertTrue(raftServerMetrics.getTimer(String.format(RAFT_CLIENT_WATCH_REQUEST, "")).getCount() > 0);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl)

Example 63 with RaftClientReply

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

the class TestRaftServerWithGrpc method testRequestMetrics.

void testRequestMetrics(MiniRaftClusterWithGrpc cluster) throws Exception {
    try (RaftClient client = cluster.createClient()) {
        // send a request to make sure leader is ready
        final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("testing"));
        Assert.assertTrue(f.get().isSuccess());
    }
    SimpleStateMachine4Testing stateMachine = SimpleStateMachine4Testing.get(cluster.getLeader());
    stateMachine.blockFlushStateMachineData();
    // Block stateMachine flush data, so that 2nd request will not be
    // completed, and so it will not be removed from pending request map.
    List<RaftClient> clients = new ArrayList<>();
    try {
        RaftClient client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
        clients.add(client);
        client.async().send(new SimpleMessage("2nd Message"));
        final SortedMap<String, Gauge> gaugeMap = getRaftServerMetrics(cluster.getLeader()).getRegistry().getGauges((s, metric) -> s.contains(REQUEST_MEGA_BYTE_SIZE));
        for (int i = 0; i < 10; i++) {
            client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
            clients.add(client);
            client.async().send(new SimpleMessage("message " + i));
        }
        // Because we have passed 11 requests, and the element queue size is 10.
        RaftTestUtil.waitFor(() -> getRaftServerMetrics(cluster.getLeader()).getCounter(REQUEST_QUEUE_LIMIT_HIT_COUNTER).getCount() == 1, 300, 5000);
        stateMachine.unblockFlushStateMachineData();
        // Send a message with 1025kb , our byte size limit is 1024kb (1mb) , so it should fail
        // and byte size counter limit will be hit.
        client = cluster.createClient(cluster.getLeader().getId(), RetryPolicies.noRetry());
        final SizeInBytes size = SizeInBytes.valueOf("1025kb");
        final ByteString bytes = randomByteString(size.getSizeInt());
        Assert.assertEquals(size.getSizeInt(), bytes.size());
        client.async().send(new SimpleMessage(size + "-message", bytes));
        clients.add(client);
        RaftTestUtil.waitFor(() -> getRaftServerMetrics(cluster.getLeader()).getCounter(REQUEST_BYTE_SIZE_LIMIT_HIT_COUNTER).getCount() == 1, 300, 5000);
        Assert.assertEquals(2, getRaftServerMetrics(cluster.getLeader()).getCounter(RESOURCE_LIMIT_HIT_COUNTER).getCount());
    } finally {
        for (RaftClient client : clients) {
            client.close();
        }
    }
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) SizeInBytes(org.apache.ratis.util.SizeInBytes) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) Gauge(com.codahale.metrics.Gauge) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 64 with RaftClientReply

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

the class TestRaftWithGrpc method runTestUpdateViaHeartbeat.

void runTestUpdateViaHeartbeat(MiniRaftClusterWithGrpc cluster) throws Exception {
    waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient()) {
        // block append requests
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockWriteStateMachineData);
        CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
        TimeDuration.valueOf(5, TimeUnit.SECONDS).sleep();
        // replyFuture should not be completed until append request is unblocked.
        Assert.assertFalse(replyFuture.isDone());
        // unblock append request.
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockWriteStateMachineData);
        final RaftLog leaderLog = cluster.getLeader().getRaftLog();
        // The entries have been appended in the followers
        // although the append entry timed out at the leader
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).forEach(raftServer -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
            final long leaderNextIndex = leaderLog.getNextIndex();
            final LogEntryHeader[] leaderEntries = leaderLog.getEntries(0, Long.MAX_VALUE);
            final RaftLog followerLog = raftServer.getRaftLog();
            Assert.assertEquals(leaderNextIndex, followerLog.getNextIndex());
            final LogEntryHeader[] serverEntries = followerLog.getEntries(0, Long.MAX_VALUE);
            Assert.assertArrayEquals(serverEntries, leaderEntries);
        }, 10, HUNDRED_MILLIS, "assertRaftLog-" + raftServer.getId(), LOG)));
        // Wait for heartbeats from leader to be received by followers
        Thread.sleep(500);
        RaftServerTestUtil.getLogAppenders(cluster.getLeader()).forEach(logAppender -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
            final long leaderNextIndex = leaderLog.getNextIndex();
            // FollowerInfo in the leader state should have updated next and match index.
            final long followerMatchIndex = logAppender.getFollower().getMatchIndex();
            Assert.assertTrue(followerMatchIndex >= leaderNextIndex - 1);
            Assert.assertEquals(followerMatchIndex + 1, logAppender.getFollower().getNextIndex());
        }, 10, HUNDRED_MILLIS, "assertRaftLog-" + logAppender.getFollower(), LOG)));
    }
}
Also used : RaftBasicTests(org.apache.ratis.RaftBasicTests) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftLog(org.apache.ratis.server.raftlog.RaftLog) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) RaftTestUtil(org.apache.ratis.RaftTestUtil) TimeUnit(java.util.concurrent.TimeUnit) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftClient(org.apache.ratis.client.RaftClient) JavaUtils(org.apache.ratis.util.JavaUtils) Assert(org.junit.Assert) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 65 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply 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

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