use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class DataStreamAsyncClusterTests method runTestDataStream.
void runTestDataStream(CLUSTER cluster, boolean stepDownLeader) throws Exception {
RaftTestUtil.waitForLeader(cluster);
final List<CompletableFuture<Long>> futures = new ArrayList<>();
futures.add(CompletableFuture.supplyAsync(() -> runTestDataStream(cluster, 5, 10, 1_000_000, 10, stepDownLeader), executor));
futures.add(CompletableFuture.supplyAsync(() -> runTestDataStream(cluster, 2, 20, 1_000, 5_000, stepDownLeader), executor));
final long maxIndex = futures.stream().map(CompletableFuture::join).max(Long::compareTo).orElseThrow(IllegalStateException::new);
if (stepDownLeader) {
final RaftPeerId oldLeader = cluster.getLeader().getId();
final CompletableFuture<RaftPeerId> changeLeader = futures.get(0).thenApplyAsync(dummy -> {
try {
return RaftTestUtil.changeLeader(cluster, oldLeader);
} catch (Exception e) {
throw new CompletionException("Failed to change leader from " + oldLeader, e);
}
});
LOG.info("Changed leader from {} to {}", oldLeader, changeLeader.join());
}
// wait for all servers to catch up
try (RaftClient client = cluster.createClient()) {
RaftClientReply reply = client.async().watch(maxIndex, ReplicationLevel.ALL).join();
Assert.assertTrue(reply.isSuccess());
}
// assert all streams are linked
for (RaftServer proxy : cluster.getServers()) {
final RaftServer.Division impl = proxy.getDivision(cluster.getGroupId());
final MultiDataStreamStateMachine stateMachine = (MultiDataStreamStateMachine) impl.getStateMachine();
for (SingleDataStream s : stateMachine.getStreams()) {
Assert.assertFalse(s.getDataChannel().isOpen());
DataStreamTestUtils.assertLogEntry(impl, s);
}
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class DataStreamClusterTests method runTestWriteFile.
void runTestWriteFile(CLUSTER cluster, int i, CheckedConsumer<DataStreamOutputImpl, Exception> testCase) throws Exception {
final RaftClientRequest request;
final CompletableFuture<RaftClientReply> reply;
final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
try (RaftClient client = cluster.createClient(primaryServer)) {
try (final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer))) {
request = out.getHeader();
reply = out.getRaftClientReplyFuture();
final Timestamp start = Timestamp.currentTime();
testCase.accept(out);
LOG.info("{}: {} elapsed {}ms", i, testCase, start.elapsedTimeMs());
}
}
watchOrSleep(cluster, reply.join().getLogIndex());
assertLogEntry(cluster, request);
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestNettyDataStreamWithMock method testMockCluster.
private void testMockCluster(int numServers, RaftException leaderException, IllegalStateException submitException, IOException getStateMachineException) throws Exception {
List<RaftServer> raftServers = new ArrayList<>();
ClientId clientId = ClientId.randomId();
RaftGroupId groupId = RaftGroupId.randomId();
for (int i = 0; i < numServers; i++) {
RaftServer raftServer = mock(RaftServer.class);
RaftPeerId peerId = RaftPeerId.valueOf("s" + i);
RaftProperties properties = new RaftProperties();
NettyConfigKeys.DataStream.setPort(properties, NetUtils.createLocalServerAddress().getPort());
RaftConfigKeys.DataStream.setType(properties, SupportedDataStreamType.NETTY);
when(raftServer.getProperties()).thenReturn(properties);
when(raftServer.getId()).thenReturn(peerId);
when(raftServer.getPeer()).thenReturn(RaftPeer.newBuilder().setId(peerId).build());
if (getStateMachineException == null) {
RaftClient client = Mockito.mock(RaftClient.class);
when(client.getId()).thenReturn(clientId);
AsyncRpcApi asyncRpcApi = Mockito.mock(AsyncRpcApi.class);
when(client.async()).thenReturn(asyncRpcApi);
final RaftServer.Division myDivision = mockDivision(raftServer, client);
when(raftServer.getDivision(Mockito.any(RaftGroupId.class))).thenReturn(myDivision);
if (submitException != null) {
when(asyncRpcApi.sendForward(Mockito.any(RaftClientRequest.class))).thenThrow(submitException);
} else if (i == 0) {
// primary
when(asyncRpcApi.sendForward(Mockito.any(RaftClientRequest.class))).thenAnswer((Answer<CompletableFuture<RaftClientReply>>) invocation -> {
final RaftClientRequest r = (RaftClientRequest) invocation.getArguments()[0];
final RaftClientReply.Builder b = RaftClientReply.newBuilder().setRequest(r);
final RaftClientReply reply = leaderException != null ? b.setException(leaderException).build() : b.setSuccess().setMessage(() -> DataStreamTestUtils.MOCK).build();
return CompletableFuture.completedFuture(reply);
});
}
} else {
when(raftServer.getDivision(Mockito.any(RaftGroupId.class))).thenThrow(getStateMachineException);
}
raftServers.add(raftServer);
}
runTestMockCluster(groupId, raftServers, clientId, 1_000_000, 10, submitException != null ? submitException : leaderException, getStateMachineException);
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RemoveCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
final List<RaftPeerId> ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), (a, b) -> {
});
try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
final List<RaftPeer> remaining = getRaftGroup().getPeers().stream().filter(raftPeer -> !ids.contains(raftPeer.getId())).collect(Collectors.toList());
System.out.println("New peer list: " + remaining);
RaftClientReply reply = client.admin().setConfiguration(remaining);
processReply(reply, () -> "Failed to change raft peer");
}
return 0;
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TakeSnapshotCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
long timeout;
final RaftPeerId peerId;
if (cl.hasOption(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME)) {
timeout = Long.parseLong(cl.getOptionValue(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME));
} else {
timeout = 3000;
}
try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
if (cl.hasOption(PEER_ID_OPTION_NAME)) {
peerId = RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
} else {
peerId = null;
}
RaftClientReply reply = raftClient.getSnapshotManagementApi(peerId).create(timeout);
processReply(reply, () -> String.format("Failed to take snapshot of peerId %s", peerId));
printf(String.format("Successful take snapshot on peerId %s, the latest snapshot index is %d", peerId, reply.getLogIndex()));
}
return 0;
}
Aggregations