|
| 1 | +/* |
| 2 | + * Copyright 2017-2019 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.ArgumentMatchers.anyMap; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.BDDMockito.given; |
| 24 | +import static org.mockito.BDDMockito.willAnswer; |
| 25 | +import static org.mockito.Mockito.inOrder; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | + |
| 29 | +import java.time.Duration; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.LinkedHashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
| 40 | + |
| 41 | +import org.apache.kafka.clients.consumer.Consumer; |
| 42 | +import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; |
| 43 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 44 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 45 | +import org.apache.kafka.common.TopicPartition; |
| 46 | +import org.apache.kafka.common.record.TimestampType; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | +import org.mockito.InOrder; |
| 49 | + |
| 50 | +import org.springframework.beans.factory.annotation.Autowired; |
| 51 | +import org.springframework.context.annotation.Bean; |
| 52 | +import org.springframework.context.annotation.Configuration; |
| 53 | +import org.springframework.kafka.annotation.EnableKafka; |
| 54 | +import org.springframework.kafka.annotation.KafkaListener; |
| 55 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 56 | +import org.springframework.kafka.config.KafkaListenerEndpointRegistry; |
| 57 | +import org.springframework.kafka.core.ConsumerFactory; |
| 58 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 59 | +import org.springframework.test.annotation.DirtiesContext; |
| 60 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 61 | + |
| 62 | +/** |
| 63 | + * @author Gary Russell |
| 64 | + * @since 2.3.2 |
| 65 | + * |
| 66 | + */ |
| 67 | +@SpringJUnitConfig |
| 68 | +@DirtiesContext |
| 69 | +public class SubBatchPerPartitionTests { |
| 70 | + |
| 71 | + private static final String CONTAINER_ID = "container"; |
| 72 | + |
| 73 | + @SuppressWarnings("rawtypes") |
| 74 | + @Autowired |
| 75 | + private Consumer consumer; |
| 76 | + |
| 77 | + @Autowired |
| 78 | + private Config config; |
| 79 | + |
| 80 | + @Autowired |
| 81 | + private KafkaListenerEndpointRegistry registry; |
| 82 | + |
| 83 | + /* |
| 84 | + * Deliver 6 records from three partitions, fail on the second record second |
| 85 | + * partition. |
| 86 | + */ |
| 87 | + @SuppressWarnings("unchecked") |
| 88 | + @Test |
| 89 | + public void discardRemainingRecordsFromPollAndSeek() throws Exception { |
| 90 | + assertThat(this.config.deliveryLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 91 | + assertThat(this.config.commitLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 92 | + assertThat(this.config.pollLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 93 | + this.registry.stop(); |
| 94 | + assertThat(this.config.closeLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 95 | + InOrder inOrder = inOrder(this.consumer); |
| 96 | + inOrder.verify(this.consumer).subscribe(any(Collection.class), any(ConsumerRebalanceListener.class)); |
| 97 | + inOrder.verify(this.consumer).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); |
| 98 | + inOrder.verify(this.consumer, times(3)).commitSync(any(), eq(Duration.ofSeconds(60))); |
| 99 | + inOrder.verify(this.consumer).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); |
| 100 | + assertThat(this.config.contents).contains("foo", "bar", "baz", "qux", "fiz", "buz"); |
| 101 | + } |
| 102 | + |
| 103 | + @Configuration |
| 104 | + @EnableKafka |
| 105 | + public static class Config { |
| 106 | + |
| 107 | + private final List<String> contents = new ArrayList<>(); |
| 108 | + |
| 109 | + private final CountDownLatch pollLatch = new CountDownLatch(2); |
| 110 | + |
| 111 | + private final CountDownLatch deliveryLatch = new CountDownLatch(3); |
| 112 | + |
| 113 | + private final CountDownLatch commitLatch = new CountDownLatch(3); |
| 114 | + |
| 115 | + private final CountDownLatch closeLatch = new CountDownLatch(1); |
| 116 | + |
| 117 | + @KafkaListener(id = CONTAINER_ID, topics = "foo") |
| 118 | + public void foo(List<String> in) { |
| 119 | + contents.addAll(in); |
| 120 | + this.deliveryLatch.countDown(); |
| 121 | + } |
| 122 | + |
| 123 | + @SuppressWarnings({ "rawtypes" }) |
| 124 | + @Bean |
| 125 | + public ConsumerFactory consumerFactory() { |
| 126 | + ConsumerFactory consumerFactory = mock(ConsumerFactory.class); |
| 127 | + final Consumer consumer = consumer(); |
| 128 | + given(consumerFactory.createConsumer(CONTAINER_ID, "", "-0", KafkaTestUtils.defaultPropertyOverrides())) |
| 129 | + .willReturn(consumer); |
| 130 | + return consumerFactory; |
| 131 | + } |
| 132 | + |
| 133 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 134 | + @Bean |
| 135 | + public Consumer consumer() { |
| 136 | + final Consumer consumer = mock(Consumer.class); |
| 137 | + final TopicPartition topicPartition0 = new TopicPartition("foo", 0); |
| 138 | + final TopicPartition topicPartition1 = new TopicPartition("foo", 1); |
| 139 | + final TopicPartition topicPartition2 = new TopicPartition("foo", 2); |
| 140 | + willAnswer(i -> { |
| 141 | + ((ConsumerRebalanceListener) i.getArgument(1)).onPartitionsAssigned( |
| 142 | + Arrays.asList(topicPartition0, topicPartition1, topicPartition2)); |
| 143 | + return null; |
| 144 | + }).given(consumer).subscribe(any(Collection.class), any(ConsumerRebalanceListener.class)); |
| 145 | + Map<TopicPartition, List<ConsumerRecord>> records1 = new LinkedHashMap<>(); |
| 146 | + records1.put(topicPartition0, Arrays.asList( |
| 147 | + new ConsumerRecord("foo", 0, 0L, 0L, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, 0, null, "foo"), |
| 148 | + new ConsumerRecord("foo", 0, 1L, 0L, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, 0, null, "bar"))); |
| 149 | + records1.put(topicPartition1, Arrays.asList( |
| 150 | + new ConsumerRecord("foo", 1, 0L, 0L, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, 0, null, "baz"), |
| 151 | + new ConsumerRecord("foo", 1, 1L, 0L, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, 0, null, "qux"))); |
| 152 | + records1.put(topicPartition2, Arrays.asList( |
| 153 | + new ConsumerRecord("foo", 2, 0L, 0L, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, 0, null, "fiz"), |
| 154 | + new ConsumerRecord("foo", 2, 1L, 0L, TimestampType.NO_TIMESTAMP_TYPE, 0, 0, 0, null, "buz"))); |
| 155 | + final AtomicInteger which = new AtomicInteger(); |
| 156 | + willAnswer(i -> { |
| 157 | + this.pollLatch.countDown(); |
| 158 | + switch (which.getAndIncrement()) { |
| 159 | + case 0: |
| 160 | + return new ConsumerRecords(records1); |
| 161 | + default: |
| 162 | + try { |
| 163 | + Thread.sleep(100); |
| 164 | + } |
| 165 | + catch (@SuppressWarnings("unused") InterruptedException e) { |
| 166 | + Thread.currentThread().interrupt(); |
| 167 | + } |
| 168 | + return new ConsumerRecords(Collections.emptyMap()); |
| 169 | + } |
| 170 | + }).given(consumer).poll(Duration.ofMillis(ContainerProperties.DEFAULT_POLL_TIMEOUT)); |
| 171 | + willAnswer(i -> { |
| 172 | + this.commitLatch.countDown(); |
| 173 | + return null; |
| 174 | + }).given(consumer).commitSync(anyMap(), any()); |
| 175 | + willAnswer(i -> { |
| 176 | + this.closeLatch.countDown(); |
| 177 | + return null; |
| 178 | + }).given(consumer).close(); |
| 179 | + return consumer; |
| 180 | + } |
| 181 | + |
| 182 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 183 | + @Bean |
| 184 | + public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { |
| 185 | + ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory(); |
| 186 | + factory.setConsumerFactory(consumerFactory()); |
| 187 | + factory.getContainerProperties().setAckOnError(false); |
| 188 | + factory.setBatchListener(true); |
| 189 | + factory.getContainerProperties().setMissingTopicsFatal(false); |
| 190 | + factory.getContainerProperties().setSubBatchPerPartition(true); |
| 191 | + return factory; |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments