Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1175,9 +1176,11 @@ public ConsumerRecords<K, V> poll(final long timeoutMs) {
* offset for the subscribed list of partitions
*
* <p>
* This method returns immediately if there are records available. Otherwise, it will await the passed timeout.
* If the timeout expires, an empty record set will be returned. Note that this method may block beyond the
* timeout in order to execute custom {@link ConsumerRebalanceListener} callbacks.
* This method returns immediately if there are records available or if the position advances past control records
* or aborted transactions when isolation.level=read_committed.
* Otherwise, it will await the passed timeout. If the timeout expires, an empty record set will be returned.
* Note that this method may block beyond the timeout in order to execute custom
* {@link ConsumerRebalanceListener} callbacks.
*
*
* @param timeout The maximum time to block (must not be greater than {@link Long#MAX_VALUE} milliseconds)
Expand Down Expand Up @@ -1235,8 +1238,8 @@ private ConsumerRecords<K, V> poll(final Timer timer, final boolean includeMetad
}
}

final Map<TopicPartition, List<ConsumerRecord<K, V>>> records = pollForFetches(timer);
if (!records.isEmpty()) {
final Fetch<K, V> fetch = pollForFetches(timer);
Comment thread
C0urante marked this conversation as resolved.
if (!fetch.isEmpty()) {
// before returning the fetched records, we can send off the next round of fetches
// and avoid block waiting for their responses to enable pipelining while the user
// is handling the fetched records.
Expand All @@ -1247,7 +1250,12 @@ private ConsumerRecords<K, V> poll(final Timer timer, final boolean includeMetad
client.transmitSends();
}

return this.interceptors.onConsume(new ConsumerRecords<>(records));
if (fetch.records().isEmpty()) {
log.trace("Returning empty records from `poll()` "
+ "since the consumer's position has advanced for at least one topic partition");
}

return this.interceptors.onConsume(new ConsumerRecords<>(fetch.records()));
}
} while (timer.notExpired());

Expand All @@ -1269,14 +1277,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)
Expand All @@ -1301,7 +1309,7 @@ private Map<TopicPartition, List<ConsumerRecord<K, V>>> pollForFetches(Timer tim
});
timer.update(pollTimer.currentTimeMs());

return fetcher.fetchedRecords();
return fetcher.collectFetch();
}

/**
Expand Down
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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;

/**
* This class manages the fetching process with the brokers.
* <p>
Expand Down Expand Up @@ -637,15 +635,15 @@ private Map<TopicPartition, Long> beginningOrEndOffset(Collection<TopicPartition
/**
* Return the fetched records, empty the record buffer and update the consumed position.
*
* NOTE: returning empty records guarantees the consumed position are NOT updated.
* NOTE: returning an {@link Fetch#isEmpty empty} fetch guarantees the consumed position is not updated.
Comment thread
C0urante marked this conversation as resolved.
Outdated
*
* @return The fetched records per partition
* @return A {@link Fetch} for the requested partitions
* @throws OffsetOutOfRangeException If there is OffsetOutOfRange error in fetchResponse and
* the defaultResetPolicy is NONE
* @throws TopicAuthorizationException If there is TopicAuthorization error in fetchResponse.
*/
public Map<TopicPartition, List<ConsumerRecord<K, V>>> fetchedRecords() {
Map<TopicPartition, List<ConsumerRecord<K, V>>> fetched = new HashMap<>();
public Fetch<K, V> collectFetch() {
Fetch<K, V> fetch = Fetch.empty();
Queue<CompletedFetch> pausedCompletedFetches = new ArrayDeque<>();
int recordsRemaining = maxPollRecords;

Expand All @@ -665,7 +663,7 @@ public Map<TopicPartition, List<ConsumerRecord<K, V>>> fetchedRecords() {
// in cases such as the TopicAuthorizationException, and the second condition ensures that no
// potential data loss due to an exception in a following record.
FetchResponseData.PartitionData partition = records.partitionData;
if (fetched.isEmpty() && FetchResponse.recordsOrFail(partition).sizeInBytes() == 0) {
if (fetch.isEmpty() && FetchResponse.recordsOrFail(partition).sizeInBytes() == 0) {
completedFetches.poll();
}
throw e;
Expand All @@ -681,39 +679,24 @@ public Map<TopicPartition, List<ConsumerRecord<K, V>>> fetchedRecords() {
pausedCompletedFetches.add(nextInLineFetch);
nextInLineFetch = null;
} else {
List<ConsumerRecord<K, V>> records = fetchRecords(nextInLineFetch, recordsRemaining);

if (!records.isEmpty()) {
TopicPartition partition = nextInLineFetch.partition;
List<ConsumerRecord<K, V>> currentRecords = fetched.get(partition);
if (currentRecords == null) {
fetched.put(partition, records);
} 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<>(records.size() + currentRecords.size());
newRecords.addAll(currentRecords);
newRecords.addAll(records);
fetched.put(partition, newRecords);
}
recordsRemaining -= records.size();
}
Fetch<K, V> nextFetch = fetchRecords(nextInLineFetch, recordsRemaining);
recordsRemaining -= nextFetch.numRecords();
fetch.add(nextFetch);
}
}
} catch (KafkaException e) {
if (fetched.isEmpty())
if (fetch.isEmpty())
throw e;
} finally {
// add any polled completed fetches for paused partitions back to the completed fetches queue to be
// re-evaluated in the next poll
completedFetches.addAll(pausedCompletedFetches);
}

return fetched;
return fetch;
}

private List<ConsumerRecord<K, V>> fetchRecords(CompletedFetch completedFetch, int maxRecords) {
private Fetch<K, V> fetchRecords(CompletedFetch completedFetch, int maxRecords) {
if (!subscriptions.isAssigned(completedFetch.partition)) {
// this can happen when a rebalance happened before fetched records are returned to the consumer's poll call
log.debug("Not returning fetched records for partition {} since it is no longer assigned",
Expand All @@ -735,13 +718,17 @@ private List<ConsumerRecord<K, V>> fetchRecords(CompletedFetch completedFetch, i
log.trace("Returning {} fetched records at offset {} for assigned partition {}",
partRecords.size(), position, completedFetch.partition);

boolean positionAdvanced = false;

if (completedFetch.nextFetchOffset > position.offset) {
FetchPosition nextPosition = new FetchPosition(
completedFetch.nextFetchOffset,
completedFetch.lastEpoch,
position.currentLeader);
log.trace("Update fetching position to {} for partition {}", nextPosition, completedFetch.partition);
log.trace("Updating fetch position from {} to {} for partition {} and returning {} records from `poll()`",
position, nextPosition, completedFetch.partition, partRecords.size());
subscriptions.position(completedFetch.partition, nextPosition);
positionAdvanced = true;
}

Long partitionLag = subscriptions.partitionLag(completedFetch.partition, isolationLevel);
Expand All @@ -753,7 +740,7 @@ private List<ConsumerRecord<K, V>> fetchRecords(CompletedFetch completedFetch, i
this.sensors.recordPartitionLead(completedFetch.partition, lead);
}

return partRecords;
return Fetch.forPartition(completedFetch.partition, partRecords, positionAdvanced);
} else {
// these records aren't next in line based on the last consumed position, ignore them
// they must be from an obsolete request
Expand All @@ -765,7 +752,7 @@ private List<ConsumerRecord<K, V>> fetchRecords(CompletedFetch completedFetch, i
log.trace("Draining fetched records for partition {}", completedFetch.partition);
completedFetch.drain();

return emptyList();
return Fetch.empty();
}

// Visible for testing
Expand Down
Loading