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());
}
}
}
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;
}
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;
}
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);
}
}
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();
}
}
}
Aggregations