-
Notifications
You must be signed in to change notification settings - Fork 1.7k
GH-2581: Fix OOO Commits with Rebalance #2586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,7 +66,9 @@ | |
| import org.springframework.kafka.event.ConsumerStartedEvent; | ||
| import org.springframework.kafka.event.ConsumerStartingEvent; | ||
| import org.springframework.kafka.event.ListenerContainerIdleEvent; | ||
| import org.springframework.kafka.listener.ContainerProperties.AckMode; | ||
| import org.springframework.kafka.listener.ContainerProperties.AssignmentCommitOption; | ||
| import org.springframework.kafka.support.Acknowledgment; | ||
| import org.springframework.kafka.test.utils.KafkaTestUtils; | ||
| import org.springframework.kafka.transaction.KafkaAwareTransactionManager; | ||
| import org.springframework.lang.Nullable; | ||
|
|
@@ -904,7 +906,7 @@ void removeFromPartitionPauseRequestedWhenNotAssigned() throws InterruptedExcept | |
|
|
||
| @SuppressWarnings({ "unchecked", "rawtypes" }) | ||
| @Test | ||
| void pruneRevokedPartitionsFromRemainingRecordsWhenSeekAfterErrorFalseLagacyAssignor() throws InterruptedException { | ||
| void pruneRevokedPartitionsFromRemainingRecordsWhenSeekAfterErrorFalseLegacyAssignor() throws InterruptedException { | ||
| TopicPartition tp0 = new TopicPartition("foo", 0); | ||
| TopicPartition tp1 = new TopicPartition("foo", 1); | ||
| TopicPartition tp2 = new TopicPartition("foo", 2); | ||
|
|
@@ -1116,6 +1118,178 @@ public boolean handleOne(Exception thrownException, ConsumerRecord<?, ?> record, | |
| assertThat(recordsDelivered.get(2)).isEqualTo(record1); | ||
| } | ||
|
|
||
| @SuppressWarnings({ "unchecked", "rawtypes" }) | ||
| @Test | ||
| void pruneRevokedPartitionsFromPendingOutOfOrderCommitsLegacyAssignor() throws InterruptedException { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gary, I don't see any
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's simulated. With a legacy assignor, all current assignments are revoked and all (or a subset) are re-assigned. With a coop assignor, only a subset are revoked and (possibly) previously unassigned partitions might be assigned. See the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's cool. Thank you! |
||
| TopicPartition tp0 = new TopicPartition("foo", 0); | ||
| TopicPartition tp1 = new TopicPartition("foo", 1); | ||
| List<TopicPartition> allAssignments = Arrays.asList(tp0, tp1); | ||
| Map<TopicPartition, List<ConsumerRecord<String, String>>> allRecordMap = new HashMap<>(); | ||
| allRecordMap.put(tp0, | ||
| List.of(new ConsumerRecord("foo", 0, 0, null, "bar"), new ConsumerRecord("foo", 0, 1, null, "bar"))); | ||
| allRecordMap.put(tp1, | ||
| List.of(new ConsumerRecord("foo", 1, 0, null, "bar"), new ConsumerRecord("foo", 1, 1, null, "bar"))); | ||
| ConsumerRecords allRecords = new ConsumerRecords<>(allRecordMap); | ||
| List<TopicPartition> afterRevokeAssignments = Arrays.asList(tp1); | ||
| AtomicInteger pollPhase = new AtomicInteger(); | ||
|
|
||
| Consumer consumer = mock(Consumer.class); | ||
| AtomicReference<ConsumerRebalanceListener> rebal = new AtomicReference<>(); | ||
| CountDownLatch subscribeLatch = new CountDownLatch(1); | ||
| willAnswer(invocation -> { | ||
| rebal.set(invocation.getArgument(1)); | ||
| subscribeLatch.countDown(); | ||
| return null; | ||
| }).given(consumer).subscribe(any(Collection.class), any()); | ||
| CountDownLatch pauseLatch = new CountDownLatch(1); | ||
| AtomicBoolean paused = new AtomicBoolean(); | ||
| willAnswer(inv -> { | ||
| paused.set(true); | ||
| pauseLatch.countDown(); | ||
| return null; | ||
| }).given(consumer).pause(any()); | ||
| ConsumerFactory cf = mock(ConsumerFactory.class); | ||
| given(cf.createConsumer(any(), any(), any(), any())).willReturn(consumer); | ||
| given(cf.getConfigurationProperties()) | ||
| .willReturn(Collections.singletonMap(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")); | ||
| ContainerProperties containerProperties = new ContainerProperties("foo"); | ||
| containerProperties.setGroupId("grp"); | ||
| containerProperties.setAckMode(AckMode.MANUAL); | ||
| containerProperties.setMessageListener(ackOffset1()); | ||
| containerProperties.setAsyncAcks(true); | ||
| ConcurrentMessageListenerContainer container = new ConcurrentMessageListenerContainer(cf, | ||
| containerProperties); | ||
| CountDownLatch pollLatch = new CountDownLatch(2); | ||
| CountDownLatch rebalLatch = new CountDownLatch(1); | ||
| CountDownLatch continueLatch = new CountDownLatch(1); | ||
| willAnswer(inv -> { | ||
| Thread.sleep(50); | ||
| pollLatch.countDown(); | ||
| switch (pollPhase.getAndIncrement()) { | ||
| case 0: | ||
| rebal.get().onPartitionsAssigned(allAssignments); | ||
| return allRecords; | ||
| case 1: | ||
| rebal.get().onPartitionsRevoked(allAssignments); | ||
| rebal.get().onPartitionsAssigned(afterRevokeAssignments); | ||
| rebalLatch.countDown(); | ||
| continueLatch.await(10, TimeUnit.SECONDS); | ||
| default: | ||
| return ConsumerRecords.empty(); | ||
| } | ||
| }).given(consumer).poll(any()); | ||
| container.start(); | ||
| assertThat(subscribeLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| KafkaMessageListenerContainer child = (KafkaMessageListenerContainer) KafkaTestUtils | ||
| .getPropertyValue(container, "containers", List.class).get(0); | ||
| assertThat(pollLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| assertThat(pauseLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| assertThat(rebalLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| Map offsets = KafkaTestUtils.getPropertyValue(child, "listenerConsumer.offsetsInThisBatch", Map.class); | ||
| assertThat(offsets).hasSize(0); | ||
| assertThat(KafkaTestUtils.getPropertyValue(child, "listenerConsumer.consumerPaused", Boolean.class)).isFalse(); | ||
| continueLatch.countDown(); | ||
| // no pause when re-assigned because all revoked | ||
| verify(consumer).pause(any()); | ||
| verify(consumer, never()).resume(any()); | ||
| container.stop(); | ||
| } | ||
|
|
||
| @SuppressWarnings({ "unchecked", "rawtypes" }) | ||
| @Test | ||
| void pruneRevokedPartitionsFromPendingOutOfOrderCommitsCoopAssignor() throws InterruptedException { | ||
| TopicPartition tp0 = new TopicPartition("foo", 0); | ||
| TopicPartition tp1 = new TopicPartition("foo", 1); | ||
| List<TopicPartition> allAssignments = Arrays.asList(tp0, tp1); | ||
| Map<TopicPartition, List<ConsumerRecord<String, String>>> allRecordMap = new HashMap<>(); | ||
| allRecordMap.put(tp0, | ||
| List.of(new ConsumerRecord("foo", 0, 0, null, "bar"), new ConsumerRecord("foo", 0, 1, null, "bar"))); | ||
| allRecordMap.put(tp1, | ||
| List.of(new ConsumerRecord("foo", 1, 0, null, "bar"), new ConsumerRecord("foo", 1, 1, null, "bar"))); | ||
| ConsumerRecords allRecords = new ConsumerRecords<>(allRecordMap); | ||
| List<TopicPartition> afterRevokeAssignments = Arrays.asList(tp1); | ||
| AtomicInteger pollPhase = new AtomicInteger(); | ||
|
|
||
| Consumer consumer = mock(Consumer.class); | ||
| AtomicReference<ConsumerRebalanceListener> rebal = new AtomicReference<>(); | ||
| CountDownLatch subscribeLatch = new CountDownLatch(1); | ||
| willAnswer(invocation -> { | ||
| rebal.set(invocation.getArgument(1)); | ||
| subscribeLatch.countDown(); | ||
| return null; | ||
| }).given(consumer).subscribe(any(Collection.class), any()); | ||
| CountDownLatch pauseLatch = new CountDownLatch(1); | ||
| AtomicBoolean paused = new AtomicBoolean(); | ||
| willAnswer(inv -> { | ||
| paused.set(true); | ||
| pauseLatch.countDown(); | ||
| return null; | ||
| }).given(consumer).pause(any()); | ||
| ConsumerFactory cf = mock(ConsumerFactory.class); | ||
| given(cf.createConsumer(any(), any(), any(), any())).willReturn(consumer); | ||
| given(cf.getConfigurationProperties()) | ||
| .willReturn(Collections.singletonMap(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")); | ||
| ContainerProperties containerProperties = new ContainerProperties("foo"); | ||
| containerProperties.setGroupId("grp"); | ||
| containerProperties.setAckMode(AckMode.MANUAL); | ||
| containerProperties.setMessageListener(ackOffset1()); | ||
| containerProperties.setAsyncAcks(true); | ||
| ConcurrentMessageListenerContainer container = new ConcurrentMessageListenerContainer(cf, | ||
| containerProperties); | ||
| CountDownLatch pollLatch = new CountDownLatch(2); | ||
| CountDownLatch rebalLatch = new CountDownLatch(1); | ||
| CountDownLatch continueLatch = new CountDownLatch(1); | ||
| willAnswer(inv -> { | ||
| Thread.sleep(50); | ||
| pollLatch.countDown(); | ||
| switch (pollPhase.getAndIncrement()) { | ||
| case 0: | ||
| rebal.get().onPartitionsAssigned(allAssignments); | ||
| return allRecords; | ||
| case 1: | ||
| rebal.get().onPartitionsRevoked(List.of(tp0)); | ||
| rebal.get().onPartitionsAssigned(List.of(new TopicPartition("foo", 2))); | ||
| rebalLatch.countDown(); | ||
| continueLatch.await(10, TimeUnit.SECONDS); | ||
| default: | ||
| return ConsumerRecords.empty(); | ||
| } | ||
| }).given(consumer).poll(any()); | ||
| container.start(); | ||
| assertThat(subscribeLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| KafkaMessageListenerContainer child = (KafkaMessageListenerContainer) KafkaTestUtils | ||
| .getPropertyValue(container, "containers", List.class).get(0); | ||
| assertThat(pollLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| assertThat(pauseLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| assertThat(rebalLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
| Map offsets = KafkaTestUtils.getPropertyValue(child, "listenerConsumer.offsetsInThisBatch", Map.class); | ||
| assertThat(offsets).hasSize(1); | ||
| assertThat(offsets.get(tp1)).isNotNull(); | ||
| assertThat(KafkaTestUtils.getPropertyValue(child, "listenerConsumer.consumerPaused", Boolean.class)).isTrue(); | ||
| continueLatch.countDown(); | ||
| verify(consumer, times(2)).pause(any()); | ||
| verify(consumer, never()).resume(any()); | ||
| container.stop(); | ||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") | ||
| private AcknowledgingMessageListener ackOffset1() { | ||
| return new AcknowledgingMessageListener() { | ||
|
|
||
| @Override | ||
| public void onMessage(ConsumerRecord rec, @Nullable Acknowledgment ack) { | ||
| if (rec.offset() == 1) { | ||
| ack.acknowledge(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(Object data) { | ||
| } | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| public static class TestMessageListener1 implements MessageListener<String, String>, ConsumerSeekAware { | ||
|
|
||
| private static ThreadLocal<ConsumerSeekCallback> callbacks = new ThreadLocal<>(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.