Skip to content

Commit 3cf2283

Browse files
authored
docs: add sample for setting timeout and retry settings (GoogleCloudPlatform#3445)
* docs: add sample for setting timeout and retry settings * fix: move start tag * docs: explain why retry settings use a separate chain
1 parent 62c126c commit 3cf2283

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2020 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
//[START spanner_set_custom_timeout_and_retry]
20+
import com.google.api.gax.retrying.RetrySettings;
21+
import com.google.api.gax.rpc.StatusCode.Code;
22+
import com.google.cloud.spanner.DatabaseClient;
23+
import com.google.cloud.spanner.DatabaseId;
24+
import com.google.cloud.spanner.Spanner;
25+
import com.google.cloud.spanner.SpannerOptions;
26+
import com.google.cloud.spanner.Statement;
27+
import com.google.cloud.spanner.TransactionContext;
28+
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
29+
import org.threeten.bp.Duration;
30+
31+
class CustomTimeoutAndRetrySettingsExample {
32+
33+
static void executeSqlWithCustomTimeoutAndRetrySettings() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "my-project";
36+
String instanceId = "my-instance";
37+
String databaseId = "my-database";
38+
39+
executeSqlWithCustomTimeoutAndRetrySettings(projectId, instanceId, databaseId);
40+
}
41+
42+
// Create a Spanner client with custom ExecuteSql timeout and retry settings.
43+
static void executeSqlWithCustomTimeoutAndRetrySettings(
44+
String projectId, String instanceId, String databaseId) {
45+
SpannerOptions.Builder builder = SpannerOptions.newBuilder().setProjectId(projectId);
46+
// Set custom timeout and retry settings for the ExecuteSql RPC.
47+
// This must be done in a separate chain as the setRetryableCodes and setRetrySettings methods
48+
// return a UnaryCallSettings.Builder instead of a SpannerOptions.Builder.
49+
builder
50+
.getSpannerStubSettingsBuilder()
51+
.executeSqlSettings()
52+
// Configure which errors should be retried.
53+
.setRetryableCodes(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE)
54+
.setRetrySettings(
55+
RetrySettings.newBuilder()
56+
// Configure retry delay settings.
57+
.setInitialRetryDelay(Duration.ofMillis(500))
58+
.setMaxRetryDelay(Duration.ofSeconds(64))
59+
.setRetryDelayMultiplier(1.5)
60+
61+
// Configure RPC and total timeout settings.
62+
.setInitialRpcTimeout(Duration.ofSeconds(60))
63+
.setMaxRpcTimeout(Duration.ofSeconds(60))
64+
.setRpcTimeoutMultiplier(1.0)
65+
.setTotalTimeout(Duration.ofSeconds(60))
66+
.build());
67+
// Create a Spanner client using the custom retry and timeout settings.
68+
try (Spanner spanner = builder.build().getService()) {
69+
DatabaseClient client =
70+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
71+
client
72+
.readWriteTransaction()
73+
.run(
74+
new TransactionCallable<Void>() {
75+
@Override
76+
public Void run(TransactionContext transaction) throws Exception {
77+
String sql =
78+
"INSERT Singers (SingerId, FirstName, LastName)\n"
79+
+ "VALUES (20, 'George', 'Washington')";
80+
long rowCount = transaction.executeUpdate(Statement.of(sql));
81+
System.out.printf("%d record inserted.%n", rowCount);
82+
return null;
83+
}
84+
});
85+
}
86+
}
87+
}
88+
// [END spanner_set_custom_timeout_and_retry]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2020 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.spanner.DatabaseAdminClient;
22+
import com.google.cloud.spanner.DatabaseId;
23+
import com.google.cloud.spanner.Instance;
24+
import com.google.cloud.spanner.Spanner;
25+
import com.google.cloud.spanner.SpannerOptions;
26+
import com.google.common.collect.ImmutableList;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
import java.util.Iterator;
30+
import java.util.UUID;
31+
import org.junit.AfterClass;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
/** Integration tests for Cloud Spanner cloud client examples. */
38+
@RunWith(JUnit4.class)
39+
public class SpannerStandaloneExamplesIT {
40+
// The instance needs to exist for tests to pass.
41+
private static String instanceId = System.getProperty("spanner.test.instance");
42+
private static String databaseId =
43+
formatForTest(System.getProperty("spanner.sample.database", "mysample"));
44+
private static DatabaseId dbId;
45+
private static DatabaseAdminClient dbClient;
46+
private static Spanner spanner;
47+
48+
private String runExample(Runnable example) {
49+
PrintStream stdOut = System.out;
50+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
51+
PrintStream out = new PrintStream(bout);
52+
System.setOut(out);
53+
example.run();
54+
System.setOut(stdOut);
55+
return bout.toString();
56+
}
57+
58+
@BeforeClass
59+
public static void createTestDatabase() throws Exception {
60+
SpannerOptions options = SpannerOptions.newBuilder().build();
61+
spanner = options.getService();
62+
dbClient = spanner.getDatabaseAdminClient();
63+
if (instanceId == null) {
64+
Iterator<Instance> iterator =
65+
spanner.getInstanceAdminClient().listInstances().iterateAll().iterator();
66+
if (iterator.hasNext()) {
67+
instanceId = iterator.next().getId().getInstance();
68+
}
69+
}
70+
dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
71+
dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase());
72+
dbClient
73+
.createDatabase(
74+
instanceId,
75+
databaseId,
76+
ImmutableList.of(
77+
"CREATE TABLE Singers ("
78+
+ " SingerId INT64 NOT NULL,"
79+
+ " FirstName STRING(1024),"
80+
+ " LastName STRING(1024),"
81+
+ " SingerInfo BYTES(MAX)"
82+
+ ") PRIMARY KEY (SingerId)"))
83+
.get();
84+
}
85+
86+
@AfterClass
87+
public static void dropTestDatabase() throws Exception {
88+
dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase());
89+
spanner.close();
90+
}
91+
92+
@Test
93+
public void executeSqlWithCustomTimeoutAndRetrySettings_shouldWriteData() {
94+
String projectId = spanner.getOptions().getProjectId();
95+
String out =
96+
runExample(
97+
() ->
98+
CustomTimeoutAndRetrySettingsExample.executeSqlWithCustomTimeoutAndRetrySettings(
99+
projectId, instanceId, databaseId));
100+
assertThat(out).contains("1 record inserted.");
101+
}
102+
103+
static String formatForTest(String name) {
104+
return name + "-" + UUID.randomUUID().toString().substring(0, 20);
105+
}
106+
}

0 commit comments

Comments
 (0)