use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class LeaderElectionTests method testTransferLeaderTimeout.
@Test
public void testTransferLeaderTimeout() throws Exception {
try (final MiniRaftCluster cluster = newCluster(3)) {
cluster.start();
final RaftServer.Division leader = waitForLeader(cluster);
try (RaftClient client = cluster.createClient(leader.getId())) {
List<RaftServer.Division> followers = cluster.getFollowers();
Assert.assertEquals(followers.size(), 2);
RaftServer.Division newLeader = followers.get(0);
// isolate new leader, so that transfer leadership will timeout
isolate(cluster, newLeader.getId());
List<RaftPeer> peers = cluster.getPeers();
List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
Assert.assertTrue(reply.isSuccess());
CompletableFuture<Boolean> transferTimeoutFuture = CompletableFuture.supplyAsync(() -> {
try {
long timeoutMs = 5000;
long start = System.currentTimeMillis();
try {
client.admin().transferLeadership(newLeader.getId(), timeoutMs);
} catch (TransferLeadershipException e) {
long cost = System.currentTimeMillis() - start;
Assert.assertTrue(cost > timeoutMs);
Assert.assertTrue(e.getMessage().contains("Failed to transfer leadership to"));
Assert.assertTrue(e.getMessage().contains("timed out"));
}
return true;
} catch (IOException e) {
return false;
}
});
// before transfer timeout, leader should in steppingDown
JavaUtils.attemptRepeatedly(() -> {
try {
client.io().send(new RaftTestUtil.SimpleMessage("message"));
} catch (LeaderSteppingDownException e) {
Assert.assertTrue(e.getMessage().contains("is stepping down"));
}
return null;
}, 5, TimeDuration.ONE_SECOND, "check leader steppingDown", RaftServer.LOG);
Assert.assertTrue(transferTimeoutFuture.get());
// after transfer timeout, leader should accept request
reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
Assert.assertTrue(reply.getReplierId().equals(leader.getId().toString()));
Assert.assertTrue(reply.isSuccess());
deIsolate(cluster, newLeader.getId());
}
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class InstallSnapshotNotificationTests method testInstallSnapshotDuringBootstrap.
private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exception {
leaderSnapshotInfoRef.set(null);
numSnapshotRequests.set(0);
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 RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// wait for the snapshot to be done
final RaftServer.Division leader = cluster.getLeader();
final long nextIndex = leader.getRaftLog().getNextIndex();
LOG.info("nextIndex = {}", nextIndex);
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
return null;
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
RaftSnapshotBaseTest.assertLeaderContent(cluster);
final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
final boolean set = leaderSnapshotInfoRef.compareAndSet(null, leaderSnapshotInfo);
Assert.assertTrue(set);
// add two more peers
final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true, true);
// trigger setConfiguration
cluster.setConfiguration(change.allPeersInNewConf);
RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
// leader snapshot.
for (RaftServer.Division follower : cluster.getFollowers()) {
Assert.assertEquals(leaderSnapshotInfo.getIndex(), RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
}
// Make sure each new peer got one snapshot notification.
Assert.assertEquals(2, numSnapshotRequests.get());
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply 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.protocol.RaftClientReply 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.protocol.RaftClientReply 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);
}
}
Aggregations