Skip to content

Commit 4babbef

Browse files
authored
Merge branch 'main' into new-mode-support
2 parents b292d98 + f641a40 commit 4babbef

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractMultiplexedSessionDatabaseClient.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
*/
2727
abstract class AbstractMultiplexedSessionDatabaseClient implements DatabaseClient {
2828

29-
@Override
30-
public Dialect getDialect() {
31-
throw new UnsupportedOperationException();
32-
}
33-
3429
@Override
3530
public String getDatabaseRole() {
3631
throw new UnsupportedOperationException();

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ private boolean canUseMultiplexedSessionsForPartitionedOps() {
132132

133133
@Override
134134
public Dialect getDialect() {
135+
MultiplexedSessionDatabaseClient client = getMultiplexedSessionDatabaseClient();
136+
if (client != null) {
137+
return client.getDialect();
138+
}
135139
return pool.getDialect();
136140
}
137141

google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ public void onSessionReady(SessionImpl session) {
277277
.getSkipVerifyBeginTransactionForMuxRW()) {
278278
verifyBeginTransactionWithRWOnMultiplexedSessionAsync(session.getName());
279279
}
280+
if (sessionClient
281+
.getSpanner()
282+
.getOptions()
283+
.getSessionPoolOptions()
284+
.isAutoDetectDialect()) {
285+
MAINTAINER_SERVICE.submit(() -> getDialect());
286+
}
280287
}
281288

282289
@Override
@@ -513,6 +520,30 @@ private int getSingleUseChannelHint() {
513520
}
514521
}
515522

523+
private final AbstractLazyInitializer<Dialect> dialectSupplier =
524+
new AbstractLazyInitializer<Dialect>() {
525+
@Override
526+
protected Dialect initialize() {
527+
try (ResultSet dialectResultSet =
528+
singleUse().executeQuery(SessionPool.DETERMINE_DIALECT_STATEMENT)) {
529+
if (dialectResultSet.next()) {
530+
return Dialect.fromName(dialectResultSet.getString(0));
531+
}
532+
}
533+
// This should not really happen, but it is the safest fallback value.
534+
return Dialect.GOOGLE_STANDARD_SQL;
535+
}
536+
};
537+
538+
@Override
539+
public Dialect getDialect() {
540+
try {
541+
return dialectSupplier.get();
542+
} catch (Exception exception) {
543+
throw SpannerExceptionFactory.asSpannerException(exception);
544+
}
545+
}
546+
516547
@Override
517548
public Timestamp write(Iterable<Mutation> mutations) throws SpannerException {
518549
return createMultiplexedSessionTransaction(/* singleUse = */ false).write(mutations);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,13 +2256,9 @@ enum Position {
22562256
@VisibleForTesting
22572257
static final Statement DETERMINE_DIALECT_STATEMENT =
22582258
Statement.newBuilder(
2259-
"SELECT 'POSTGRESQL' AS DIALECT\n"
2260-
+ "FROM INFORMATION_SCHEMA.SCHEMATA\n"
2261-
+ "WHERE SCHEMA_NAME='information_schema'\n"
2262-
+ "UNION ALL\n"
2263-
+ "SELECT 'GOOGLE_STANDARD_SQL' AS DIALECT\n"
2264-
+ "FROM INFORMATION_SCHEMA.SCHEMATA\n"
2265-
+ "WHERE SCHEMA_NAME='INFORMATION_SCHEMA' AND CATALOG_NAME=''")
2259+
"select option_value "
2260+
+ "from information_schema.database_options "
2261+
+ "where option_name='database_dialect'")
22662262
.build();
22672263

22682264
private final SessionPoolOptions options;
@@ -3211,7 +3207,9 @@ public void onSessionReady(SessionImpl session) {
32113207
if (allSessions.size() >= minSessions) {
32123208
waitOnMinSessionsLatch.countDown();
32133209
}
3214-
if (options.isAutoDetectDialect() && !detectDialectStarted) {
3210+
if (options.isAutoDetectDialect()
3211+
&& !detectDialectStarted
3212+
&& !options.getUseMultiplexedSession()) {
32153213
// Get the dialect of the underlying database if that has not yet been done. Note that
32163214
// this method will release the session into the pool once it is done.
32173215
detectDialectStarted = true;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,7 +3998,7 @@ public void testCreateSessionsFailure_shouldNotPropagateToCloseMethod() {
39983998
try {
39993999
// Simulate session creation failures on the backend.
40004000
mockSpanner.setCreateSessionExecutionTime(
4001-
SimulatedExecutionTime.ofStickyException(Status.RESOURCE_EXHAUSTED.asRuntimeException()));
4001+
SimulatedExecutionTime.ofStickyException(Status.PERMISSION_DENIED.asRuntimeException()));
40024002
// This will not cause any failure as getting a session from the pool is guaranteed to be
40034003
// non-blocking, and any exceptions will be delayed until actual query execution.
40044004
mockSpanner.freeze();
@@ -4007,8 +4007,8 @@ public void testCreateSessionsFailure_shouldNotPropagateToCloseMethod() {
40074007
DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
40084008
try (ResultSet rs = client.singleUse().executeQuery(SELECT1)) {
40094009
mockSpanner.unfreeze();
4010-
SpannerException e = assertThrows(SpannerException.class, rs::next);
4011-
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
4010+
SpannerException exception = assertThrows(SpannerException.class, rs::next);
4011+
assertEquals(ErrorCode.PERMISSION_DENIED, exception.getErrorCode());
40124012
}
40134013
} finally {
40144014
mockSpanner.setCreateSessionExecutionTime(SimulatedExecutionTime.none());
@@ -4547,6 +4547,8 @@ public void testGetDialectPostgreSQLPreloaded() {
45474547
public void testGetDialect_FailsDirectlyIfDatabaseNotFound() {
45484548
mockSpanner.setBatchCreateSessionsExecutionTime(
45494549
SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database"));
4550+
mockSpanner.setCreateSessionExecutionTime(
4551+
SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database"));
45504552
DatabaseClient client =
45514553
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
45524554

@@ -4563,6 +4565,8 @@ public void testGetDialect_FailsDirectlyIfDatabaseNotFound() {
45634565
public void testGetDialectDefaultPreloaded_FailsDirectlyIfDatabaseNotFound() {
45644566
mockSpanner.setBatchCreateSessionsExecutionTime(
45654567
SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database"));
4568+
mockSpanner.setCreateSessionExecutionTime(
4569+
SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database"));
45664570
try (Spanner spanner =
45674571
this.spanner
45684572
.getOptions()

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/AbstractMockServerTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner.connection;
1818

19+
import com.google.cloud.spanner.Dialect;
1920
import com.google.cloud.spanner.ForceCloseSpannerFunction;
2021
import com.google.cloud.spanner.MockSpannerServiceImpl;
2122
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
@@ -198,6 +199,8 @@ public void getOperation(
198199
mockSpanner.putStatementResult(
199200
StatementResult.query(SELECT_RANDOM_STATEMENT, RANDOM_RESULT_SET));
200201
mockSpanner.putStatementResult(StatementResult.query(SELECT1_STATEMENT, SELECT1_RESULTSET));
202+
mockSpanner.putStatementResult(
203+
StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL));
201204

202205
futureParentHandlers = Logger.getLogger(AbstractFuture.class.getName()).getUseParentHandlers();
203206
exceptionRunnableParentHandlers =

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ public void testPostgreSQLGetDialect() {
674674
public void testGetDialect_DatabaseNotFound() throws Exception {
675675
mockSpanner.setBatchCreateSessionsExecutionTime(
676676
SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database"));
677+
mockSpanner.setCreateSessionExecutionTime(
678+
SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database"));
677679
try (Connection connection = createConnection()) {
678680
SpannerException exception = assertThrows(SpannerException.class, connection::getDialect);
679681
assertEquals(ErrorCode.NOT_FOUND, exception.getErrorCode());

0 commit comments

Comments
 (0)