Skip to content

Commit 056f54f

Browse files
authored
docs: add sample for timeout for one RPC (#707)
* docs: add sample for timeout for one RPC Adds a sample for setting a timeout for a single RPC. Fixes GoogleCloudPlatform/java-docs-samples#3805 * fix: fix import order * fix: format code with the correct formatter plugin * fix: delete test data after each test * fix: auto-throttle admin requests
1 parent c255323 commit 056f54f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 com.google.api.gax.grpc.GrpcCallContext;
20+
import com.google.api.gax.rpc.ApiCallContext;
21+
import com.google.cloud.spanner.DatabaseClient;
22+
import com.google.cloud.spanner.DatabaseId;
23+
import com.google.cloud.spanner.Spanner;
24+
import com.google.cloud.spanner.SpannerOptions;
25+
import com.google.cloud.spanner.SpannerOptions.CallContextConfigurator;
26+
import com.google.cloud.spanner.Statement;
27+
import com.google.cloud.spanner.TransactionContext;
28+
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
29+
import com.google.spanner.v1.SpannerGrpc;
30+
import io.grpc.CallOptions;
31+
import io.grpc.Context;
32+
import io.grpc.MethodDescriptor;
33+
import java.util.concurrent.TimeUnit;
34+
35+
class StatementTimeoutExample {
36+
37+
static void executeSqlWithTimeout() {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String projectId = "my-project";
40+
String instanceId = "my-instance";
41+
String databaseId = "my-database";
42+
43+
try (Spanner spanner =
44+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
45+
DatabaseClient client =
46+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
47+
executeSqlWithTimeout(client);
48+
}
49+
}
50+
51+
static void executeSqlWithTimeout(DatabaseClient client) {
52+
CallContextConfigurator configurator = new CallContextConfigurator() {
53+
public <ReqT, RespT> ApiCallContext configure(ApiCallContext context, ReqT request,
54+
MethodDescriptor<ReqT, RespT> method) {
55+
// DML uses the ExecuteSql RPC.
56+
if (method == SpannerGrpc.getExecuteSqlMethod()) {
57+
return GrpcCallContext.createDefault()
58+
.withCallOptions(CallOptions.DEFAULT.withDeadlineAfter(60L, TimeUnit.SECONDS));
59+
}
60+
// Return null to indicate that the default should be used for other methods.
61+
return null;
62+
}
63+
};
64+
// Create a context that uses the custom call configuration.
65+
Context context =
66+
Context.current().withValue(SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY, configurator);
67+
// Run the transaction in the custom context.
68+
context.run(new Runnable() {
69+
public void run() {
70+
client.readWriteTransaction().run(new TransactionCallable<long[]>() {
71+
public long[] run(TransactionContext transaction) throws Exception {
72+
String sql = "INSERT Singers (SingerId, FirstName, LastName)\n"
73+
+ "VALUES (20, 'George', 'Washington')";
74+
long rowCount = transaction.executeUpdate(Statement.of(sql));
75+
System.out.printf("%d record inserted.%n", rowCount);
76+
return null;
77+
}
78+
});
79+
}
80+
});
81+
}
82+
}

samples/snippets/src/test/java/com/example/spanner/SpannerStandaloneExamplesIT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public void deleteTestData() {
107107
String projectId = spanner.getOptions().getProjectId();
108108
DatabaseClient client =
109109
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
110+
client.write(Collections.singleton(Mutation.delete("Singers", KeySet.all())));
110111
client.write(Collections.singleton(Mutation.delete("Venues", KeySet.all())));
111112
}
112113

@@ -121,6 +122,15 @@ public void executeSqlWithCustomTimeoutAndRetrySettings_shouldWriteData() {
121122
assertThat(out).contains("1 record inserted.");
122123
}
123124

125+
@Test
126+
public void executeSqlWithTimeout_shouldWriteData() {
127+
String projectId = spanner.getOptions().getProjectId();
128+
DatabaseClient client =
129+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
130+
String out = runExample(() -> StatementTimeoutExample.executeSqlWithTimeout(client));
131+
assertThat(out).contains("1 record inserted.");
132+
}
133+
124134
@Test
125135
public void addNumericColumn_shouldSuccessfullyAddColumn()
126136
throws InterruptedException, ExecutionException {

0 commit comments

Comments
 (0)