Search in sources :

Example 6 with StreamingRetryAlgorithm

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

the class StreamingRetryAlgorithmTest method testNextAttemptReturnsNullWhenShouldNotRetry.

@Test
public void testNextAttemptReturnsNullWhenShouldNotRetry() {
    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(false);
    ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    TimedAttemptSettings attempt = algorithm.createNextAttempt(context, exception, null, mock(TimedAttemptSettings.class));
    assertThat(attempt).isNull();
    TimedAttemptSettings attemptWithoutContext = algorithm.createNextAttempt(exception, null, mock(TimedAttemptSettings.class));
    assertThat(attemptWithoutContext).isNull();
}
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 7 with StreamingRetryAlgorithm

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

the class StreamingRetryAlgorithmTest method testFirstAttemptUsesDefaultSettings.

@Test
public void testFirstAttemptUsesDefaultSettings() {
    RetryingContext context = mock(RetryingContext.class);
    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(DEFAULT_RETRY_SETTINGS);
    assertThat(attempt.getRpcTimeout()).isEqualTo(DEFAULT_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 8 with StreamingRetryAlgorithm

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

the class StreamingRetryAlgorithmTest method testShouldNotRetryIfAttemptIsNonResumable.

@Test
public void testShouldNotRetryIfAttemptIsNonResumable() {
    RetryingContext context = mock(RetryingContext.class);
    ServerStreamingAttemptException exception = mock(ServerStreamingAttemptException.class);
    when(exception.canResume()).thenReturn(false);
    UnavailableException cause = mock(UnavailableException.class);
    when(exception.getCause()).thenReturn(cause);
    BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
    ExponentialRetryAlgorithm timedAlgorithm = new ExponentialRetryAlgorithm(DEFAULT_RETRY_SETTINGS, mock(ApiClock.class));
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    // This should return false because the attempt exception indicates that it is non-resumable.
    boolean shouldRetry = algorithm.shouldRetry(context, exception, null, mock(TimedAttemptSettings.class));
    assertThat(shouldRetry).isFalse();
    boolean shouldRetryWithoutContext = algorithm.shouldRetry(exception, null, mock(TimedAttemptSettings.class));
    assertThat(shouldRetryWithoutContext).isFalse();
}
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) Test(org.junit.Test)

Example 9 with StreamingRetryAlgorithm

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

the class StreamingRetryAlgorithmTest method testShouldRetryIfAllSayYes.

@Test
public void testShouldRetryIfAllSayYes() {
    RetryingContext context = mock(RetryingContext.class);
    ServerStreamingAttemptException exception = mock(ServerStreamingAttemptException.class);
    when(exception.canResume()).thenReturn(true);
    UnavailableException cause = mock(UnavailableException.class);
    when(exception.getCause()).thenReturn(cause);
    BasicResultRetryAlgorithm<String> resultAlgorithm = new BasicResultRetryAlgorithm<>();
    ExponentialRetryAlgorithm timedAlgorithm = mock(ExponentialRetryAlgorithm.class);
    when(timedAlgorithm.shouldRetry(Mockito.eq(context), any(TimedAttemptSettings.class))).thenReturn(true);
    StreamingRetryAlgorithm<String> algorithm = new StreamingRetryAlgorithm<>(resultAlgorithm, timedAlgorithm);
    boolean shouldRetry = algorithm.shouldRetry(context, exception, null, mock(TimedAttemptSettings.class));
    assertThat(shouldRetry).isTrue();
}
Also used : 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) Test(org.junit.Test)

Example 10 with StreamingRetryAlgorithm

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

the class EnhancedBigQueryStorageStub method readRowsCallable.

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

        @Override
        public Map<String, String> extract(ReadRowsRequest request) {
            return ImmutableMap.of("read_position.stream.name", String.valueOf(request.getReadPosition().getStream().getName()));
        }
    }).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.v1beta1.stub.readrows.ReadRowsRetryingCallable) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.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.v1beta1.Storage.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