-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-12980: Return empty record batch from Consumer::poll when position advances due to aborted transactions #11046
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
Changes from 1 commit
4175df1
62706bd
c90d85f
e3fcdaf
cb30115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| import org.apache.kafka.clients.consumer.internals.ConsumerInterceptors; | ||
| import org.apache.kafka.clients.consumer.internals.ConsumerMetadata; | ||
| import org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient; | ||
| import org.apache.kafka.clients.consumer.internals.Fetch; | ||
| import org.apache.kafka.clients.consumer.internals.Fetcher; | ||
| import org.apache.kafka.clients.consumer.internals.FetcherMetricsRegistry; | ||
| import org.apache.kafka.clients.consumer.internals.KafkaConsumerMetrics; | ||
|
|
@@ -1235,9 +1236,15 @@ private ConsumerRecords<K, V> poll(final Timer timer, final boolean includeMetad | |
| } | ||
| } | ||
|
|
||
| final Map<TopicPartition, List<ConsumerRecord<K, V>>> records = pollForFetches(timer); | ||
| if (!records.isEmpty()) { | ||
| // before returning the fetched records, we can send off the next round of fetches | ||
| final Fetch<K, V> fetch = pollForFetches(timer); | ||
| if (!fetch.isEmpty()) { | ||
| if (fetch.records().isEmpty()) { | ||
| log.debug( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not so sure about the value of this log line. Maybe it would be more useful to ensure that we have enough logging in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that works. My goal with this line was to help people understand the cause for this new behavior, as it may seem like a bug at first if Also, this section was pretty buggy as-was since it skipped the call to |
||
| "Returning empty records from poll since the consumer's position has advanced " | ||
| + "for at least one topic partition; this may happen in the case of aborted or empty transactions" | ||
| ); | ||
| return ConsumerRecords.empty(); | ||
| } // before returning the fetched records, we can send off the next round of fetches | ||
|
C0urante marked this conversation as resolved.
Outdated
|
||
| // and avoid block waiting for their responses to enable pipelining while the user | ||
| // is handling the fetched records. | ||
| // | ||
|
|
@@ -1247,7 +1254,7 @@ private ConsumerRecords<K, V> poll(final Timer timer, final boolean includeMetad | |
| client.transmitSends(); | ||
| } | ||
|
|
||
| return this.interceptors.onConsume(new ConsumerRecords<>(records)); | ||
| return this.interceptors.onConsume(new ConsumerRecords<>(fetch.records())); | ||
| } | ||
| } while (timer.notExpired()); | ||
|
|
||
|
|
@@ -1269,14 +1276,14 @@ boolean updateAssignmentMetadataIfNeeded(final Timer timer, final boolean waitFo | |
| /** | ||
| * @throws KafkaException if the rebalance callback throws exception | ||
| */ | ||
| private Map<TopicPartition, List<ConsumerRecord<K, V>>> pollForFetches(Timer timer) { | ||
| private Fetch<K, V> pollForFetches(Timer timer) { | ||
| long pollTimeout = coordinator == null ? timer.remainingMs() : | ||
| Math.min(coordinator.timeToNextPoll(timer.currentTimeMs()), timer.remainingMs()); | ||
|
|
||
| // if data is available already, return it immediately | ||
| final Map<TopicPartition, List<ConsumerRecord<K, V>>> records = fetcher.fetchedRecords(); | ||
| if (!records.isEmpty()) { | ||
| return records; | ||
| final Fetch<K, V> fetch = fetcher.collectFetch(); | ||
| if (!fetch.isEmpty()) { | ||
| return fetch; | ||
| } | ||
|
|
||
| // send any new fetches (won't resend pending fetches) | ||
|
|
@@ -1301,7 +1308,7 @@ private Map<TopicPartition, List<ConsumerRecord<K, V>>> pollForFetches(Timer tim | |
| }); | ||
| timer.update(pollTimer.currentTimeMs()); | ||
|
|
||
| return fetcher.fetchedRecords(); | ||
| return fetcher.collectFetch(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 | ||
| * | ||
| * http://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.apache.kafka.clients.consumer.internals; | ||
|
|
||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.common.TopicPartition; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.apache.kafka.common.utils.Utils.mkEntry; | ||
| import static org.apache.kafka.common.utils.Utils.mkMap; | ||
|
|
||
| public class Fetch<K, V> { | ||
| private final Map<TopicPartition, List<ConsumerRecord<K, V>>> records; | ||
| private boolean positionAdvanced; | ||
| private int numRecords; | ||
|
|
||
| public static <K, V> Fetch<K, V> empty() { | ||
| return new Fetch<>(new HashMap<>(), false, 0); | ||
| } | ||
|
|
||
| public static <K, V> Fetch<K, V> forPartition( | ||
| TopicPartition partition, | ||
| List<ConsumerRecord<K, V>> records, | ||
| boolean positionAdvanced | ||
| ) { | ||
| Map<TopicPartition, List<ConsumerRecord<K, V>>> recordsMap = records.isEmpty() | ||
| ? new HashMap<>() | ||
| : mkMap(mkEntry(partition, records)); | ||
| return new Fetch<>(recordsMap, positionAdvanced, records.size()); | ||
| } | ||
|
|
||
| private Fetch( | ||
| Map<TopicPartition, List<ConsumerRecord<K, V>>> records, | ||
| boolean positionAdvanced, | ||
| int numRecords | ||
| ) { | ||
| this.records = records; | ||
| this.positionAdvanced = positionAdvanced; | ||
| this.numRecords = numRecords; | ||
| } | ||
|
|
||
| /** | ||
| * Add another {@link Fetch} to this one; all of its records will be added to this fetch's | ||
| * {@link #records()} records}, and if the other fetch | ||
| * {@link #positionAdvanced() advanced the consume position for any topic partition}, | ||
| * this fetch will be marked as having advanced the consume position as well. | ||
| * @param fetch the other fetch to add; may not be null | ||
| */ | ||
| public void add(Fetch<K, V> fetch) { | ||
| Objects.requireNonNull(fetch); | ||
| addRecords(fetch.records); | ||
| this.positionAdvanced |= fetch.positionAdvanced; | ||
| } | ||
|
|
||
| /** | ||
| * @return all of the non-control messages for this fetch, grouped by partition | ||
| */ | ||
| public Map<TopicPartition, List<ConsumerRecord<K, V>>> records() { | ||
| return Collections.unmodifiableMap(records); | ||
| } | ||
|
|
||
| /** | ||
| * @return whether the fetch caused the consumer's | ||
| * {@link org.apache.kafka.clients.consumer.KafkaConsumer#position(TopicPartition) position} to advance for at | ||
| * least one of the topic partitions in this fetch | ||
| */ | ||
| public boolean positionAdvanced() { | ||
| return positionAdvanced; | ||
| } | ||
|
|
||
| /** | ||
| * @return the total number of non-control messages for this fetch, across all partitions | ||
| */ | ||
| public int numRecords() { | ||
| return numRecords; | ||
| } | ||
|
|
||
| /** | ||
| * @return {@code true} if and only if this fetch did not return any user-visible (i.e., non-control) records, and | ||
| * did not cause the consumer position to advance for any topic partitions | ||
| */ | ||
| public boolean isEmpty() { | ||
| return numRecords == 0 && !positionAdvanced; | ||
| } | ||
|
|
||
| private void addRecords(Map<TopicPartition, List<ConsumerRecord<K, V>>> records) { | ||
| records.forEach((partition, partRecords) -> { | ||
| this.numRecords += partRecords.size(); | ||
| List<ConsumerRecord<K, V>> currentRecords = this.records.get(partition); | ||
| if (currentRecords == null) { | ||
| this.records.put(partition, partRecords); | ||
| } else { | ||
| // this case shouldn't usually happen because we only send one fetch at a time per partition, | ||
| // but it might conceivably happen in some rare cases (such as partition leader changes). | ||
| // we have to copy to a new list because the old one may be immutable | ||
| List<ConsumerRecord<K, V>> newRecords = new ArrayList<>(currentRecords.size() + partRecords.size()); | ||
| newRecords.addAll(currentRecords); | ||
| newRecords.addAll(partRecords); | ||
| this.records.put(partition, newRecords); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
C0urante marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.