Skip to content

Commit

Permalink
Merge pull request #202 from mziccard/fix-and-test-storage-exceptions
Browse files Browse the repository at this point in the history
Make StorageImpl and related methods throw StorageException
  • Loading branch information
aozarov committed Oct 2, 2015
2 parents f3a19a6 + 36f2867 commit d82500d
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.gcloud.RetryHelper.runWithRetries;

import com.google.api.services.storage.model.StorageObject;
import com.google.gcloud.RetryHelper;
import com.google.gcloud.spi.StorageRpc;

import java.io.IOException;
Expand Down Expand Up @@ -119,12 +120,16 @@ public int read(ByteBuffer byteBuffer) throws IOException {
return -1;
}
final int toRead = Math.max(byteBuffer.remaining(), chunkSize);
buffer = runWithRetries(new Callable<byte[]>() {
@Override
public byte[] call() {
return storageRpc.read(storageObject, requestOptions, position, toRead);
}
}, serviceOptions.retryParams(), StorageImpl.EXCEPTION_HANDLER);
try {
buffer = runWithRetries(new Callable<byte[]>() {
@Override
public byte[] call() {
return storageRpc.read(storageObject, requestOptions, position, toRead);
}
}, serviceOptions.retryParams(), StorageImpl.EXCEPTION_HANDLER);
} catch (RetryHelper.RetryHelperException e) {
throw StorageException.translateAndThrow(e);
}
if (toRead > buffer.length) {
endOfStream = true;
if (buffer.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.concurrent.Executors.callable;

import com.google.api.services.storage.model.StorageObject;
import com.google.gcloud.RetryHelper;
import com.google.gcloud.spi.StorageRpc;

import java.io.IOException;
Expand Down Expand Up @@ -68,12 +69,16 @@ private void writeObject(ObjectOutputStream out) throws IOException {
private void flush(boolean compact) {
if (limit >= chunkSize || compact && limit >= MIN_CHUNK_SIZE) {
final int length = limit - limit % MIN_CHUNK_SIZE;
runWithRetries(callable(new Runnable() {
@Override
public void run() {
storageRpc.write(uploadId, buffer, 0, storageObject, position, length, false);
}
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
try {
runWithRetries(callable(new Runnable() {
@Override
public void run() {
storageRpc.write(uploadId, buffer, 0, storageObject, position, length, false);
}
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
} catch (RetryHelper.RetryHelperException e) {
throw StorageException.translateAndThrow(e);
}
position += length;
limit -= length;
byte[] temp = new byte[compact ? limit : chunkSize];
Expand Down Expand Up @@ -124,12 +129,16 @@ public boolean isOpen() {
@Override
public void close() throws IOException {
if (isOpen) {
runWithRetries(callable(new Runnable() {
@Override
public void run() {
storageRpc.write(uploadId, buffer, 0, storageObject, position, limit, true);
}
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
try {
runWithRetries(callable(new Runnable() {
@Override
public void run() {
storageRpc.write(uploadId, buffer, 0, storageObject, position, limit, true);
}
}), options.retryParams(), StorageImpl.EXCEPTION_HANDLER);
} catch (RetryHelper.RetryHelperException e) {
throw StorageException.translateAndThrow(e);
}
position += buffer.length;
isOpen = false;
buffer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.gcloud.storage;

import com.google.gcloud.RetryHelper;
import com.google.gcloud.RetryHelper.RetryHelperException;

/**
* Storage service exception.
*
Expand All @@ -25,6 +28,7 @@
public class StorageException extends RuntimeException {

private static final long serialVersionUID = -3748432005065428084L;
private static final int UNKNOWN_CODE = -1;

private final int code;
private final boolean retryable;
Expand All @@ -45,4 +49,21 @@ public int code() {
public boolean retryable() {
return retryable;
}

/**
* Translate RetryHelperException to the StorageException that caused the error. This method will
* always throw an exception.
*
* @throws StorageException when {@code ex} was caused by a {@code StorageException}
* @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException}
*/
static StorageException translateAndThrow(RetryHelperException ex) {
if (ex.getCause() instanceof StorageException) {
throw (StorageException) ex.getCause();
}
if (ex instanceof RetryHelper.RetryInterruptedException) {
RetryHelper.RetryInterruptedException.propagate();
}
throw new StorageException(UNKNOWN_CODE, ex.getMessage(), false);
}
}
Loading

0 comments on commit d82500d

Please sign in to comment.