|
65 | 65 | import java.util.Collections; |
66 | 66 | import java.util.HashSet; |
67 | 67 | import java.util.Locale; |
| 68 | +import java.util.concurrent.BrokenBarrierException; |
| 69 | +import java.util.concurrent.CountDownLatch; |
| 70 | +import java.util.concurrent.CyclicBarrier; |
68 | 71 | import java.util.concurrent.ExecutionException; |
69 | 72 | import java.util.concurrent.TimeUnit; |
70 | 73 | import java.util.concurrent.atomic.AtomicBoolean; |
@@ -346,6 +349,51 @@ public void testReplicaProxy() throws InterruptedException, ExecutionException { |
346 | 349 | } |
347 | 350 | } |
348 | 351 |
|
| 352 | + public void testConcurrentWriteReplicaResultCompletion() throws InterruptedException { |
| 353 | + IndexShard replica = mock(IndexShard.class); |
| 354 | + when(replica.getTranslogDurability()).thenReturn(Translog.Durability.ASYNC); |
| 355 | + TestRequest request = new TestRequest(); |
| 356 | + request.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); |
| 357 | + TransportWriteAction.WriteReplicaResult<TestRequest> replicaResult = new TransportWriteAction.WriteReplicaResult<>( |
| 358 | + request, new Translog.Location(0, 0, 0), null, replica, logger); |
| 359 | + CyclicBarrier barrier = new CyclicBarrier(2); |
| 360 | + Runnable waitForBarrier = () -> { |
| 361 | + try { |
| 362 | + barrier.await(); |
| 363 | + } catch (InterruptedException | BrokenBarrierException e) { |
| 364 | + throw new AssertionError(e); |
| 365 | + } |
| 366 | + }; |
| 367 | + CountDownLatch completionLatch = new CountDownLatch(1); |
| 368 | + threadPool.generic().execute(() -> { |
| 369 | + waitForBarrier.run(); |
| 370 | + replicaResult.respond(new ActionListener<TransportResponse.Empty>() { |
| 371 | + @Override |
| 372 | + public void onResponse(TransportResponse.Empty empty) { |
| 373 | + completionLatch.countDown(); |
| 374 | + } |
| 375 | + |
| 376 | + @Override |
| 377 | + public void onFailure(Exception e) { |
| 378 | + completionLatch.countDown(); |
| 379 | + } |
| 380 | + }); |
| 381 | + }); |
| 382 | + if (randomBoolean()) { |
| 383 | + threadPool.generic().execute(() -> { |
| 384 | + waitForBarrier.run(); |
| 385 | + replicaResult.onFailure(null); |
| 386 | + }); |
| 387 | + } else { |
| 388 | + threadPool.generic().execute(() -> { |
| 389 | + waitForBarrier.run(); |
| 390 | + replicaResult.onSuccess(false); |
| 391 | + }); |
| 392 | + } |
| 393 | + |
| 394 | + assertTrue(completionLatch.await(30, TimeUnit.SECONDS)); |
| 395 | + } |
| 396 | + |
349 | 397 | private class TestAction extends TransportWriteAction<TestRequest, TestRequest, TestResponse> { |
350 | 398 |
|
351 | 399 | private final boolean withDocumentFailureOnPrimary; |
|
0 commit comments