use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class WatchRequestTests method runTestWatchRequestTimeout.
static void runTestWatchRequestTimeout(TestParameters p) throws Exception {
final Logger LOG = p.log;
final MiniRaftCluster cluster = p.cluster;
final int numMessages = p.numMessages;
final RaftProperties properties = cluster.getProperties();
final TimeDuration watchTimeout = RaftServerConfigKeys.Watch.timeout(properties);
final TimeDuration watchTimeoutDenomination = RaftServerConfigKeys.Watch.timeoutDenomination(properties);
// blockStartTransaction of the leader so that no transaction can be committed MAJORITY
final RaftServer.Division leader = cluster.getLeader();
LOG.info("block leader {}", leader.getId());
SimpleStateMachine4Testing.get(leader).blockStartTransaction();
// blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
final List<RaftServer.Division> followers = cluster.getFollowers();
final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
LOG.info("block follower {}", blockedFollower.getId());
SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
// send a message
final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
p.sendRequests(replies, watches);
Assert.assertEquals(numMessages, replies.size());
Assert.assertEquals(numMessages, watches.size());
watchTimeout.sleep();
// for roundup error
watchTimeoutDenomination.sleep();
assertNotDone(replies);
assertNotDone(watches);
// unblock leader so that the transaction can be committed.
SimpleStateMachine4Testing.get(leader).unblockStartTransaction();
LOG.info("unblock leader {}", leader.getId());
checkMajority(replies, watches, LOG);
checkTimeout(replies, watches, LOG);
SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
LOG.info("unblock follower {}", blockedFollower.getId());
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class TestRaftLogMetrics method testRaftLogMetrics.
@Test
public void testRaftLogMetrics() throws Exception {
try (final MiniRaftCluster cluster = newCluster(NUM_SERVERS)) {
cluster.start();
runTestRaftLogMetrics(cluster);
}
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class RaftAsyncTests method runTestNoRetryWaitOnNotLeaderException.
private void runTestNoRetryWaitOnNotLeaderException(MiniRaftCluster cluster) throws Exception {
final RaftServer.Division leader = waitForLeader(cluster);
final List<RaftServer.Division> followers = cluster.getFollowers();
Assert.assertNotNull(followers);
Assert.assertEquals(2, followers.size());
Assert.assertNotSame(leader, followers.get(0));
Assert.assertNotSame(leader, followers.get(1));
// send a message to make sure that the leader is ready
try (final RaftClient client = cluster.createClient(leader.getId())) {
final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("first"));
FIVE_SECONDS.apply(f::get);
}
// if sleep interval defined by retry policy is used the test will timeout
final RetryPolicy r = event -> () -> TimeDuration.valueOf(60, TimeUnit.SECONDS);
try (final RaftClient client = cluster.createClient(followers.get(0).getId(), cluster.getGroup(), r)) {
final CompletableFuture<RaftClientReply> f = client.async().send(new SimpleMessage("abc"));
FIVE_SECONDS.apply(f::get);
} catch (TimeoutException e) {
throw new AssertionError("Failed to get async result", e);
}
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class WatchRequestTests method runTestWatchRequestAsyncChangeLeader.
static void runTestWatchRequestAsyncChangeLeader(TestParameters p) throws Exception {
final Logger LOG = p.log;
final MiniRaftCluster cluster = p.cluster;
final int numMessages = p.numMessages;
// blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
final List<RaftServer.Division> followers = cluster.getFollowers();
final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
LOG.info("block follower {}", blockedFollower.getId());
SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
p.sendRequests(replies, watches);
Assert.assertEquals(numMessages, replies.size());
Assert.assertEquals(numMessages, watches.size());
// since only one follower is blocked commit, requests can be committed MAJORITY and ALL but not ALL_COMMITTED.
checkMajority(replies, watches, LOG);
TimeUnit.SECONDS.sleep(1);
assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.allCommitted));
// Now change leader
RaftTestUtil.changeLeader(cluster, cluster.getLeader().getId());
// unblock follower so that the transaction can be replicated and committed to all.
SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
LOG.info("unblock follower {}", blockedFollower.getId());
checkAll(watches, LOG);
}
use of org.apache.ratis.server.impl.MiniRaftCluster in project incubator-ratis by apache.
the class WatchRequestTests method runTestWatchRequestAsync.
static void runTestWatchRequestAsync(TestParameters p) throws Exception {
final Logger LOG = p.log;
final MiniRaftCluster cluster = p.cluster;
final int numMessages = p.numMessages;
// blockStartTransaction of the leader so that no transaction can be committed MAJORITY
final RaftServer.Division leader = cluster.getLeader();
LOG.info("block leader {}", leader.getId());
SimpleStateMachine4Testing.get(leader).blockStartTransaction();
// blockFlushStateMachineData a follower so that no transaction can be ALL_COMMITTED
final List<RaftServer.Division> followers = cluster.getFollowers();
final RaftServer.Division blockedFollower = followers.get(ThreadLocalRandom.current().nextInt(followers.size()));
LOG.info("block follower {}", blockedFollower.getId());
SimpleStateMachine4Testing.get(blockedFollower).blockFlushStateMachineData();
// send a message
final List<CompletableFuture<RaftClientReply>> replies = new ArrayList<>();
final List<CompletableFuture<WatchReplies>> watches = new ArrayList<>();
p.sendRequests(replies, watches);
Assert.assertEquals(numMessages, replies.size());
Assert.assertEquals(numMessages, watches.size());
// since leader is blocked, nothing can be done.
TimeUnit.SECONDS.sleep(1);
assertNotDone(replies);
assertNotDone(watches);
// unblock leader so that the transaction can be committed.
SimpleStateMachine4Testing.get(leader).unblockStartTransaction();
LOG.info("unblock leader {}", leader.getId());
checkMajority(replies, watches, LOG);
Assert.assertEquals(numMessages, watches.size());
// but not replicated/committed to all.
TimeUnit.SECONDS.sleep(1);
assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.all));
assertNotDone(watches.stream().map(CompletableFuture::join).map(w -> w.allCommitted));
// unblock follower so that the transaction can be replicated and committed to all.
LOG.info("unblock follower {}", blockedFollower.getId());
SimpleStateMachine4Testing.get(blockedFollower).unblockFlushStateMachineData();
checkAll(watches, LOG);
}
Aggregations