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

The following examples show how to use org.apache.ratis.util.TimeDuration#toLong() . 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: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private void setRatisLeaderElectionTimeout(RaftProperties properties) {
  long duration;
  TimeUnit leaderElectionMinTimeoutUnit =
      OzoneConfigKeys.
          DFS_RATIS_LEADER_ELECTION_MINIMUM_TIMEOUT_DURATION_DEFAULT
          .getUnit();
  duration = conf.getTimeDuration(
      OzoneConfigKeys.DFS_RATIS_LEADER_ELECTION_MINIMUM_TIMEOUT_DURATION_KEY,
      OzoneConfigKeys.
          DFS_RATIS_LEADER_ELECTION_MINIMUM_TIMEOUT_DURATION_DEFAULT
          .getDuration(), leaderElectionMinTimeoutUnit);
  final TimeDuration leaderElectionMinTimeout =
      TimeDuration.valueOf(duration, leaderElectionMinTimeoutUnit);
  RaftServerConfigKeys.Rpc
      .setTimeoutMin(properties, leaderElectionMinTimeout);
  long leaderElectionMaxTimeout =
      leaderElectionMinTimeout.toLong(TimeUnit.MILLISECONDS) + 200;
  RaftServerConfigKeys.Rpc.setTimeoutMax(properties,
      TimeDuration.valueOf(leaderElectionMaxTimeout, TimeUnit.MILLISECONDS));
}
 
Example 2
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 3
Source File: TestExponentialBackoffRetry.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void assertSleep(ExponentialBackoffRetry retryPolicy,
    TimeDuration baseSleep, TimeDuration maxSleep) {
  for (int i = 1; i <= 50; i++) {
    int attempt = i;
    RetryPolicy.Action action = retryPolicy.handleAttemptFailure(() -> attempt);

    // sleep time based on geometric progresssion
    long d = (1L << attempt) * baseSleep.toLong(TimeUnit.MILLISECONDS);
    d = Math.min(d, maxSleep != null ? maxSleep.toLong(TimeUnit.MILLISECONDS) : Long.MAX_VALUE);

    // sleep time with randomness added
    long randomizedDuration = action.getSleepTime().toLong(TimeUnit.MILLISECONDS);

    Assert.assertTrue(action.shouldRetry());
    Assert.assertTrue(randomizedDuration >= d * 0.5);
    Assert.assertTrue(randomizedDuration <= d * 1.5);
  }
}
 
Example 4
Source File: TestClientProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void print(String name, TimeDuration t, int n) {
  final long ns = t.toLong(TimeUnit.NANOSECONDS);
  System.out.printf("%s: avg = %s (total = %s)%n", name, ns2String(ns/n), ns2String(ns));
}
 
Example 5
Source File: RaftClientImpl.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public boolean isRequestTimeout(TimeDuration timeout) {
  if (timeout == null) {
    return false;
  }
  return System.currentTimeMillis() - creationTimeInMs > timeout.toLong(TimeUnit.MILLISECONDS);
}