|
| 1 | +/* |
| 2 | + * Copyright 2017-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.BDDMockito.given; |
| 22 | +import static org.mockito.BDDMockito.willAnswer; |
| 23 | +import static org.mockito.Mockito.atLeastOnce; |
| 24 | +import static org.mockito.Mockito.inOrder; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | + |
| 27 | +import java.time.Duration; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.CountDownLatch; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +import org.apache.kafka.clients.consumer.Consumer; |
| 35 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 36 | +import org.apache.kafka.common.TopicPartition; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.mockito.InOrder; |
| 39 | + |
| 40 | +import org.springframework.beans.factory.annotation.Autowired; |
| 41 | +import org.springframework.context.annotation.Bean; |
| 42 | +import org.springframework.context.annotation.Configuration; |
| 43 | +import org.springframework.kafka.annotation.EnableKafka; |
| 44 | +import org.springframework.kafka.annotation.KafkaListener; |
| 45 | +import org.springframework.kafka.annotation.PartitionOffset; |
| 46 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 47 | +import org.springframework.kafka.config.KafkaListenerEndpointRegistry; |
| 48 | +import org.springframework.kafka.core.ConsumerFactory; |
| 49 | +import org.springframework.kafka.listener.ContainerProperties.AckMode; |
| 50 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 51 | +import org.springframework.test.annotation.DirtiesContext; |
| 52 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 53 | + |
| 54 | +/** |
| 55 | + * @author Gary Russell |
| 56 | + * @since 2.0.1 |
| 57 | + * |
| 58 | + */ |
| 59 | +@SpringJUnitConfig |
| 60 | +@DirtiesContext |
| 61 | +public class ManualAssignmentInitialSeekTests { |
| 62 | + |
| 63 | + @SuppressWarnings("rawtypes") |
| 64 | + @Autowired |
| 65 | + private Consumer consumer; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private Config config; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private KafkaListenerEndpointRegistry registry; |
| 72 | + |
| 73 | + /* |
| 74 | + * Deliver 6 records from three partitions, fail on the second record second |
| 75 | + * partition, first attempt; verify partition 0,1 committed and a total of 7 records |
| 76 | + * handled after seek. |
| 77 | + */ |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + @Test |
| 80 | + public void discardRemainingRecordsFromPollAndSeek() throws Exception { |
| 81 | + assertThat(this.config.pollLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 82 | + this.registry.stop(); |
| 83 | + assertThat(this.config.closeLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 84 | + InOrder inOrder = inOrder(this.consumer); |
| 85 | + inOrder.verify(this.consumer).assign(any(Collection.class)); |
| 86 | + inOrder.verify(this.consumer).seekToBeginning(any()); |
| 87 | + inOrder.verify(this.consumer, atLeastOnce()).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); |
| 88 | + assertThat(this.config.registerSeekCallbackCalled).isTrue(); |
| 89 | + assertThat(this.config.partitionsAssignedCalled).isTrue(); |
| 90 | + assertThat(this.config.assignments).hasSize(3); |
| 91 | + } |
| 92 | + |
| 93 | + @Configuration |
| 94 | + @EnableKafka |
| 95 | + public static class Config extends AbstractConsumerSeekAware { |
| 96 | + |
| 97 | + final CountDownLatch pollLatch = new CountDownLatch(1); |
| 98 | + |
| 99 | + final CountDownLatch closeLatch = new CountDownLatch(1); |
| 100 | + |
| 101 | + volatile boolean registerSeekCallbackCalled; |
| 102 | + |
| 103 | + volatile boolean partitionsAssignedCalled; |
| 104 | + |
| 105 | + volatile Map<TopicPartition, Long> assignments; |
| 106 | + |
| 107 | + @KafkaListener(groupId = "grp", |
| 108 | + topicPartitions = @org.springframework.kafka.annotation.TopicPartition(topic = "foo", |
| 109 | + partitions = "#{'0,1,2'.split(',')}", |
| 110 | + partitionOffsets = @PartitionOffset(partition = "*", initialOffset = "0"))) |
| 111 | + public void foo(String in) { |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressWarnings({ "rawtypes" }) |
| 115 | + @Bean |
| 116 | + public ConsumerFactory consumerFactory() { |
| 117 | + ConsumerFactory consumerFactory = mock(ConsumerFactory.class); |
| 118 | + final Consumer consumer = consumer(); |
| 119 | + given(consumerFactory.createConsumer("grp", "", "-0", KafkaTestUtils.defaultPropertyOverrides())) |
| 120 | + .willReturn(consumer); |
| 121 | + return consumerFactory; |
| 122 | + } |
| 123 | + |
| 124 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 125 | + @Bean |
| 126 | + public Consumer consumer() { |
| 127 | + final Consumer consumer = mock(Consumer.class); |
| 128 | + willAnswer(i -> { |
| 129 | + this.pollLatch.countDown(); |
| 130 | + try { |
| 131 | + Thread.sleep(50); |
| 132 | + } |
| 133 | + catch (InterruptedException e) { |
| 134 | + Thread.currentThread().interrupt(); |
| 135 | + } |
| 136 | + return new ConsumerRecords(Collections.emptyMap()); |
| 137 | + }).given(consumer).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); |
| 138 | + willAnswer(i -> { |
| 139 | + this.closeLatch.countDown(); |
| 140 | + return null; |
| 141 | + }).given(consumer).close(); |
| 142 | + return consumer; |
| 143 | + } |
| 144 | + |
| 145 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 146 | + @Bean |
| 147 | + public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { |
| 148 | + ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory(); |
| 149 | + factory.setConsumerFactory(consumerFactory()); |
| 150 | + factory.setErrorHandler(new SeekToCurrentErrorHandler()); |
| 151 | + factory.getContainerProperties().setAckMode(AckMode.RECORD); |
| 152 | + factory.getContainerProperties().setDeliveryAttemptHeader(true); |
| 153 | + return factory; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void registerSeekCallback(ConsumerSeekCallback callback) { |
| 158 | + super.registerSeekCallback(callback); |
| 159 | + this.registerSeekCallbackCalled = true; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback) { |
| 164 | + super.onPartitionsAssigned(assignments, callback); |
| 165 | + this.partitionsAssignedCalled = true; |
| 166 | + this.assignments = assignments; |
| 167 | + callback.seekToBeginning(assignments.keySet()); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments