Search in sources :

Example 26 with RaftClient

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

the class TestDataStreamDisabled method testDataStreamDisabled.

@Test
public void testDataStreamDisabled() {
    final RaftProperties properties = new RaftProperties();
    Assert.assertEquals(SupportedDataStreamType.DISABLED, RaftConfigKeys.DataStream.type(properties, LOG::info));
    final RaftPeer server = RaftPeer.newBuilder().setId("s0").build();
    final RaftClient client = RaftClient.newBuilder().setRaftGroup(RaftGroup.valueOf(RaftGroupId.randomId(), server)).setProperties(properties).build();
    exception.expect(UnsupportedOperationException.class);
    exception.expectMessage(DisabledDataStreamClientFactory.class.getName() + "$1 does not support streamAsync");
    // stream() will create a header request, thus it will hit UnsupportedOperationException due to
    // DisabledDataStreamFactory.
    client.getDataStreamApi().stream();
}
Also used : RaftProperties(org.apache.ratis.conf.RaftProperties) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 27 with RaftClient

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

the class TestLogAppenderWithGrpc method runTestRestartLogAppender.

private void runTestRestartLogAppender(MiniRaftClusterWithGrpc cluster) throws Exception {
    final RaftServer.Division leader = waitForLeader(cluster);
    int messageCount = 0;
    // Send some messages
    try (RaftClient client = cluster.createClient(leader.getId())) {
        for (int i = 0; i < 10; i++) {
            final RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + ++messageCount));
            Assert.assertTrue(reply.isSuccess());
        }
    }
    // assert INCONSISTENCY counter == 0
    final GrpcServerMetrics leaderMetrics = new GrpcServerMetrics(leader.getMemberId().toString());
    final String counter = String.format(GrpcServerMetrics.RATIS_GRPC_METRICS_LOG_APPENDER_INCONSISTENCY, cluster.getFollowers().iterator().next().getMemberId().getPeerId());
    Assert.assertEquals(0L, leaderMetrics.getRegistry().counter(counter).getCount());
    // restart LogAppender
    RaftServerTestUtil.restartLogAppenders(leader);
    // Send some more messages
    try (RaftClient client = cluster.createClient(leader.getId())) {
        for (int i = 0; i < 10; i++) {
            final RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + ++messageCount));
            Assert.assertTrue(reply.isSuccess());
        }
    }
    final RaftServer.Division newLeader = waitForLeader(cluster);
    if (leader == newLeader) {
        final GrpcServerMetrics newleaderMetrics = new GrpcServerMetrics(leader.getMemberId().toString());
        // assert INCONSISTENCY counter >= 1
        // If old LogAppender die before new LogAppender start, INCONSISTENCY equal to 1,
        // else INCONSISTENCY greater than 1
        Assert.assertTrue(newleaderMetrics.getRegistry().counter(counter).getCount() >= 1L);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) GrpcServerMetrics(org.apache.ratis.grpc.metrics.GrpcServerMetrics) RaftClient(org.apache.ratis.client.RaftClient)

Example 28 with RaftClient

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

the class Client method getClients.

public List<FileStoreClient> getClients(RaftProperties raftProperties) {
    List<FileStoreClient> fileStoreClients = new ArrayList<>();
    for (int i = 0; i < numClients; i++) {
        final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())), getPeers());
        RaftClient.Builder builder = RaftClient.newBuilder().setProperties(raftProperties);
        builder.setRaftGroup(raftGroup);
        builder.setClientRpc(new GrpcFactory(new org.apache.ratis.conf.Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
        RaftPeer[] peers = getPeers();
        builder.setPrimaryDataStreamServer(peers[0]);
        RaftClient client = builder.build();
        fileStoreClients.add(new FileStoreClient(client));
    }
    return fileStoreClients;
}
Also used : GrpcFactory(org.apache.ratis.grpc.GrpcFactory) ArrayList(java.util.ArrayList) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) FileStoreClient(org.apache.ratis.examples.filestore.FileStoreClient) RaftClient(org.apache.ratis.client.RaftClient)

Example 29 with RaftClient

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

the class Client method run.

@Override
public void run() throws Exception {
    RaftProperties raftProperties = new RaftProperties();
    final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())), getPeers());
    RaftClient.Builder builder = RaftClient.newBuilder().setProperties(raftProperties);
    builder.setRaftGroup(raftGroup);
    builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
    RaftClient client = builder.build();
    operation(client);
}
Also used : Parameters(org.apache.ratis.conf.Parameters) GrpcFactory(org.apache.ratis.grpc.GrpcFactory) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftClient(org.apache.ratis.client.RaftClient)

Example 30 with RaftClient

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

the class CounterClient method buildClient.

/**
 * build the RaftClient instance which is used to communicate to
 * Counter cluster
 *
 * @return the created client of Counter cluster
 */
private static RaftClient buildClient() {
    RaftProperties raftProperties = new RaftProperties();
    RaftClient.Builder builder = RaftClient.newBuilder().setProperties(raftProperties).setRaftGroup(Constants.RAFT_GROUP).setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
    return builder.build();
}
Also used : Parameters(org.apache.ratis.conf.Parameters) GrpcFactory(org.apache.ratis.grpc.GrpcFactory) RaftProperties(org.apache.ratis.conf.RaftProperties) 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