/
JdbcGrpcErrorTest.java
404 lines (374 loc) · 15.6 KB
/
JdbcGrpcErrorTest.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
/*
* 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
*
* 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.google.cloud.spanner.jdbc;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
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.Statement;
import com.google.cloud.spanner.admin.database.v1.MockDatabaseAdminImpl;
import com.google.cloud.spanner.admin.instance.v1.MockInstanceAdminImpl;
import com.google.cloud.spanner.connection.SpannerPool;
import com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl;
import com.google.protobuf.ListValue;
import com.google.protobuf.Value;
import com.google.rpc.Code;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.StructType;
import com.google.spanner.v1.StructType.Field;
import com.google.spanner.v1.Type;
import com.google.spanner.v1.TypeCode;
import io.grpc.Server;
import io.grpc.Status;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Test that the JDBC driver propagates {@link SQLException}s when a gRPC error occurs. */
@RunWith(JUnit4.class)
public class JdbcGrpcErrorTest {
private static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1");
private static final ResultSetMetadata SELECT1_METADATA =
ResultSetMetadata.newBuilder()
.setRowType(
StructType.newBuilder()
.addFields(
Field.newBuilder()
.setName("COL1")
.setType(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(Value.newBuilder().setStringValue("1").build())
.build())
.setMetadata(SELECT1_METADATA)
.build();
private static final Statement UPDATE_STATEMENT =
Statement.of("UPDATE FOO SET BAR=1 WHERE BAZ=2");
private static final int UPDATE_COUNT = 1;
private static final Statement INVALID_UPDATE_STATEMENT =
Statement.of("UPDATE NON_EXISING_TABLE SET FOO=1 WHERE BAR=2");
private static MockSpannerServiceImpl mockSpanner;
private static MockInstanceAdminImpl mockInstanceAdmin;
private static MockDatabaseAdminImpl mockDatabaseAdmin;
private static Server server;
private static InetSocketAddress address;
// FAILED_PRECONDITION is chosen as the test error code as it should never be retryable.
private final Exception serverException =
Status.FAILED_PRECONDITION.withDescription("test exception").asRuntimeException();
private final SpannerJdbcExceptionMatcher<JdbcSqlExceptionImpl> testExceptionMatcher =
SpannerJdbcExceptionMatcher.matchCodeAndMessage(
JdbcSqlExceptionImpl.class, Code.FAILED_PRECONDITION, "test exception");
@BeforeClass
public static void startStaticServer() throws IOException {
mockSpanner = new MockSpannerServiceImpl();
mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions.
mockSpanner.putStatementResult(StatementResult.query(SELECT1, SELECT1_RESULTSET));
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
mockSpanner.putStatementResult(
StatementResult.exception(
INVALID_UPDATE_STATEMENT,
Status.NOT_FOUND.withDescription("Unknown table name").asRuntimeException()));
mockInstanceAdmin = new MockInstanceAdminImpl();
mockDatabaseAdmin = new MockDatabaseAdminImpl();
address = new InetSocketAddress("localhost", 0);
server =
NettyServerBuilder.forAddress(address)
.addService(mockSpanner)
.addService(mockInstanceAdmin)
.addService(mockDatabaseAdmin)
.build()
.start();
}
@AfterClass
public static void stopServer() throws Exception {
SpannerPool.closeSpannerPool();
server.shutdown();
server.awaitTermination();
}
@After
public void reset() {
// Close Spanner pool to prevent reusage of the same Spanner instance (and thereby the same
// session pool).
SpannerPool.closeSpannerPool();
mockSpanner.removeAllExecutionTimes();
mockSpanner.reset();
}
private String createUrl() {
return String.format(
"jdbc:cloudspanner://localhost:%d/projects/%s/instances/%s/databases/%s?usePlainText=true;minSessions=0",
server.getPort(), "proj", "inst", "db");
}
private Connection createConnection() throws SQLException {
return DriverManager.getConnection(createUrl());
}
@Test
public void autocommitBeginTransaction() {
mockSpanner.setBeginTransactionExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
// This triggers a retry with an explicit BeginTransaction RPC.
mockSpanner.abortNextStatement();
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void autocommitBeginPDMLTransaction() {
mockSpanner.setBeginTransactionExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.createStatement().execute("SET AUTOCOMMIT_DML_MODE='PARTITIONED_NON_ATOMIC'");
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void transactionalBeginTransaction() {
mockSpanner.setBeginTransactionExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
// This triggers a retry with an explicit BeginTransaction RPC.
mockSpanner.abortNextStatement();
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void readOnlyBeginTransaction() {
mockSpanner.setBeginTransactionExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.setReadOnly(true);
connection.createStatement().executeQuery(SELECT1.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void autocommitExecuteSql() {
mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void autocommitPDMLExecuteSql() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.createStatement().execute("SET AUTOCOMMIT_DML_MODE='PARTITIONED_NON_ATOMIC'");
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void transactionalExecuteSql() {
mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void autocommitExecuteBatchDml() throws SQLException {
mockSpanner.setExecuteBatchDmlExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
try (java.sql.Statement statement = connection.createStatement()) {
statement.addBatch(UPDATE_STATEMENT.getSql());
statement.addBatch(UPDATE_STATEMENT.getSql());
statement.executeBatch();
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
}
@Test
public void transactionalExecuteBatchDml() throws SQLException {
mockSpanner.setExecuteBatchDmlExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (java.sql.Statement statement = connection.createStatement()) {
statement.addBatch(UPDATE_STATEMENT.getSql());
statement.addBatch(UPDATE_STATEMENT.getSql());
statement.executeBatch();
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
}
@Test
public void autocommitCommit() {
mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void transactionalCommit() {
mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
connection.commit();
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void autocommitRollback() {
// The JDBC driver should throw the exception of the SQL statement and ignore any errors from
// the rollback() method.
mockSpanner.setRollbackExecutionTime(SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.createStatement().executeUpdate(INVALID_UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(
SpannerJdbcExceptionMatcher.matchCodeAndMessage(
JdbcSqlExceptionImpl.class, Code.NOT_FOUND, "Unknown table name")
.matches(e))
.isTrue();
}
}
@Test
public void transactionalRollback() throws SQLException {
// Rollback exceptions are ignored by the client library and not propagated to the JDBC driver.
// This method will therefore not throw any errors.
mockSpanner.setRollbackExecutionTime(SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
connection.rollback();
}
}
@Test
public void autocommitExecuteStreamingSql() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
try (java.sql.ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
rs.next();
fail("missing expected exception");
}
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void transactionalExecuteStreamingSql() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (java.sql.ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
rs.next();
fail("missing expected exception");
}
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void readOnlyExecuteStreamingSql() {
mockSpanner.setExecuteStreamingSqlExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.setReadOnly(true);
try (java.sql.ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
rs.next();
fail("missing expected exception");
}
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void autocommitCreateSession() {
mockSpanner.setBatchCreateSessionsExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void transactionalCreateSession() {
mockSpanner.setBatchCreateSessionsExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
@Test
public void readOnlyCreateSession() {
mockSpanner.setBatchCreateSessionsExecutionTime(
SimulatedExecutionTime.ofException(serverException));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.setReadOnly(true);
connection.createStatement().executeQuery(SELECT1.getSql());
fail("missing expected exception");
} catch (SQLException e) {
assertThat(testExceptionMatcher.matches(e)).isTrue();
}
}
}