use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RetryCacheTests method runTestRetryOnNewLeader.
void runTestRetryOnNewLeader(CLUSTER cluster) throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, new SimpleMessage("message"));
assertReply(rpc.sendRequest(r), client, callId);
final long oldLastApplied = cluster.getLeader().getInfo().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);
final RaftPeerId newLeaderId = JavaUtils.attemptRepeatedly(() -> {
final RaftPeerId id = RaftTestUtil.waitForLeader(cluster).getId();
Assert.assertNotEquals(leaderId, id);
return id;
}, 10, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS), "wait for a leader different than " + leaderId, LOG);
Assert.assertNotEquals(leaderId, newLeaderId);
// same clientId and callId in the request
r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, new SimpleMessage("message"));
rpc.addRaftPeers(Arrays.asList(change.newPeers));
for (int i = 0; i < 10; i++) {
try {
assertReply(rpc.sendRequest(r), client, callId);
LOG.info("successfully sent out the retry request_" + i);
} catch (Exception e) {
LOG.info("hit exception while retrying the same request: " + r, e);
}
Thread.sleep(100);
}
// check the new leader and make sure the retry did not get committed
Assert.assertEquals(0, count(cluster.getLeader().getRaftLog(), oldLastApplied + 1));
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftAsyncExceptionTests method runTestTimeoutException.
private void runTestTimeoutException(CLUSTER cluster) throws Exception {
// send a message to make sure the cluster is working
try (RaftClient client = cluster.createClient()) {
final RaftClientReply reply = client.io().send(new SimpleMessage("m0"));
Assert.assertTrue(reply.isSuccess());
RaftClientConfigKeys.Rpc.setRequestTimeout(properties.get(), ONE_SECOND);
// Block StartTransaction
StreamSupport.stream(cluster.getServers().spliterator(), false).map(cluster::getDivision).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockStartTransaction);
final CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("m1"));
FIVE_SECONDS.sleep();
// Unblock StartTransaction
StreamSupport.stream(cluster.getServers().spliterator(), false).map(cluster::getDivision).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockStartTransaction);
// The request should succeed after start transaction is unblocked
Assert.assertTrue(replyFuture.get(FIVE_SECONDS.getDuration(), FIVE_SECONDS.getUnit()).isSuccess());
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftAsyncExceptionTests method runTestGroupMismatchException.
private void runTestGroupMismatchException(CLUSTER cluster) throws Exception {
// send a message to make sure the cluster is working
try (RaftClient client = cluster.createClient()) {
final RaftClientReply reply = client.async().send(new SimpleMessage("first")).get();
Assert.assertTrue(reply.isSuccess());
}
// create another group
final RaftGroup clusterGroup = cluster.getGroup();
final RaftGroup anotherGroup = RaftGroup.valueOf(RaftGroupId.randomId(), clusterGroup.getPeers());
Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());
// create another client using another group
final SimpleMessage[] messages = SimpleMessage.create(5);
try (RaftClient client = cluster.createClient(anotherGroup)) {
// send a few messages
final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
for (SimpleMessage m : messages) {
futures.add(client.async().send(m));
}
Assert.assertEquals(messages.length, futures.size());
// check replies
final Iterator<CompletableFuture<RaftClientReply>> i = futures.iterator();
testFailureCase("First reply is GroupMismatchException", () -> i.next().get(), ExecutionException.class, GroupMismatchException.class);
for (; i.hasNext(); ) {
testFailureCase("Following replies are AlreadyClosedException caused by GroupMismatchException", () -> i.next().get(), ExecutionException.class, AlreadyClosedException.class, GroupMismatchException.class);
}
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftBasicTests method testStateMachineMetrics.
public static void testStateMachineMetrics(boolean async, MiniRaftCluster cluster, Logger LOG) throws Exception {
RaftServer.Division leader = waitForLeader(cluster);
try (final RaftClient client = cluster.createClient()) {
Gauge appliedIndexGauge = getStatemachineGaugeWithName(leader, STATEMACHINE_APPLIED_INDEX_GAUGE);
Gauge smAppliedIndexGauge = getStatemachineGaugeWithName(leader, STATEMACHINE_APPLY_COMPLETED_GAUGE);
long appliedIndexBefore = (Long) appliedIndexGauge.getValue();
long smAppliedIndexBefore = (Long) smAppliedIndexGauge.getValue();
checkFollowerCommitLagsLeader(cluster);
if (async) {
CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("abc"));
replyFuture.get();
} else {
client.io().send(new SimpleMessage("abc"));
}
long appliedIndexAfter = (Long) appliedIndexGauge.getValue();
long smAppliedIndexAfter = (Long) smAppliedIndexGauge.getValue();
checkFollowerCommitLagsLeader(cluster);
Assert.assertTrue("StateMachine Applied Index not incremented", appliedIndexAfter > appliedIndexBefore);
Assert.assertTrue("StateMachine Apply completed Index not incremented", smAppliedIndexAfter > smAppliedIndexBefore);
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftExceptionBaseTest method testHandleNotLeaderException.
private void testHandleNotLeaderException(boolean killNewLeader) throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
RaftClientReply reply = client.send(new SimpleMessage("m1"));
Assert.assertTrue(reply.isSuccess());
// enforce leader change
RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId);
if (killNewLeader) {
// kill the new leader
cluster.killServer(newLeader);
}
RaftClientRpc rpc = client.getClientRpc();
reply = null;
for (int i = 0; reply == null && i < 10; i++) {
try {
reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), leaderId, new SimpleMessage("m2")));
} 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());
reply = client.send(new SimpleMessage("m3"));
Assert.assertTrue(reply.isSuccess());
client.close();
}
Aggregations