-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 all 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
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
153 changes: 153 additions & 0 deletions
153
...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,153 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.kafka.listener; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
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; | ||
|
||
/** | ||
* @author Borahm Lee | ||
* @since 3.3 | ||
*/ | ||
@DirtiesContext | ||
@SpringJUnitConfig | ||
@EmbeddedKafka(topics = {AbstractConsumerSeekAwareTests.TOPIC}, partitions = 3) | ||
class AbstractConsumerSeekAwareTests { | ||
|
||
static final String TOPIC = "Seek"; | ||
|
||
@Autowired | ||
Config config; | ||
|
||
@Autowired | ||
KafkaTemplate<String, String> template; | ||
|
||
@Autowired | ||
MultiGroupListener multiGroupListener; | ||
|
||
@Test | ||
void seekForAllGroups() throws Exception { | ||
template.send(TOPIC, "test-data"); | ||
template.send(TOPIC, "test-data"); | ||
assertThat(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)).isTrue(); | ||
assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); | ||
|
||
MultiGroupListener.latch1 = new CountDownLatch(2); | ||
MultiGroupListener.latch2 = new CountDownLatch(2); | ||
|
||
multiGroupListener.seekToBeginning(); | ||
assertThat(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)).isTrue(); | ||
assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); | ||
} | ||
|
||
@Test | ||
void seekForSpecificGroup() throws Exception { | ||
template.send(TOPIC, "test-data"); | ||
template.send(TOPIC, "test-data"); | ||
assertThat(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)).isTrue(); | ||
assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); | ||
|
||
MultiGroupListener.latch1 = new CountDownLatch(2); | ||
MultiGroupListener.latch2 = new CountDownLatch(2); | ||
|
||
multiGroupListener.seekToBeginningForGroup("group2"); | ||
assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); | ||
assertThat(MultiGroupListener.latch1.await(100, TimeUnit.MICROSECONDS)).isFalse(); | ||
sobychacko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertThat(MultiGroupListener.latch1.getCount()).isEqualTo(2); | ||
} | ||
|
||
@EnableKafka | ||
@Configuration | ||
static class Config { | ||
|
||
@Autowired | ||
EmbeddedKafkaBroker broker; | ||
|
||
@Bean | ||
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) { | ||
latch1.countDown(); | ||
} | ||
|
||
@KafkaListener(groupId = "group2", topics = TOPIC) | ||
void listenForGroup2(String in) { | ||
latch2.countDown(); | ||
} | ||
|
||
void seekToBeginningForGroup(String groupIdForSeek) { | ||
getCallbacksAndTopics().forEach((cb, topics) -> { | ||
if (groupIdForSeek.equals(cb.getGroupId())) { | ||
topics.forEach(tp -> cb.seekToBeginning(tp.topic(), tp.partition())); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
} |
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.