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

docs: add new sample storage_configure_retries #1152

Merged
merged 2 commits into from Jan 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -227,6 +227,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Configure Retries | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) |
| Quickstart Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) |


Expand Down
@@ -0,0 +1,67 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.storage;
// [START storage_configure_retries]

import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.StorageRetryStrategy;
import org.threeten.bp.Duration;

public final class ConfigureRetries {
public static void main(String[] args) {
String bucketName = "my-bucket";
String blobName = "blob/to/delete";
deleteBlob(bucketName, blobName);
}

static void deleteBlob(String bucketName, String blobName) {
// Update the retry settings
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
RetrySettings retrySettings =
StorageOptions.getDefaultRetrySettings()
.toBuilder()
// to set the max number of attempts to 10
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
.setMaxAttempts(10)
// to set the backoff multiplier to 3.0
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
.setRetryDelayMultiplier(3.0)
// to set the max duration of all attempts to 5 minutes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unrealistically long for a delete, but fine if this is what other langs have.

BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
.setTotalTimeout(Duration.ofMinutes(5))
.build();

StorageOptions alwaysRetryStorageOptions =
StorageOptions.newBuilder()
// Configure our options so all requests will be retried even if they are
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
// non-idempotent.
.setStorageRetryStrategy(StorageRetryStrategy.getUniformStorageRetryStrategy())
// provide the previously configured retrySettings
.setRetrySettings(retrySettings)
.build();

// Instantiate a client with our options
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
Storage storage = alwaysRetryStorageOptions.getService();

// Delete the blob
BlobId blobId = BlobId.of(bucketName, blobName);
boolean success = storage.delete(blobId);

System.out.printf(
"Deletion of Blob %s completed %s.%n", blobId, success ? "successfully" : "unsuccessfully");
}
}
// [END storage_configure_retries]
@@ -0,0 +1,66 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.storage;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.cloud.testing.junit4.StdOutCaptureRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public final class ConfigureRetriesTest {
@Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule();

private String bucketName;
private Storage storage;
private String blobName;

private Blob blob;

@Before
public void setUp() {
blobName = "blob";
bucketName = RemoteStorageHelper.generateBucketName();
storage = StorageOptions.getDefaultInstance().getService();
storage.create(BucketInfo.of(bucketName));
blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build());
}

@After
public void tearDown() {
if (blob != null && blob.exists()) {
blob.delete();
}
storage.delete(bucketName);
}

@Test
public void testConfigureRetries() {
ConfigureRetries.deleteBlob(bucketName, blobName);
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Deletion");
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("successfully");
assertThat(stdOut.getCapturedOutputAsUtf8String()).doesNotContain("unsuccessfully");
}
}