-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Storage: Fix slow download performance #5791
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c00cb4c
download blob using direct download method
ajaaym 615dab7
update test case
ajaaym ae1ffab
fix format
ajaaym 1bdc864
add test case for retries
ajaaym 48777c2
remove method
ajaaym 0aebf4c
update test retry setting
ajaaym 58469f5
fix lint and add comment
ajaaym cb65519
fix lint
ajaaym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ | |
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import com.google.api.core.ApiClock; | ||
| import com.google.api.gax.retrying.RetrySettings; | ||
| import com.google.api.services.storage.model.StorageObject; | ||
| import com.google.cloud.ReadChannel; | ||
| import com.google.cloud.storage.Acl.Project; | ||
| import com.google.cloud.storage.Acl.Project.ProjectRole; | ||
|
|
@@ -41,12 +44,13 @@ | |
| import com.google.cloud.storage.Blob.BlobSourceOption; | ||
| import com.google.cloud.storage.Storage.BlobWriteOption; | ||
| import com.google.cloud.storage.Storage.CopyRequest; | ||
| import com.google.cloud.storage.spi.v1.StorageRpc; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.io.BaseEncoding; | ||
| import java.io.File; | ||
| import java.io.OutputStream; | ||
| import java.net.URL; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.file.Files; | ||
| import java.security.Key; | ||
| import java.util.List; | ||
|
|
@@ -131,6 +135,24 @@ public class BlobTest { | |
| private static final Key KEY = | ||
| new SecretKeySpec(BaseEncoding.base64().decode(BASE64_KEY), "AES256"); | ||
|
|
||
| // This retrying setting is used by test testDownloadWithRetries. This unit test is setup | ||
| // to write one byte and then throw retryable exception, it then writes another bytes on | ||
| // second call succeeds. | ||
| private static final RetrySettings RETRY_SETTINGS = | ||
| RetrySettings.newBuilder().setMaxAttempts(2).build(); | ||
| private static final ApiClock API_CLOCK = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document how retries are being exercised in these unit tests. It will help for posterity. |
||
| new ApiClock() { | ||
| @Override | ||
| public long nanoTime() { | ||
| return 42_000_000_000L; | ||
| } | ||
|
|
||
| @Override | ||
| public long millisTime() { | ||
| return 42_000L; | ||
| } | ||
| }; | ||
|
|
||
| private Storage storage; | ||
| private Blob blob; | ||
| private Blob expectedBlob; | ||
|
|
@@ -566,28 +588,75 @@ public void testBuilder() { | |
| @Test | ||
| public void testDownload() throws Exception { | ||
| final byte[] expected = {1, 2}; | ||
| StorageRpc mockStorageRpc = createNiceMock(StorageRpc.class); | ||
| expect(storage.getOptions()).andReturn(mockOptions).times(1); | ||
| replay(storage); | ||
| expect(mockOptions.getStorageRpcV1()).andReturn(mockStorageRpc); | ||
| expect(mockOptions.getRetrySettings()).andReturn(RETRY_SETTINGS); | ||
| expect(mockOptions.getClock()).andReturn(API_CLOCK); | ||
| replay(mockOptions); | ||
| blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO)); | ||
| expect( | ||
| mockStorageRpc.read( | ||
| anyObject(StorageObject.class), | ||
| anyObject(Map.class), | ||
| eq(0l), | ||
| anyObject(OutputStream.class))) | ||
| .andAnswer( | ||
| new IAnswer<Long>() { | ||
| @Override | ||
| public Long answer() throws Throwable { | ||
| ((OutputStream) getCurrentArguments()[3]).write(expected); | ||
| return 2l; | ||
| } | ||
| }); | ||
| replay(mockStorageRpc); | ||
| File file = File.createTempFile("blob", ".tmp"); | ||
| blob.downloadTo(file.toPath()); | ||
| byte actual[] = Files.readAllBytes(file.toPath()); | ||
| assertArrayEquals(expected, actual); | ||
| } | ||
|
|
||
| initializeExpectedBlob(2); | ||
| ReadChannel channel = createNiceMock(ReadChannel.class); | ||
| @Test | ||
| public void testDownloadWithRetries() throws Exception { | ||
| final byte[] expected = {1, 2}; | ||
| StorageRpc mockStorageRpc = createNiceMock(StorageRpc.class); | ||
| expect(storage.getOptions()).andReturn(mockOptions); | ||
| expect(storage.reader(BLOB_INFO.getBlobId())).andReturn(channel); | ||
| replay(storage); | ||
| // First read should return 2 bytes. | ||
| expect(channel.read(anyObject(ByteBuffer.class))) | ||
| expect(mockOptions.getStorageRpcV1()).andReturn(mockStorageRpc); | ||
| expect(mockOptions.getRetrySettings()).andReturn(RETRY_SETTINGS); | ||
| expect(mockOptions.getClock()).andReturn(API_CLOCK); | ||
| replay(mockOptions); | ||
| blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO)); | ||
| expect( | ||
| mockStorageRpc.read( | ||
| anyObject(StorageObject.class), | ||
| anyObject(Map.class), | ||
| eq(0l), | ||
| anyObject(OutputStream.class))) | ||
| .andAnswer( | ||
| new IAnswer<Integer>() { | ||
| new IAnswer<Long>() { | ||
| @Override | ||
| public Integer answer() throws Throwable { | ||
| // Modify the argument to match the expected behavior of `read`. | ||
| ((ByteBuffer) getCurrentArguments()[0]).put(expected); | ||
| return 2; | ||
| public Long answer() throws Throwable { | ||
| ((OutputStream) getCurrentArguments()[3]).write(expected[0]); | ||
| throw new StorageException(504, "error"); | ||
| } | ||
| }); | ||
| // Second read should return 0 bytes. | ||
| expect(channel.read(anyObject(ByteBuffer.class))).andReturn(0); | ||
| replay(channel); | ||
| initializeBlob(); | ||
|
|
||
| expect( | ||
| mockStorageRpc.read( | ||
| anyObject(StorageObject.class), | ||
| anyObject(Map.class), | ||
| eq(1l), | ||
| anyObject(OutputStream.class))) | ||
| .andAnswer( | ||
| new IAnswer<Long>() { | ||
| @Override | ||
| public Long answer() throws Throwable { | ||
| ((OutputStream) getCurrentArguments()[3]).write(expected[1]); | ||
| return 1l; | ||
| } | ||
| }); | ||
| replay(mockStorageRpc); | ||
| File file = File.createTempFile("blob", ".tmp"); | ||
| blob.downloadTo(file.toPath()); | ||
| byte actual[] = Files.readAllBytes(file.toPath()); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's heavy retry settings in the tests. Is this by design and how do they help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated retry settings needed for test.