use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class ParameterizedBaseTest method setAndStart.
/**
* Set {@link #currentCluster} to the given cluster and start it if {@link #currentCluster} is changed.
*/
public static void setAndStart(MiniRaftCluster cluster) throws InterruptedException, IOException {
final MiniRaftCluster previous = currentCluster.getAndSet(cluster);
if (previous != cluster) {
if (previous != null) {
previous.shutdown();
}
cluster.start();
RaftTestUtil.waitForLeader(cluster);
}
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method getSnapshotFiles.
public static List<File> getSnapshotFiles(MiniRaftCluster cluster, long startIndex, long endIndex) {
final RaftServer.Division leader = cluster.getLeader();
final SimpleStateMachineStorage storage = SimpleStateMachine4Testing.get(leader).getStateMachineStorage();
final long term = leader.getInfo().getCurrentTerm();
return LongStream.range(startIndex, endIndex).mapToObj(i -> storage.getSnapshotFile(term, i)).collect(Collectors.toList());
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class TestGrpcMessageMetrics method testGrpcMessageMetrics.
@Test
public void testGrpcMessageMetrics() throws Exception {
try (final MiniRaftCluster cluster = newCluster(NUM_SERVERS)) {
cluster.start();
sendMessages(cluster);
}
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class LogAppenderTests method testFollowerHeartbeatMetric.
@Test
public void testFollowerHeartbeatMetric() throws IOException, InterruptedException {
// Start a 3 node Ratis ring.
final MiniRaftCluster cluster = newCluster(3);
cluster.start();
final RaftServer.Division leaderServer = waitForLeader(cluster);
// Write 10 messages to leader.
try (RaftClient client = cluster.createClient(leaderServer.getId())) {
for (int i = 1; i <= 10; i++) {
client.io().send(new RaftTestUtil.SimpleMessage("Msg to make leader ready " + i));
}
} catch (IOException e) {
throw e;
}
final RatisMetricRegistry ratisMetricRegistry = ((RaftServerMetricsImpl) leaderServer.getRaftServerMetrics()).getRegistry();
// Get all last_heartbeat_elapsed_time metric gauges. Should be equal to number of followers.
SortedMap<String, Gauge> heartbeatElapsedTimeGauges = ratisMetricRegistry.getGauges((s, metric) -> s.contains("lastHeartbeatElapsedTime"));
assertTrue(heartbeatElapsedTimeGauges.size() == 2);
for (RaftServer.Division followerServer : cluster.getFollowers()) {
String followerId = followerServer.getId().toString();
Gauge metric = heartbeatElapsedTimeGauges.entrySet().parallelStream().filter(e -> e.getKey().contains(followerId)).iterator().next().getValue();
// Metric for this follower exists.
assertTrue(metric != null);
// Metric in nanos > 0.
assertTrue((long) metric.getValue() > 0);
// Try to get Heartbeat metrics for follower.
final RaftServerMetricsImpl followerMetrics = (RaftServerMetricsImpl) followerServer.getRaftServerMetrics();
// Metric should not exist. It only exists in leader.
assertTrue(followerMetrics.getRegistry().getGauges((s, m) -> s.contains("lastHeartbeatElapsedTime")).isEmpty());
for (boolean heartbeat : new boolean[] { true, false }) {
assertTrue(followerMetrics.getFollowerAppendEntryTimer(heartbeat).getMeanRate() > 0.0d);
assertTrue(followerMetrics.getFollowerAppendEntryTimer(heartbeat).getCount() > 0L);
}
}
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class RaftBasicTests method testWithLoad.
static void testWithLoad(final int numClients, final int numMessages, boolean useAsync, MiniRaftCluster cluster, Logger LOG) throws Exception {
LOG.info("Running testWithLoad: numClients=" + numClients + ", numMessages=" + numMessages + ", async=" + useAsync);
waitForLeader(cluster);
final List<Client4TestWithLoad> clients = Stream.iterate(0, i -> i + 1).limit(numClients).map(i -> new Client4TestWithLoad(i, numMessages, useAsync, cluster, LOG)).collect(Collectors.toList());
final AtomicInteger lastStep = new AtomicInteger();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
private int previousLastStep = lastStep.get();
@Override
public void run() {
LOG.info(cluster.printServers());
LOG.info(BlockRequestHandlingInjection.getInstance().toString());
LOG.info(cluster.toString());
clients.forEach(c -> LOG.info(" " + c));
JavaUtils.dumpAllThreads(s -> LOG.info(s));
final int last = lastStep.get();
if (last != previousLastStep) {
previousLastStep = last;
} else {
final RaftServer.Division leader = cluster.getLeader();
LOG.info("NO PROGRESS at " + last + ", try to restart leader=" + leader);
if (leader != null) {
try {
cluster.restartServer(leader.getId(), false);
LOG.info("Restarted leader=" + leader);
} catch (IOException e) {
LOG.error("Failed to restart leader=" + leader);
}
}
}
}
}, 5_000L, 10_000L);
clients.forEach(Thread::start);
int count = 0;
for (; ; ) {
if (clients.stream().noneMatch(Client4TestWithLoad::isRunning)) {
break;
}
final int n = clients.stream().mapToInt(c -> c.step.get()).sum();
Assert.assertTrue(n >= lastStep.get());
if (n - lastStep.get() < 50 * numClients) {
// Change leader at least 50 steps.
Thread.sleep(10);
continue;
}
lastStep.set(n);
count++;
try {
RaftServer.Division leader = cluster.getLeader();
if (leader != null) {
RaftTestUtil.changeLeader(cluster, leader.getId());
}
} catch (IllegalStateException e) {
LOG.error("Failed to change leader ", e);
}
}
LOG.info("Leader change count=" + count);
timer.cancel();
for (Client4TestWithLoad c : clients) {
if (c.exceptionInClientThread.get() != null) {
throw new AssertionError(c.exceptionInClientThread.get());
}
RaftTestUtil.assertLogEntries(cluster, c.messages);
}
}
Aggregations