use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class TestRaftStateMachineException method testHandleStateMachineException.
@Test
public void testHandleStateMachineException() throws Exception {
setAndStart(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
client.send(new SimpleMessage("m"));
fail("Exception expected");
} catch (StateMachineException e) {
e.printStackTrace();
Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception"));
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class TestRaftStateMachineException method testRetryOnExceptionDuringReplication.
@Test
public void testRetryOnExceptionDuringReplication() throws Exception {
setAndStart(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(true).getId();
// turn on the preAppend failure switch
failPreAppend = true;
final RaftClient client = cluster.createClient(leaderId);
final RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final long seqNum = 111;
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message"));
RaftClientReply reply = rpc.sendRequest(r);
Objects.requireNonNull(reply.getStateMachineException());
RetryCache.CacheEntry oldEntry = RaftServerTestUtil.getRetryEntry(cluster.getLeader(), client.getId(), callId);
Assert.assertNotNull(oldEntry);
Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(oldEntry));
// retry
reply = rpc.sendRequest(r);
Objects.requireNonNull(reply.getStateMachineException());
RetryCache.CacheEntry currentEntry = RaftServerTestUtil.getRetryEntry(cluster.getLeader(), client.getId(), callId);
Assert.assertNotNull(currentEntry);
Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(currentEntry));
Assert.assertNotEquals(oldEntry, currentEntry);
failPreAppend = false;
client.close();
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method testNoChangeRequest.
/**
* When a request's new configuration is the same with the current one, make
* sure we return success immediately and no log entry is recorded.
*/
@Test
public void testNoChangeRequest() throws Exception {
LOG.info("Start testNoChangeRequest");
// originally 3 peers
final MiniRaftCluster cluster = getCluster(3);
try {
cluster.start();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
client.send(new SimpleMessage("m"));
final long committedIndex = cluster.getLeader().getState().getLog().getLastCommittedIndex();
final RaftConfiguration confBefore = cluster.getLeader().getRaftConf();
// no real configuration change in the request
RaftClientReply reply = client.setConfiguration(cluster.getPeers().toArray(new RaftPeer[0]));
Assert.assertTrue(reply.isSuccess());
Assert.assertEquals(committedIndex, cluster.getLeader().getState().getLog().getLastCommittedIndex());
Assert.assertSame(confBefore, cluster.getLeader().getRaftConf());
client.close();
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage 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.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method testReconfTwice.
@Test(timeout = 30000)
public void testReconfTwice() throws Exception {
LOG.info("Start testReconfTwice");
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());
}
final AtomicBoolean reconf1 = new AtomicBoolean(false);
final AtomicBoolean reconf2 = new AtomicBoolean(false);
final AtomicReference<RaftPeer[]> finalPeers = new AtomicReference<>(null);
final AtomicReference<RaftPeer[]> deadPeers = new AtomicReference<>(null);
CountDownLatch latch = new CountDownLatch(1);
Thread clientThread = new Thread(() -> {
try {
PeerChanges c1 = cluster.addNewPeers(2, true);
LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
RaftClientReply reply = client.setConfiguration(c1.allPeersInNewConf);
reconf1.set(reply.isSuccess());
PeerChanges c2 = cluster.removePeers(2, true, asList(c1.newPeers));
finalPeers.set(c2.allPeersInNewConf);
deadPeers.set(c2.removedPeers);
LOG.info("Start changing the configuration again: {}", asList(c2.allPeersInNewConf));
reply = client.setConfiguration(c2.allPeersInNewConf);
reconf2.set(reply.isSuccess());
latch.countDown();
client.close();
} catch (IOException ignored) {
}
});
clientThread.start();
latch.await();
Assert.assertTrue(reconf1.get());
Assert.assertTrue(reconf2.get());
waitAndCheckNewConf(cluster, finalPeers.get(), 2, null);
// check configuration manager's internal state
// each reconf will generate two configurations: (old, new) and (new)
cluster.getServerAliveStream().forEach(server -> {
ConfigurationManager confManager = (ConfigurationManager) Whitebox.getInternalState(server.getState(), "configurationManager");
// each reconf will generate two configurations: (old, new) and (new)
Assert.assertEquals(5, confManager.numOfConf());
});
} finally {
cluster.shutdown();
}
}
Aggregations