use of org.apache.ratis.server.storage.RaftLog 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.storage.RaftLog in project incubator-ratis by apache.
the class TestRaftStream method testSimpleWrite.
@Test
public void testSimpleWrite() throws Exception {
final int numRequests = 500;
LOG.info("Running testSimpleWrite, numRequests=" + numRequests);
// default 64K is too large for a test
GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(4));
cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(NUM_SERVERS, prop);
cluster.start();
RaftServerImpl leader = waitForLeader(cluster);
try (RaftOutputStream out = new RaftOutputStream(prop, ClientId.randomId(), cluster.getGroup(), leader.getId())) {
for (int i = 0; i < numRequests; i++) {
// generate requests
out.write(toBytes(i));
}
}
// check the leader's raft log
final RaftLog raftLog = leader.getState().getLog();
final AtomicInteger i = new AtomicInteger();
checkLog(raftLog, numRequests, () -> toBytes(i.getAndIncrement()));
}
use of org.apache.ratis.server.storage.RaftLog in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method testRevertConfigurationChange.
/**
* Test a scenario where the follower truncates its log entries which causes
* configuration change.
*/
@Test
public void testRevertConfigurationChange() throws Exception {
LOG.info("Start testRevertConfigurationChange");
RaftLog log2 = null;
final MiniRaftCluster cluster = getCluster(5);
try {
cluster.start();
RaftTestUtil.waitForLeader(cluster);
final RaftServerImpl leader = cluster.getLeader();
final RaftPeerId leaderId = leader.getId();
final RaftLog log = leader.getState().getLog();
log2 = log;
Thread.sleep(1000);
// we block the incoming msg for the leader and block its requests to
// followers, so that we force the leader change and the old leader will
// not know
LOG.info("start blocking the leader");
BlockRequestHandlingInjection.getInstance().blockReplier(leaderId.toString());
cluster.setBlockRequestsFrom(leaderId.toString(), true);
PeerChanges change = cluster.removePeers(1, false, new ArrayList<>());
AtomicBoolean gotNotLeader = new AtomicBoolean(false);
final Thread clientThread = new Thread(() -> {
try (final RaftClient client = cluster.createClient(leaderId)) {
LOG.info("client starts to change conf");
final RaftClientRpc sender = client.getClientRpc();
RaftClientReply reply = sender.sendRequest(cluster.newSetConfigurationRequest(client.getId(), leaderId, change.allPeersInNewConf));
if (reply.getNotLeaderException() != null) {
gotNotLeader.set(true);
}
} catch (IOException e) {
LOG.warn("Got unexpected exception when client1 changes conf", e);
}
});
clientThread.start();
// find CONFIGURATIONENTRY, there may be NOOP before and after it.
final long confIndex = JavaUtils.attempt(() -> {
final long last = log.getLastEntryTermIndex().getIndex();
for (long i = 1; i <= last; i++) {
if (log.get(i).getLogEntryBodyCase() == CONFIGURATIONENTRY) {
return i;
}
}
throw new Exception("CONFIGURATIONENTRY not found: last=" + last);
}, 10, 500, "confIndex", LOG);
// wait till the old leader persist the new conf
JavaUtils.attempt(() -> log.getLatestFlushedIndex() >= confIndex, 10, 500L, "FLUSH", LOG);
final long committed = log.getLastCommittedIndex();
Assert.assertTrue(committed < confIndex);
// unblock the old leader
BlockRequestHandlingInjection.getInstance().unblockReplier(leaderId.toString());
cluster.setBlockRequestsFrom(leaderId.toString(), false);
// the client should get NotLeaderException
clientThread.join(5000);
Assert.assertTrue(gotNotLeader.get());
// the old leader should have truncated the setConf from the log
JavaUtils.attempt(() -> log.getLastCommittedIndex() >= confIndex, 10, 500L, "COMMIT", LOG);
Assert.assertEquals(NOOP, log.get(confIndex).getLogEntryBodyCase());
log2 = null;
} finally {
RaftStorageTestUtils.printLog(log2, s -> LOG.info(s));
cluster.shutdown();
}
}
use of org.apache.ratis.server.storage.RaftLog in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method testBootstrapReconf.
@Test
public void testBootstrapReconf() throws Exception {
LOG.info("Start testBootstrapReconf");
// originally 3 peers
final MiniRaftCluster cluster = getCluster(3);
cluster.start();
try {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
// submit some msgs before reconf
for (int i = 0; i < getStagingGap() * 2; i++) {
RaftClientReply reply = client.send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
PeerChanges c1 = cluster.addNewPeers(2, true);
LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
final AtomicReference<Boolean> success = new AtomicReference<>();
Thread clientThread = new Thread(() -> {
try {
RaftClientReply reply = client.setConfiguration(c1.allPeersInNewConf);
success.set(reply.isSuccess());
client.close();
} catch (IOException ioe) {
LOG.error("FAILED", ioe);
}
});
clientThread.start();
Thread.sleep(5000);
LOG.info(cluster.printServers());
assertSuccess(success);
final RaftLog leaderLog = cluster.getLeader().getState().getLog();
for (RaftPeer newPeer : c1.newPeers) {
Assert.assertArrayEquals(leaderLog.getEntries(0, Long.MAX_VALUE), cluster.getServer(newPeer.getId()).getImpl().getState().getLog().getEntries(0, Long.MAX_VALUE));
}
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.server.storage.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 (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();
}
Aggregations