use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method assertLeaderContent.
static void assertLeaderContent(MiniRaftCluster cluster) throws InterruptedException {
final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
Assert.assertEquals(SNAPSHOT_TRIGGER_THRESHOLD * 2, leader.getState().getLog().getLastCommittedIndex());
final LogEntryProto[] entries = SimpleStateMachine4Testing.get(leader.getProxy()).getContent();
for (int i = 1; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
Assert.assertEquals(i + 1, entries[i].getIndex());
Assert.assertArrayEquals(new SimpleMessage("m" + i).getContent().toByteArray(), entries[i].getSmLogEntry().getData().toByteArray());
}
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class TestStateMachine method testTransactionContextIsPassedBack.
@Test
public void testTransactionContextIsPassedBack() throws Throwable {
// tests that the TrxContext set by the StateMachine in Leader is passed back to the SM
properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SMTransactionContext.class, StateMachine.class);
startCluster();
int numTrx = 100;
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numTrx);
try (final RaftClient client = cluster.createClient()) {
for (RaftTestUtil.SimpleMessage message : messages) {
client.send(message);
}
}
// TODO: there eshould be a better way to ensure all data is replicated and applied
Thread.sleep(cluster.getMaxTimeout() + 100);
for (RaftServerProxy raftServer : cluster.getServers()) {
final SMTransactionContext sm = SMTransactionContext.get(raftServer);
sm.rethrowIfException();
assertEquals(numTrx, sm.numApplied.get());
}
// check leader
RaftServerImpl raftServer = cluster.getLeader();
// assert every transaction has obtained context in leader
final SMTransactionContext sm = SMTransactionContext.get(raftServer.getProxy());
List<Long> ll = sm.applied.stream().collect(Collectors.toList());
Collections.sort(ll);
assertEquals(ll.toString(), ll.size(), numTrx);
for (int i = 0; i < numTrx; i++) {
assertEquals(ll.toString(), Long.valueOf(i + 1), ll.get(i));
}
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class MiniRaftCluster method printAllLogs.
public String printAllLogs() {
StringBuilder b = new StringBuilder("\n#servers = " + servers.size() + "\n");
for (RaftServerImpl s : iterateServerImpls()) {
b.append(" ");
b.append(s).append("\n");
final RaftLog log = s.getState().getLog();
if (log instanceof MemoryRaftLog) {
b.append(" ");
b.append(((MemoryRaftLog) log).getEntryString());
}
}
return b.toString();
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftBasicTests method runTestBasicAppendEntries.
static void runTestBasicAppendEntries(boolean async, int numMessages, MiniRaftCluster cluster, Logger LOG) throws Exception {
LOG.info("runTestBasicAppendEntries: async? " + async + ", numMessages=" + numMessages);
RaftServerImpl leader = waitForLeader(cluster);
final long term = leader.getState().getCurrentTerm();
final RaftPeerId killed = cluster.getFollowers().get(0).getId();
cluster.killServer(killed);
LOG.info(cluster.printServers());
final SimpleMessage[] messages = SimpleMessage.create(numMessages);
try (final RaftClient client = cluster.createClient()) {
final AtomicInteger asyncReplyCount = new AtomicInteger();
final CompletableFuture<Void> f = new CompletableFuture<>();
for (SimpleMessage message : messages) {
if (async) {
client.sendAsync(message).thenAcceptAsync(reply -> {
if (!reply.isSuccess()) {
f.completeExceptionally(new AssertionError("Failed with reply " + reply));
} else if (asyncReplyCount.incrementAndGet() == messages.length) {
f.complete(null);
}
});
} else {
client.send(message);
}
}
if (async) {
f.join();
Assert.assertEquals(messages.length, asyncReplyCount.get());
}
}
Thread.sleep(cluster.getMaxTimeout() + 100);
LOG.info(cluster.printAllLogs());
cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.assertLogEntries(log, async, term, messages));
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftTestUtil method waitForLeader.
static RaftServerImpl waitForLeader(MiniRaftCluster cluster, final String leaderId) throws InterruptedException {
LOG.info(cluster.printServers());
for (int i = 0; !cluster.tryEnforceLeader(leaderId) && i < 10; i++) {
RaftServerImpl currLeader = cluster.getLeader();
LOG.info("try enforcing leader to " + leaderId + " but " + (currLeader == null ? "no leader for this round" : "new leader is " + currLeader.getId()));
}
LOG.info(cluster.printServers());
final RaftServerImpl leader = cluster.getLeader();
Assert.assertEquals(leaderId, leader.getId().toString());
return leader;
}
Aggregations