use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testRestartPeer.
/**
* Keep generating writing traffic and make sure snapshots are taken.
* We then restart the whole raft peer and check if it can correctly load
* snapshots + raft log.
*/
@Test
public void testRestartPeer() throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
int i = 0;
try (final RaftClient client = cluster.createClient(leaderId)) {
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
LOG.info("nextIndex = {}", nextIndex);
// wait for the snapshot to be done
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
return null;
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
// restart the peer and check if it can correctly load snapshot
cluster.restart(false);
try {
// 200 messages + two leader elections --> last committed = 201
assertLeaderContent(cluster);
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class SnapshotManagementTest method runTestTakeSnapshotOnSpecificServer.
void runTestTakeSnapshotOnSpecificServer(CLUSTER cluster) throws Exception {
final RaftClientReply snapshotReply;
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftServer.Division follower = cluster.getFollowers().get(0);
final RaftPeerId followerId = follower.getId();
Assert.assertTrue(follower.getInfo().isFollower());
try (final RaftClient client = cluster.createClient(followerId)) {
for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()); i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
snapshotReply = client.getSnapshotManagementApi(followerId).create(3000);
}
Assert.assertTrue(snapshotReply.isSuccess());
final long snapshotIndex = snapshotReply.getLogIndex();
LOG.info("snapshotIndex = {} on {} server {}", snapshotIndex, follower.getInfo().getCurrentRole(), follower.getId());
final File snapshotFile = SimpleStateMachine4Testing.get(follower).getStateMachineStorage().getSnapshotFile(follower.getInfo().getCurrentTerm(), snapshotIndex);
Assert.assertTrue(snapshotFile.exists());
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class DataStreamAsyncClusterTests method runTestDataStream.
long runTestDataStream(CLUSTER cluster, int numStreams, int bufferSize, int bufferNum, boolean stepDownLeader) {
final Iterable<RaftServer> servers = CollectionUtils.as(cluster.getServers(), s -> s);
final RaftPeerId leader = cluster.getLeader().getId();
final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
try (RaftClient client = cluster.createClient(primaryServer)) {
ClientId primaryClientId = getPrimaryClientId(cluster, primaryServer);
for (int i = 0; i < numStreams; i++) {
final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer));
futures.add(CompletableFuture.supplyAsync(() -> DataStreamTestUtils.writeAndCloseAndAssertReplies(servers, leader, out, bufferSize, bufferNum, primaryClientId, client.getId(), stepDownLeader).join(), executor));
}
Assert.assertEquals(numStreams, futures.size());
return futures.stream().map(CompletableFuture::join).map(RaftClientReply::getLogIndex).max(Long::compareTo).orElseThrow(IllegalStateException::new);
} catch (IOException e) {
throw new CompletionException(e);
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class DataStreamClusterTests method runTestDataStreamOutput.
void runTestDataStreamOutput(CLUSTER cluster) 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();
// write using DataStreamOutput
DataStreamTestUtils.writeAndAssertReplies(out, 1000, 10);
}
}
watchOrSleep(cluster, reply.join().getLogIndex());
assertLogEntry(cluster, request);
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method runTestKillLeaderDuringReconf.
void runTestKillLeaderDuringReconf(CLUSTER cluster) throws Exception {
final AtomicBoolean clientRunning = new AtomicBoolean(true);
Thread clientThread = null;
try {
final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
PeerChanges c1 = cluster.addNewPeers(1, false);
PeerChanges c2 = cluster.removePeers(1, false, asList(c1.newPeers));
LOG.info("Start setConf: {}", asList(c2.allPeersInNewConf));
LOG.info(cluster.printServers());
final CompletableFuture<Void> setConf = new CompletableFuture<>();
clientThread = new Thread(() -> {
try (final RaftClient client = cluster.createClient(leaderId)) {
for (int i = 0; clientRunning.get() && !setConf.isDone(); i++) {
final RaftClientReply reply = client.admin().setConfiguration(c2.allPeersInNewConf);
if (reply.isSuccess()) {
setConf.complete(null);
}
LOG.info("setConf attempt #{} failed, {}", i, cluster.printServers());
}
} catch (Exception e) {
LOG.error("Failed to setConf", e);
setConf.completeExceptionally(e);
}
});
clientThread.start();
TimeUnit.SECONDS.sleep(1);
// the leader cannot generate the (old, new) conf, and it will keep
// bootstrapping the 2 new peers since they have not started yet
Assert.assertFalse(((RaftConfigurationImpl) cluster.getLeader().getRaftConf()).isTransitional());
// only (0) the first conf entry, (1) the 1st setConf entry and (2) a metadata entry
{
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
for (LogEntryProto e : RaftTestUtil.getLogEntryProtos(leaderLog)) {
LOG.info("{}", LogProtoUtils.toLogEntryString(e));
}
final long commitIndex = leaderLog.getLastCommittedIndex();
Assert.assertTrue("commitIndex = " + commitIndex + " > 2", commitIndex <= 2);
}
final RaftPeerId killed = RaftTestUtil.waitAndKillLeader(cluster);
Assert.assertEquals(leaderId, killed);
final RaftPeerId newLeaderId = RaftTestUtil.waitForLeader(cluster).getId();
LOG.info("newLeaderId: {}", newLeaderId);
LOG.info("start new peers: {}", Arrays.asList(c1.newPeers));
for (RaftPeer np : c1.newPeers) {
cluster.restartServer(np.getId(), false);
}
try {
setConf.get(10, TimeUnit.SECONDS);
} catch (TimeoutException ignored) {
}
// the client fails with the first leader, and then retry the same setConfiguration request
waitAndCheckNewConf(cluster, c2.allPeersInNewConf, 2, Collections.singletonList(leaderId));
setConf.get(1, TimeUnit.SECONDS);
} finally {
if (clientThread != null) {
clientRunning.set(false);
clientThread.interrupt();
}
}
}
Aggregations