Search in sources :

Example 1 with StreamingRetryAlgorithm

use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.

the class StreamingRetryAlgorithmTest method testNextAttemptReturnsResultAlgorithmSettingsWhenShouldRetry.

@Test
public void testNextAttemptReturnsResultAlgorithmSettingsWhenShouldRetry() {
    RetryingContext context = mock(RetryingContext.class);
    @SuppressWarnings("unchecked") BasicResultRetryAlgorithm<String> resultAlgorithm = mock(BasicResultRetryAlgorithm.class);
    UnavailableException exception = mock(UnavailableException.class);
    when(resultAlgorithm.shouldRetry(context, exception, null)).thenReturn(true);
    TimedAttemptSettings next = mock(TimedAttemptSettings.class);
    when(resultAlgorithm.createNextAttempt(Mockito.eq(context), Mockito.eq(exception), Mockito.<String>isNull(), any(TimedAttemptSettings.class))).thenReturn(next);
    ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    TimedAttemptSettings first = algorithm.createFirstAttempt(context);
    TimedAttemptSettings attempt = algorithm.createNextAttempt(context, exception, null, first);
    assertThat(attempt).isSameInstanceAs(next);
}
Also used : ApiClock(com.google.api.core.ApiClock) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) TimedAttemptSettings(com.google.api.gax.retrying.TimedAttemptSettings) RetryingContext(com.google.api.gax.retrying.RetryingContext) Test(org.junit.Test)

Example 2 with StreamingRetryAlgorithm

use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.

the class StreamingRetryAlgorithmTest method testFirstAttemptUsesContextSettings.

@Test
public void testFirstAttemptUsesContextSettings() {
    RetryingContext context = mock(RetryingContext.class);
    when(context.getRetrySettings()).thenReturn(CONTEXT_RETRY_SETTINGS);
    BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
    ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    TimedAttemptSettings attempt = algorithm.createFirstAttempt(context);
    assertThat(attempt.getGlobalSettings()).isSameInstanceAs(CONTEXT_RETRY_SETTINGS);
    assertThat(attempt.getRpcTimeout()).isEqualTo(CONTEXT_RETRY_SETTINGS.getInitialRpcTimeout());
}
Also used : ApiClock(com.google.api.core.ApiClock) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) BasicResultRetryAlgorithm(com.google.api.gax.retrying.BasicResultRetryAlgorithm) TimedAttemptSettings(com.google.api.gax.retrying.TimedAttemptSettings) RetryingContext(com.google.api.gax.retrying.RetryingContext) Test(org.junit.Test)

Example 3 with StreamingRetryAlgorithm

use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.

the class StreamingRetryAlgorithmTest method testNextAttemptResetsTimedSettings.

@Test
public void testNextAttemptResetsTimedSettings() {
    RetryingContext context = mock(RetryingContext.class);
    BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
    ServerStreamingAttemptException exception = mock(ServerStreamingAttemptException.class);
    when(exception.canResume()).thenReturn(true);
    when(exception.hasSeenResponses()).thenReturn(true);
    UnavailableException cause = mock(UnavailableException.class);
    when(exception.getCause()).thenReturn(cause);
    ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    TimedAttemptSettings first = algorithm.createFirstAttempt(context);
    TimedAttemptSettings second = algorithm.createNextAttempt(context, mock(Exception.class), null, first);
    TimedAttemptSettings third = algorithm.createNextAttempt(context, exception, null, second);
    assertThat(third.getFirstAttemptStartTimeNanos()).isEqualTo(first.getFirstAttemptStartTimeNanos());
    // The timeout values are reset to the second call.
    assertThat(third.getRpcTimeout()).isEqualTo(second.getRpcTimeout());
}
Also used : ApiClock(com.google.api.core.ApiClock) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) BasicResultRetryAlgorithm(com.google.api.gax.retrying.BasicResultRetryAlgorithm) TimedAttemptSettings(com.google.api.gax.retrying.TimedAttemptSettings) RetryingContext(com.google.api.gax.retrying.RetryingContext) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException) Test(org.junit.Test)

Example 4 with StreamingRetryAlgorithm

use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project gax-java by googleapis.

the class Callables method retrying.

public static <RequestT, ResponseT> ServerStreamingCallable<RequestT, ResponseT> retrying(ServerStreamingCallable<RequestT, ResponseT> innerCallable, ServerStreamingCallSettings<RequestT, ResponseT> callSettings, ClientContext clientContext) {
    ServerStreamingCallSettings<RequestT, ResponseT> settings = callSettings;
    if (areRetriesDisabled(settings.getRetryableCodes(), settings.getRetrySettings())) {
        // When retries are disabled, the total timeout can be treated as the rpc timeout.
        settings = settings.toBuilder().setSimpleTimeoutNoRetries(settings.getRetrySettings().getTotalTimeout()).build();
    }
    StreamingRetryAlgorithm<Void> retryAlgorithm = new StreamingRetryAlgorithm<>(new ApiResultRetryAlgorithm<Void>(), new ExponentialRetryAlgorithm(settings.getRetrySettings(), clientContext.getClock()));
    ScheduledRetryingExecutor<Void> retryingExecutor = new ScheduledRetryingExecutor<>(retryAlgorithm, clientContext.getExecutor());
    return new RetryingServerStreamingCallable<>(innerCallable, retryingExecutor, settings.getResumptionStrategy());
}
Also used : ScheduledRetryingExecutor(com.google.api.gax.retrying.ScheduledRetryingExecutor) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm)

Example 5 with StreamingRetryAlgorithm

use of com.google.api.gax.retrying.StreamingRetryAlgorithm in project java-bigquerystorage by googleapis.

the class EnhancedBigQueryReadStub method readRowsCallable.

public ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> readRowsCallable() {
    ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> innerCallable = GrpcRawCallableFactory.createServerStreamingCallable(GrpcCallSettings.<ReadRowsRequest, ReadRowsResponse>newBuilder().setMethodDescriptor(BigQueryReadGrpc.getReadRowsMethod()).setParamsExtractor(new RequestParamsExtractor<ReadRowsRequest>() {

        @Override
        public Map<String, String> extract(ReadRowsRequest request) {
            return ImmutableMap.of("read_stream", String.valueOf(request.getReadStream()));
        }
    }).build(), stubSettings.readRowsSettings().getRetryableCodes());
    ServerStreamingCallSettings<ReadRowsRequest, ReadRowsResponse> callSettings = stubSettings.readRowsSettings();
    StreamingRetryAlgorithm<Void> retryAlgorithm = new StreamingRetryAlgorithm<>(new ApiResultRetryAlgorithm<Void>(readRowsRetryAttemptListener), new ExponentialRetryAlgorithm(callSettings.getRetrySettings(), context.getClock()));
    ScheduledRetryingExecutor<Void> retryingExecutor = new ScheduledRetryingExecutor<>(retryAlgorithm, context.getExecutor());
    if (context.getStreamWatchdog() != null) {
        innerCallable = Callables.watched(innerCallable, callSettings, context);
    }
    ReadRowsRetryingCallable outerCallable = new ReadRowsRetryingCallable(context.getDefaultCallContext(), innerCallable, retryingExecutor, callSettings.getResumptionStrategy());
    ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> traced = new TracedServerStreamingCallable<>(outerCallable, context.getTracerFactory(), SpanName.of(TRACING_OUTER_CLIENT_NAME, "ReadRows"));
    return traced.withDefaultCallContext(context.getDefaultCallContext());
}
Also used : TracedServerStreamingCallable(com.google.api.gax.tracing.TracedServerStreamingCallable) ReadRowsRetryingCallable(com.google.cloud.bigquery.storage.v1beta2.stub.readrows.ReadRowsRetryingCallable) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) ScheduledRetryingExecutor(com.google.api.gax.retrying.ScheduledRetryingExecutor) StreamingRetryAlgorithm(com.google.api.gax.retrying.StreamingRetryAlgorithm) ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ExponentialRetryAlgorithm (com.google.api.gax.retrying.ExponentialRetryAlgorithm)11 StreamingRetryAlgorithm (com.google.api.gax.retrying.StreamingRetryAlgorithm)11 RetryingContext (com.google.api.gax.retrying.RetryingContext)7 TimedAttemptSettings (com.google.api.gax.retrying.TimedAttemptSettings)7 Test (org.junit.Test)7 ApiClock (com.google.api.core.ApiClock)6 BasicResultRetryAlgorithm (com.google.api.gax.retrying.BasicResultRetryAlgorithm)5 ScheduledRetryingExecutor (com.google.api.gax.retrying.ScheduledRetryingExecutor)4 ServerStreamingAttemptException (com.google.api.gax.retrying.ServerStreamingAttemptException)3 TracedServerStreamingCallable (com.google.api.gax.tracing.TracedServerStreamingCallable)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)1 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)1 ReadRowsRetryingCallable (com.google.cloud.bigquery.storage.v1.stub.readrows.ReadRowsRetryingCallable)1 ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)1 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse)1 ReadRowsRetryingCallable (com.google.cloud.bigquery.storage.v1beta1.stub.readrows.ReadRowsRetryingCallable)1 ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest)1 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse)1