use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class MiniRaftCluster method setConfiguration.
public void setConfiguration(RaftPeer... peers) throws IOException {
final RaftServerImpl leader = getLeader();
final SetConfigurationRequest r = newSetConfigurationRequest(ClientId.randomId(), leader.getId(), peers);
LOG.info("Start changing the configuration: {}", r);
leader.setConfiguration(r);
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftBasicTests method testOldLeaderNotCommit.
@Test
public void testOldLeaderNotCommit() throws Exception {
LOG.info("Running testOldLeaderNotCommit");
final MiniRaftCluster cluster = getCluster();
final RaftPeerId leaderId = waitForLeader(cluster).getId();
List<RaftServerImpl> followers = cluster.getFollowers();
final RaftServerImpl followerToCommit = followers.get(0);
for (int i = 1; i < NUM_SERVERS - 1; i++) {
RaftServerImpl follower = followers.get(i);
cluster.killServer(follower.getId());
}
SimpleMessage[] messages = SimpleMessage.create(1);
sendMessageInNewThread(cluster, messages);
Thread.sleep(cluster.getMaxTimeout() + 100);
logEntriesContains(followerToCommit.getState().getLog(), messages);
cluster.killServer(leaderId);
cluster.killServer(followerToCommit.getId());
for (int i = 1; i < NUM_SERVERS - 1; i++) {
RaftServerImpl follower = followers.get(i);
cluster.restartServer(follower.getId(), false);
}
waitForLeader(cluster);
Thread.sleep(cluster.getMaxTimeout() + 100);
final Predicate<LogEntryProto> predicate = l -> l.getTerm() != 1;
cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.checkLogEntries(log, messages, predicate));
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftBasicTests method testOldLeaderCommit.
@Test
public void testOldLeaderCommit() throws Exception {
LOG.info("Running testOldLeaderCommit");
final MiniRaftCluster cluster = getCluster();
final RaftServerImpl leader = waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
final long term = leader.getState().getCurrentTerm();
List<RaftServerImpl> followers = cluster.getFollowers();
final RaftServerImpl followerToSendLog = followers.get(0);
for (int i = 1; i < NUM_SERVERS - 1; i++) {
RaftServerImpl follower = followers.get(i);
cluster.killServer(follower.getId());
}
SimpleMessage[] messages = SimpleMessage.create(1);
RaftTestUtil.sendMessageInNewThread(cluster, messages);
Thread.sleep(cluster.getMaxTimeout() + 100);
RaftLog followerLog = followerToSendLog.getState().getLog();
assertTrue(logEntriesContains(followerLog, messages));
LOG.info(String.format("killing old leader: %s", leaderId.toString()));
cluster.killServer(leaderId);
for (int i = 1; i < 3; i++) {
RaftServerImpl follower = followers.get(i);
LOG.info(String.format("restarting follower: %s", follower.getId().toString()));
cluster.restartServer(follower.getId(), false);
}
Thread.sleep(cluster.getMaxTimeout() * 5);
// confirm the server with log is elected as new leader.
final RaftPeerId newLeaderId = waitForLeader(cluster).getId();
Assert.assertEquals(followerToSendLog.getId(), newLeaderId);
cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.assertLogEntries(log, false, term, messages));
LOG.info("terminating testOldLeaderCommit test");
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftBasicTests method testWithLoad.
public 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 RaftServerImpl 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().filter(Client4TestWithLoad::isRunning).count() == 0) {
break;
}
final int n = clients.stream().mapToInt(c -> c.step.get()).sum();
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 {
RaftServerImpl 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.getServers(), c.messages);
}
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftTestUtil method waitAndKillLeader.
static String waitAndKillLeader(MiniRaftCluster cluster, boolean expectLeader) throws InterruptedException {
final RaftServerImpl leader = waitForLeader(cluster);
if (!expectLeader) {
Assert.assertNull(leader);
} else {
Assert.assertNotNull(leader);
LOG.info("killing leader = " + leader);
cluster.killServer(leader.getId());
}
return leader != null ? leader.getId().toString() : null;
}
Aggregations