org.apache.ratis.retry.RetryPolicy Java Examples

The following examples show how to use org.apache.ratis.retry.RetryPolicy. 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: RatisPipelineUtils.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Sends ratis command to destroy pipeline on the given datanode.
 *
 * @param dn         - Datanode on which pipeline needs to be destroyed
 * @param pipelineID - ID of pipeline to be destroyed
 * @param ozoneConf  - Ozone configuration
 * @param grpcTlsConfig - grpc tls configuration
 * @throws IOException
 */
static void destroyPipeline(DatanodeDetails dn, PipelineID pipelineID,
    ConfigurationSource ozoneConf, GrpcTlsConfig grpcTlsConfig)
    throws IOException {
  final String rpcType = ozoneConf
      .get(ScmConfigKeys.DFS_CONTAINER_RATIS_RPC_TYPE_KEY,
          ScmConfigKeys.DFS_CONTAINER_RATIS_RPC_TYPE_DEFAULT);
  final RetryPolicy retryPolicy = RatisHelper.createRetryPolicy(ozoneConf);
  final RaftPeer p = RatisHelper.toRaftPeer(dn);
  try(RaftClient client = RatisHelper
      .newRaftClient(SupportedRpcType.valueOfIgnoreCase(rpcType), p,
          retryPolicy, grpcTlsConfig, ozoneConf)) {
    client.groupRemove(RaftGroupId.valueOf(pipelineID.getId()),
        true, p.getId());
  }
}
 
Example #2
Source File: RaftClientImpl.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
RaftClientImpl(ClientId clientId, RaftGroup group, RaftPeerId leaderId,
    RaftClientRpc clientRpc, RaftProperties properties, RetryPolicy retryPolicy) {
  this.clientId = clientId;
  this.clientRpc = clientRpc;
  this.peers = new ConcurrentLinkedQueue<>(group.getPeers());
  this.groupId = group.getGroupId();
  this.leaderId = leaderId != null? leaderId
      : !peers.isEmpty()? peers.iterator().next().getId(): null;
  Preconditions.assertTrue(retryPolicy != null, "retry policy can't be null");
  this.retryPolicy = retryPolicy;

  scheduler = TimeoutScheduler.getInstance();
  clientRpc.addServers(peers);

  this.orderedAsync = JavaUtils.memoize(() -> OrderedAsync.newInstance(this, properties));
  this.streamApi = JavaUtils.memoize(() -> StreamImpl.newInstance(this, properties));
}
 
Example #3
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Table mapping exception type to retry policy used for the exception in
 * write and watch request.
 * ---------------------------------------------------------------------------
 * |        Exception            | RetryPolicy for     | RetryPolicy for     |
 * |                             | Write request       | Watch request       |
 * |-------------------------------------------------------------------------|
 * | NotReplicatedException      | NO_RETRY            | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | GroupMismatchException      | NO_RETRY            | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | StateMachineException       | NO_RETRY            | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | TimeoutIOException          | EXPONENTIAL_BACKOFF | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | ResourceUnavailableException| EXPONENTIAL_BACKOFF | EXPONENTIAL_BACKOFF |
 * |-------------------------------------------------------------------------|
 * | Others                      | MULTILINEAR_RANDOM  | MULTILINEAR_RANDOM  |
 * |                             | _RETRY             | _RETRY               |
 * ---------------------------------------------------------------------------
 */
public static RetryPolicy createRetryPolicy(ConfigurationSource conf) {
  RatisClientConfig ratisClientConfig = OzoneConfiguration.of(conf)
      .getObject(RatisClientConfig.class);
  ExponentialBackoffRetry exponentialBackoffRetry =
      createExponentialBackoffPolicy(ratisClientConfig);
  MultipleLinearRandomRetry multipleLinearRandomRetry =
      MultipleLinearRandomRetry
          .parseCommaSeparated(ratisClientConfig.getMultilinearPolicy());

  long writeTimeout = ratisClientConfig.getWriteRequestTimeoutInMs();
  long watchTimeout = ratisClientConfig.getWatchRequestTimeoutInMs();

  return RequestTypeDependentRetryPolicy.newBuilder()
      .setRetryPolicy(RaftProtos.RaftClientRequestProto.TypeCase.WRITE,
          createExceptionDependentPolicy(exponentialBackoffRetry,
              multipleLinearRandomRetry, exponentialBackoffRetry))
      .setRetryPolicy(RaftProtos.RaftClientRequestProto.TypeCase.WATCH,
          createExceptionDependentPolicy(exponentialBackoffRetry,
              multipleLinearRandomRetry, RetryPolicies.noRetry()))
      .setTimeout(RaftProtos.RaftClientRequestProto.TypeCase.WRITE,
          TimeDuration.valueOf(writeTimeout, TimeUnit.MILLISECONDS))
      .setTimeout(RaftProtos.RaftClientRequestProto.TypeCase.WATCH,
          TimeDuration.valueOf(watchTimeout, TimeUnit.MILLISECONDS))
      .build();
}
 
Example #4
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public RaftClient createClient(RaftPeerId leaderId, RaftGroup group, RetryPolicy retryPolicy) {
  RaftClient.Builder builder = RaftClient.newBuilder()
      .setRaftGroup(group)
      .setLeaderId(leaderId)
      .setProperties(properties)
      .setParameters(parameters)
      .setRetryPolicy(retryPolicy);
  return builder.build();
}
 
Example #5
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
CompletableFuture<RaftClientReply> sendWatchRequest(long logIndex, RetryPolicy policy)
    throws Exception {

  try (final RaftClient watchClient =
      cluster.createClient(RaftTestUtil.waitForLeader(cluster).getId(), policy)) {

    CompletableFuture<RaftClientReply> reply =
        watchClient.sendAsync(new RaftTestUtil.SimpleMessage("message"));
    long writeIndex = reply.get().getLogIndex();
    Assert.assertTrue(writeIndex > 0);
    watchClient.sendWatchAsync(writeIndex, ReplicationLevel.MAJORITY_COMMITTED);
    return watchClient.sendWatchAsync(logIndex, ReplicationLevel.MAJORITY);
  }

}
 
Example #6
Source File: TestRetryPolicy.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryMultipleTimesWithFixedSleep() {
  RetryPolicy retryPolicy = RetryPolicies
      .retryUpToMaximumCountWithFixedSleep(2,
          TimeDuration.valueOf(1000L, TimeUnit.MILLISECONDS));
   boolean shouldRetry = retryPolicy.shouldRetry(1);
  Assert.assertTrue(shouldRetry);
  Assert.assertTrue(1000 == retryPolicy.getSleepTime().getDuration());
  Assert.assertFalse(retryPolicy.shouldRetry(3));
}
 
Example #7
Source File: RequestTypeDependentRetryPolicy.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private RequestTypeDependentRetryPolicy(
    EnumMap<RaftProtos.RaftClientRequestProto.TypeCase, RetryPolicy> map,
    EnumMap<RaftProtos.RaftClientRequestProto.TypeCase, TimeDuration> timeoutMap) {
  this.retryPolicyMap = Collections.unmodifiableMap(map);
  this.timeoutMap = timeoutMap;
  this.myString = () -> {
    final StringBuilder b = new StringBuilder(getClass().getSimpleName()).append("{");
    map.forEach((key, value) -> b.append(key).append("->").append(value).append(", "));
    b.setLength(b.length() - 2);
    return b.append("}").toString();
  };
}
 
Example #8
Source File: RaftClientImpl.java    From ratis with Apache License 2.0 5 votes vote down vote up
RaftClientImpl(ClientId clientId, RaftGroup group, RaftPeerId leaderId,
    RaftClientRpc clientRpc, RaftProperties properties, RetryPolicy retryPolicy) {
  this.clientId = clientId;
  this.clientRpc = clientRpc;
  this.peers = new ConcurrentLinkedQueue<>(group.getPeers());
  this.groupId = group.getGroupId();
  this.leaderId = leaderId != null? leaderId
      : !peers.isEmpty()? peers.iterator().next().getId(): null;
  Preconditions.assertTrue(retryPolicy != null, "retry policy can't be null");
  this.retryPolicy = retryPolicy;

  asyncRequestSemaphore = new Semaphore(RaftClientConfigKeys.Async.maxOutstandingRequests(properties));
  scheduler = TimeoutScheduler.newInstance(RaftClientConfigKeys.Async.schedulerThreads(properties));
  clientRpc.addServers(peers);
}
 
Example #9
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 5 votes vote down vote up
public RaftClient createClient(RaftPeerId leaderId, RaftGroup group,
    ClientId clientId, RetryPolicy retryPolicy) {
  RaftClient.Builder builder = RaftClient.newBuilder()
      .setClientId(clientId)
      .setRaftGroup(group)
      .setLeaderId(leaderId)
      .setProperties(properties)
      .setParameters(parameters)
      .setRetryPolicy(retryPolicy);
  return builder.build();
}
 
Example #10
Source File: OrderedAsync.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void scheduleWithTimeout(PendingOrderedRequest pending,
    RaftClientRequest request, RetryPolicy retryPolicy, Throwable e) {
  final int attempt = pending.getAttemptCount();
  final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
  final TimeDuration sleepTime = client.getEffectiveSleepTime(e,
      retryPolicy.handleAttemptFailure(event).getSleepTime());
  LOG.debug("schedule* attempt #{} with sleep {} and policy {} for {}", attempt, sleepTime, retryPolicy, request);
  scheduleWithTimeout(pending, sleepTime, getSlidingWindow(request));
}
 
Example #11
Source File: OrderedAsync.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void sendRequestWithRetry(PendingOrderedRequest pending) {
  final CompletableFuture<RaftClientReply> f = pending.getReplyFuture();
  if (f.isDone()) {
    return;
  }

  final RaftClientRequest request = pending.newRequestImpl();
  if (request == null) { // already done
    LOG.debug("{} newRequestImpl returns null", pending);
    return;
  }

  final RetryPolicy retryPolicy = client.getRetryPolicy();
  sendRequest(pending).thenAccept(reply -> {
    if (f.isDone()) {
      return;
    }
    if (reply == null) {
      scheduleWithTimeout(pending, request, retryPolicy, null);
    } else {
      f.complete(reply);
    }
  }).exceptionally(e -> {
    if (e instanceof CompletionException) {
      e = JavaUtils.unwrapCompletionException(e);
      scheduleWithTimeout(pending, request, retryPolicy, e);
      return null;
    }
    f.completeExceptionally(e);
    return null;
  });
}
 
Example #12
Source File: TestCreatePipelineCommandHandler.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private RaftClient.Builder mockRaftClientBuilder() {
  final RaftClient.Builder builder = Mockito.mock(RaftClient.Builder.class);
  Mockito.when(builder.setClientId(Mockito.any(ClientId.class)))
      .thenReturn(builder);
  Mockito.when(builder.setRaftGroup(Mockito.any(RaftGroup.class)))
      .thenReturn(builder);
  Mockito.when(builder.setLeaderId(Mockito.any(RaftPeerId.class)))
      .thenReturn(builder);
  Mockito.when(builder.setProperties(Mockito.any(RaftProperties.class)))
      .thenReturn(builder);
  Mockito.when(builder.setRetryPolicy(Mockito.any(RetryPolicy.class)))
      .thenReturn(builder);
  return builder;
}
 
Example #13
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private static ExceptionDependentRetry createExceptionDependentPolicy(
    ExponentialBackoffRetry exponentialBackoffRetry,
    MultipleLinearRandomRetry multipleLinearRandomRetry,
    RetryPolicy timeoutPolicy) {
  ExceptionDependentRetry.Builder builder =
      ExceptionDependentRetry.newBuilder();
  for (Class c : NO_RETRY_EXCEPTIONS) {
    builder.setExceptionToPolicy(c, RetryPolicies.noRetry());
  }
  return builder.setExceptionToPolicy(ResourceUnavailableException.class,
          exponentialBackoffRetry)
      .setExceptionToPolicy(TimeoutIOException.class, timeoutPolicy)
      .setDefaultPolicy(multipleLinearRandomRetry)
      .build();
}
 
Example #14
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("checkstyle:ParameterNumber")
private static RaftClient newRaftClient(RpcType rpcType, RaftPeerId leader,
    RaftGroup group, RetryPolicy retryPolicy,
    GrpcTlsConfig tlsConfig, ConfigurationSource ozoneConfiguration) {
  if (LOG.isTraceEnabled()) {
    LOG.trace("newRaftClient: {}, leader={}, group={}",
        rpcType, leader, group);
  }
  final RaftProperties properties = new RaftProperties();

  RaftConfigKeys.Rpc.setType(properties, rpcType);

  // Set the ratis client headers which are matching with regex.
  createRaftClientProperties(ozoneConfiguration, properties);

  RaftClient.Builder builder =  RaftClient.newBuilder()
      .setRaftGroup(group)
      .setLeaderId(leader)
      .setProperties(properties)
      .setRetryPolicy(retryPolicy);

  // TODO: GRPC TLS only for now, netty/hadoop RPC TLS support later.
  if (tlsConfig != null && rpcType == SupportedRpcType.GRPC) {
    builder.setParameters(GrpcFactory.newRaftParameters(tlsConfig));
  }
  return builder.build();
}
 
Example #15
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
public static RaftClient newRaftClient(RpcType rpcType, RaftPeer leader,
    RetryPolicy retryPolicy,
    ConfigurationSource ozoneConfiguration) {
  return newRaftClient(rpcType, leader.getId(),
      newRaftGroup(Collections.singletonList(leader)), retryPolicy, null,
      ozoneConfiguration);
}
 
Example #16
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
public static RaftClient newRaftClient(RpcType rpcType, RaftPeer leader,
    RetryPolicy retryPolicy, GrpcTlsConfig tlsConfig,
    ConfigurationSource configuration) {
  return newRaftClient(rpcType, leader.getId(),
      newRaftGroup(Collections.singletonList(leader)), retryPolicy,
      tlsConfig, configuration);
}
 
Example #17
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
public static RaftClient newRaftClient(RpcType rpcType, Pipeline pipeline,
    RetryPolicy retryPolicy, GrpcTlsConfig tlsConfig,
    ConfigurationSource ozoneConfiguration) throws IOException {
  return newRaftClient(rpcType,
      toRaftPeerId(pipeline.getLeaderNode()),
      newRaftGroup(RaftGroupId.valueOf(pipeline.getId().getId()),
          pipeline.getNodes()), retryPolicy, tlsConfig, ozoneConfiguration);
}
 
Example #18
Source File: XceiverClientRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a client.
 */
private XceiverClientRatis(Pipeline pipeline, RpcType rpcType,
    RetryPolicy retryPolicy, GrpcTlsConfig tlsConfig,
    ConfigurationSource configuration) {
  super();
  this.pipeline = pipeline;
  this.rpcType = rpcType;
  this.retryPolicy = retryPolicy;
  commitInfoMap = new ConcurrentHashMap<>();
  this.tlsConfig = tlsConfig;
  metrics = XceiverClientManager.getXceiverClientMetrics();
  this.ozoneConfiguration = configuration;
}
 
Example #19
Source File: XceiverClientRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
public static XceiverClientRatis newXceiverClientRatis(
    org.apache.hadoop.hdds.scm.pipeline.Pipeline pipeline,
    ConfigurationSource ozoneConf, X509Certificate caCert) {
  final String rpcType = ozoneConf
      .get(ScmConfigKeys.DFS_CONTAINER_RATIS_RPC_TYPE_KEY,
          ScmConfigKeys.DFS_CONTAINER_RATIS_RPC_TYPE_DEFAULT);
  final RetryPolicy retryPolicy = RatisHelper.createRetryPolicy(ozoneConf);
  final GrpcTlsConfig tlsConfig = RatisHelper.createTlsClientConfig(new
      SecurityConfig(ozoneConf), caCert);
  return new XceiverClientRatis(pipeline,
      SupportedRpcType.valueOfIgnoreCase(rpcType),
      retryPolicy, tlsConfig, ozoneConf);
}
 
Example #20
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 4 votes vote down vote up
public RaftClient createClient(RaftPeerId leaderId, RetryPolicy retryPolicy) {
  return createClient(leaderId, group, null, retryPolicy);
}
 
Example #21
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 4 votes vote down vote up
private RetryPolicy getDefaultRetryPolicy() {
  return RetryPolicies.retryForeverWithSleep(RETRY_INTERVAL_DEFAULT);
}
 
Example #22
Source File: RaftClient.java    From ratis with Apache License 2.0 4 votes vote down vote up
/** Set {@link RetryPolicy}. */
public Builder setRetryPolicy(RetryPolicy retryPolicy) {
  this.retryPolicy = retryPolicy;
  return this;
}
 
Example #23
Source File: ClientImplUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
public static RaftClient newRaftClient(ClientId clientId, RaftGroup group,
    RaftPeerId leaderId, RaftClientRpc clientRpc, RaftProperties properties,
    RetryPolicy retryPolicy) {
  return new RaftClientImpl(clientId, group, leaderId, clientRpc, properties,
      retryPolicy);
}
 
Example #24
Source File: RaftAsyncTests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static void assertRaftRetryFailureException(RaftRetryFailureException rfe, RetryPolicy retryPolicy, String name) {
  Assert.assertNotNull(name + " does not have RaftRetryFailureException", rfe);
  Assert.assertTrue(name + ": unexpected error message, rfe=" + rfe + ", retryPolicy=" + retryPolicy,
      rfe.getMessage().contains(retryPolicy.toString()));
}
 
Example #25
Source File: RaftAsyncTests.java    From ratis with Apache License 2.0 4 votes vote down vote up
static void assertRaftRetryFailureException(RaftRetryFailureException rfe, RetryPolicy retryPolicy, String name) {
  Assert.assertNotNull(name + " does not have RaftRetryFailureException", rfe);
  Assert.assertTrue(name + ": unexpected error message, rfe=" + rfe + ", retryPolicy=" + retryPolicy,
      rfe.getMessage().contains(retryPolicy.toString()));
}
 
Example #26
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftClient createClient(RaftPeerId leaderId, RetryPolicy retryPolicy) {
  return createClient(leaderId, group, retryPolicy);
}
 
Example #27
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftClient createClient(RetryPolicy retryPolicy) {
  return createClient(null, group, retryPolicy);
}
 
Example #28
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private RetryPolicy getDefaultRetryPolicy() {
  return RetryPolicies.retryForeverWithSleep(RETRY_INTERVAL_DEFAULT);
}
 
Example #29
Source File: RequestTypeDependentRetryPolicy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** Set the given policy for the given type. */
public Builder setRetryPolicy(RaftProtos.RaftClientRequestProto.TypeCase type, RetryPolicy policy) {
  final RetryPolicy previous = retryPolicyMap.put(type, policy);
  Preconditions.assertNull(previous, () -> "The retryPolicy for type " + type + " is already set to " + previous);
  return this;
}
 
Example #30
Source File: RaftClient.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** Set {@link RetryPolicy}. */
public Builder setRetryPolicy(RetryPolicy retryPolicy) {
  this.retryPolicy = retryPolicy;
  return this;
}