Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1554: Partition wildcard for initial offsets
Resolves #1554 When using manual assignment it was difficult to specify the initial offset for multiple partitions - especially when dynamically determined. Add a wildcard to indicate the offset should be applied to all partitions. **cherry-pick to 2.5.x** * Call onPartitionsAssigned() with manual assignment Resolves #1553 * Fix test class name. # Conflicts: # src/reference/asciidoc/whats-new.adoc
- Loading branch information
1 parent
dee102f
commit 4587d7e
Showing
9 changed files
with
277 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
172 changes: 172 additions & 0 deletions
172
...ka/src/test/java/org/springframework/kafka/listener/ManualAssignmentInitialSeekTests.java
This file contains 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,172 @@ | ||
/* | ||
* Copyright 2017-2020 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 static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.willAnswer; | ||
import static org.mockito.Mockito.atLeastOnce; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InOrder; | ||
|
||
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.annotation.PartitionOffset; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.config.KafkaListenerEndpointRegistry; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.listener.ContainerProperties.AckMode; | ||
import org.springframework.kafka.test.utils.KafkaTestUtils; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
/** | ||
* @author Gary Russell | ||
* @since 2.0.1 | ||
* | ||
*/ | ||
@SpringJUnitConfig | ||
@DirtiesContext | ||
public class ManualAssignmentInitialSeekTests { | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Autowired | ||
private Consumer consumer; | ||
|
||
@Autowired | ||
private Config config; | ||
|
||
@Autowired | ||
private KafkaListenerEndpointRegistry registry; | ||
|
||
/* | ||
* Deliver 6 records from three partitions, fail on the second record second | ||
* partition, first attempt; verify partition 0,1 committed and a total of 7 records | ||
* handled after seek. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void discardRemainingRecordsFromPollAndSeek() throws Exception { | ||
assertThat(this.config.pollLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
this.registry.stop(); | ||
assertThat(this.config.closeLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
InOrder inOrder = inOrder(this.consumer); | ||
inOrder.verify(this.consumer).assign(any(Collection.class)); | ||
inOrder.verify(this.consumer).seekToBeginning(any()); | ||
inOrder.verify(this.consumer, atLeastOnce()).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); | ||
assertThat(this.config.registerSeekCallbackCalled).isTrue(); | ||
assertThat(this.config.partitionsAssignedCalled).isTrue(); | ||
assertThat(this.config.assignments).hasSize(3); | ||
} | ||
|
||
@Configuration | ||
@EnableKafka | ||
public static class Config extends AbstractConsumerSeekAware { | ||
|
||
final CountDownLatch pollLatch = new CountDownLatch(1); | ||
|
||
final CountDownLatch closeLatch = new CountDownLatch(1); | ||
|
||
volatile boolean registerSeekCallbackCalled; | ||
|
||
volatile boolean partitionsAssignedCalled; | ||
|
||
volatile Map<TopicPartition, Long> assignments; | ||
|
||
@KafkaListener(groupId = "grp", | ||
topicPartitions = @org.springframework.kafka.annotation.TopicPartition(topic = "foo", | ||
partitions = "#{'0,1,2'.split(',')}", | ||
partitionOffsets = @PartitionOffset(partition = "*", initialOffset = "0"))) | ||
public void foo(String in) { | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes" }) | ||
@Bean | ||
public ConsumerFactory consumerFactory() { | ||
ConsumerFactory consumerFactory = mock(ConsumerFactory.class); | ||
final Consumer consumer = consumer(); | ||
given(consumerFactory.createConsumer("grp", "", "-0", KafkaTestUtils.defaultPropertyOverrides())) | ||
.willReturn(consumer); | ||
return consumerFactory; | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
@Bean | ||
public Consumer consumer() { | ||
final Consumer consumer = mock(Consumer.class); | ||
willAnswer(i -> { | ||
this.pollLatch.countDown(); | ||
try { | ||
Thread.sleep(50); | ||
} | ||
catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
return new ConsumerRecords(Collections.emptyMap()); | ||
}).given(consumer).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); | ||
willAnswer(i -> { | ||
this.closeLatch.countDown(); | ||
return null; | ||
}).given(consumer).close(); | ||
return consumer; | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory(); | ||
factory.setConsumerFactory(consumerFactory()); | ||
factory.setErrorHandler(new SeekToCurrentErrorHandler()); | ||
factory.getContainerProperties().setAckMode(AckMode.RECORD); | ||
factory.getContainerProperties().setDeliveryAttemptHeader(true); | ||
return factory; | ||
} | ||
|
||
@Override | ||
public void registerSeekCallback(ConsumerSeekCallback callback) { | ||
super.registerSeekCallback(callback); | ||
this.registerSeekCallbackCalled = true; | ||
} | ||
|
||
@Override | ||
public void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback) { | ||
super.onPartitionsAssigned(assignments, callback); | ||
this.partitionsAssignedCalled = true; | ||
this.assignments = assignments; | ||
callback.seekToBeginning(assignments.keySet()); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.