|
30 | 30 | import org.junit.Before;
|
31 | 31 |
|
32 | 32 | import java.util.ArrayList;
|
| 33 | +import java.util.Collections; |
33 | 34 | import java.util.List;
|
34 | 35 | import java.util.Locale;
|
35 | 36 | import java.util.concurrent.CountDownLatch;
|
|
38 | 39 | import java.util.concurrent.Future;
|
39 | 40 | import java.util.concurrent.ScheduledExecutorService;
|
40 | 41 | import java.util.concurrent.TimeUnit;
|
| 42 | +import java.util.concurrent.atomic.AtomicBoolean; |
41 | 43 | import java.util.concurrent.atomic.AtomicInteger;
|
42 | 44 | import java.util.concurrent.atomic.AtomicReference;
|
43 | 45 | import java.util.function.BiConsumer;
|
@@ -408,6 +410,128 @@ public void testRejections() throws Exception {
|
408 | 410 | consumerExecutor.shutdown();
|
409 | 411 | }
|
410 | 412 |
|
| 413 | + public void testAddWithNoBackpressureThrowsException() throws InterruptedException { |
| 414 | + /* |
| 415 | + * This tests that if we reduce the configured batch size and max bytes in flight then we can force add() to throw |
| 416 | + * EsRejectedExecutionExceptions. |
| 417 | + */ |
| 418 | + BulkResponse bulkResponse = new BulkResponse( |
| 419 | + new BulkItemResponse[] { BulkItemResponse.success(0, randomFrom(DocWriteRequest.OpType.values()), mockResponse()) }, |
| 420 | + 0 |
| 421 | + ); |
| 422 | + final BiConsumer<BulkRequest, ActionListener<BulkResponse>> consumer = (request, listener) -> { |
| 423 | + threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { |
| 424 | + try { |
| 425 | + Thread.sleep(50); |
| 426 | + } catch (InterruptedException e) { |
| 427 | + throw new RuntimeException(e); |
| 428 | + } |
| 429 | + listener.onResponse(bulkResponse); |
| 430 | + }); |
| 431 | + }; |
| 432 | + int numberOfRequests = 100; |
| 433 | + final AtomicBoolean haveSeenRejections = new AtomicBoolean(false); |
| 434 | + final BulkProcessor2.Listener listener = new BulkProcessor2.Listener() { |
| 435 | + |
| 436 | + @Override |
| 437 | + public void beforeBulk(long executionId, BulkRequest request) {} |
| 438 | + |
| 439 | + @Override |
| 440 | + public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {} |
| 441 | + |
| 442 | + @Override |
| 443 | + public void afterBulk(long executionId, BulkRequest request, Exception failure) { |
| 444 | + fail("afterBulk should not return exception"); |
| 445 | + } |
| 446 | + }; |
| 447 | + BulkProcessor2 bulkProcessor = BulkProcessor2.builder(consumer, listener, threadPool) |
| 448 | + .setBulkSize(ByteSizeValue.ofBytes(512)) |
| 449 | + .setMaxBytesInFlight(ByteSizeValue.ofBytes(1024)) |
| 450 | + .setFlushInterval(TimeValue.timeValueMillis(1)) |
| 451 | + .build(); |
| 452 | + try { |
| 453 | + for (int i = 0; i < numberOfRequests; i++) { |
| 454 | + bulkProcessor.add(new IndexRequest().source(Collections.singletonMap("this_is_a_key" + i, "this_is_a_value" + i))); |
| 455 | + } |
| 456 | + } catch (EsRejectedExecutionException e) { |
| 457 | + // We are throwing more data at the processor than the max bytes in flight setting can handle |
| 458 | + haveSeenRejections.set(true); |
| 459 | + } finally { |
| 460 | + bulkProcessor.awaitClose(1, TimeUnit.SECONDS); |
| 461 | + } |
| 462 | + assertThat(haveSeenRejections.get(), equalTo(true)); |
| 463 | + } |
| 464 | + |
| 465 | + public void testAddWithBackpressure() throws InterruptedException { |
| 466 | + /* |
| 467 | + * This test reduces the configured batch size and max bytes in flight so that using add() would throw |
| 468 | + * EsRejectedExecutionExceptions (see testAddWithNoBackpressureThrowsException). But instead this test uses addWithBackpressure |
| 469 | + * so that it blocks the calling thread until requests can be added. |
| 470 | + */ |
| 471 | + BulkResponse bulkResponse = new BulkResponse( |
| 472 | + new BulkItemResponse[] { BulkItemResponse.success(0, randomFrom(DocWriteRequest.OpType.values()), mockResponse()) }, |
| 473 | + 0 |
| 474 | + ); |
| 475 | + final BiConsumer<BulkRequest, ActionListener<BulkResponse>> consumer = (request, listener) -> { |
| 476 | + threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { |
| 477 | + try { |
| 478 | + Thread.sleep(50); |
| 479 | + } catch (InterruptedException e) { |
| 480 | + throw new RuntimeException(e); |
| 481 | + } |
| 482 | + listener.onResponse(bulkResponse); |
| 483 | + }); |
| 484 | + }; |
| 485 | + int numberOfRequests = 100; |
| 486 | + final CountDownLatch countDownLatch = new CountDownLatch(numberOfRequests); |
| 487 | + final BulkProcessor2.Listener listener = new BulkProcessor2.Listener() { |
| 488 | + |
| 489 | + @Override |
| 490 | + public void beforeBulk(long executionId, BulkRequest request) {} |
| 491 | + |
| 492 | + @Override |
| 493 | + public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { |
| 494 | + for (int i = 0; i < request.requests.size(); i++) { |
| 495 | + countDownLatch.countDown(); |
| 496 | + } |
| 497 | + } |
| 498 | + |
| 499 | + @Override |
| 500 | + public void afterBulk(long executionId, BulkRequest request, Exception failure) { |
| 501 | + fail("afterBulk should not return exception"); |
| 502 | + } |
| 503 | + }; |
| 504 | + BulkProcessor2 bulkProcessor = BulkProcessor2.builder(consumer, listener, threadPool) |
| 505 | + .setBulkSize(ByteSizeValue.ofBytes(512)) |
| 506 | + .setMaxBytesInFlight(ByteSizeValue.ofBytes(1024)) |
| 507 | + .setFlushInterval(TimeValue.timeValueMillis(1)) |
| 508 | + .build(); |
| 509 | + AtomicBoolean abort = new AtomicBoolean(false); |
| 510 | + final AtomicBoolean haveSeenRejections = new AtomicBoolean(false); |
| 511 | + try { |
| 512 | + for (int i = 0; i < numberOfRequests; i++) { |
| 513 | + bulkProcessor.addWithBackpressure( |
| 514 | + new IndexRequest().source(Collections.singletonMap("this_is_a_key" + i, "this_is_a_value" + i)), |
| 515 | + abort::get |
| 516 | + ); |
| 517 | + } |
| 518 | + assertTrue(countDownLatch.await(5, TimeUnit.MINUTES)); |
| 519 | + assertThat(bulkProcessor.getTotalBytesInFlight(), equalTo(0L)); |
| 520 | + abort.set(true); |
| 521 | + try { |
| 522 | + bulkProcessor.addWithBackpressure( |
| 523 | + new IndexRequest().source(Collections.singletonMap("this_is_a_key", "this_is_a_value")), |
| 524 | + abort::get |
| 525 | + ); |
| 526 | + } catch (EsRejectedExecutionException e) { |
| 527 | + haveSeenRejections.set(true); |
| 528 | + } |
| 529 | + } finally { |
| 530 | + bulkProcessor.awaitClose(1, TimeUnit.SECONDS); |
| 531 | + } |
| 532 | + assertThat(haveSeenRejections.get(), equalTo(true)); |
| 533 | + } |
| 534 | + |
411 | 535 | private BulkProcessor2.Listener emptyListener() {
|
412 | 536 | return new BulkProcessor2.Listener() {
|
413 | 537 | @Override
|
|
0 commit comments