@@ -386,11 +386,19 @@ public void testReadFailureWaitsForTimerButNewReadDemandCanResumeImmediately() t |
386 | 386 | } |
387 | 387 | } |
388 | 388 |
|
389 | | - @Test |
390 | | - public void testReadFailureTerminatesWhenRetrySchedulingIsRejectedWhileExecutorIsRunning() throws Exception { |
| 389 | + @DataProvider |
| 390 | + public Object[][] retrySchedulingFailures() { |
| 391 | + return new Object[][] { |
| 392 | + {new RejectedExecutionException("retry rejected")}, |
| 393 | + {new IllegalStateException("retry scheduling failed")} |
| 394 | + }; |
| 395 | + } |
| 396 | + |
| 397 | + @Test(dataProvider = "retrySchedulingFailures") |
| 398 | + public void testReadFailureTerminatesWhenRetrySchedulingFails(RuntimeException failure) throws Exception { |
391 | 399 | TestReplicatorFixture fixture = newTestReplicatorFixture(); |
392 | 400 | TestPersistentReplicator replicator = fixture.replicator; |
393 | | - fixture.rejectScheduledWork.set(true); |
| 401 | + doThrow(failure).when(fixture.executor).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)); |
394 | 402 | when(fixture.executor.isShuttingDown()).thenReturn(false); |
395 | 403 | List<ReadRequest> requests = new ArrayList<>(); |
396 | 404 | doAnswer(invocation -> { |
@@ -417,6 +425,52 @@ public void testReadFailureTerminatesWhenRetrySchedulingIsRejectedWhileExecutorI |
417 | 425 | verify(fixture.cursor).setInactive(); |
418 | 426 | } |
419 | 427 |
|
| 428 | + @Test |
| 429 | + public void testProducerRestartFailureKeepsScheduledRetry() throws Exception { |
| 430 | + TestReplicatorFixture fixture = newTestReplicatorFixture(); |
| 431 | + fixture.replicator.markDisconnected(); |
| 432 | + fixture.replicator.producerStartFailure = new IllegalStateException("producer startup failed"); |
| 433 | + |
| 434 | + fixture.replicator.readMoreEntries(); |
| 435 | + assertThat(fixture.replicator.getState()).isEqualTo(State.Disconnected); |
| 436 | + fixture.takeScheduledWork().command.run(); |
| 437 | + assertThat(fixture.replicator.getState()).isEqualTo(State.Disconnected); |
| 438 | + assertThat(fixture.scheduledWork).hasSize(1); |
| 439 | + verify(fixture.cursor, never()).asyncReadEntriesOrWait(anyInt(), anyLong(), any(), any(), any()); |
| 440 | + } |
| 441 | + |
| 442 | + @Test |
| 443 | + public void testRejectedReadCompletionRewindsBeforeRetriedRead() throws Exception { |
| 444 | + TestReplicatorFixture fixture = newTestReplicatorFixture(); |
| 445 | + List<ReadRequest> requests = new ArrayList<>(); |
| 446 | + doAnswer(invocation -> { |
| 447 | + requests.add(new ReadRequest(invocation.getArgument(2), invocation.getArgument(3))); |
| 448 | + return null; |
| 449 | + }).when(fixture.cursor).asyncReadEntriesOrWait(anyInt(), anyLong(), any(), any(), any()); |
| 450 | + Position start = PositionFactory.create(1, 1); |
| 451 | + Position advanced = PositionFactory.create(1, 3); |
| 452 | + when(fixture.cursor.getReadPosition()).thenReturn(start); |
| 453 | + fixture.replicator.readMoreEntries(); |
| 454 | + when(fixture.cursor.getReadPosition()).thenReturn(advanced); |
| 455 | + doAnswer(invocation -> { |
| 456 | + when(fixture.cursor.getReadPosition()).thenReturn(start); |
| 457 | + return null; |
| 458 | + }).when(fixture.cursor).rewind(); |
| 459 | + |
| 460 | + ReadRequest read = requests.get(0); |
| 461 | + read.callback.readEntriesFailed(new ManagedLedgerException(new RejectedExecutionException("handoff rejected")), |
| 462 | + read.context); |
| 463 | + assertThat(requests).hasSize(1); |
| 464 | + fixture.takeScheduledWork().command.run(); |
| 465 | + |
| 466 | + assertThat(requests).hasSize(2); |
| 467 | + assertThat(((InFlightTask) requests.get(1).context).getReadPos()).isEqualTo(start); |
| 468 | + InOrder cursorCalls = inOrder(fixture.cursor); |
| 469 | + cursorCalls.verify(fixture.cursor).asyncReadEntriesOrWait(anyInt(), anyLong(), any(), any(), any()); |
| 470 | + cursorCalls.verify(fixture.cursor).rewind(); |
| 471 | + cursorCalls.verify(fixture.cursor).asyncReadEntriesOrWait(anyInt(), anyLong(), any(), any(), any()); |
| 472 | + } |
| 473 | + |
420 | 474 | @Test |
421 | 475 | public void testProducerAckDuringReservedReadRetriesFailedReadBeforeTimerWithoutOverlappingReads() |
422 | 476 | throws Exception { |
@@ -725,15 +779,11 @@ private static TestReplicatorFixture newTestReplicatorFixture(ServiceConfigurati |
725 | 779 | EventLoopGroup executor = mock(EventLoopGroup.class); |
726 | 780 | Queue<Runnable> queuedWork = new ConcurrentLinkedQueue<>(); |
727 | 781 | Queue<ScheduledWork> scheduledWork = new ConcurrentLinkedQueue<>(); |
728 | | - AtomicBoolean rejectScheduledWork = new AtomicBoolean(); |
729 | 782 | doAnswer(invocation -> { |
730 | 783 | queuedWork.add(invocation.getArgument(0)); |
731 | 784 | return null; |
732 | 785 | }).when(executor).execute(any(Runnable.class)); |
733 | 786 | doAnswer(invocation -> { |
734 | | - if (rejectScheduledWork.get()) { |
735 | | - throw new RejectedExecutionException("test retry scheduler rejection"); |
736 | | - } |
737 | 787 | scheduledWork.add(new ScheduledWork(invocation.getArgument(0), invocation.getArgument(1), |
738 | 788 | invocation.getArgument(2))); |
739 | 789 | return null; |
@@ -765,7 +815,7 @@ private static TestReplicatorFixture newTestReplicatorFixture(ServiceConfigurati |
765 | 815 |
|
766 | 816 | TestPersistentReplicator replicator = new TestPersistentReplicator(topic, cursor, brokerService, |
767 | 817 | replicationClient, mock(PulsarAdmin.class)); |
768 | | - return new TestReplicatorFixture(replicator, cursor, executor, queuedWork, scheduledWork, rejectScheduledWork); |
| 818 | + return new TestReplicatorFixture(replicator, cursor, executor, queuedWork, scheduledWork); |
769 | 819 | } |
770 | 820 |
|
771 | 821 | private record ReadRequest(ReadEntriesCallback callback, Object context) { |
@@ -780,18 +830,15 @@ private static final class TestReplicatorFixture { |
780 | 830 | private final EventLoopGroup executor; |
781 | 831 | private final Queue<Runnable> queuedWork; |
782 | 832 | private final Queue<ScheduledWork> scheduledWork; |
783 | | - private final AtomicBoolean rejectScheduledWork; |
784 | 833 |
|
785 | 834 | private TestReplicatorFixture(TestPersistentReplicator replicator, ManagedCursor cursor, |
786 | 835 | EventLoopGroup executor, |
787 | | - Queue<Runnable> queuedWork, Queue<ScheduledWork> scheduledWork, |
788 | | - AtomicBoolean rejectScheduledWork) { |
| 836 | + Queue<Runnable> queuedWork, Queue<ScheduledWork> scheduledWork) { |
789 | 837 | this.replicator = replicator; |
790 | 838 | this.cursor = cursor; |
791 | 839 | this.executor = executor; |
792 | 840 | this.queuedWork = queuedWork; |
793 | 841 | this.scheduledWork = scheduledWork; |
794 | | - this.rejectScheduledWork = rejectScheduledWork; |
795 | 842 | } |
796 | 843 |
|
797 | 844 | private void runQueuedWork() { |
@@ -821,6 +868,7 @@ private static final class TestPersistentReplicator extends PersistentReplicator |
821 | 868 | private final AtomicBoolean rewindBeforeCursorInvocation = new AtomicBoolean(); |
822 | 869 | private final AtomicBoolean cancelBeforeCursorInvocation = new AtomicBoolean(); |
823 | 870 | private EntryObserver entryObserver; |
| 871 | + private RuntimeException producerStartFailure; |
824 | 872 |
|
825 | 873 | private TestPersistentReplicator(PersistentTopic topic, ManagedCursor cursor, BrokerService brokerService, |
826 | 874 | PulsarClientImpl replicationClient, PulsarAdmin replicationAdmin) |
@@ -833,6 +881,9 @@ private TestPersistentReplicator(PersistentTopic topic, ManagedCursor cursor, Br |
833 | 881 | @Override |
834 | 882 | protected void startProducer() { |
835 | 883 | // The test drives read scheduling directly. |
| 884 | + if (producerStartFailure != null) { |
| 885 | + throw producerStartFailure; |
| 886 | + } |
836 | 887 | } |
837 | 888 |
|
838 | 889 | @Override |
@@ -861,6 +912,10 @@ private void markTerminated() { |
861 | 912 | state = State.Terminated; |
862 | 913 | } |
863 | 914 |
|
| 915 | + private void markDisconnected() { |
| 916 | + state = State.Disconnected; |
| 917 | + } |
| 918 | + |
864 | 919 | private void setProducerForTest(ProducerImpl<?> producer) { |
865 | 920 | this.producer = producer; |
866 | 921 | } |
|