use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class GroupListCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
final RaftPeerId peerId;
final String address;
if (cl.hasOption(PEER_ID_OPTION_NAME)) {
peerId = RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
address = getRaftGroup().getPeer(peerId).getAddress();
} else if (cl.hasOption(SERVER_ADDRESS_OPTION_NAME)) {
address = cl.getOptionValue(SERVER_ADDRESS_OPTION_NAME);
final InetSocketAddress serverAddress = parseInetSocketAddress(address);
peerId = RaftUtils.getPeerId(serverAddress);
} else {
throw new IllegalArgumentException("Both " + PEER_ID_OPTION_NAME + " and " + SERVER_ADDRESS_OPTION_NAME + " options are missing.");
}
try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
GroupListReply reply = raftClient.getGroupManagementApi(peerId).list();
processReply(reply, () -> String.format("Failed to get group information of peerId %s (server %s)", peerId, address));
printf(String.format("The peerId %s (server %s) is in %d groups, and the groupIds is: %s", peerId, address, reply.getGroupIds().size(), reply.getGroupIds()));
}
return 0;
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftStateMachineExceptionTests method runTestRetryOnExceptionDuringReplication.
private void runTestRetryOnExceptionDuringReplication(CLUSTER cluster) throws Exception {
final RaftServer.Division oldLeader = RaftTestUtil.waitForLeader(cluster);
cluster.getLeaderAndSendFirstMessage(true);
// turn on the preAppend failure switch
failPreAppend = true;
try (final RaftClient client = cluster.createClient(oldLeader.getId())) {
final RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final SimpleMessage message = new SimpleMessage("message");
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), oldLeader.getId(), callId, message);
RaftClientReply reply = rpc.sendRequest(r);
Objects.requireNonNull(reply.getStateMachineException());
final RetryCache.Entry oldEntry = RetryCacheTestUtil.get(oldLeader, client.getId(), callId);
Assert.assertNotNull(oldEntry);
Assert.assertTrue(RetryCacheTestUtil.isFailed(oldEntry));
// At this point of time the old leader would have stepped down. wait for leader election to complete
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
// retry
r = cluster.newRaftClientRequest(client.getId(), leader.getId(), callId, message);
reply = rpc.sendRequest(r);
Objects.requireNonNull(reply.getStateMachineException());
final RetryCache.Entry currentEntry = RetryCacheTestUtil.get(leader, client.getId(), callId);
Assert.assertNotNull(currentEntry);
Assert.assertTrue(RetryCacheTestUtil.isFailed(currentEntry));
Assert.assertNotEquals(oldEntry, currentEntry);
failPreAppend = false;
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftStateMachineExceptionTests method runTestRetryOnStateMachineException.
private void runTestRetryOnStateMachineException(CLUSTER cluster) throws Exception {
RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
cluster.getLeaderAndSendFirstMessage(true);
final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
try (final RaftClient client = cluster.createClient(leaderId)) {
final RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final SimpleMessage message = new SimpleMessage("message");
final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, message);
RaftClientReply reply = rpc.sendRequest(r);
Assert.assertFalse(reply.isSuccess());
Assert.assertNotNull(reply.getStateMachineException());
// retry with the same callId
for (int i = 0; i < 5; i++) {
reply = rpc.sendRequest(r);
Assert.assertEquals(client.getId(), reply.getClientId());
Assert.assertEquals(callId, reply.getCallId());
Assert.assertFalse(reply.isSuccess());
Assert.assertNotNull(reply.getStateMachineException());
}
for (RaftServer.Division server : cluster.iterateDivisions()) {
LOG.info("check server " + server.getId());
JavaUtils.attemptRepeatedly(() -> {
Assert.assertNotNull(RetryCacheTestUtil.get(server, client.getId(), callId));
return null;
}, 5, BaseTest.ONE_SECOND, "GetRetryEntry", LOG);
final RaftLog log = server.getRaftLog();
RaftTestUtil.logEntriesContains(log, oldLastApplied + 1, log.getNextIndex(), message);
}
cluster.shutdown();
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftStateMachineExceptionTests method runTestHandleStateMachineException.
private void runTestHandleStateMachineException(CLUSTER cluster) throws Exception {
RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
client.io().send(new RaftTestUtil.SimpleMessage("m"));
fail("Exception expected");
} catch (StateMachineException e) {
e.printStackTrace();
Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception"));
}
cluster.shutdown();
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class ResumeCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
String strAddr = cl.getOptionValue(ADDRESS_OPTION_NAME);
final RaftPeerId peerId = getRaftGroup().getPeers().stream().filter(p -> p.getAddress().equals(strAddr)).findAny().map(RaftPeer::getId).orElse(null);
if (peerId == null) {
printf("Can't find a sever with the address:%s", strAddr);
return -1;
}
try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
RaftClientReply reply = raftClient.getLeaderElectionManagementApi(peerId).resume();
processReply(reply, () -> String.format("Failed to resume leader election on peer %s", strAddr));
printf(String.format("Successful pause leader election on peer %s", strAddr));
}
return 0;
}
Aggregations