-
Notifications
You must be signed in to change notification settings - Fork 1.7k
GH-2302: Enable consumer seek only on matching group Id #3318
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3b3490f
GH-2302: Enable consumer seek only on matching group Id
bky373 a81a06d
Add groupId getter to ConsumerSeekCallback and remove match method
bky373 991bc8f
remove line for class
bky373 a724510
Resolve checkstyle violations and add missing copyright section, auth…
bky373 d7ce604
Drop `public` modifiers
bky373 2f3cdb7
Update "seek.adoc" and "whats-new.adoc"
bky373 f94938c
Restore dlt style in "whats-new.adoc"
bky373 1ef1ccd
Update "whats-new.adoc"
bky373 d79ea1a
Update "seek.adoc"
bky373 4248be5
Revert the existing javadocs
bky373 74a533a
Use consumerGroupId field instead
bky373 f1e15ce
Remove useless method
bky373 0b1aae4
Modify test
bky373 b34a610
Add assert
bky373 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...afka/src/test/java/org/springframework/kafka/listener/AbstractConsumerSeekAwareTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package org.springframework.kafka.listener; | ||
artembilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.kafka.annotation.EnableKafka; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
| import org.springframework.kafka.core.ConsumerFactory; | ||
| import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
| import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
| import org.springframework.kafka.core.ProducerFactory; | ||
| import org.springframework.kafka.listener.AbstractConsumerSeekAwareTests.Config.MultiGroupListener; | ||
| import org.springframework.kafka.test.EmbeddedKafkaBroker; | ||
| import org.springframework.kafka.test.context.EmbeddedKafka; | ||
| import org.springframework.kafka.test.utils.KafkaTestUtils; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.test.annotation.DirtiesContext; | ||
| import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @DirtiesContext | ||
| @SpringJUnitConfig | ||
| @EmbeddedKafka(topics = {AbstractConsumerSeekAwareTests.TOPIC}, partitions = 3) | ||
| public class AbstractConsumerSeekAwareTests { | ||
sobychacko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| static final String TOPIC = "Seek"; | ||
|
|
||
| @Autowired | ||
| Config config; | ||
|
|
||
| @Autowired | ||
| KafkaTemplate<String, String> template; | ||
|
|
||
| @Autowired | ||
| MultiGroupListener multiGroupListener; | ||
|
|
||
| @Test | ||
| public void seekForAllGroups() throws Exception { | ||
sobychacko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| template.send(TOPIC, "test-data"); | ||
| template.send(TOPIC, "test-data"); | ||
| assertTrue(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)); | ||
| assertTrue(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)); | ||
|
|
||
| MultiGroupListener.latch1 = new CountDownLatch(2); | ||
| MultiGroupListener.latch2 = new CountDownLatch(2); | ||
|
|
||
| multiGroupListener.seekToBeginning(); | ||
| assertTrue(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)); | ||
| assertTrue(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| @Test | ||
| public void seekForSpecificGroup() throws Exception { | ||
sobychacko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| template.send(TOPIC, "test-data"); | ||
| template.send(TOPIC, "test-data"); | ||
| assertTrue(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)); | ||
| assertTrue(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)); | ||
|
|
||
| MultiGroupListener.latch1 = new CountDownLatch(2); | ||
| MultiGroupListener.latch2 = new CountDownLatch(2); | ||
|
|
||
| multiGroupListener.seekToBeginningForGroup("group2"); | ||
| assertThat(MultiGroupListener.latch1.getCount()).isEqualTo(2); | ||
| assertTrue(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| @EnableKafka | ||
| @Configuration | ||
| static class Config { | ||
|
|
||
| @Autowired | ||
| EmbeddedKafkaBroker broker; | ||
|
|
||
| @Bean | ||
| public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory( | ||
| ConsumerFactory<String, String> consumerFactory) { | ||
| ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
| factory.setConsumerFactory(consumerFactory); | ||
| return factory; | ||
| } | ||
|
|
||
| @Bean | ||
| ConsumerFactory<String, String> consumerFactory() { | ||
| return new DefaultKafkaConsumerFactory<>(KafkaTestUtils.consumerProps("test-group", "false", this.broker)); | ||
| } | ||
|
|
||
| @Bean | ||
| ProducerFactory<String, String> producerFactory() { | ||
| return new DefaultKafkaProducerFactory<>(KafkaTestUtils.producerProps(this.broker)); | ||
| } | ||
|
|
||
| @Bean | ||
| KafkaTemplate<String, String> template(ProducerFactory<String, String> pf) { | ||
| return new KafkaTemplate<>(pf); | ||
| } | ||
|
|
||
| @Component | ||
| static class MultiGroupListener extends AbstractConsumerSeekAware { | ||
|
|
||
| static CountDownLatch latch1 = new CountDownLatch(2); | ||
| static CountDownLatch latch2 = new CountDownLatch(2); | ||
|
|
||
| @KafkaListener(groupId = "group1", topics = TOPIC) | ||
| void listenForGroup1(String in) { | ||
| // System.out.printf("[group1] in = %s\n", in); // TODO remove | ||
| latch1.countDown(); | ||
| } | ||
|
|
||
| @KafkaListener(groupId = "group2", topics = TOPIC) | ||
| void listenForGroup2(String in) { | ||
| // System.out.printf("[group2] in = %s\n", in); // TODO remove | ||
| latch2.countDown(); | ||
| } | ||
|
|
||
| public void seekToBeginningForGroup(String groupIdForSeek) { | ||
| getCallbacksAndTopics().forEach((cb, topics) -> { | ||
| if (groupIdForSeek.equals(cb.getGroupId())) { | ||
| topics.forEach(tp -> cb.seekToBeginning(tp.topic(), tp.partition())); | ||
| } | ||
| }); | ||
| } | ||
sobychacko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.