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