use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class TransferLeadership method start.
CompletableFuture<RaftClientReply> start(TransferLeadershipRequest request) {
final MemoizedSupplier<PendingRequest> supplier = JavaUtils.memoize(() -> new PendingRequest(request));
final PendingRequest previous = pending.getAndUpdate(f -> f != null ? f : supplier.get());
if (previous != null) {
if (request.getNewLeader().equals(previous.getRequest().getNewLeader())) {
final CompletableFuture<RaftClientReply> replyFuture = new CompletableFuture<>();
previous.getReplyFuture().whenComplete((r, e) -> {
if (e != null) {
replyFuture.completeExceptionally(e);
} else {
replyFuture.complete(r.isSuccess() ? server.newSuccessReply(request) : server.newExceptionReply(request, r.getException()));
}
});
return replyFuture;
} else {
final TransferLeadershipException tle = new TransferLeadershipException(server.getMemberId() + "Failed to transfer leadership to " + request.getNewLeader() + ": a previous " + previous + " exists");
return CompletableFuture.completedFuture(server.newExceptionReply(request, tle));
}
}
scheduler.onTimeout(TimeDuration.valueOf(request.getTimeoutMs(), TimeUnit.MILLISECONDS), () -> finish(server.getState().getLeaderId(), true), LOG, () -> "Timeout check failed for append entry request: " + request);
return supplier.get().getReplyFuture();
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class TestCounter method testSeveralCounter.
@Test
public void testSeveralCounter() throws IOException, InterruptedException {
setAndStart(cluster);
try (final RaftClient client = cluster.createClient()) {
for (int i = 0; i < 10; i++) {
client.io().send(Message.valueOf("INCREMENT"));
}
RaftClientReply reply1 = client.io().sendReadOnly(Message.valueOf("GET"));
Assert.assertEquals("10", reply1.getMessage().getContent().toString(Charset.defaultCharset()));
for (int i = 0; i < 10; i++) {
client.io().send(Message.valueOf("INCREMENT"));
}
RaftClientReply reply2 = client.io().sendReadOnly(Message.valueOf("GET"));
Assert.assertEquals("20", reply2.getMessage().getContent().toString(Charset.defaultCharset()));
for (int i = 0; i < 10; i++) {
client.io().send(Message.valueOf("INCREMENT"));
}
RaftClientReply reply3 = client.io().sendReadOnly(Message.valueOf("GET"));
Assert.assertEquals("30", reply3.getMessage().getContent().toString(Charset.defaultCharset()));
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class DataStreamManagement method replyDataStreamException.
static void replyDataStreamException(RaftServer server, Throwable cause, RaftClientRequest raftClientRequest, DataStreamRequestByteBuf request, ChannelHandlerContext ctx) {
final RaftClientReply reply = RaftClientReply.newBuilder().setRequest(raftClientRequest).setException(new DataStreamException(server.getId(), cause)).build();
sendDataStreamException(cause, request, reply, ctx);
}
use of org.apache.ratis.protocol.RaftClientReply 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.protocol.RaftClientReply 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);
}
Aggregations