com.google.api.gax.retrying.RetrySettings类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(16.4k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中com.google.api.gax.retrying.RetrySettings类的一些代码示例,展示了RetrySettings类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RetrySettings类的具体详情如下:
包路径:com.google.api.gax.retrying.RetrySettings
类名称:RetrySettings

RetrySettings介绍

[英]Holds the parameters for retry or poll logic with jitter, timeout and exponential backoff. Actual implementation of the logic is elsewhere.

The intent of these settings is to be used with a call to a remote server, which could either fail (and return an error code) or not respond (and cause a timeout). When there is a failure or timeout, the logic should keep trying until the total timeout has passed.

The "total timeout" parameter has ultimate control over how long the logic should keep trying the remote call until it gives up completely. The higher the total timeout, the more retries can be attempted. The other settings are considered more advanced.

Retry delay and timeout start at specific values, and are tracked separately from each other. The very first call (before any retries) will use the initial timeout.

If the last remote call is a failure, then the retrier will wait for the current retry delay before attempting another call, and then the retry delay will be multiplied by the retry delay multiplier for the next failure. The timeout will not be affected, except in the case where the timeout would result in a deadline past the total timeout; in that circumstance, a new timeout value is computed which will terminate the call when the total time is up.

If the last remote call is a timeout, then the retrier will compute a new timeout and make another call. The new timeout is computed by multiplying the current timeout by the timeout multiplier, but if that results in a deadline after the total timeout, then a new timeout value is computed which will terminate the call when the total time is up.

Server streaming RPCs interpret RPC timeouts a bit differently. For server streaming RPCs, the RPC timeout gets converted into a wait timeout com.google.api.gax.rpc.ApiCallContext#withStreamWaitTimeout(Duration).
[中]保存带有抖动、超时和指数退避的重试或轮询逻辑的参数。逻辑的实际实现在别处。
这些设置的目的是用于对远程服务器的调用,远程服务器可能会失败(并返回错误代码)或不响应(并导致超时)。当出现故障或超时时,逻辑应继续尝试,直到超过总超时。
“total timeout”参数可以最终控制逻辑在完全放弃远程调用之前应该持续尝试多长时间。总超时越高,可以尝试的重试次数越多。其他设置被认为更高级。
重试延迟和超时从特定值开始,并相互独立跟踪。第一次调用(重试之前)将使用初始超时。
如果最后一次远程调用失败,那么重试器将在尝试另一次调用之前等待当前重试延迟,然后重试延迟将乘以下一次失败的重试延迟乘数。超时将不受影响,除非超时会导致最后期限超过总超时;在这种情况下,将计算一个新的超时值,该值将在总时间结束时终止调用。
如果最后一次远程调用是超时,那么重试器将计算一个新的超时并进行另一次调用。新的超时是通过将当前超时乘以超时乘数来计算的,但是如果这导致总超时之后有一个截止日期,那么将计算一个新的超时值,该值将在总时间结束时终止调用。
服务器流式RPC对RPC超时的解释略有不同。对于服务器流式RPC,RPC超时将转换为等待超时com。谷歌。应用程序编程接口。加克斯。rpc。ApiCallContext#with streamWaitTimeout(持续时间)。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

private static RetrySettings retrySettings() {
 return RetrySettings.newBuilder()
   .setMaxRetryDelay(Duration.ofMillis(30000L))
   .setTotalTimeout(Duration.ofMillis(120000L))
   .setInitialRetryDelay(Duration.ofMillis(250L))
   .setRetryDelayMultiplier(1.0)
   .setInitialRpcTimeout(Duration.ofMillis(120000L))
   .setRpcTimeoutMultiplier(1.0)
   .setMaxRpcTimeout(Duration.ofMillis(120000L))
   .build();
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateFromStream() {
 RemoteComputeHelper helper = RemoteComputeHelper.create(PROJECT_ID, JSON_KEY_STREAM);
 ComputeOptions options = helper.getOptions();
 assertEquals(PROJECT_ID, options.getProjectId());
 assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getConnectTimeout());
 assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getReadTimeout());
 assertEquals(10, options.getRetrySettings().getMaxAttempts());
 assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelay());
 assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeout());
 assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelay());
}

代码示例来源:origin: googleapis/google-cloud-java

return settings;
RetrySettings.Builder builder = settings.toBuilder();
for (RetryOption option : options) {
 switch (option.type) {
  case TOTAL_TIMEOUT:
   builder.setTotalTimeout((Duration) option.value);
   break;
  case INITIAL_RETRY_DELAY:
   builder.setInitialRetryDelay((Duration) option.value);
   break;
  case RETRY_DELAY_MULTIPLIER:
   builder.setRetryDelayMultiplier((Double) option.value);
   break;
  case MAX_RETRY_DELAY:

代码示例来源:origin: googleapis/google-cloud-java

/** Configures the Publisher's retry parameters. */
public Builder setRetrySettings(RetrySettings retrySettings) {
 Preconditions.checkArgument(
   retrySettings.getTotalTimeout().compareTo(MIN_TOTAL_TIMEOUT) >= 0);
 Preconditions.checkArgument(
   retrySettings.getInitialRpcTimeout().compareTo(MIN_RPC_TIMEOUT) >= 0);
 this.retrySettings = retrySettings;
 return this;
}

代码示例来源:origin: googleapis/google-cloud-java

private void verifyRetrySettings(Set<Code> retryCodes, RetrySettings retrySettings) {
  assertThat(retryCodes).containsAllOf(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE);
  assertThat(retrySettings.getTotalTimeout()).isGreaterThan(Duration.ZERO);
  assertThat(retrySettings.getInitialRetryDelay()).isGreaterThan(Duration.ZERO);
  assertThat(retrySettings.getRetryDelayMultiplier()).isAtLeast(1.0);
  assertThat(retrySettings.getMaxRetryDelay()).isGreaterThan(Duration.ZERO);
  assertThat(retrySettings.getInitialRpcTimeout()).isGreaterThan(Duration.ZERO);
  assertThat(retrySettings.getRpcTimeoutMultiplier()).isAtLeast(1.0);
  assertThat(retrySettings.getMaxRpcTimeout()).isGreaterThan(Duration.ZERO);
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testReadRowsSettings() {
 ServerStreamingCallSettings.Builder<ReadRowsRequest, ReadRowsResponse> builder =
   EnhancedBigQueryStorageStubSettings.newBuilder().readRowsSettings();
 assertThat(builder.getRetryableCodes()).containsAllOf(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE);
 RetrySettings retrySettings = builder.getRetrySettings();
 assertThat(retrySettings.getInitialRetryDelay()).isEqualTo(Duration.ofMillis(100L));
 assertThat(retrySettings.getRetryDelayMultiplier()).isWithin(1e-6).of(1.3);
 assertThat(retrySettings.getMaxRetryDelay()).isEqualTo(Duration.ofMinutes(1L));
 assertThat(retrySettings.getInitialRpcTimeout()).isEqualTo(Duration.ofDays(1L));
 assertThat(retrySettings.getRpcTimeoutMultiplier()).isWithin(1e-6).of(1.0);
 assertThat(retrySettings.getMaxRpcTimeout()).isEqualTo(Duration.ofDays(1L));
 assertThat(retrySettings.getTotalTimeout()).isEqualTo(Duration.ofDays(1L));
 assertThat(builder.getIdleTimeout()).isEqualTo(Duration.ZERO);
}

代码示例来源:origin: googleapis/google-cloud-java

.bulkMutationsSettings()
    .getRetrySettings()
    .toBuilder()
    .setMaxAttempts(MAX_ATTEMPTS)
    .setInitialRetryDelay(Duration.ofMillis(10))
    .setRetryDelayMultiplier(2)
    .setMaxRetryDelay(Duration.ofMillis(100))
    .build())
.setBatchingSettings(

代码示例来源:origin: com.google.api/gax

long newRetryDelay = settings.getInitialRetryDelay().toMillis();
if (prevSettings.getAttemptCount() > 0) {
 newRetryDelay =
   (long) (settings.getRetryDelayMultiplier() * prevSettings.getRetryDelay().toMillis());
 newRetryDelay = Math.min(newRetryDelay, settings.getMaxRetryDelay().toMillis());
  (long) (settings.getRpcTimeoutMultiplier() * prevSettings.getRpcTimeout().toMillis());
newRpcTimeout = Math.min(newRpcTimeout, settings.getMaxRpcTimeout().toMillis());

代码示例来源:origin: com.google.api/gax

public RetrySettings build() {
 RetrySettings params = autoBuild();
 if (params.getTotalTimeout().toMillis() < 0) {
  throw new IllegalStateException("total timeout must not be negative");
 }
 if (params.getInitialRetryDelay().toMillis() < 0) {
  throw new IllegalStateException("initial retry delay must not be negative");
 }
 if (params.getRetryDelayMultiplier() < 1.0) {
  throw new IllegalStateException("retry delay multiplier must be at least 1");
 }
 if (params.getMaxRetryDelay().compareTo(params.getInitialRetryDelay()) < 0) {
  throw new IllegalStateException("max retry delay must not be shorter than initial delay");
 }
 if (params.getMaxAttempts() < 0) {
  throw new IllegalStateException("max attempts must be non-negative");
 }
 if (params.getInitialRpcTimeout().toMillis() < 0) {
  throw new IllegalStateException("initial rpc timeout must not be negative");
 }
 if (params.getMaxRpcTimeout().compareTo(params.getInitialRpcTimeout()) < 0) {
  throw new IllegalStateException("max rpc timeout must not be shorter than initial timeout");
 }
 if (params.getRpcTimeoutMultiplier() < 1.0) {
  throw new IllegalStateException("rpc timeout multiplier must be at least 1");
 }
 return params;
}

代码示例来源:origin: googleapis/google-cloud-java

public Publisher getPublisherWithCustomRetrySettings(ProjectTopicName topicName)
  throws Exception {
 // [START pubsub_publisher_retry_settings]
 // Retry settings control how the publisher handles retryable failures
 Duration retryDelay = Duration.ofMillis(100); // default : 1 ms
 double retryDelayMultiplier = 2.0; // back off for repeated failures
 Duration maxRetryDelay = Duration.ofSeconds(5); // default : 10 seconds
 Duration totalTimeout = Duration.ofSeconds(1); // default: 0
 Duration initialRpcTimeout = Duration.ofSeconds(1); // default: 0
 Duration maxRpcTimeout = Duration.ofSeconds(10); // default: 0
 RetrySettings retrySettings =
   RetrySettings.newBuilder()
     .setInitialRetryDelay(retryDelay)
     .setRetryDelayMultiplier(retryDelayMultiplier)
     .setMaxRetryDelay(maxRetryDelay)
     .setTotalTimeout(totalTimeout)
     .setInitialRpcTimeout(initialRpcTimeout)
     .setMaxRpcTimeout(maxRpcTimeout)
     .build();
 Publisher publisher = Publisher.newBuilder(topicName).setRetrySettings(retrySettings).build();
 // [END pubsub_publisher_retry_settings]
 return publisher;
}

代码示例来源:origin: googleapis/gax-java

@Test
public void retrySettingsMerge() {
 RetrySettings.Builder builder =
   RetrySettings.newBuilder()
     .setTotalTimeout(Duration.ofMillis(45000))
     .setInitialRpcTimeout(Duration.ofMillis(2000))
     .setRpcTimeoutMultiplier(1.5)
     .setMaxRpcTimeout(Duration.ofMillis(30000))
     .setInitialRetryDelay(Duration.ofMillis(100))
     .setRetryDelayMultiplier(1.2)
     .setMaxRetryDelay(Duration.ofMillis(1000));
 RetrySettings.Builder mergedBuilder = RetrySettings.newBuilder();
 mergedBuilder.merge(builder);
 RetrySettings settingsA = builder.build();
 RetrySettings settingsB = mergedBuilder.build();
 Truth.assertThat(settingsA.getTotalTimeout()).isEqualTo(settingsB.getTotalTimeout());
 Truth.assertThat(settingsA.getInitialRetryDelay()).isEqualTo(settingsB.getInitialRetryDelay());
 Truth.assertThat(settingsA.getRpcTimeoutMultiplier())
   .isWithin(0)
   .of(settingsB.getRpcTimeoutMultiplier());
 Truth.assertThat(settingsA.getMaxRpcTimeout()).isEqualTo(settingsB.getMaxRpcTimeout());
 Truth.assertThat(settingsA.getInitialRetryDelay()).isEqualTo(settingsB.getInitialRetryDelay());
 Truth.assertThat(settingsA.getRetryDelayMultiplier())
   .isWithin(0)
   .of(settingsB.getRetryDelayMultiplier());
 Truth.assertThat(settingsA.getMaxRetryDelay()).isEqualTo(settingsB.getMaxRetryDelay());
}

代码示例来源:origin: googleapis/google-cloud-java

RetrySettings.newBuilder()
  .setMaxAttempts(10)
  .setTotalTimeout(Duration.ofHours(1))
  .setInitialRpcTimeout(Duration.ofSeconds(10))
  .setRpcTimeoutMultiplier(1)
  .setMaxRpcTimeout(Duration.ofSeconds(10))
  .setJittered(true)
  .build();

代码示例来源:origin: googleapis/google-cloud-java

private AwaitReplicationCallable createAwaitReplicationCallable() {
 // TODO(igorbernstein2): expose polling settings
 RetrySettings pollingSettings =
   RetrySettings.newBuilder()
     // use overall timeout from checkConsistencyCallable
     // NOTE: The overall timeout might exceed this value due to underlying retries
     .setTotalTimeout(
       settings.checkConsistencySettings().getRetrySettings().getTotalTimeout())
     // Use constant polling with jitter
     .setInitialRetryDelay(Duration.ofSeconds(10))
     .setRetryDelayMultiplier(1.0)
     .setMaxRetryDelay(Duration.ofSeconds(10))
     .setJittered(true)
     // These rpc timeouts are ignored, instead the rpc timeouts defined for
     // generateConsistencyToken and checkConsistency callables will be used.
     .setInitialRpcTimeout(Duration.ZERO)
     .setMaxRpcTimeout(Duration.ZERO)
     .setRpcTimeoutMultiplier(1.0)
     .build();
 return AwaitReplicationCallable.create(
   generateConsistencyTokenCallable(),
   checkConsistencyCallable(),
   clientContext,
   pollingSettings);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testMergeToSettings() {
 RetrySettings defRetrySettings = RetrySettings.newBuilder().build();
   defRetrySettings.toBuilder().setTotalTimeout(Duration.ofMillis(420L)).build();
 mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, TOTAL_TIMEOUT);
 assertEquals(defRetrySettings, mergedRetrySettings);
   defRetrySettings.toBuilder().setMaxRetryDelay(Duration.ofMillis(100)).build();
 mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, MAX_RETRY_DELAY);
 assertEquals(defRetrySettings, mergedRetrySettings);
   defRetrySettings.toBuilder().setInitialRetryDelay(Duration.ofMillis(42L)).build();
 mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, INITIAL_RETRY_DELAY);
 assertEquals(defRetrySettings, mergedRetrySettings);
 defRetrySettings = defRetrySettings.toBuilder().setRetryDelayMultiplier(1.5).build();
 mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, RETRY_DELAY_MULTIPLIER);
 assertEquals(defRetrySettings, mergedRetrySettings);
 defRetrySettings = defRetrySettings.toBuilder().setMaxAttempts(100).build();
 mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, MAX_ATTEMPTS);
 assertEquals(defRetrySettings, mergedRetrySettings);
 defRetrySettings = defRetrySettings.toBuilder().setJittered(false).build();
 mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, JITTERED);
 assertEquals(defRetrySettings, mergedRetrySettings);

代码示例来源:origin: googleapis/gax-java

OperationTimedPollAlgorithm.create(
    FAST_RECHECKING_SETTINGS
      .toBuilder()
      .setInitialRpcTimeout(Duration.ofMillis(100))
      .setMaxRpcTimeout(Duration.ofSeconds(1))
      .setRpcTimeoutMultiplier(2)
      .build(),
    clock);
  Lists.newArrayList(Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(400));
assertThat(actualTimeouts).isEqualTo(expectedTimeouts);

代码示例来源:origin: googleapis/google-cloud-java

RetrySettings.newBuilder()
  .setMaxAttempts(10)
  .setTotalTimeout(Duration.ofHours(1))
  .setInitialRpcTimeout(Duration.ofSeconds(10))
  .setRpcTimeoutMultiplier(1)
  .setMaxRpcTimeout(Duration.ofSeconds(10))
  .setJittered(true)
  .build();

代码示例来源:origin: googleapis/google-cloud-java

@Test(expected = ExecutionException.class)
public void testPublishFailureRetries_retriesDisabled() throws Exception {
 Publisher publisher =
   getTestPublisherBuilder()
     .setExecutorProvider(SINGLE_THREAD_EXECUTOR)
     .setRetrySettings(
       Publisher.Builder.DEFAULT_RETRY_SETTINGS
         .toBuilder()
         .setTotalTimeout(Duration.ofSeconds(10))
         .setMaxAttempts(1)
         .build())
     .build();
 testPublisherServiceImpl.addPublishError(new Throwable("Transiently failing"));
 ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A");
 try {
  publishFuture1.get();
 } finally {
  assertSame(testPublisherServiceImpl.getCapturedRequests().size(), 1);
  publisher.shutdown();
  publisher.awaitTermination(1, TimeUnit.MINUTES);
 }
}

代码示例来源:origin: googleapis/google-cloud-java

MockRetryingFuture(Duration totalTimeout) {
 this.timedAttemptSettings =
   TimedAttemptSettings.newBuilder()
     .setRpcTimeout(Duration.ofSeconds(1))
     .setRetryDelay(Duration.ZERO)
     .setRandomizedRetryDelay(Duration.ZERO)
     .setAttemptCount(0)
     .setFirstAttemptStartTimeNanos(0)
     .setGlobalSettings(RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build())
     .build();
}

代码示例来源:origin: apache/nifi

@Override
  protected StorageOptions getServiceOptions(ProcessContext context, GoogleCredentials credentials) {
    final String projectId = context.getProperty(PROJECT_ID).evaluateAttributeExpressions().getValue();
    final Integer retryCount = context.getProperty(RETRY_COUNT).asInteger();

    StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder()
        .setCredentials(credentials)
        .setRetrySettings(RetrySettings.newBuilder()
            .setMaxAttempts(retryCount)
            .build());

    if (!projectId.isEmpty()) {
      storageOptionsBuilder.setProjectId(projectId);
    }

    return  storageOptionsBuilder.setTransportOptions(getTransportOptions(context)).build();
  }
}

代码示例来源:origin: GoogleCloudPlatform/cloud-bigtable-client

protected Long getNextBackoff() {
 if (currentBackoff == null) {
  // Historically, the client waited for "total timeout" after the first failure.  For now,
  // that behavior is preserved, even though that's not the ideal.
  //
  // TODO: Think through retries, and create policy that works with the mental model most
  //       users would have of relating to retries.  That would likely involve updating some
  //       default settings in addition to changing the algorithm.
  currentBackoff = exponentialRetryAlgorithm.createFirstAttempt();
 }
 currentBackoff = exponentialRetryAlgorithm.createNextAttempt(currentBackoff);
 if (!exponentialRetryAlgorithm.shouldRetry(currentBackoff)) {
  // TODO: consider creating a subclass of exponentialRetryAlgorithm to encapsulate this logic
  long timeLeftNs =  currentBackoff.getGlobalSettings().getTotalTimeout().toNanos() -
    (clock.nanoTime() - currentBackoff.getFirstAttemptStartTimeNanos());
  long timeLeftMs = TimeUnit.NANOSECONDS.toMillis(timeLeftNs);
  if (timeLeftMs > currentBackoff.getGlobalSettings().getInitialRetryDelay().toMillis()) {
   // The backoff algorithm doesn't always wait until the timeout is achieved.  Wait
   // one final time so that retries hit
   return timeLeftMs;
  } else {
   // Finish for real.
   return null;
  }
 } else {
  return currentBackoff.getRetryDelay().toMillis();
 }
}

相关文章