Search in sources :

Example 6 with StorageException

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;
    }
}
Also used : StorageException(com.google.cloud.storage.StorageException)

Example 7 with StorageException

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);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StorageException(com.google.cloud.storage.StorageException)

Example 8 with StorageException

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);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StorageException(com.google.cloud.storage.StorageException)

Example 9 with StorageException

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);
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 10 with StorageException

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));
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StorageException(com.google.cloud.storage.StorageException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Test(org.junit.Test)

Aggregations

StorageException (com.google.cloud.storage.StorageException)29 Test (org.junit.Test)20 BlobInfo (com.google.cloud.storage.BlobInfo)16 Blob (com.google.cloud.storage.Blob)15 BlobId (com.google.cloud.storage.BlobId)8 IOException (java.io.IOException)5 Acl (com.google.cloud.storage.Acl)4 ByteBuffer (java.nio.ByteBuffer)4 Storage (com.google.cloud.storage.Storage)3 StorageBatch (com.google.cloud.storage.StorageBatch)3 ReadChannel (com.google.cloud.ReadChannel)2 WriteChannel (com.google.cloud.WriteChannel)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpResponse (com.google.api.client.http.HttpResponse)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 Get (com.google.api.services.storage.Storage.Objects.Get)1 BatchResult (com.google.cloud.BatchResult)1 User (com.google.cloud.storage.Acl.User)1