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

The following examples show how to use org.apache.ratis.util.TimeDuration#valueOf() . 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: TestExponentialBackoffRetry.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testExponentialBackoffRetry() {
  TimeDuration baseSleep = TimeDuration.valueOf(1, TimeUnit.SECONDS);
  TimeDuration maxSleep = TimeDuration.valueOf(40, TimeUnit.SECONDS);

  // Test maxAttempts
  ExponentialBackoffRetry retryPolicy = createPolicy(baseSleep, null, 1);
  Assert.assertFalse(retryPolicy.handleAttemptFailure(() -> 1).shouldRetry());

  try {
    // baseSleep should not be null
    createPolicy(null, null, 1);
    Assert.fail("Policy creation should have failed");
  } catch (Throwable t) {
  }

  // test policy without max sleep
  retryPolicy = createPolicy(baseSleep, null,100);
  assertSleep(retryPolicy, baseSleep, null);

  // test policy with max sleep
  retryPolicy = createPolicy(baseSleep, maxSleep,100);
  assertSleep(retryPolicy, baseSleep, maxSleep);
}
 
Example 3
Source File: BaseServer.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Sets common Ratis server properties for both the log and metadata state machines.
 */
void setRaftProperties(RaftProperties properties) {
  // Set the ports for the server
  GrpcConfigKeys.Server.setPort(properties, opts.getPort());
  NettyConfigKeys.Server.setPort(properties, opts.getPort());

  // Ozone sets the leader election timeout (min) to 1second.
  long leaderElectionTimeoutMinVal = getLeaderElectionTimeoutMin();
  TimeDuration leaderElectionTimeoutMin = TimeDuration.valueOf(leaderElectionTimeoutMinVal,
    TimeUnit.MILLISECONDS);
  RaftServerConfigKeys.Rpc.setTimeoutMin(properties, leaderElectionTimeoutMin);
  long leaderElectionTimeoutMaxVal = getLeaderElectionTimeoutMax();

  TimeDuration leaderElectionMaxTimeout = TimeDuration.valueOf(
    leaderElectionTimeoutMaxVal,
      TimeUnit.MILLISECONDS);
  RaftServerConfigKeys.Rpc.setTimeoutMax(properties, leaderElectionMaxTimeout);
}
 
Example 4
Source File: ServerImplUtils.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
public static RaftServerProxy newRaftServer(
    RaftPeerId id, StateMachine.Registry stateMachineRegistry, RaftProperties properties, Parameters parameters)
    throws IOException {
  final TimeDuration sleepTime = TimeDuration.valueOf(500, TimeUnit.MILLISECONDS);
  final RaftServerProxy proxy;
  try {
    // attempt multiple times to avoid temporary bind exception
    proxy = JavaUtils.attemptRepeatedly(
        () -> new RaftServerProxy(id, stateMachineRegistry, properties, parameters),
        5, sleepTime, "new RaftServerProxy", RaftServerProxy.LOG);
  } catch (InterruptedException e) {
    throw IOUtils.toInterruptedIOException(
        "Interrupted when creating RaftServer " + id, e);
  }
  return proxy;
}
 
Example 5
Source File: TestRetryCacheMetrics.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() {
  RaftServerImpl raftServer = mock(RaftServerImpl.class);

  RaftGroupId raftGroupId = RaftGroupId.randomId();
  RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
  RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId
      .valueOf(raftPeerId, raftGroupId);
  when(raftServer.getMemberId()).thenReturn(raftGroupMemberId);

  retryCache = new RetryCache(TimeDuration.valueOf(60, TimeUnit.SECONDS));
  when(raftServer.getRetryCache()).thenReturn(retryCache);

  RaftServerMetrics raftServerMetrics = RaftServerMetrics
      .getRaftServerMetrics(raftServer);
  ratisMetricRegistry = raftServerMetrics.getRegistry();
}
 
Example 6
Source File: RaftBasicTests.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void testRequestTimeout(boolean async, MiniRaftCluster cluster, Logger LOG) throws Exception {
  waitForLeader(cluster);
  long time = System.currentTimeMillis();
  try (final RaftClient client = cluster.createClient()) {
    // Get the next callId to be used by the client
    long callId = RaftClientTestUtil.getCallId(client);
    // Create an entry corresponding to the callId and clientId
    // in each server's retry cache.
    cluster.getServerAliveStream().forEach(
        raftServer -> RetryCacheTestUtil.getOrCreateEntry(raftServer.getRetryCache(), client.getId(), callId));
    // Client request for the callId now waits
    // as there is already a cache entry in the server for the request.
    // Ideally the client request should timeout and the client should retry.
    // The retry is successful when the retry cache entry for the corresponding callId and clientId expires.
    if (async) {
      CompletableFuture<RaftClientReply> replyFuture = client.sendAsync(new SimpleMessage("abc"));
      replyFuture.get();
    } else {
      client.send(new SimpleMessage("abc"));
    }
    // Eventually the request would be accepted by the server
    // when the retry cache entry is invalidated.
    // The duration for which the client waits should be more than the retryCacheExpiryDuration.
    TimeDuration duration = TimeDuration.valueOf(System.currentTimeMillis() - time, TimeUnit.MILLISECONDS);
    TimeDuration retryCacheExpiryDuration = RaftServerConfigKeys.RetryCache.expiryTime(cluster.getProperties());
    Assert.assertTrue(duration.compareTo(retryCacheExpiryDuration) >= 0);
  }
}
 
Example 7
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 8
Source File: RaftProperties.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Return time duration in the given time unit. Valid units are encoded in
 * properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
 * (ms), seconds (s), minutes (m), hours (h), and days (d).
 * @param name Property name
 * @param defaultValue Value returned if no mapping exists.
 * @throws NumberFormatException If the property stripped of its unit is not
 *         a number
 */
public TimeDuration getTimeDuration(
    String name, TimeDuration defaultValue, TimeUnit defaultUnit) {
  final String value = getTrimmed(name);
  if (null == value) {
    return defaultValue;
  }
  try {
    return TimeDuration.valueOf(value, defaultUnit);
  } catch(NumberFormatException e) {
    throw new IllegalArgumentException("Failed to parse "
        + name + " = " + value, e);
  }
}
 
Example 9
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 10
Source File: RaftProperties.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Return time duration in the given time unit. Valid units are encoded in
 * properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
 * (ms), seconds (s), minutes (m), hours (h), and days (d).
 * @param name Property name
 * @param defaultValue Value returned if no mapping exists.
 * @throws NumberFormatException If the property stripped of its unit is not
 *         a number
 */
public TimeDuration getTimeDuration(
    String name, TimeDuration defaultValue, TimeUnit defaultUnit) {
  final String value = getTrimmed(name);
  if (null == value) {
    return defaultValue;
  }
  try {
    return TimeDuration.valueOf(value, defaultUnit);
  } catch(NumberFormatException e) {
    throw new IllegalArgumentException("Failed to parse "
        + name + " = " + value, e);
  }
}
 
Example 11
Source File: MultipleLinearRandomRetry.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static TimeDuration parsePositiveTime(String s) {
  final TimeDuration t = TimeDuration.valueOf(s, TimeUnit.MILLISECONDS);
  if (t.isNonPositive()) {
    throw new IllegalArgumentException("Non-positive value: " + t);
  }
  return t;
}
 
Example 12
Source File: OzoneUtils.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Return the TimeDuration configured for the given key. If not configured,
 * return the default value.
 */
public static TimeDuration getTimeDuration(ConfigurationSource conf,
    String key,
    TimeDuration defaultValue) {
  TimeUnit defaultTimeUnit = defaultValue.getUnit();
  long timeDurationInDefaultUnit = conf.getTimeDuration(key,
      defaultValue.getDuration(), defaultTimeUnit);
  return TimeDuration.valueOf(timeDurationInDefaultUnit, defaultTimeUnit);
}
 
Example 13
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void setTimeoutForRetryCache(RaftProperties properties) {
  TimeUnit timeUnit;
  long duration;
  timeUnit =
      OzoneConfigKeys.DFS_RATIS_SERVER_RETRY_CACHE_TIMEOUT_DURATION_DEFAULT
          .getUnit();
  duration = conf.getTimeDuration(
      OzoneConfigKeys.DFS_RATIS_SERVER_RETRY_CACHE_TIMEOUT_DURATION_KEY,
      OzoneConfigKeys.DFS_RATIS_SERVER_RETRY_CACHE_TIMEOUT_DURATION_DEFAULT
          .getDuration(), timeUnit);
  final TimeDuration retryCacheTimeout =
      TimeDuration.valueOf(duration, timeUnit);
  RaftServerConfigKeys.RetryCache
      .setExpiryTime(properties, retryCacheTimeout);
}
 
Example 14
Source File: RaftReconfigurationBaseTest.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void runTestRevertConfigurationChange(CLUSTER cluster) throws Exception {
  RaftLog log2 = null;
  try {
    RaftTestUtil.waitForLeader(cluster);

    final RaftServerImpl leader = cluster.getLeader();
    final RaftPeerId leaderId = leader.getId();

    final RaftLog log = leader.getState().getLog();
    log2 = log;
    Thread.sleep(1000);

    // we block the incoming msg for the leader and block its requests to
    // followers, so that we force the leader change and the old leader will
    // not know
    LOG.info("start blocking the leader");
    BlockRequestHandlingInjection.getInstance().blockReplier(leaderId.toString());
    cluster.setBlockRequestsFrom(leaderId.toString(), true);

    PeerChanges change = cluster.removePeers(1, false, new ArrayList<>());

    AtomicBoolean gotNotLeader = new AtomicBoolean(false);
    final Thread clientThread = new Thread(() -> {
      try(final RaftClient client = cluster.createClient(leaderId)) {
        LOG.info("client starts to change conf");
        final RaftClientRpc sender = client.getClientRpc();
        RaftClientReply reply = sender.sendRequest(cluster.newSetConfigurationRequest(
            client.getId(), leaderId, change.allPeersInNewConf));
        if (reply.getNotLeaderException() != null) {
          gotNotLeader.set(true);
        }
      } catch (IOException e) {
        LOG.warn("Got unexpected exception when client1 changes conf", e);
      }
    });
    clientThread.start();

    // find ConfigurationEntry
    final TimeDuration sleepTime = TimeDuration.valueOf(500, TimeUnit.MILLISECONDS);
    final long confIndex = JavaUtils.attemptRepeatedly(() -> {
      final long last = log.getLastEntryTermIndex().getIndex();
      for (long i = last; i >= 1; i--) {
        if (log.get(i).hasConfigurationEntry()) {
          return i;
        }
      }
      throw new Exception("ConfigurationEntry not found: last=" + last);
    }, 10, sleepTime, "confIndex", LOG);

    // wait till the old leader persist the new conf
    JavaUtils.attemptRepeatedly(() -> {
      Assert.assertTrue(log.getFlushIndex() >= confIndex);
      return null;
    }, 10, sleepTime, "FLUSH", LOG);
    final long committed = log.getLastCommittedIndex();
    Assert.assertTrue(committed < confIndex);

    // unblock the old leader
    BlockRequestHandlingInjection.getInstance().unblockReplier(leaderId.toString());
    cluster.setBlockRequestsFrom(leaderId.toString(), false);

    // the client should get NotLeaderException
    clientThread.join(5000);
    Assert.assertTrue(gotNotLeader.get());

    // the old leader should have truncated the setConf from the log
    JavaUtils.attemptRepeatedly(() -> {
      Assert.assertTrue(log.getLastCommittedIndex() >= confIndex);
      return null;
    }, 10, ONE_SECOND, "COMMIT", LOG);
    Assert.assertTrue(log.get(confIndex).hasConfigurationEntry());
    log2 = null;
  } finally {
    RaftStorageTestUtils.printLog(log2, s -> LOG.info(s));
  }
}
 
Example 15
Source File: RetryCacheTestUtil.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public static RetryCache createRetryCache(){
  return new RetryCache(TimeDuration.valueOf(60, TimeUnit.SECONDS));
}
 
Example 16
Source File: RetryCacheTestUtil.java    From ratis with Apache License 2.0 4 votes vote down vote up
public static RetryCache createRetryCache(){
  return new RetryCache(5000, TimeDuration.valueOf(60, TimeUnit.SECONDS));
}
 
Example 17
Source File: ServerRestartTests.java    From ratis with Apache License 2.0 4 votes vote down vote up
void runTestRestartCommitIndex(MiniRaftCluster cluster) throws Exception {
  final TimeDuration sleepTime = TimeDuration.valueOf(100, TimeUnit.MILLISECONDS);
  final SimpleMessage[] messages = SimpleMessage.create(10);
  try (final RaftClient client = cluster.createClient()) {
    for(SimpleMessage m : messages) {
      Assert.assertTrue(client.send(m).isSuccess());
    }
  }

  final List<RaftPeerId> ids = new ArrayList<>();
  final RaftLog leaderLog = cluster.getLeader().getState().getLog();
  final RaftPeerId leaderId = leaderLog.getSelfId();
  ids.add(leaderId);

  // check that the last logged commit index is equal to the index of the last committed StateMachineLogEntry
  final long lastIndex = leaderLog.getLastEntryTermIndex().getIndex();
  LOG.info("{}: leader lastIndex={}", leaderId, lastIndex);
  JavaUtils.attempt(() -> leaderLog.getLastCommittedIndex() == lastIndex,
      10, sleepTime, "leader(commitIndex == lastIndex)", LOG);

  final LogEntryProto lastEntry = leaderLog.get(lastIndex);
  LOG.info("{}: leader lastEntry entry[{}] = {}", leaderId, lastIndex, ServerProtoUtils.toLogEntryString(lastEntry));
  Assert.assertTrue(lastEntry.hasMetadataEntry());
  final long loggedCommitIndex = lastEntry.getMetadataEntry().getCommitIndex();
  for(long i = lastIndex - 1; i > loggedCommitIndex; i--) {
    final LogEntryProto entry = leaderLog.get(i);
    LOG.info("{}: leader entry[{}] =  {}", leaderId, i, ServerProtoUtils.toLogEntryString(entry));
    Assert.assertFalse(entry.hasStateMachineLogEntry());
  }
  final LogEntryProto lastCommittedEntry = leaderLog.get(loggedCommitIndex);
  LOG.info("{}: leader lastCommittedEntry = entry[{}] = {}",
      leaderId, loggedCommitIndex, ServerProtoUtils.toLogEntryString(lastCommittedEntry));
  Assert.assertTrue(lastCommittedEntry.hasStateMachineLogEntry());

  // check follower logs
  for(RaftServerImpl s : cluster.iterateServerImpls()) {
    if (!s.getId().equals(leaderId)) {
      ids.add(s.getId());
      RaftTestUtil.assertSameLog(leaderLog, s.getState().getLog());
    }
  }

  // kill all servers
  ids.forEach(cluster::killServer);

  // Restart and kill servers one by one so that they won't talk to each other.
  for(RaftPeerId id : ids) {
    cluster.restartServer(id, false);
    final RaftServerImpl server = cluster.getRaftServerImpl(id);
    final RaftLog raftLog = server.getState().getLog();
    JavaUtils.attempt(() -> raftLog.getLastCommittedIndex() >= loggedCommitIndex,
        10, sleepTime, id + "(commitIndex >= loggedCommitIndex)", LOG);
    JavaUtils.attempt(() -> server.getState().getLastAppliedIndex() >= loggedCommitIndex,
        10, sleepTime, id + "(lastAppliedIndex >= loggedCommitIndex)", LOG);
    LOG.info("{}: commitIndex={}, lastAppliedIndex={}",
        id, raftLog.getLastCommittedIndex(), server.getState().getLastAppliedIndex());
    cluster.killServer(id);
  }
}