Java Code Examples for org.apache.ratis.util.TimeDuration#sleep()

The following examples show how to use org.apache.ratis.util.TimeDuration#sleep() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRaftServerNoLeaderTimeout.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeaderElectionDetection() throws Exception {
  RaftTestUtil.waitForLeader(cluster);
  final TimeDuration noLeaderTimeout = RaftServerConfigKeys.Notification.noLeaderTimeout(cluster.getProperties());

  RaftServerImpl healthyFollower = cluster.getFollowers().get(1);
  RaftServerImpl failedFollower = cluster.getFollowers().get(0);
  // fail the leader and one of the followers to that quorum is not present
  // for next leader election to succeed.
  cluster.killServer(failedFollower.getId());
  cluster.killServer(cluster.getLeader().getId());

  // Wait to ensure that leader election is triggered and also state machine callback is triggered
  noLeaderTimeout.sleep();
  noLeaderTimeout.sleep();

  RaftProtos.RoleInfoProto roleInfoProto =
      SimpleStateMachine4Testing.get(healthyFollower).getLeaderElectionTimeoutInfo();
  Assert.assertNotNull(roleInfoProto);

  Assert.assertEquals(roleInfoProto.getRole(), RaftProtos.RaftPeerRole.CANDIDATE);
  final long noLeaderTimeoutMs = noLeaderTimeout.toLong(TimeUnit.MILLISECONDS);
  Assert.assertTrue(roleInfoProto.getCandidateInfo().getLastLeaderElapsedTimeMs() > noLeaderTimeoutMs);
}
 
Example 2
Source File: LeaderElectionTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testLateServerStart() throws Exception {
  final int numServer = 3;
  LOG.info("Running testLateServerStart");
  final MiniRaftCluster cluster = newCluster(numServer);
  cluster.initServers();

  // start all except one servers
  final Iterator<RaftServerProxy> i = cluster.getServers().iterator();
  for(int j = 1; j < numServer; j++) {
    i.next().start();
  }

  final RaftServerImpl leader = waitForLeader(cluster);
  final TimeDuration sleepTime = TimeDuration.valueOf(3, TimeUnit.SECONDS);
  LOG.info("sleep " + sleepTime);
  sleepTime.sleep();

  // start the last server
  final RaftServerProxy lastServer = i.next();
  lastServer.start();
  final RaftPeerId lastServerLeaderId = JavaUtils.attemptRepeatedly(
      () -> Optional.ofNullable(lastServer.getImpls().iterator().next().getState().getLeaderId())
          .orElseThrow(() -> new IllegalStateException("No leader yet")),
      10, ONE_SECOND, "getLeaderId", LOG);
  LOG.info(cluster.printServers());
  Assert.assertEquals(leader.getId(), lastServerLeaderId);
}
 
Example 3
Source File: LeaderElectionTests.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testLateServerStart() throws Exception {
  final int numServer = 3;
  LOG.info("Running testLateServerStart");
  final MiniRaftCluster cluster = newCluster(numServer);
  cluster.initServers();

  // start all except one servers
  final Iterator<RaftServerProxy> i = cluster.getServers().iterator();
  for(int j = 1; j < numServer; j++) {
    i.next().start();
  }

  final RaftServerImpl leader = waitForLeader(cluster);
  final TimeDuration sleepTime = TimeDuration.valueOf(3, TimeUnit.SECONDS);
  LOG.info("sleep " + sleepTime);
  sleepTime.sleep();

  // start the last server
  final RaftServerProxy lastServer = i.next();
  lastServer.start();
  final RaftPeerId lastServerLeaderId = JavaUtils.attempt(
      () -> getLeader(lastServer.getImpls().iterator().next().getState()),
      10, 1000, "getLeaderId", LOG);
  LOG.info(cluster.printServers());
  Assert.assertEquals(leader.getId(), lastServerLeaderId);
}
 
Example 4
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
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 RaftServerImpl 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<RaftServerImpl> followers = cluster.getFollowers();
  final RaftServerImpl 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();
  watchTimeoutDenomination.sleep(); // for roundup error
  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());
}
 
Example 5
Source File: WatchRequestTests.java    From ratis with Apache License 2.0 4 votes vote down vote up
static void runTestWatchRequestTimeout(TestParameters p) throws Exception {
  final Logger LOG = p.log;
  final MiniRaftCluster cluster = p.cluster;
  final int numMessages = p.numMessages;

  final TimeDuration watchTimeout = RaftServerConfigKeys.watchTimeout(cluster.getProperties());
  final TimeDuration watchTimeoutDenomination = RaftServerConfigKeys.watchTimeoutDenomination(cluster.getProperties());

  // blockStartTransaction of the leader so that no transaction can be committed MAJORITY
  final RaftServerImpl 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<RaftServerImpl> followers = cluster.getFollowers();
  final RaftServerImpl 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();
  watchTimeoutDenomination.sleep(); // for roundup error
  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());
}