|
29 | 29 | import static org.junit.Assert.assertFalse; |
30 | 30 | import static org.junit.Assert.assertNotNull; |
31 | 31 | import static org.junit.Assert.assertNull; |
| 32 | +import static org.junit.Assert.assertSame; |
32 | 33 | import static org.junit.Assert.assertThrows; |
33 | 34 | import static org.junit.Assert.assertTrue; |
34 | 35 |
|
|
115 | 116 | import java.util.concurrent.Executors; |
116 | 117 | import java.util.concurrent.TimeUnit; |
117 | 118 | import java.util.concurrent.atomic.AtomicInteger; |
| 119 | +import java.util.concurrent.atomic.AtomicReference; |
118 | 120 | import java.util.function.Function; |
119 | 121 | import java.util.logging.Level; |
120 | 122 | import java.util.logging.Logger; |
@@ -3145,6 +3147,122 @@ public void testGetDialectDefaultPreloaded_FailsDirectlyIfDatabaseNotFound() { |
3145 | 3147 | } |
3146 | 3148 | } |
3147 | 3149 |
|
| 3150 | + @Test |
| 3151 | + public void testGetDialect_RecoversAfterTransientCreateSessionFailure() throws Exception { |
| 3152 | + mockSpanner.putStatementResult(StatementResult.detectDialectResult(Dialect.POSTGRESQL)); |
| 3153 | + mockSpanner.setCreateSessionExecutionTime( |
| 3154 | + SimulatedExecutionTime.ofException( |
| 3155 | + Status.DEADLINE_EXCEEDED |
| 3156 | + .withDescription("Simulated transient outage on createSession call #1") |
| 3157 | + .asRuntimeException())); |
| 3158 | + mockSpanner.freeze(); |
| 3159 | + try { |
| 3160 | + DatabaseClient client = |
| 3161 | + spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); |
| 3162 | + AtomicReference<Throwable> firstCallError = new AtomicReference<>(); |
| 3163 | + Thread firstCaller = |
| 3164 | + new Thread( |
| 3165 | + () -> { |
| 3166 | + try { |
| 3167 | + client.getDialect(); |
| 3168 | + } catch (Throwable t) { |
| 3169 | + firstCallError.set(t); |
| 3170 | + } |
| 3171 | + }); |
| 3172 | + firstCaller.start(); |
| 3173 | + // Wait (up to 10s) until firstCaller is blocked waiting on the initial multiplexed session. |
| 3174 | + Stopwatch watch = Stopwatch.createStarted(); |
| 3175 | + while (firstCaller.getState() != Thread.State.WAITING |
| 3176 | + && firstCaller.getState() != Thread.State.TIMED_WAITING |
| 3177 | + && firstCaller.getState() != Thread.State.TERMINATED |
| 3178 | + && watch.elapsed(TimeUnit.SECONDS) < 10) { |
| 3179 | + Thread.yield(); |
| 3180 | + } |
| 3181 | + mockSpanner.unfreeze(); |
| 3182 | + firstCaller.join(10_000L); |
| 3183 | + assertFalse(firstCaller.isAlive()); |
| 3184 | + |
| 3185 | + assertThat(firstCallError.get()).isInstanceOf(SpannerException.class); |
| 3186 | + SpannerException firstException = (SpannerException) firstCallError.get(); |
| 3187 | + assertEquals(ErrorCode.DEADLINE_EXCEEDED, firstException.getErrorCode()); |
| 3188 | + |
| 3189 | + // Wait for the retry CreateSession request to reach the server so multiplexedSessionReference |
| 3190 | + // has been updated to the new session future. |
| 3191 | + mockSpanner.waitForRequestsToContain( |
| 3192 | + message -> message instanceof CreateSessionRequest, 10_000L); |
| 3193 | + try (ResultSet rs = client.singleUse().executeQuery(SELECT1)) { |
| 3194 | + assertTrue(rs.next()); |
| 3195 | + } |
| 3196 | + |
| 3197 | + assertEquals(Dialect.POSTGRESQL, client.getDialect()); |
| 3198 | + assertEquals(Dialect.POSTGRESQL, client.getDialect()); |
| 3199 | + assertEquals( |
| 3200 | + Statement.newBuilder("select id from test where b=$1").bind("p1").to(true).build(), |
| 3201 | + client |
| 3202 | + .getStatementFactory() |
| 3203 | + .withUnnamedParameters("select id from test where b=?", true)); |
| 3204 | + } finally { |
| 3205 | + mockSpanner.unfreeze(); |
| 3206 | + mockSpanner.putStatementResult( |
| 3207 | + StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL)); |
| 3208 | + } |
| 3209 | + } |
| 3210 | + |
| 3211 | + @Test |
| 3212 | + public void testGetDialectAndStatementFactory_RecoverAfterTransientQueryFailure() { |
| 3213 | + mockSpanner.putStatementResult( |
| 3214 | + StatementResult.exception( |
| 3215 | + MultiplexedSessionDatabaseClient.DETERMINE_DIALECT_STATEMENT, |
| 3216 | + Status.DEADLINE_EXCEEDED |
| 3217 | + .withDescription("Transient dialect query timeout") |
| 3218 | + .asRuntimeException())); |
| 3219 | + try { |
| 3220 | + DatabaseClient client = |
| 3221 | + spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); |
| 3222 | + |
| 3223 | + SpannerException dialectException = assertThrows(SpannerException.class, client::getDialect); |
| 3224 | + assertEquals(ErrorCode.DEADLINE_EXCEEDED, dialectException.getErrorCode()); |
| 3225 | + |
| 3226 | + SpannerException factoryException = |
| 3227 | + assertThrows(SpannerException.class, client::getStatementFactory); |
| 3228 | + assertEquals(ErrorCode.DEADLINE_EXCEEDED, factoryException.getErrorCode()); |
| 3229 | + |
| 3230 | + mockSpanner.putStatementResult(StatementResult.detectDialectResult(Dialect.POSTGRESQL)); |
| 3231 | + |
| 3232 | + assertEquals(Dialect.POSTGRESQL, client.getDialect()); |
| 3233 | + assertEquals( |
| 3234 | + Statement.newBuilder("select id from test where b=$1").bind("p1").to(true).build(), |
| 3235 | + client |
| 3236 | + .getStatementFactory() |
| 3237 | + .withUnnamedParameters("select id from test where b=?", true)); |
| 3238 | + } finally { |
| 3239 | + mockSpanner.putStatementResult( |
| 3240 | + StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL)); |
| 3241 | + } |
| 3242 | + } |
| 3243 | + |
| 3244 | + @Test |
| 3245 | + public void testGetDialect_CachesNonRetryableQueryFailure() { |
| 3246 | + mockSpanner.putStatementResult( |
| 3247 | + StatementResult.exception( |
| 3248 | + MultiplexedSessionDatabaseClient.DETERMINE_DIALECT_STATEMENT, |
| 3249 | + Status.PERMISSION_DENIED |
| 3250 | + .withDescription("Permission denied on information_schema") |
| 3251 | + .asRuntimeException())); |
| 3252 | + try { |
| 3253 | + DatabaseClient client = |
| 3254 | + spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, "non-retryable-db")); |
| 3255 | + |
| 3256 | + SpannerException e1 = assertThrows(SpannerException.class, client::getDialect); |
| 3257 | + assertEquals(ErrorCode.PERMISSION_DENIED, e1.getErrorCode()); |
| 3258 | + SpannerException e2 = assertThrows(SpannerException.class, client::getDialect); |
| 3259 | + assertSame(e1, e2); |
| 3260 | + } finally { |
| 3261 | + mockSpanner.putStatementResult( |
| 3262 | + StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL)); |
| 3263 | + } |
| 3264 | + } |
| 3265 | + |
3148 | 3266 | @Test |
3149 | 3267 | public void testUntypedNullParameters() { |
3150 | 3268 | Statement statement = |
|
0 commit comments