Search in sources :

Example 11 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method copy.

@Override
public CopyWriter copy(final CopyRequest copyRequest) {
    final StorageObject source = copyRequest.getSource().toPb();
    final Map<StorageRpc.Option, ?> sourceOptions = optionMap(copyRequest.getSource().getGeneration(), null, copyRequest.getSourceOptions(), true);
    final StorageObject targetObject = copyRequest.getTarget().toPb();
    final Map<StorageRpc.Option, ?> targetOptions = optionMap(copyRequest.getTarget().getGeneration(), copyRequest.getTarget().getMetageneration(), copyRequest.getTargetOptions());
    try {
        RewriteResponse rewriteResponse = runWithRetries(new Callable<RewriteResponse>() {

            @Override
            public RewriteResponse call() {
                return storageRpc.openRewrite(new StorageRpc.RewriteRequest(source, sourceOptions, copyRequest.overrideInfo(), targetObject, targetOptions, copyRequest.getMegabytesCopiedPerChunk()));
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
        return new CopyWriter(getOptions(), rewriteResponse);
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) RewriteResponse(com.google.cloud.storage.spi.v1.StorageRpc.RewriteResponse) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 12 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method compose.

@Override
public Blob compose(final ComposeRequest composeRequest) {
    final List<StorageObject> sources = Lists.newArrayListWithCapacity(composeRequest.getSourceBlobs().size());
    for (ComposeRequest.SourceBlob sourceBlob : composeRequest.getSourceBlobs()) {
        sources.add(BlobInfo.newBuilder(BlobId.of(composeRequest.getTarget().getBucket(), sourceBlob.getName(), sourceBlob.getGeneration())).build().toPb());
    }
    final StorageObject target = composeRequest.getTarget().toPb();
    final Map<StorageRpc.Option, ?> targetOptions = optionMap(composeRequest.getTarget().getGeneration(), composeRequest.getTarget().getMetageneration(), composeRequest.getTargetOptions());
    try {
        return Blob.fromPb(this, runWithRetries(new Callable<StorageObject>() {

            @Override
            public StorageObject call() {
                return storageRpc.compose(sources, target, targetOptions);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()));
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) Callable(java.util.concurrent.Callable) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 13 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method update.

@Override
public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
    final StorageObject storageObject = blobInfo.toPb();
    final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
    try {
        return Blob.fromPb(this, runWithRetries(new Callable<StorageObject>() {

            @Override
            public StorageObject call() {
                return storageRpc.patch(storageObject, optionsMap);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()));
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) Callable(java.util.concurrent.Callable) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Example 14 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new BigQueryException(503, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        BigQueryException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(503, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        BigQueryException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(BigQueryException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) HttpResponseException(com.google.api.client.http.HttpResponseException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Aggregations

RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)14 StorageObject (com.google.api.services.storage.model.StorageObject)7 BaseServiceException (com.google.cloud.BaseServiceException)6 IOException (java.io.IOException)6 SocketTimeoutException (java.net.SocketTimeoutException)6 Test (org.junit.Test)6 Callable (java.util.concurrent.Callable)3 HttpResponseException (com.google.api.client.http.HttpResponseException)2 TestIamPermissionsResponse (com.google.api.services.storage.model.TestIamPermissionsResponse)1 RewriteResponse (com.google.cloud.storage.spi.v1.StorageRpc.RewriteResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1