use of org.apache.ratis.server.raftlog.RaftLog in project incubator-ratis by apache.
the class ServerPauseResumeTest method runTestPauseResume.
void runTestPauseResume(CLUSTER cluster) throws InterruptedException, IOException {
// wait leader be elected.
final RaftServer.Division leader = waitForLeader(cluster);
RaftPeerId leaderId = leader.getId();
final List<RaftServer.Division> followers = cluster.getFollowers();
Assert.assertTrue(followers.size() >= 1);
final RaftServerImpl follower = (RaftServerImpl) followers.get(0);
SimpleMessage[] batch1 = SimpleMessage.create(100, "batch1");
Thread writeThread = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch1);
writeThread.join();
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
final RaftLog leaderLog = leader.getRaftLog();
// leader should contain all logs.
Assert.assertTrue(RaftTestUtil.logEntriesContains(leaderLog, batch1));
RaftLog followerLog = follower.getRaftLog();
// follower should contain all logs.
Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch1));
// pause follower.
boolean isSuccess = follower.pause();
Assert.assertTrue(isSuccess);
Assert.assertTrue(follower.getInfo().getLifeCycleState().isPausingOrPaused());
SimpleMessage[] batch2 = SimpleMessage.create(100, "batch2");
Thread writeThread2 = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch2);
writeThread2.join();
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
// paused follower should not have any batch2 message in its raftlog.
Assert.assertTrue(RaftTestUtil.logEntriesNotContains(followerLog, batch2));
// resume follower.
isSuccess = follower.resume();
Assert.assertTrue(isSuccess);
Assert.assertFalse(follower.getInfo().getLifeCycleState().isPausingOrPaused());
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
// follower should contain all logs.
Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch2));
}
use of org.apache.ratis.server.raftlog.RaftLog in project incubator-ratis by apache.
the class MiniRaftCluster method printAllLogs.
public String printAllLogs() {
StringBuilder b = new StringBuilder("\n#servers = " + servers.size() + "\n");
for (RaftServer.Division s : iterateDivisions()) {
b.append(" ");
b.append(s).append("\n");
final RaftLog log = s.getRaftLog();
if (log instanceof MemoryRaftLog) {
b.append(" ");
b.append(((MemoryRaftLog) log).getEntryString());
}
}
return b.toString();
}
use of org.apache.ratis.server.raftlog.RaftLog in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method runTestBootstrapReconf.
void runTestBootstrapReconf(int numNewPeer, boolean startNewPeer, CLUSTER cluster) throws Exception {
LOG.info("Originally {} peer(s), add {} more, startNewPeer={}", cluster.getNumServers(), numNewPeer, startNewPeer);
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
// submit some msgs before reconf
for (int i = 0; i < STAGING_CATCHUP_GAP * 2; i++) {
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
final PeerChanges c1 = cluster.addNewPeers(numNewPeer, startNewPeer);
LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
final AtomicReference<Boolean> success = new AtomicReference<>();
Thread clientThread = new Thread(() -> {
try {
RaftClientReply reply = client.admin().setConfiguration(c1.allPeersInNewConf);
success.set(reply.isSuccess());
} catch (IOException ioe) {
LOG.error("FAILED", ioe);
}
});
clientThread.start();
if (!startNewPeer) {
// Make sure that set configuration is run inside the thread
RaftTestUtil.waitFor(() -> clientThread.isAlive(), 300, 5000);
ONE_SECOND.sleep();
LOG.info("start new peer(s): {}", c1.newPeers);
for (RaftPeer p : c1.newPeers) {
cluster.restartServer(p.getId(), true);
}
}
RaftTestUtil.waitFor(() -> success.get() != null && success.get(), 300, 15000);
LOG.info(cluster.printServers());
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
for (RaftPeer newPeer : c1.newPeers) {
final RaftServer.Division d = cluster.getDivision(newPeer.getId());
RaftTestUtil.waitFor(() -> leaderLog.getEntries(0, Long.MAX_VALUE).length == d.getRaftLog().getEntries(0, Long.MAX_VALUE).length, 300, 15000);
Assert.assertArrayEquals(leaderLog.getEntries(0, Long.MAX_VALUE), d.getRaftLog().getEntries(0, Long.MAX_VALUE));
}
}
}
use of org.apache.ratis.server.raftlog.RaftLog in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method runTestNoChangeRequest.
void runTestNoChangeRequest(CLUSTER cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
try (final RaftClient client = cluster.createClient(leader.getId())) {
client.io().send(new SimpleMessage("m"));
final RaftLog leaderLog = leader.getRaftLog();
final long committedIndex = leaderLog.getLastCommittedIndex();
final RaftConfiguration confBefore = cluster.getLeader().getRaftConf();
// no real configuration change in the request
final RaftClientReply reply = client.admin().setConfiguration(cluster.getPeers().toArray(RaftPeer.emptyArray()));
Assert.assertTrue(reply.isSuccess());
final long newCommittedIndex = leaderLog.getLastCommittedIndex();
for (long i = committedIndex + 1; i <= newCommittedIndex; i++) {
final LogEntryProto e = leaderLog.get(i);
Assert.assertTrue(e.hasMetadataEntry());
}
Assert.assertSame(confBefore, cluster.getLeader().getRaftConf());
}
}
use of org.apache.ratis.server.raftlog.RaftLog in project incubator-ratis by apache.
the class RaftTestUtil method countEntries.
static EnumMap<LogEntryBodyCase, AtomicLong> countEntries(RaftLog raftLog) throws Exception {
final EnumMap<LogEntryBodyCase, AtomicLong> counts = new EnumMap<>(LogEntryBodyCase.class);
for (long i = 0; i < raftLog.getNextIndex(); i++) {
final LogEntryProto e = raftLog.get(i);
counts.computeIfAbsent(e.getLogEntryBodyCase(), c -> new AtomicLong()).incrementAndGet();
}
return counts;
}
Aggregations