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);
}
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);
}
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();
}
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();
}
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)));
}
}
}
Aggregations