Search in sources :

Example 6 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project elasticsearch by elastic.

the class GoogleCloudStorageBlobStore method deleteBlobs.

/**
     * Deletes multiple blobs in the given bucket (uses a batch request to perform this)
     *
     * @param blobNames names of the bucket to delete
     */
void deleteBlobs(Collection<String> blobNames) throws IOException {
    if (blobNames == null || blobNames.isEmpty()) {
        return;
    }
    if (blobNames.size() == 1) {
        deleteBlob(blobNames.iterator().next());
        return;
    }
    final List<Storage.Objects.Delete> deletions = new ArrayList<>();
    final Iterator<String> blobs = blobNames.iterator();
    SocketAccess.doPrivilegedVoidIOException(() -> {
        while (blobs.hasNext()) {
            // Create a delete request for each blob to delete
            deletions.add(client.objects().delete(bucket, blobs.next()));
            if (blobs.hasNext() == false || deletions.size() == MAX_BATCHING_REQUESTS) {
                try {
                    // Deletions are executed using a batch request
                    BatchRequest batch = client.batch();
                    // Used to track successful deletions
                    CountDown countDown = new CountDown(deletions.size());
                    for (Storage.Objects.Delete delete : deletions) {
                        // Queue the delete request in batch
                        delete.queue(batch, new JsonBatchCallback<Void>() {

                            @Override
                            public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
                                logger.error("failed to delete blob [{}] in bucket [{}]: {}", delete.getObject(), delete.getBucket(), e.getMessage());
                            }

                            @Override
                            public void onSuccess(Void aVoid, HttpHeaders responseHeaders) throws IOException {
                                countDown.countDown();
                            }
                        });
                    }
                    batch.execute();
                    if (countDown.isCountedDown() == false) {
                        throw new IOException("Failed to delete all [" + deletions.size() + "] blobs");
                    }
                } finally {
                    deletions.clear();
                }
            }
        }
    });
}
Also used : BatchRequest(com.google.api.client.googleapis.batch.BatchRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDown(org.elasticsearch.common.util.concurrent.CountDown) Storage(com.google.api.services.storage.Storage) Objects(com.google.api.services.storage.model.Objects) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 7 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project google-cloud-java by GoogleCloudPlatform.

the class BaseHttpServiceException method makeExceptionData.

private static ExceptionData makeExceptionData(IOException exception, boolean idempotent, Set<BaseServiceException.Error> retryableErrors) {
    int code = UNKNOWN_CODE;
    String reason = null;
    String location = null;
    String debugInfo = null;
    Boolean retryable = null;
    if (exception instanceof HttpResponseException) {
        if (exception instanceof GoogleJsonResponseException) {
            GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails();
            if (jsonError != null) {
                BaseServiceException.Error error = new BaseServiceException.Error(jsonError.getCode(), reason(jsonError));
                code = error.getCode();
                reason = error.getReason();
                retryable = error.isRetryable(idempotent, retryableErrors);
                if (reason != null) {
                    GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0);
                    location = errorInfo.getLocation();
                    debugInfo = (String) errorInfo.get("debugInfo");
                }
            } else {
                code = ((GoogleJsonResponseException) exception).getStatusCode();
            }
        } else {
            // In cases where an exception is an instance of HttpResponseException but not
            // an instance of GoogleJsonResponseException, check the status code to determine whether it's retryable
            code = ((HttpResponseException) exception).getStatusCode();
            retryable = BaseServiceException.isRetryable(code, null, idempotent, retryableErrors);
        }
    }
    return ExceptionData.newBuilder().setMessage(message(exception)).setCause(exception).setRetryable(MoreObjects.firstNonNull(retryable, BaseServiceException.isRetryable(idempotent, exception))).setCode(code).setReason(reason).setLocation(location).setDebugInfo(debugInfo).build();
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) BaseServiceException(com.google.cloud.BaseServiceException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) HttpResponseException(com.google.api.client.http.HttpResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 8 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project google-cloud-java by GoogleCloudPlatform.

the class BaseHttpServiceExceptionTest method testBaseServiceException.

@Test
public void testBaseServiceException() {
    BaseServiceException serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertNull(serviceException.getCause());
    serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertNull(serviceException.getCause());
    Exception cause = new RuntimeException();
    serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS, cause);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertEquals(cause, serviceException.getCause());
    serviceException = new BaseHttpServiceException(CODE, MESSAGE, REASON, NOT_IDEMPOTENT, EMPTY_RETRYABLE_ERRORS, cause);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertFalse(serviceException.isRetryable());
    assertEquals(cause, serviceException.getCause());
    IOException exception = new SocketTimeoutException();
    serviceException = new BaseHttpServiceException(exception, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertTrue(serviceException.isRetryable());
    assertNull(serviceException.getMessage());
    assertEquals(exception, serviceException.getCause());
    exception = new SocketException();
    serviceException = new BaseHttpServiceException(exception, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertTrue(serviceException.isRetryable());
    assertNull(serviceException.getMessage());
    assertEquals(exception, serviceException.getCause());
    exception = new IOException("insufficient data written");
    serviceException = new BaseHttpServiceException(exception, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertTrue(serviceException.isRetryable());
    assertEquals("insufficient data written", serviceException.getMessage());
    assertEquals(exception, serviceException.getCause());
    GoogleJsonError error = new GoogleJsonError();
    error.setCode(CODE);
    error.setMessage(MESSAGE);
    serviceException = new BaseHttpServiceException(error, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertFalse(serviceException.isRetryable());
    serviceException = new CustomServiceException(CODE, MESSAGE, REASON, IDEMPOTENT);
    assertEquals(CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertEquals(RETRYABLE, serviceException.isRetryable());
    serviceException = new CustomServiceException(CODE_NO_REASON, MESSAGE, null, IDEMPOTENT);
    assertEquals(CODE_NO_REASON, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertNull(serviceException.getReason());
    assertEquals(RETRYABLE, serviceException.isRetryable());
    serviceException = new CustomServiceException(UNKNOWN_CODE, MESSAGE, REASON, IDEMPOTENT);
    assertEquals(UNKNOWN_CODE, serviceException.getCode());
    assertEquals(MESSAGE, serviceException.getMessage());
    assertEquals(REASON, serviceException.getReason());
    assertEquals(RETRYABLE, serviceException.isRetryable());
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) BaseServiceException(com.google.cloud.BaseServiceException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException) BaseServiceException(com.google.cloud.BaseServiceException) IOException(java.io.IOException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.Test)

Example 9 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testGetAllArray.

@Test
public void testGetAllArray() {
    BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1);
    BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2);
    RpcBatch batchMock = EasyMock.createMock(RpcBatch.class);
    Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance();
    Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance();
    batchMock.addGet(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    batchMock.addGet(EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock);
    batchMock.submit();
    EasyMock.replay(storageRpcMock, batchMock);
    initializeService();
    List<Blob> resultBlobs = storage.get(blobId1, blobId2);
    callback1.getValue().onSuccess(BLOB_INFO1.toPb());
    callback2.getValue().onFailure(new GoogleJsonError());
    assertEquals(2, resultBlobs.size());
    assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0));
    assertNull(resultBlobs.get(1));
    EasyMock.verify(batchMock);
}
Also used : BlobWriteOption(com.google.cloud.storage.Storage.BlobWriteOption) BucketSourceOption(com.google.cloud.storage.Storage.BucketSourceOption) BlobSourceOption(com.google.cloud.storage.Storage.BlobSourceOption) BlobTargetOption(com.google.cloud.storage.Storage.BlobTargetOption) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) RpcBatch(com.google.cloud.storage.spi.v1.RpcBatch) Test(org.junit.Test)

Example 10 with GoogleJsonError

use of com.google.api.client.googleapis.json.GoogleJsonError in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testGetAllArrayIterable.

@Test
public void testGetAllArrayIterable() {
    BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1);
    BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2);
    RpcBatch batchMock = EasyMock.createMock(RpcBatch.class);
    Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance();
    Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance();
    batchMock.addGet(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    batchMock.addGet(EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of()));
    EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock);
    batchMock.submit();
    EasyMock.replay(storageRpcMock, batchMock);
    initializeService();
    List<Blob> resultBlobs = storage.get(ImmutableList.of(blobId1, blobId2));
    callback1.getValue().onSuccess(BLOB_INFO1.toPb());
    callback2.getValue().onFailure(new GoogleJsonError());
    assertEquals(2, resultBlobs.size());
    assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0));
    assertNull(resultBlobs.get(1));
    EasyMock.verify(batchMock);
}
Also used : BlobWriteOption(com.google.cloud.storage.Storage.BlobWriteOption) BucketSourceOption(com.google.cloud.storage.Storage.BucketSourceOption) BlobSourceOption(com.google.cloud.storage.Storage.BlobSourceOption) BlobTargetOption(com.google.cloud.storage.Storage.BlobTargetOption) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) RpcBatch(com.google.cloud.storage.spi.v1.RpcBatch) Test(org.junit.Test)

Aggregations

GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)20 Test (org.junit.Test)13 StorageObject (com.google.api.services.storage.model.StorageObject)8 IOException (java.io.IOException)8 BlobSourceOption (com.google.cloud.storage.Storage.BlobSourceOption)6 BlobTargetOption (com.google.cloud.storage.Storage.BlobTargetOption)6 BlobWriteOption (com.google.cloud.storage.Storage.BlobWriteOption)6 BucketSourceOption (com.google.cloud.storage.Storage.BucketSourceOption)6 RpcBatch (com.google.cloud.storage.spi.v1.RpcBatch)6 HttpHeaders (com.google.api.client.http.HttpHeaders)5 DnsRpc (com.google.cloud.dns.spi.v1.DnsRpc)4 RpcBatch (com.google.cloud.dns.spi.v1.RpcBatch)4 Map (java.util.Map)4 HttpResponseException (com.google.api.client.http.HttpResponseException)3 Objects (com.google.api.services.storage.model.Objects)3 SocketTimeoutException (java.net.SocketTimeoutException)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpResponse (com.google.api.client.http.HttpResponse)2 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)2