use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageReadChannel method read.
@Override
public int read(ByteBuffer dst) throws IOException {
synchronized (this) {
checkOpen();
int amt;
int retries = 0;
int maxRetries = Math.max(3, maxChannelReopens);
dst.mark();
while (true) {
try {
dst.reset();
amt = channel.read(dst);
break;
} catch (StorageException exs) {
if (isReopenable(exs)) {
// these errors aren't marked as retryable since the channel is closed;
// but here at this higher level we can retry them.
reopens++;
if (reopens > maxChannelReopens) {
throw new StorageException(exs.getCode(), "All reopens failed", exs);
}
sleepForAttempt(reopens);
innerOpen();
continue;
} else if (exs.isRetryable() || exs.getCode() == 500 || exs.getCode() == 503) {
retries++;
if (retries > maxRetries) {
// further retries."
throw new StorageException(exs.getCode(), "All retries failed", exs);
}
sleepForAttempt(retries);
continue;
}
// exception is neither reopenable nor retryable
throw exs;
}
}
if (amt > 0) {
position += amt;
// XXX: This would only ever happen if the fetchSize() race-condition occurred.
if (position > size) {
size = position;
}
}
return amt;
}
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class FakeStorageRpc method open.
@Override
public String open(StorageObject object, Map<Option, ?> options) throws StorageException {
String key = fullname(object);
boolean mustNotExist = false;
for (Option option : options.keySet()) {
// this is a bit of a hack, since we don't implement generations.
if (option == Option.IF_GENERATION_MATCH && ((Long) options.get(option)) == 0L) {
mustNotExist = true;
}
}
if (mustNotExist && metadata.containsKey(key)) {
throw new StorageException(new FileAlreadyExistsException(key));
}
metadata.put(key, object);
return fullname(object);
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class FakeStorageRpc method openRewrite.
@Override
public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException {
String sourceKey = fullname(rewriteRequest.source);
// a little hackish, just good enough for the tests to work.
if (!contents.containsKey(sourceKey)) {
throw new StorageException(404, "File not found: " + sourceKey);
}
boolean mustNotExist = false;
for (Option option : rewriteRequest.targetOptions.keySet()) {
// this is a bit of a hack, since we don't implement generations.
if (option == Option.IF_GENERATION_MATCH && (Long) rewriteRequest.targetOptions.get(option) == 0L) {
mustNotExist = true;
}
}
String destKey = fullname(rewriteRequest.target);
if (mustNotExist && contents.containsKey(destKey)) {
throw new StorageException(new FileAlreadyExistsException(destKey));
}
metadata.put(destKey, rewriteRequest.target);
byte[] data = contents.get(sourceKey);
contents.put(destKey, Arrays.copyOf(data, data.length));
return new RewriteResponse(rewriteRequest, rewriteRequest.target, data.length, true, "rewriteToken goes here", data.length);
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageReadChannelTest method testReadRetryEventuallyGivesUp.
@Test
public void testReadRetryEventuallyGivesUp() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1);
when(gcsChannel.read(eq(buffer))).thenThrow(new StorageException(new IOException("Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040"))).thenThrow(new StorageException(new IOException("Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040"))).thenReturn(1);
assertThat(chan.position()).isEqualTo(0L);
thrown.expect(StorageException.class);
chan.read(buffer);
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageReadChannelTest method testReadRetrySSLHandshake.
@Test
public void testReadRetrySSLHandshake() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1);
when(gcsChannel.read(eq(buffer))).thenThrow(new StorageException(new IOException("something", new IOException("thing", new SSLHandshakeException("connection closed due to throttling"))))).thenReturn(1);
assertThat(chan.position()).isEqualTo(0L);
assertThat(chan.read(buffer)).isEqualTo(1);
assertThat(chan.position()).isEqualTo(1L);
verify(gcsChannel, times(2)).read(any(ByteBuffer.class));
}
Aggregations