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

Add retry of get blob in gcsstorage. #1753

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 @@ -65,9 +65,8 @@ private RetryExecutor getRetryExecutor()
public StorageObject open(String object)
{
checkArgument(object != null, "object is null");
Blob blob = storage.get(bucket, object);
String errorMessage = "opening file bucket " + bucket + " key " + object;
byte[] content = getWithRetry(errorMessage, () -> blob.getContent());
byte[] content = getWithRetry(errorMessage, () -> storage.get(bucket, object).getContent());
InputStream byteStream = new ByteArrayInputStream(content);
return new StorageObject(byteStream, content.length);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.digdag.storage.gcs;

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.io.ByteStreams;
Expand All @@ -11,6 +12,8 @@
import org.junit.Test;

import org.mockito.Mock;
import org.mockito.Mockito;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -24,6 +27,10 @@
import static io.digdag.util.Md5CountInputStream.digestMd5;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -100,6 +107,30 @@ public void listWithPrefix()
assertThat(all, containsInAnyOrder(dummyStorageObjectSummary("test/file/1", 1), dummyStorageObjectSummary("test/file/2", 1)));
}

@Test
public void testRetryGetContent()
throws Exception
{
com.google.cloud.storage.Storage gcsStorage = mock(com.google.cloud.storage.Storage.class);
Blob blob = mock(Blob.class);

ConfigFactory cf = new ConfigFactory(objectMapper());
String bucket = UUID.randomUUID().toString();
Config config = cf.create()
.set("bucket", bucket); // use unique bucket name
Storage storage = new GCSStorageFactory().newStorage(gcsStorage, config);

doThrow(new NullPointerException())
.doThrow(new NullPointerException())
.doReturn(blob)
.when(gcsStorage).get(Mockito.anyString(), Mockito.anyString());

doReturn("content".getBytes()).when(blob).getContent();

assertEquals("content", readString(storage.open("object").getContentInputStream()));
Mockito.verify(gcsStorage, Mockito.times(3)).get(Mockito.anyString(), Mockito.anyString());
}

public static StorageObjectSummary dummyStorageObjectSummary(String object, int contentLength){
return StorageObjectSummary.builder()
.key(object)
Expand Down