|
54 | 54 | import java.time.Duration; |
55 | 55 | import java.util.*; |
56 | 56 | import java.util.concurrent.CountDownLatch; |
| 57 | +import java.util.concurrent.ExecutorService; |
| 58 | +import java.util.concurrent.Executors; |
57 | 59 | import java.util.concurrent.TimeUnit; |
58 | 60 | import java.util.concurrent.atomic.AtomicInteger; |
59 | 61 | import java.util.concurrent.atomic.AtomicReference; |
@@ -245,6 +247,143 @@ public void testUnimplementedErrorOnCreation_fallsBackToRegularSessions() { |
245 | 247 | assertEquals(0L, client.multiplexedSessionDatabaseClient.getNumSessionsReleased().get()); |
246 | 248 | } |
247 | 249 |
|
| 250 | + @Test |
| 251 | + public void testDeadlineExceededErrorWithOneRetry() { |
| 252 | + // Setting up two exceptions |
| 253 | + mockSpanner.setCreateSessionExecutionTime( |
| 254 | + SimulatedExecutionTime.ofExceptions( |
| 255 | + Arrays.asList( |
| 256 | + Status.DEADLINE_EXCEEDED |
| 257 | + .withDescription( |
| 258 | + "CallOptions deadline exceeded after 22.986872393s. " |
| 259 | + + "Name resolution delay 6.911918521 seconds. [closed=[], " |
| 260 | + + "open=[[connecting_and_lb_delay=32445014148ns, was_still_waiting]]]") |
| 261 | + .asRuntimeException(), |
| 262 | + Status.DEADLINE_EXCEEDED |
| 263 | + .withDescription( |
| 264 | + "CallOptions deadline exceeded after 22.986872393s. " |
| 265 | + + "Name resolution delay 6.911918521 seconds. [closed=[], " |
| 266 | + + "open=[[connecting_and_lb_delay=32445014148ns, was_still_waiting]]]") |
| 267 | + .asRuntimeException()))); |
| 268 | + DatabaseClientImpl client = |
| 269 | + (DatabaseClientImpl) spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 270 | + assertNotNull(client.multiplexedSessionDatabaseClient); |
| 271 | + |
| 272 | + // initial fetch call fails with exception |
| 273 | + // this call will try to fetch it again which again throws an exception |
| 274 | + assertThrows( |
| 275 | + SpannerException.class, |
| 276 | + () -> { |
| 277 | + try (ResultSet resultSet = client.singleUse().executeQuery(STATEMENT)) { |
| 278 | + //noinspection StatementWithEmptyBody |
| 279 | + while (resultSet.next()) { |
| 280 | + // ignore |
| 281 | + } |
| 282 | + } |
| 283 | + }); |
| 284 | + |
| 285 | + // When third request comes it should succeed |
| 286 | + try (ResultSet resultSet = client.singleUse().executeQuery(STATEMENT)) { |
| 287 | + //noinspection StatementWithEmptyBody |
| 288 | + while (resultSet.next()) { |
| 289 | + // ignore |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + // Verify that we received one ExecuteSqlRequest, and that it used a multiplexed session. |
| 294 | + assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); |
| 295 | + List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class); |
| 296 | + |
| 297 | + Session session = mockSpanner.getSession(requests.get(0).getSession()); |
| 298 | + assertNotNull(session); |
| 299 | + assertTrue(session.getMultiplexed()); |
| 300 | + |
| 301 | + assertNotNull(client.multiplexedSessionDatabaseClient); |
| 302 | + assertEquals(1L, client.multiplexedSessionDatabaseClient.getNumSessionsAcquired().get()); |
| 303 | + assertEquals(1L, client.multiplexedSessionDatabaseClient.getNumSessionsReleased().get()); |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void testDeadlineExceededErrorWithOneRetryWithParallelRequests() |
| 308 | + throws InterruptedException { |
| 309 | + mockSpanner.setCreateSessionExecutionTime( |
| 310 | + SimulatedExecutionTime.ofMinimumAndRandomTimeAndExceptions( |
| 311 | + 2000, |
| 312 | + 0, |
| 313 | + Arrays.asList( |
| 314 | + Status.DEADLINE_EXCEEDED |
| 315 | + .withDescription( |
| 316 | + "CallOptions deadline exceeded after 22.986872393s. " |
| 317 | + + "Name resolution delay 6.911918521 seconds. [closed=[], " |
| 318 | + + "open=[[connecting_and_lb_delay=32445014148ns, was_still_waiting]]]") |
| 319 | + .asRuntimeException(), |
| 320 | + Status.DEADLINE_EXCEEDED |
| 321 | + .withDescription( |
| 322 | + "CallOptions deadline exceeded after 22.986872393s. " |
| 323 | + + "Name resolution delay 6.911918521 seconds. [closed=[], " |
| 324 | + + "open=[[connecting_and_lb_delay=32445014148ns, was_still_waiting]]]") |
| 325 | + .asRuntimeException()))); |
| 326 | + DatabaseClientImpl client = |
| 327 | + (DatabaseClientImpl) spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 328 | + assertNotNull(client.multiplexedSessionDatabaseClient); |
| 329 | + |
| 330 | + ExecutorService executor = Executors.newCachedThreadPool(); |
| 331 | + |
| 332 | + // First set of request should fail with an error |
| 333 | + CountDownLatch failureCountDownLatch = new CountDownLatch(3); |
| 334 | + for (int i = 0; i < 3; i++) { |
| 335 | + executor.submit( |
| 336 | + () -> { |
| 337 | + try { |
| 338 | + try (ResultSet resultSet = client.singleUse().executeQuery(STATEMENT)) { |
| 339 | + //noinspection StatementWithEmptyBody |
| 340 | + while (resultSet.next()) { |
| 341 | + // ignore |
| 342 | + } |
| 343 | + } |
| 344 | + } catch (SpannerException e) { |
| 345 | + failureCountDownLatch.countDown(); |
| 346 | + } |
| 347 | + }); |
| 348 | + } |
| 349 | + |
| 350 | + assertTrue(failureCountDownLatch.await(2, TimeUnit.SECONDS)); |
| 351 | + assertEquals(0, failureCountDownLatch.getCount()); |
| 352 | + |
| 353 | + // Second set of requests should pass |
| 354 | + CountDownLatch countDownLatch = new CountDownLatch(3); |
| 355 | + for (int i = 0; i < 3; i++) { |
| 356 | + executor.submit( |
| 357 | + () -> { |
| 358 | + try { |
| 359 | + try (ResultSet resultSet = client.singleUse().executeQuery(STATEMENT)) { |
| 360 | + //noinspection StatementWithEmptyBody |
| 361 | + while (resultSet.next()) { |
| 362 | + // ignore |
| 363 | + } |
| 364 | + } |
| 365 | + } catch (SpannerException e) { |
| 366 | + countDownLatch.countDown(); |
| 367 | + } |
| 368 | + }); |
| 369 | + } |
| 370 | + |
| 371 | + assertFalse(countDownLatch.await(3, TimeUnit.SECONDS)); |
| 372 | + assertEquals(3, countDownLatch.getCount()); |
| 373 | + |
| 374 | + // Verify that we received 3 ExecuteSqlRequest, and that it used a multiplexed session. |
| 375 | + assertEquals(3, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); |
| 376 | + List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class); |
| 377 | + |
| 378 | + Session session = mockSpanner.getSession(requests.get(0).getSession()); |
| 379 | + assertNotNull(session); |
| 380 | + assertTrue(session.getMultiplexed()); |
| 381 | + |
| 382 | + assertNotNull(client.multiplexedSessionDatabaseClient); |
| 383 | + assertEquals(3L, client.multiplexedSessionDatabaseClient.getNumSessionsAcquired().get()); |
| 384 | + assertEquals(3L, client.multiplexedSessionDatabaseClient.getNumSessionsReleased().get()); |
| 385 | + } |
| 386 | + |
248 | 387 | @Test |
249 | 388 | public void |
250 | 389 | testUnimplementedErrorOnCreation_firstReceivesError_secondFallsBackToRegularSessions() { |
|
0 commit comments