use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftStateMachineExceptionTests method runTestRetryOnStateMachineException.
private void runTestRetryOnStateMachineException(CLUSTER cluster) throws Exception {
RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
cluster.getLeaderAndSendFirstMessage(true);
final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
try (final RaftClient client = cluster.createClient(leaderId)) {
final RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final SimpleMessage message = new SimpleMessage("message");
final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, message);
RaftClientReply reply = rpc.sendRequest(r);
Assert.assertFalse(reply.isSuccess());
Assert.assertNotNull(reply.getStateMachineException());
// retry with the same callId
for (int i = 0; i < 5; i++) {
reply = rpc.sendRequest(r);
Assert.assertEquals(client.getId(), reply.getClientId());
Assert.assertEquals(callId, reply.getCallId());
Assert.assertFalse(reply.isSuccess());
Assert.assertNotNull(reply.getStateMachineException());
}
for (RaftServer.Division server : cluster.iterateDivisions()) {
LOG.info("check server " + server.getId());
JavaUtils.attemptRepeatedly(() -> {
Assert.assertNotNull(RetryCacheTestUtil.get(server, client.getId(), callId));
return null;
}, 5, BaseTest.ONE_SECOND, "GetRetryEntry", LOG);
final RaftLog log = server.getRaftLog();
RaftTestUtil.logEntriesContains(log, oldLastApplied + 1, log.getNextIndex(), message);
}
cluster.shutdown();
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage 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.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testBasicInstallSnapshot.
/**
* Basic test for install snapshot: start a one node cluster and let it
* generate a snapshot. Then delete the log and restart the node, and add more
* nodes as followers.
*/
@Test
public void testBasicInstallSnapshot() throws Exception {
final List<LogSegmentPath> logs;
int i = 0;
try {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// wait for the snapshot to be done
final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
LOG.info("nextIndex = {}", nextIndex);
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
return null;
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
verifyTakeSnapshotMetric(cluster.getLeader());
logs = LogSegmentPath.getLogSegmentPaths(cluster.getLeader().getRaftStorage());
} finally {
cluster.shutdown();
}
// delete the log segments from the leader
for (LogSegmentPath path : logs) {
FileUtils.delete(path.getPath());
}
// restart the peer
LOG.info("Restarting the cluster");
cluster.restart(false);
try {
assertLeaderContent(cluster);
// generate some more traffic
try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
Assert.assertTrue(client.io().send(new SimpleMessage("m" + i)).isSuccess());
}
// add two more peers
String[] newPeers = new String[] { "s3", "s4" };
MiniRaftCluster.PeerChanges change = cluster.addNewPeers(newPeers, true, false);
// trigger setConfiguration
cluster.setConfiguration(change.allPeersInNewConf);
for (String newPeer : newPeers) {
final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
}
// Verify installSnapshot counter on leader before restart.
verifyInstallSnapshotMetric(cluster.getLeader());
RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
Timer timer = getTakeSnapshotTimer(cluster.getLeader());
long count = timer.getCount();
// restart the peer and check if it can correctly handle conf change
cluster.restartServer(cluster.getLeader().getId(), false);
assertLeaderContent(cluster);
// verify that snapshot was taken when stopping the server
Assert.assertTrue(count < timer.getCount());
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method testLeaderNotReadyException.
/**
* Delay the commit of the leader placeholder log entry and see if the client
* can correctly receive and handle the LeaderNotReadyException.
*/
@Test
public void testLeaderNotReadyException() throws Exception {
LOG.info("Start testLeaderNotReadyException");
final MiniRaftCluster cluster = newCluster(1).initServers();
try {
// delay 1s for each logSync call
cluster.getServers().forEach(peer -> leaderPlaceHolderDelay.setDelayMs(peer.getId().toString(), 2000));
cluster.start();
AtomicBoolean caughtNotReady = new AtomicBoolean(false);
AtomicBoolean success = new AtomicBoolean(false);
final RaftPeerId leaderId = cluster.getPeers().iterator().next().getId();
new Thread(() -> {
try (final RaftClient client = cluster.createClient(leaderId)) {
final RaftClientRpc sender = client.getClientRpc();
final RaftClientRequest request = cluster.newRaftClientRequest(client.getId(), leaderId, new SimpleMessage("test"));
while (!success.get()) {
try {
final RaftClientReply reply = sender.sendRequest(request);
success.set(reply.isSuccess());
if (reply.getException() != null && reply.getException() instanceof LeaderNotReadyException) {
caughtNotReady.set(true);
}
} catch (IOException e) {
LOG.info("Hit other IOException", e);
}
if (!success.get()) {
try {
Thread.sleep(200);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
}
} catch (Exception ignored) {
LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
}
}).start();
RaftTestUtil.waitForLeader(cluster);
for (int i = 0; !success.get() && i < 5; i++) {
Thread.sleep(1000);
}
Assert.assertTrue(success.get());
Assert.assertTrue(caughtNotReady.get());
} finally {
leaderPlaceHolderDelay.clear();
cluster.shutdown();
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage 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));
}
}
}
Aggregations