Search in sources :

Example 21 with ApiException

use of com.google.api.gax.rpc.ApiException in project beam by apache.

the class BigQueryServicesImplTest method testCreateReadSessionSetsRequestCountMetricOnError.

@Test
public void testCreateReadSessionSetsRequestCountMetricOnError() throws InterruptedException, IOException {
    BigQueryServicesImpl.StorageClientImpl client = mock(BigQueryServicesImpl.StorageClientImpl.class);
    CreateReadSessionRequest.Builder builder = CreateReadSessionRequest.newBuilder();
    builder.getReadSessionBuilder().setTable("myproject:mydataset.mytable");
    CreateReadSessionRequest request = builder.build();
    StatusCode statusCode = new StatusCode() {

        @Override
        public Code getCode() {
            return Code.NOT_FOUND;
        }

        @Override
        public Object getTransportCode() {
            return null;
        }
    };
    when(client.callCreateReadSession(request)).thenThrow(// Mock implementation.
    new ApiException("Not Found", null, statusCode, false));
    // Real implementation.
    when(client.createReadSession(any())).thenCallRealMethod();
    thrown.expect(ApiException.class);
    thrown.expectMessage("Not Found");
    client.createReadSession(request);
    verifyReadMetricWasSet("myproject", "mydataset", "mytable", "not_found", 1);
}
Also used : StatusCode(com.google.api.gax.rpc.StatusCode) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 22 with ApiException

use of com.google.api.gax.rpc.ApiException in project beam by apache.

the class BigQueryServicesImplTest method testReadRowsSetsRequestCountMetricOnError.

@Test
public void testReadRowsSetsRequestCountMetricOnError() throws InterruptedException, IOException {
    BigQueryServices.StorageClient client = mock(BigQueryServicesImpl.StorageClientImpl.class);
    ReadRowsRequest request = null;
    StatusCode statusCode = new StatusCode() {

        @Override
        public Code getCode() {
            return Code.INTERNAL;
        }

        @Override
        public Object getTransportCode() {
            return null;
        }
    };
    when(client.readRows(request)).thenThrow(// Mock implementation.
    new ApiException("Internal", null, statusCode, false));
    // Real implementation.
    when(client.readRows(any(), any())).thenCallRealMethod();
    thrown.expect(ApiException.class);
    thrown.expectMessage("Internal");
    client.readRows(request, "myproject:mydataset.mytable");
    verifyReadMetricWasSet("myproject", "mydataset", "mytable", "internal", 1);
}
Also used : ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) StatusCode(com.google.api.gax.rpc.StatusCode) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 23 with ApiException

use of com.google.api.gax.rpc.ApiException in project beam by apache.

the class BaseFirestoreV1WriteFnTest method attemptsExhaustedForRetryableError.

@Override
@Test
public final void attemptsExhaustedForRetryableError() throws Exception {
    Instant attemptStart = Instant.ofEpochMilli(0);
    Instant rpc1Start = Instant.ofEpochMilli(1);
    Instant rpc1End = Instant.ofEpochMilli(2);
    Instant rpc2Start = Instant.ofEpochMilli(3);
    Instant rpc2End = Instant.ofEpochMilli(4);
    Instant rpc3Start = Instant.ofEpochMilli(5);
    Instant rpc3End = Instant.ofEpochMilli(6);
    Write write = newWrite();
    Element<Write> element1 = new WriteElement(0, write, window);
    when(ff.getFirestoreStub(any())).thenReturn(stub);
    when(ff.getRpcQos(any())).thenReturn(rpcQos);
    when(rpcQos.newWriteAttempt(FirestoreV1RpcAttemptContexts.V1FnRpcAttemptContext.BatchWrite)).thenReturn(attempt);
    when(stub.batchWriteCallable()).thenReturn(callable);
    FlushBuffer<Element<Write>> flushBuffer = spy(newFlushBuffer(rpcQosOptions));
    when(attempt.awaitSafeToProceed(any())).thenReturn(true);
    when(attempt.<Write, Element<Write>>newFlushBuffer(attemptStart)).thenReturn(flushBuffer);
    when(flushBuffer.offer(element1)).thenReturn(true);
    when(flushBuffer.iterator()).thenReturn(newArrayList(element1).iterator());
    when(flushBuffer.getBufferedElementsCount()).thenReturn(1);
    when(flushBuffer.isFull()).thenReturn(true);
    when(callable.call(any())).thenThrow(RETRYABLE_ERROR, RETRYABLE_ERROR, RETRYABLE_ERROR);
    doNothing().when(attempt).recordWriteCounts(any(), anyInt(), anyInt());
    doNothing().doNothing().doThrow(RETRYABLE_ERROR).when(attempt).checkCanRetry(any(), eq(RETRYABLE_ERROR));
    when(processContext.element()).thenReturn(write);
    try {
        runFunction(getFn(clock, ff, rpcQosOptions, CounterFactory.DEFAULT, DistributionFactory.DEFAULT));
        fail("Expected ApiException to be throw after exhausted attempts");
    } catch (ApiException e) {
        assertSame(RETRYABLE_ERROR, e);
    }
    verify(attempt, times(1)).awaitSafeToProceed(attemptStart);
    verify(attempt, times(1)).recordRequestStart(rpc1Start, 1);
    verify(attempt, times(1)).recordWriteCounts(rpc1End, 0, 1);
    verify(attempt, times(1)).recordRequestStart(rpc2Start, 1);
    verify(attempt, times(1)).recordWriteCounts(rpc2End, 0, 1);
    verify(attempt, times(1)).recordRequestStart(rpc3Start, 1);
    verify(attempt, times(1)).recordWriteCounts(rpc3End, 0, 1);
    verify(attempt, times(0)).recordWriteCounts(any(), gt(0), anyInt());
    verify(attempt, never()).completeSuccess();
}
Also used : Write(com.google.firestore.v1.Write) FirestoreProtoHelpers.newWrite(org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite) Instant(org.joda.time.Instant) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) Element(org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element) WriteElement(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 24 with ApiException

use of com.google.api.gax.rpc.ApiException in project beam by apache.

the class RpcQosTest method attemptThrowsOnNonRetryableErrorCode.

@Test
public void attemptThrowsOnNonRetryableErrorCode() throws InterruptedException {
    RpcQosOptions rpcQosOptions = options.toBuilder().withMaxAttempts(3).unsafeBuild();
    RpcQos qos = new RpcQosImpl(rpcQosOptions, random, sleeper, counterFactory, distributionFactory);
    RpcReadAttempt readAttempt = qos.newReadAttempt(RPC_ATTEMPT_CONTEXT);
    readAttempt.recordRequestStart(monotonicClock.instant());
    // try 1
    readAttempt.recordRequestFailed(monotonicClock.instant());
    try {
        readAttempt.checkCanRetry(monotonicClock.instant(), RETRYABLE_ERROR_WITH_NON_RETRYABLE_CODE);
        fail("expected non-retryable error to throw error on first occurrence");
    } catch (ApiException e) {
        assertSame(e, RETRYABLE_ERROR_WITH_NON_RETRYABLE_CODE);
    }
    verify(counterThrottlingMs, times(0)).inc(anyLong());
    verify(counterRpcFailures, times(1)).inc();
    verify(counterRpcSuccesses, times(0)).inc();
    verify(counterRpcStreamValueReceived, times(0)).inc();
}
Also used : RpcReadAttempt(org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcReadAttempt) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 25 with ApiException

use of com.google.api.gax.rpc.ApiException in project instrumentation-java by census-instrumentation.

the class CreateTimeSeriesExporter method export.

@Override
public void export(Collection<Metric> metrics) {
    List<TimeSeries> timeSeriesList = new ArrayList<>(metrics.size());
    for (Metric metric : metrics) {
        timeSeriesList.addAll(StackdriverExportUtils.createTimeSeriesList(metric, monitoredResource, domain, projectName.getProject(), constantLabels));
    }
    Span span = tracer.getCurrentSpan();
    for (List<TimeSeries> batchedTimeSeries : Lists.partition(timeSeriesList, MAX_BATCH_EXPORT_SIZE)) {
        span.addAnnotation("Export Stackdriver TimeSeries.");
        try {
            CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(projectName.toString()).addAllTimeSeries(batchedTimeSeries).build();
            metricServiceClient.createTimeSeries(request);
            span.addAnnotation("Finish exporting TimeSeries.");
        } catch (ApiException e) {
            logger.log(Level.WARNING, "ApiException thrown when exporting TimeSeries.", e);
            span.setStatus(Status.CanonicalCode.valueOf(e.getStatusCode().getCode().name()).toStatus().withDescription("ApiException thrown when exporting TimeSeries: " + StackdriverExportUtils.exceptionMessage(e)));
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
            span.setStatus(Status.UNKNOWN.withDescription("Exception thrown when exporting TimeSeries: " + StackdriverExportUtils.exceptionMessage(e)));
        }
    }
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) Metric(io.opencensus.metrics.export.Metric) Span(io.opencensus.trace.Span) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

ApiException (com.google.api.gax.rpc.ApiException)30 Test (org.junit.Test)21 IOException (java.io.IOException)5 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 StatusCode (com.google.api.gax.rpc.StatusCode)3 Publisher (com.google.cloud.pubsub.v1.Publisher)3 CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)3 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)3 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)3 RpcReadAttempt (org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcReadAttempt)3 Code (com.google.api.gax.rpc.StatusCode.Code)2 Offset (com.google.cloud.pubsublite.Offset)2 Write (com.google.firestore.v1.Write)2 ByteString (com.google.protobuf.ByteString)2 TopicName (com.google.pubsub.v1.TopicName)2 Span (io.opencensus.trace.Span)2 FirestoreProtoHelpers.newWrite (org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite)2 WriteElement (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement)2 SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)2 Instant (org.joda.time.Instant)2