Search in sources :

Example 6 with InternalException

use of com.google.api.gax.rpc.InternalException in project java-bigquerystorage by googleapis.

the class BigQueryReadClientTest method readRowsRetryingHttp2StreamRstTest.

@Test
@SuppressWarnings("all")
public void readRowsRetryingHttp2StreamRstTest() throws ExecutionException, InterruptedException {
    ApiException exception = new InternalException(new StatusRuntimeException(Status.INTERNAL.withDescription("HTTP/2 error code: INTERNAL_ERROR\nReceived Rst Stream")), GrpcStatusCode.of(Code.INTERNAL), /* retryable = */
    false);
    mockBigQueryRead.addException(exception);
    long rowCount = 1340416618L;
    ReadRowsResponse expectedResponse = ReadRowsResponse.newBuilder().setRowCount(rowCount).build();
    mockBigQueryRead.addResponse(expectedResponse);
    ReadRowsRequest request = ReadRowsRequest.newBuilder().build();
    MockStreamObserver<ReadRowsResponse> responseObserver = new MockStreamObserver<>();
    ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> callable = client.readRowsCallable();
    callable.serverStreamingCall(request, responseObserver);
    List<ReadRowsResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(retryCount, 1);
    Assert.assertEquals(lastRetryStatusCode, Code.INTERNAL);
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ApiException(com.google.api.gax.rpc.ApiException) InternalException(com.google.api.gax.rpc.InternalException) Test(org.junit.Test)

Example 7 with InternalException

use of com.google.api.gax.rpc.InternalException in project java-spanner by googleapis.

the class PartitionedDmlTransaction method executeStreamingPartitionedUpdate.

/**
 * Executes the {@link Statement} using a partitioned dml transaction with automatic retry if the
 * transaction was aborted. The update method uses the ExecuteStreamingSql RPC to execute the
 * statement, and will retry the stream if an {@link UnavailableException} is thrown, using the
 * last seen resume token if the server returns any.
 */
long executeStreamingPartitionedUpdate(final Statement statement, final Duration timeout, final UpdateOption... updateOptions) {
    checkState(isValid, "Partitioned DML has been invalidated by a new operation on the session");
    LOGGER.log(Level.FINER, "Starting PartitionedUpdate statement");
    ByteString resumeToken = ByteString.EMPTY;
    boolean foundStats = false;
    long updateCount = 0L;
    Stopwatch stopwatch = Stopwatch.createStarted(ticker);
    Options options = Options.fromUpdateOptions(updateOptions);
    try {
        ExecuteSqlRequest request = newTransactionRequestFrom(statement, options);
        while (true) {
            final Duration remainingTimeout = tryUpdateTimeout(timeout, stopwatch);
            try {
                ServerStream<PartialResultSet> stream = rpc.executeStreamingPartitionedDml(request, session.getOptions(), remainingTimeout);
                for (PartialResultSet rs : stream) {
                    if (rs.getResumeToken() != null && !rs.getResumeToken().isEmpty()) {
                        resumeToken = rs.getResumeToken();
                    }
                    if (rs.hasStats()) {
                        foundStats = true;
                        updateCount += rs.getStats().getRowCountLowerBound();
                    }
                }
                break;
            } catch (UnavailableException e) {
                LOGGER.log(Level.FINER, "Retrying PartitionedDml transaction after UnavailableException", e);
                request = resumeOrRestartRequest(resumeToken, statement, request, options);
            } catch (InternalException e) {
                if (!isRetryableInternalErrorPredicate.apply(e)) {
                    throw e;
                }
                LOGGER.log(Level.FINER, "Retrying PartitionedDml transaction after InternalException - EOS", e);
                request = resumeOrRestartRequest(resumeToken, statement, request, options);
            } catch (AbortedException e) {
                LOGGER.log(Level.FINER, "Retrying PartitionedDml transaction after AbortedException", e);
                resumeToken = ByteString.EMPTY;
                foundStats = false;
                updateCount = 0L;
                request = newTransactionRequestFrom(statement, options);
            }
        }
        if (!foundStats) {
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Partitioned DML response missing stats possibly due to non-DML statement as input");
        }
        LOGGER.log(Level.FINER, "Finished PartitionedUpdate statement");
        return updateCount;
    } catch (Exception e) {
        throw SpannerExceptionFactory.newSpannerException(e);
    }
}
Also used : TransactionOptions(com.google.spanner.v1.TransactionOptions) RequestOptions(com.google.spanner.v1.RequestOptions) ByteString(com.google.protobuf.ByteString) Stopwatch(com.google.common.base.Stopwatch) UnavailableException(com.google.api.gax.rpc.UnavailableException) Duration(org.threeten.bp.Duration) UnavailableException(com.google.api.gax.rpc.UnavailableException) InternalException(com.google.api.gax.rpc.InternalException) AbortedException(com.google.api.gax.rpc.AbortedException) DeadlineExceededException(com.google.api.gax.rpc.DeadlineExceededException) InternalException(com.google.api.gax.rpc.InternalException) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) AbortedException(com.google.api.gax.rpc.AbortedException) PartialResultSet(com.google.spanner.v1.PartialResultSet)

Example 8 with InternalException

use of com.google.api.gax.rpc.InternalException in project java-spanner by googleapis.

the class IsRetryableInternalErrorTest method eosInternalExceptionIsRetryable.

@Test
public void eosInternalExceptionIsRetryable() {
    final InternalException e = new InternalException("INTERNAL: Received unexpected EOS on DATA frame from server.", null, GrpcStatusCode.of(Code.INTERNAL), false);
    assertThat(predicate.apply(e)).isTrue();
}
Also used : InternalException(com.google.api.gax.rpc.InternalException) Test(org.junit.Test)

Example 9 with InternalException

use of com.google.api.gax.rpc.InternalException in project java-spanner by googleapis.

the class PartitionedDmlTransactionTest method testExecuteStreamingPartitionedUpdateGenericInternalException.

@Test
public void testExecuteStreamingPartitionedUpdateGenericInternalException() {
    PartialResultSet p1 = PartialResultSet.newBuilder().setResumeToken(resumeToken).build();
    ServerStream<PartialResultSet> stream1 = mock(ServerStream.class);
    Iterator<PartialResultSet> iterator = mock(Iterator.class);
    when(iterator.hasNext()).thenReturn(true, true, false);
    when(iterator.next()).thenReturn(p1).thenThrow(new InternalException("INTERNAL: Error", null, GrpcStatusCode.of(Code.INTERNAL), false));
    when(stream1.iterator()).thenReturn(iterator);
    when(rpc.executeStreamingPartitionedDml(Mockito.eq(executeRequestWithoutResumeToken), anyMap(), any(Duration.class))).thenReturn(stream1);
    PartitionedDmlTransaction tx = new PartitionedDmlTransaction(session, rpc, ticker);
    SpannerException e = assertThrows(SpannerException.class, () -> tx.executeStreamingPartitionedUpdate(Statement.of(sql), Duration.ofMinutes(10)));
    assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
    verify(rpc).beginTransaction(any(BeginTransactionRequest.class), anyMap());
    verify(rpc).executeStreamingPartitionedDml(Mockito.eq(executeRequestWithoutResumeToken), anyMap(), any(Duration.class));
}
Also used : BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Duration(org.threeten.bp.Duration) PartialResultSet(com.google.spanner.v1.PartialResultSet) InternalException(com.google.api.gax.rpc.InternalException) Test(org.junit.Test)

Example 10 with InternalException

use of com.google.api.gax.rpc.InternalException in project java-spanner by googleapis.

the class IsRetryableInternalErrorTest method genericInternalExceptionIsNotRetryable.

@Test
public void genericInternalExceptionIsNotRetryable() {
    final InternalException e = new InternalException("INTERNAL: Generic.", null, GrpcStatusCode.of(Code.INTERNAL), false);
    assertThat(predicate.apply(e)).isFalse();
}
Also used : InternalException(com.google.api.gax.rpc.InternalException) Test(org.junit.Test)

Aggregations

InternalException (com.google.api.gax.rpc.InternalException)16 Test (org.junit.Test)15 ApiException (com.google.api.gax.rpc.ApiException)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)6 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 Duration (org.threeten.bp.Duration)3 ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)2 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse)2 ByteString (com.google.protobuf.ByteString)2 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)2 AbortedException (com.google.api.gax.rpc.AbortedException)1 DeadlineExceededException (com.google.api.gax.rpc.DeadlineExceededException)1 UnavailableException (com.google.api.gax.rpc.UnavailableException)1 BaseServiceException (com.google.cloud.BaseServiceException)1 Stopwatch (com.google.common.base.Stopwatch)1 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)1 RequestOptions (com.google.spanner.v1.RequestOptions)1 ResultSetStats (com.google.spanner.v1.ResultSetStats)1 TransactionOptions (com.google.spanner.v1.TransactionOptions)1