Search in sources :

Example 86 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class DataStreamBaseTest method runTestMockCluster.

void runTestMockCluster(ClientId clientId, int bufferSize, int bufferNum, Exception expectedException, Exception headerException) throws IOException {
    try (final RaftClient client = newRaftClientForDataStream(clientId)) {
        final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, DataStreamTestUtils.getRoutingTableChainTopology(peers, getPrimaryServer().getPeer()));
        if (headerException != null) {
            final DataStreamReply headerReply = out.getHeaderFuture().join();
            Assert.assertFalse(headerReply.isSuccess());
            final RaftClientReply clientReply = ClientProtoUtils.toRaftClientReply(((DataStreamReplyByteBuffer) headerReply).slice());
            Assert.assertTrue(clientReply.getException().getMessage().contains(headerException.getMessage()));
            return;
        }
        final RaftClientReply clientReply = DataStreamTestUtils.writeAndCloseAndAssertReplies(CollectionUtils.as(servers, Server::getRaftServer), null, out, bufferSize, bufferNum, getPrimaryClientId(), client.getId(), false).join();
        if (expectedException != null) {
            Assert.assertFalse(clientReply.isSuccess());
            Assert.assertTrue(clientReply.getException().getMessage().contains(expectedException.getMessage()));
        } else {
            Assert.assertTrue(clientReply.isSuccess());
        }
    }
}
Also used : DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamServer(org.apache.ratis.server.DataStreamServer) RaftServer(org.apache.ratis.server.RaftServer) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 87 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class AddCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    final Map<RaftPeerId, InetSocketAddress> peersInfo = new HashMap<>();
    final List<RaftPeerId> ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), peersInfo::put);
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        final Stream<RaftPeer> remaining = getRaftGroup().getPeers().stream();
        final Stream<RaftPeer> adding = ids.stream().map(raftPeerId -> RaftPeer.newBuilder().setId(raftPeerId).setAddress(peersInfo.get(raftPeerId)).setPriority(0).build());
        final List<RaftPeer> peers = Stream.concat(remaining, adding).collect(Collectors.toList());
        System.out.println("New peer list: " + peers);
        RaftClientReply reply = client.admin().setConfiguration(peers);
        processReply(reply, () -> "Failed to change raft peer");
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 88 with RaftClient

use of org.apache.ratis.client.RaftClient 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 89 with RaftClient

use of org.apache.ratis.client.RaftClient 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 90 with RaftClient

use of org.apache.ratis.client.RaftClient 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)

Aggregations

RaftClient (org.apache.ratis.client.RaftClient)134 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)66 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)54 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)46 RaftServer (org.apache.ratis.server.RaftServer)46 Test (org.junit.Test)44 IOException (java.io.IOException)41 RaftPeer (org.apache.ratis.protocol.RaftPeer)33 BaseTest (org.apache.ratis.BaseTest)27 RaftTestUtil (org.apache.ratis.RaftTestUtil)22 ArrayList (java.util.ArrayList)20 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)20 RaftGroup (org.apache.ratis.protocol.RaftGroup)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 RaftProperties (org.apache.ratis.conf.RaftProperties)14 File (java.io.File)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)11 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)11