/
GapicSpannerRpcTest.java
470 lines (432 loc) · 17.9 KB
/
GapicSpannerRpcTest.java
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
465
466
467
468
469
470
/*
* 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.spi.v1;
import static com.google.common.truth.Truth.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.MockSpannerServiceImpl;
import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime;
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.SpannerOptions.CallContextConfigurator;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.spi.v1.GapicSpannerRpc.AdminRequestsLimitExceededRetryAlgorithm;
import com.google.cloud.spanner.spi.v1.SpannerRpc.Option;
import com.google.protobuf.ListValue;
import com.google.rpc.ErrorInfo;
import com.google.spanner.v1.ExecuteSqlRequest;
import com.google.spanner.v1.GetSessionRequest;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.SpannerGrpc;
import com.google.spanner.v1.StructType;
import com.google.spanner.v1.StructType.Field;
import com.google.spanner.v1.TypeCode;
import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
import io.grpc.MethodDescriptor;
import io.grpc.Server;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.Status;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import io.grpc.protobuf.lite.ProtoLiteUtils;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.threeten.bp.Duration;
@RunWith(Parameterized.class)
public class GapicSpannerRpcTest {
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_FOO_STATEMENT =
Statement.of("UPDATE FOO SET BAR=1 WHERE BAZ=2");
private static final String STATIC_OAUTH_TOKEN = "STATIC_TEST_OAUTH_TOKEN";
private static final String VARIABLE_OAUTH_TOKEN = "VARIABLE_TEST_OAUTH_TOKEN";
private static final OAuth2Credentials STATIC_CREDENTIALS =
OAuth2Credentials.create(
new AccessToken(
STATIC_OAUTH_TOKEN,
new java.util.Date(
System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS))));
private static final OAuth2Credentials VARIABLE_CREDENTIALS =
OAuth2Credentials.create(
new AccessToken(
VARIABLE_OAUTH_TOKEN,
new java.util.Date(
System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS))));
private static MockSpannerServiceImpl mockSpanner;
private static Server server;
private static InetSocketAddress address;
private static final Map<SpannerRpc.Option, Object> optionsMap = new HashMap<>();
private static Metadata lastSeenHeaders;
private static String defaultUserAgent;
private static Spanner spanner;
@Parameter public Dialect dialect;
@Parameters(name = "dialect = {0}")
public static Object[] data() {
return Dialect.values();
}
@Before
public void startServer() throws IOException {
assumeTrue(
"Skip tests when emulator is enabled as this test interferes with the check whether the emulator is running",
System.getenv("SPANNER_EMULATOR_HOST") == null);
defaultUserAgent = "spanner-java/" + GaxProperties.getLibraryVersion(GapicSpannerRpc.class);
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_FOO_STATEMENT, 1L));
address = new InetSocketAddress("localhost", 0);
server =
NettyServerBuilder.forAddress(address)
.addService(mockSpanner)
// Add a server interceptor that will check that we receive the variable OAuth token
// from the CallCredentials, and not the one set as static credentials.
.intercept(
new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
lastSeenHeaders = headers;
String auth =
headers.get(Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER));
assertThat(auth).isEqualTo("Bearer " + VARIABLE_OAUTH_TOKEN);
return Contexts.interceptCall(Context.current(), call, headers, next);
}
})
.build()
.start();
optionsMap.put(Option.CHANNEL_HINT, 1L);
spanner = createSpannerOptions().getService();
}
@After
public void reset() throws InterruptedException {
if (mockSpanner != null) {
mockSpanner.reset();
}
if (spanner != null) {
spanner.close();
}
if (server != null) {
server.shutdown();
server.awaitTermination();
}
}
@Test
public void testCallCredentialsProviderPreferenceAboveCredentials() {
SpannerOptions options =
SpannerOptions.newBuilder()
.setProjectId("some-project")
.setCredentials(STATIC_CREDENTIALS)
.setCallCredentialsProvider(() -> MoreCallCredentials.from(VARIABLE_CREDENTIALS))
.build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
// GoogleAuthLibraryCallCredentials doesn't implement equals, so we can only check for the
// existence.
assertThat(
rpc.newCallContext(
optionsMap,
"/some/resource",
GetSessionRequest.getDefaultInstance(),
SpannerGrpc.getGetSessionMethod())
.getCallOptions()
.getCredentials())
.isNotNull();
rpc.shutdown();
}
@Test
public void testCallCredentialsProviderReturnsNull() {
SpannerOptions options =
SpannerOptions.newBuilder()
.setProjectId("some-project")
.setCredentials(STATIC_CREDENTIALS)
.setCallCredentialsProvider(() -> null)
.build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
assertThat(
rpc.newCallContext(
optionsMap,
"/some/resource",
GetSessionRequest.getDefaultInstance(),
SpannerGrpc.getGetSessionMethod())
.getCallOptions()
.getCredentials())
.isNull();
rpc.shutdown();
}
@Test
public void testNoCallCredentials() {
SpannerOptions options =
SpannerOptions.newBuilder()
.setProjectId("some-project")
.setCredentials(STATIC_CREDENTIALS)
.build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
assertThat(
rpc.newCallContext(
optionsMap,
"/some/resource",
GetSessionRequest.getDefaultInstance(),
SpannerGrpc.getGetSessionMethod())
.getCallOptions()
.getCredentials())
.isNull();
rpc.shutdown();
}
@Test
public void testClientCompressorGzip() {
SpannerOptions options =
SpannerOptions.newBuilder().setProjectId("some-project").setCompressorName("gzip").build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
assertEquals(
"gzip",
rpc.newCallContext(
optionsMap,
"/some/resource",
GetSessionRequest.getDefaultInstance(),
SpannerGrpc.getGetSessionMethod())
.getCallOptions()
.getCompressor());
rpc.shutdown();
}
@Test
public void testClientCompressorIdentity() {
SpannerOptions options =
SpannerOptions.newBuilder()
.setProjectId("some-project")
.setCompressorName("identity")
.build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
assertEquals(
"identity",
rpc.newCallContext(
optionsMap,
"/some/resource",
GetSessionRequest.getDefaultInstance(),
SpannerGrpc.getGetSessionMethod())
.getCallOptions()
.getCompressor());
rpc.shutdown();
}
@Test
public void testClientCompressorDefault() {
SpannerOptions options = SpannerOptions.newBuilder().setProjectId("some-project").build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
assertNull(
rpc.newCallContext(
optionsMap,
"/some/resource",
GetSessionRequest.getDefaultInstance(),
SpannerGrpc.getGetSessionMethod())
.getCallOptions()
.getCompressor());
rpc.shutdown();
}
private static final class TimeoutHolder {
private Duration timeout;
}
@Test
public void testCallContextTimeout() {
// Create a CallContextConfigurator that uses a variable timeout value.
final TimeoutHolder timeoutHolder = new TimeoutHolder();
CallContextConfigurator configurator =
new CallContextConfigurator() {
@Override
public <ReqT, RespT> ApiCallContext configure(
ApiCallContext context, ReqT request, MethodDescriptor<ReqT, RespT> method) {
// Only configure a timeout for the ExecuteSql method as this method is used for
// executing DML statements.
if (request instanceof ExecuteSqlRequest
&& method.equals(SpannerGrpc.getExecuteSqlMethod())) {
ExecuteSqlRequest sqlRequest = (ExecuteSqlRequest) request;
// Sequence numbers are only assigned for DML statements, which means that
// this is an update statement.
if (sqlRequest.getSeqno() > 0L) {
return context.withTimeout(timeoutHolder.timeout);
}
}
return null;
}
};
mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0));
final DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
Context context =
Context.current().withValue(SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY, configurator);
context.run(
() -> {
// First try with a 1ns timeout. This should always cause a DEADLINE_EXCEEDED
// exception.
timeoutHolder.timeout = Duration.ofNanos(1L);
SpannerException e =
assertThrows(
SpannerException.class,
() ->
client
.readWriteTransaction()
.run(transaction -> transaction.executeUpdate(UPDATE_FOO_STATEMENT)));
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
// Then try with a longer timeout. This should now succeed.
timeoutHolder.timeout = Duration.ofMinutes(1L);
long updateCount =
client
.readWriteTransaction()
.run(transaction -> transaction.executeUpdate(UPDATE_FOO_STATEMENT));
assertEquals(1L, updateCount);
});
}
@Test
public void testNewCallContextWithNullRequestAndNullMethod() {
SpannerOptions options = SpannerOptions.newBuilder().setProjectId("some-project").build();
GapicSpannerRpc rpc = new GapicSpannerRpc(options, false);
assertThat(rpc.newCallContext(optionsMap, "/some/resource", null, null)).isNotNull();
rpc.shutdown();
}
@Test
public void testAdminRequestsLimitExceededRetryAlgorithm() {
AdminRequestsLimitExceededRetryAlgorithm<Long> alg =
new AdminRequestsLimitExceededRetryAlgorithm<>();
assertThat(alg.shouldRetry(null, 1L)).isFalse();
ErrorInfo info =
ErrorInfo.newBuilder()
.putMetadata("quota_limit", "AdminMethodQuotaPerMinutePerProject")
.build();
Metadata.Key<ErrorInfo> key =
Metadata.Key.of(
info.getDescriptorForType().getFullName() + Metadata.BINARY_HEADER_SUFFIX,
ProtoLiteUtils.metadataMarshaller(info));
Metadata trailers = new Metadata();
trailers.put(key, info);
SpannerException adminRateExceeded =
SpannerExceptionFactory.newSpannerException(
Status.RESOURCE_EXHAUSTED.withDescription("foo").asRuntimeException(trailers));
assertThat(alg.shouldRetry(adminRateExceeded, null)).isTrue();
SpannerException numDatabasesExceeded =
SpannerExceptionFactory.newSpannerException(
Status.RESOURCE_EXHAUSTED
.withDescription("Too many databases on instance")
.asRuntimeException());
assertThat(alg.shouldRetry(numDatabasesExceeded, null)).isFalse();
assertThat(alg.shouldRetry(new Exception("random exception"), null)).isFalse();
}
@Test
public void testDefaultUserAgent() {
final DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
rs.next();
}
assertThat(lastSeenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER)))
.contains(defaultUserAgent);
}
@Test
public void testCustomUserAgent() {
for (String headerId : new String[] {"user-agent", "User-Agent", "USER-AGENT"}) {
final HeaderProvider userAgentHeaderProvider =
() -> {
final Map<String, String> headers = new HashMap<>();
headers.put(headerId, "test-agent");
return headers;
};
final SpannerOptions options =
createSpannerOptions().toBuilder().setHeaderProvider(userAgentHeaderProvider).build();
try (Spanner spanner = options.getService()) {
final DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
rs.next();
}
assertThat(lastSeenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER)))
.contains("test-agent " + defaultUserAgent);
}
}
}
private SpannerOptions createSpannerOptions() {
String endpoint = address.getHostString() + ":" + server.getPort();
return SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
// Set a custom channel configurator to allow http instead of https.
.setChannelConfigurator(
input -> {
input.usePlaintext();
return input;
})
.setHost("http://" + endpoint)
// Set static credentials that will return the static OAuth test token.
.setCredentials(STATIC_CREDENTIALS)
// Also set a CallCredentialsProvider. These credentials should take precedence above
// the static credentials.
.setCallCredentialsProvider(() -> MoreCallCredentials.from(VARIABLE_CREDENTIALS))
.build();
}
}