use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftExceptionBaseTest method testLogAppenderBufferCapacity.
@Test
public void testLogAppenderBufferCapacity() throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
byte[] bytes = new byte[8192];
Arrays.fill(bytes, (byte) 1);
SimpleMessage msg = new SimpleMessage(new String(bytes));
try {
client.send(msg);
Assert.fail("Expected StateMachineException not thrown");
} catch (StateMachineException sme) {
Assert.assertTrue(sme.getMessage().contains("exceeds the max buffer limit"));
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftExceptionBaseTest method testNotLeaderExceptionWithReconf.
@Test
public void testNotLeaderExceptionWithReconf() throws Exception {
Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster));
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
// enforce leader change
RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId);
// also add two new peers
// add two more peers
MiniRaftCluster.PeerChanges change = cluster.addNewPeers(new String[] { "ss1", "ss2" }, true);
// trigger setConfiguration
LOG.info("Start changing the configuration: {}", Arrays.asList(change.allPeersInNewConf));
try (final RaftClient c2 = cluster.createClient(newLeader)) {
RaftClientReply reply = c2.setConfiguration(change.allPeersInNewConf);
Assert.assertTrue(reply.isSuccess());
}
LOG.info(cluster.printServers());
RaftClientRpc rpc = client.getClientRpc();
RaftClientReply reply = null;
// it is possible that the remote peer's rpc server is not ready. need retry
for (int i = 0; reply == null && i < 10; i++) {
try {
reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), leaderId, new SimpleMessage("m1")));
} catch (IOException ignored) {
Thread.sleep(1000);
}
}
Assert.assertNotNull(reply);
Assert.assertFalse(reply.isSuccess());
final NotLeaderException nle = reply.getNotLeaderException();
Objects.requireNonNull(nle);
Assert.assertEquals(newLeader, nle.getSuggestedLeader().getId());
Collection<RaftPeer> peers = cluster.getPeers();
RaftPeer[] peersFromReply = reply.getNotLeaderException().getPeers();
Assert.assertEquals(peers.size(), peersFromReply.length);
for (RaftPeer p : peersFromReply) {
Assert.assertTrue(peers.contains(p));
}
reply = client.send(new SimpleMessage("m2"));
Assert.assertTrue(reply.isSuccess());
client.close();
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RetryCacheTests method testRetryOnNewLeader.
/**
* Test retry while the leader changes to another peer
*/
@Test
public void testRetryOnNewLeader() throws Exception {
final MiniRaftCluster cluster = getCluster();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
final RaftClient client = cluster.createClient(leaderId);
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);
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
// trigger the reconfiguration, make sure the original leader is kicked out
PeerChanges change = cluster.addNewPeers(2, true);
RaftPeer[] allPeers = cluster.removePeers(2, true, asList(change.newPeers)).allPeersInNewConf;
// trigger setConfiguration
cluster.setConfiguration(allPeers);
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId newLeaderId = cluster.getLeader().getId();
Assert.assertNotEquals(leaderId, newLeaderId);
// same clientId and callId in the request
r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, seqNum, new SimpleMessage("message"));
for (int i = 0; i < 10; i++) {
try {
reply = rpc.sendRequest(r);
LOG.info("successfully sent out the retry request_" + i);
Assert.assertEquals(client.getId(), reply.getClientId());
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
} catch (Exception e) {
LOG.info("hit exception while retrying the same request: " + e);
}
Thread.sleep(100);
}
// check the new leader and make sure the retry did not get committed
Assert.assertEquals(oldLastApplied + 3, cluster.getLeader().getState().getLastAppliedIndex());
client.close();
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RetryCacheTests method testBasicRetry.
/**
* make sure the retry cache can correct capture the retry from a client,
* and returns the result from the previous request
*/
@Test
public void testBasicRetry() throws Exception {
final MiniRaftCluster cluster = getCluster();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
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);
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
// 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.assertTrue(reply.isSuccess());
}
long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex();
// make sure retry cache has the entry
for (RaftServerImpl server : cluster.iterateServerImpls()) {
LOG.info("check server " + server.getId());
if (server.getState().getLastAppliedIndex() < leaderApplied) {
Thread.sleep(1000);
}
Assert.assertEquals(2, RaftServerTestUtil.getRetryCacheSize(server));
Assert.assertNotNull(RaftServerTestUtil.getRetryEntry(server, client.getId(), callId));
// make sure there is only one log entry committed
Assert.assertEquals(oldLastApplied + 1, server.getState().getLastAppliedIndex());
}
client.close();
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class TestRaftStateMachineException method testRetryOnStateMachineException.
@Test
public void testRetryOnStateMachineException() throws Exception {
setAndStart(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(true).getId();
long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
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);
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());
}
long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex();
// make sure retry cache has the entry
for (RaftServerImpl server : cluster.iterateServerImpls()) {
LOG.info("check server " + server.getId());
if (server.getState().getLastAppliedIndex() < leaderApplied) {
Thread.sleep(1000);
}
Assert.assertNotNull(RaftServerTestUtil.getRetryEntry(server, client.getId(), callId));
Assert.assertEquals(oldLastApplied + 1, server.getState().getLastAppliedIndex());
}
client.close();
}
Aggregations