Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StorageImpl and related methods throw StorageException #202

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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