This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathSpannerGaxRetryTest.java
More file actions
464 lines (431 loc) · 17.7 KB
/
Copy pathSpannerGaxRetryTest.java
File metadata and controls
464 lines (431 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright 2019 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
*
* https://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.google.cloud.spanner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import com.google.api.gax.grpc.testing.LocalChannelProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime;
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
import com.google.protobuf.ListValue;
import com.google.rpc.RetryInfo;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.StructType;
import com.google.spanner.v1.StructType.Field;
import com.google.spanner.v1.TypeCode;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.protobuf.ProtoUtils;
import java.time.Duration;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class SpannerGaxRetryTest {
private static final Statement SELECT1AND2 =
Statement.of("SELECT 1 AS COL1 UNION ALL SELECT 2 AS COL1");
private static final ResultSetMetadata SELECT1AND2_METADATA =
ResultSetMetadata.newBuilder()
.setRowType(
StructType.newBuilder()
.addFields(
Field.newBuilder()
.setName("COL1")
.setType(
com.google.spanner.v1.Type.newBuilder()
.setCode(TypeCode.INT64)
.build())
.build())
.build())
.build();
private static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET =
com.google.spanner.v1.ResultSet.newBuilder()
.addRows(
ListValue.newBuilder()
.addValues(com.google.protobuf.Value.newBuilder().setStringValue("1").build())
.build())
.addRows(
ListValue.newBuilder()
.addValues(com.google.protobuf.Value.newBuilder().setStringValue("2").build())
.build())
.setMetadata(SELECT1AND2_METADATA)
.build();
private static final Statement UPDATE_STATEMENT =
Statement.of("UPDATE FOO SET BAR=1 WHERE BAZ=2");
private static final long UPDATE_COUNT = 1L;
private static final SimulatedExecutionTime ONE_SECOND =
SimulatedExecutionTime.ofMinimumAndRandomTime(1000, 0);
private static final StatusRuntimeException UNAVAILABLE =
io.grpc.Status.UNAVAILABLE.withDescription("Retryable test exception.").asRuntimeException();
private static final StatusRuntimeException RESOURCE_EXHAUSTED_NON_RETRYABLE =
Status.RESOURCE_EXHAUSTED
.withDescription("Non-retryable test exception.")
.asRuntimeException();
private static final StatusRuntimeException RESOURCE_EXHAUSTED_RETRYABLE =
Status.RESOURCE_EXHAUSTED
.withDescription("Retryable test exception.")
.asRuntimeException(createRetryInfo());
private static final StatusRuntimeException FAILED_PRECONDITION =
io.grpc.Status.FAILED_PRECONDITION
.withDescription("Non-retryable test exception.")
.asRuntimeException();
private static MockSpannerServiceImpl mockSpanner;
private static Server server;
private static LocalChannelProvider channelProvider;
private Spanner spanner;
private DatabaseClient client;
private Spanner spannerWithTimeout;
private DatabaseClient clientWithTimeout;
@BeforeClass
public static void startStaticServer() throws Exception {
mockSpanner = new MockSpannerServiceImpl();
mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions.
mockSpanner.putStatementResult(StatementResult.query(SELECT1AND2, SELECT1_RESULTSET));
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
String uniqueName = InProcessServerBuilder.generateName();
server =
InProcessServerBuilder.forName(uniqueName)
// We need to use a real executor for timeouts to occur.
.scheduledExecutorService(new ScheduledThreadPoolExecutor(1))
.addService(mockSpanner)
.build()
.start();
channelProvider = LocalChannelProvider.create(uniqueName);
}
@AfterClass
public static void stopServer() throws InterruptedException {
server.shutdown();
server.awaitTermination();
}
@Before
public void setUp() throws Exception {
mockSpanner.reset();
mockSpanner.removeAllExecutionTimes();
SpannerOptions.Builder builder =
SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
.setChannelProvider(channelProvider)
.setCredentials(NoCredentials.getInstance());
// Make sure the session pool is empty by default.
SessionPoolOptions sessionPoolOptions =
SessionPoolOptions.newBuilder().setMinSessions(0).build();
// Add a wait time for sessions to be initialized. In this case, since minSessions = 0, the
// wait time is for multiplexed sessions
if (sessionPoolOptions.getUseMultiplexedSession()) {
sessionPoolOptions =
sessionPoolOptions.toBuilder()
.setWaitForMinSessionsDuration(Duration.ofSeconds(5))
.build();
}
builder.setSessionPoolOption(sessionPoolOptions);
// Create one client with default timeout values and one with short timeout values specifically
// for the test cases that expect a DEADLINE_EXCEEDED.
spanner = builder.build().getService();
client = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
final RetrySettings retrySettings =
RetrySettings.newBuilder()
.setInitialRetryDelayDuration(Duration.ofMillis(1L))
.setMaxRetryDelayDuration(Duration.ofMillis(1L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(175L))
.setMaxRpcTimeoutDuration(Duration.ofMillis(175L))
.setMaxAttempts(3)
.setTotalTimeoutDuration(Duration.ofMillis(200L))
.build();
RetrySettings commitRetrySettings =
RetrySettings.newBuilder()
.setInitialRetryDelayDuration(Duration.ofMillis(1L))
.setMaxRetryDelayDuration(Duration.ofMillis(1L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(5000L))
.setMaxRpcTimeoutDuration(Duration.ofMillis(10000L))
.setMaxAttempts(1)
.setTotalTimeoutDuration(Duration.ofMillis(20000L))
.build();
builder
.getSpannerStubSettingsBuilder()
.applyToAllUnaryMethods(
input -> {
input.setRetrySettings(retrySettings);
return null;
});
builder
.getSpannerStubSettingsBuilder()
.executeStreamingSqlSettings()
.setRetrySettings(retrySettings);
builder.getSpannerStubSettingsBuilder().commitSettings().setRetrySettings(commitRetrySettings);
builder
.getSpannerStubSettingsBuilder()
.executeStreamingSqlSettings()
.setRetrySettings(retrySettings);
builder.getSpannerStubSettingsBuilder().streamingReadSettings().setRetrySettings(retrySettings);
spannerWithTimeout = builder.build().getService();
clientWithTimeout =
spannerWithTimeout.getDatabaseClient(
DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
}
@After
public void tearDown() {
spannerWithTimeout.close();
spanner.close();
}
static Metadata createRetryInfo() {
Metadata trailers = new Metadata();
RetryInfo retryInfo =
RetryInfo.newBuilder()
.setRetryDelay(
com.google.protobuf.Duration.newBuilder()
.setNanos((int) TimeUnit.MILLISECONDS.toNanos(1L))
.setSeconds(0L))
.build();
trailers.put(ProtoUtils.keyForProto(RetryInfo.getDefaultInstance()), retryInfo);
return trailers;
}
private void warmUpSessionPool(DatabaseClient client) {
for (int i = 0; i < 10; i++) {
int retryCount = 0;
while (true) {
try {
TransactionRunner runner = client.readWriteTransaction();
long updateCount = runner.run(transaction -> transaction.executeUpdate(UPDATE_STATEMENT));
assertThat(updateCount, is(equalTo(UPDATE_COUNT)));
break;
} catch (SpannerException e) {
// On slow systems there is a chance of DEADLINE_EXCEEDED errors.
// These should be retried.
retryCount++;
if (e.getErrorCode() != ErrorCode.DEADLINE_EXCEEDED || retryCount > 10) {
throw e;
}
}
}
}
}
@Test
public void singleUseTimeout() {
if (isMultiplexedSessionsEnabled()) {
// for multiplexed sessions CreateSessions RPC is already completed during start-up
// hence, we are setting a strict delay with the next RPC
mockSpanner.setExecuteStreamingSqlExecutionTime(ONE_SECOND);
}
mockSpanner.setBatchCreateSessionsExecutionTime(ONE_SECOND);
try (ResultSet rs = clientWithTimeout.singleUse().executeQuery(SELECT1AND2)) {
SpannerException e = assertThrows(SpannerException.class, () -> rs.next());
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
}
@Test
public void singleUseUnavailable() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(UNAVAILABLE));
try (ResultSet resultSet = client.singleUse().executeQuery(SELECT1AND2)) {
assertTrue(resultSet.next());
}
}
@Test
public void singleUseResourceExhausted_nonRetryable() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(RESOURCE_EXHAUSTED_NON_RETRYABLE));
try (ResultSet resultSet = client.singleUse().executeQuery(SELECT1AND2)) {
SpannerException exception = assertThrows(SpannerException.class, resultSet::next);
assertEquals(ErrorCode.RESOURCE_EXHAUSTED, exception.getErrorCode());
}
}
@Test
public void singleUseResourceExhausted_retryable() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(RESOURCE_EXHAUSTED_RETRYABLE));
try (ResultSet resultSet = client.singleUse().executeQuery(SELECT1AND2)) {
assertTrue(resultSet.next());
}
}
@Test
public void singleUseNonRetryableError() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(FAILED_PRECONDITION));
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
SpannerException e = assertThrows(SpannerException.class, () -> rs.next());
assertEquals(ErrorCode.FAILED_PRECONDITION, e.getErrorCode());
}
}
@Test
public void singleUseNonRetryableErrorOnNext() {
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(FAILED_PRECONDITION));
SpannerException e = assertThrows(SpannerException.class, () -> rs.next());
assertEquals(ErrorCode.FAILED_PRECONDITION, e.getErrorCode());
}
}
@Test
public void singleUseInternal() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(new IllegalArgumentException()));
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
SpannerException e = assertThrows(SpannerException.class, () -> rs.next());
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
}
}
@Test
public void singleUseReadOnlyTransactionTimeout() {
if (isMultiplexedSessionsEnabled()) {
// for multiplexed sessions CreateSessions RPC is already completed during start-up
// hence, we are setting a strict delay with the next RPC
mockSpanner.setExecuteStreamingSqlExecutionTime(ONE_SECOND);
}
mockSpanner.setBatchCreateSessionsExecutionTime(ONE_SECOND);
try (ResultSet rs =
clientWithTimeout.singleUseReadOnlyTransaction().executeQuery(SELECT1AND2)) {
SpannerException e = assertThrows(SpannerException.class, () -> rs.next());
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
}
@Test
public void singleUseReadOnlyTransactionUnavailable() {
mockSpanner.addException(UNAVAILABLE);
try (ResultSet rs = client.singleUseReadOnlyTransaction().executeQuery(SELECT1AND2)) {
while (rs.next()) {}
}
}
@Test
public void singleUseExecuteStreamingSqlTimeout() {
try (ResultSet rs = clientWithTimeout.singleUse().executeQuery(SELECT1AND2)) {
mockSpanner.setExecuteStreamingSqlExecutionTime(ONE_SECOND);
SpannerException e = assertThrows(SpannerException.class, () -> rs.next());
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
}
@Test
public void singleUseExecuteStreamingSqlUnavailable() {
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
mockSpanner.addException(UNAVAILABLE);
while (rs.next()) {}
}
}
@Test
public void readWriteTransactionTimeout() {
mockSpanner.setBeginTransactionExecutionTime(ONE_SECOND);
SpannerException e =
assertThrows(
SpannerException.class,
() -> clientWithTimeout.readWriteTransaction().run(transaction -> null));
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
@Test
public void readWriteTransactionUnavailable() {
warmUpSessionPool(client);
mockSpanner.addException(UNAVAILABLE);
TransactionRunner runner = client.readWriteTransaction();
long updateCount = runner.run(transaction -> transaction.executeUpdate(UPDATE_STATEMENT));
assertThat(updateCount, is(equalTo(UPDATE_COUNT)));
}
@Test
public void readWriteTransactionStatementAborted() {
TransactionRunner runner = client.readWriteTransaction();
final AtomicInteger attempts = new AtomicInteger();
long updateCount =
runner.run(
transaction -> {
if (attempts.getAndIncrement() == 0) {
mockSpanner.abortNextStatement();
}
return transaction.executeUpdate(UPDATE_STATEMENT);
});
assertThat(updateCount, is(equalTo(UPDATE_COUNT)));
assertThat(attempts.get(), is(equalTo(2)));
}
@Test
public void readWriteTransactionCommitAborted() {
TransactionRunner runner = client.readWriteTransaction();
final AtomicInteger attempts = new AtomicInteger();
long updateCount =
runner.run(
transaction -> {
long res = transaction.executeUpdate(UPDATE_STATEMENT);
if (attempts.getAndIncrement() == 0) {
mockSpanner.abortTransaction(transaction);
}
return res;
});
assertThat(updateCount, is(equalTo(UPDATE_COUNT)));
assertThat(attempts.get(), is(equalTo(2)));
}
@Test(expected = Exception.class)
public void readWriteTransactionCheckedException() {
TransactionRunner runner = client.readWriteTransaction();
runner.run(
transaction -> {
transaction.executeUpdate(UPDATE_STATEMENT);
throw new Exception("test");
});
}
@Test(expected = SpannerException.class)
public void readWriteTransactionUncheckedException() {
TransactionRunner runner = client.readWriteTransaction();
runner.run(
transaction -> {
transaction.executeUpdate(UPDATE_STATEMENT);
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "test");
});
}
@Test
public void transactionManagerTimeout() {
mockSpanner.setExecuteSqlExecutionTime(ONE_SECOND);
try (TransactionManager txManager = clientWithTimeout.transactionManager()) {
TransactionContext tx = txManager.begin();
SpannerException e =
assertThrows(SpannerException.class, () -> tx.executeUpdate(UPDATE_STATEMENT));
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
}
@SuppressWarnings("resource")
@Test
public void transactionManagerUnavailable() {
warmUpSessionPool(client);
mockSpanner.addException(UNAVAILABLE);
try (TransactionManager txManager = client.transactionManager()) {
TransactionContext tx = txManager.begin();
while (true) {
try {
assertThat(tx.executeUpdate(UPDATE_STATEMENT), is(equalTo(UPDATE_COUNT)));
txManager.commit();
break;
} catch (AbortedException e) {
tx = txManager.resetForRetry();
}
}
}
}
private boolean isMultiplexedSessionsEnabled() {
if (spanner.getOptions() == null || spanner.getOptions().getSessionPoolOptions() == null) {
return false;
}
return spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSession();
}
}