kirktrue commented on code in PR #14359:
URL: https://github.com/apache/kafka/pull/14359#discussion_r1323494877


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchCollector.java:
##########
@@ -0,0 +1,372 @@
+/*
+ * 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.ConsumerConfig;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.OffsetOutOfRangeException;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.TopicAuthorizationException;
+import org.apache.kafka.common.message.FetchResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.common.requests.FetchResponse;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.slf4j.Logger;
+
+import java.util.ArrayDeque;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+
+import static 
org.apache.kafka.clients.consumer.internals.AbstractFetch.requestMetadataUpdate;
+
+/**
+ * {@code FetchCollector} operates at the {@link RecordBatch} level, as that 
is what is stored in the
+ * {@link FetchBuffer}. Each {@link org.apache.kafka.common.record.Record} in 
the {@link RecordBatch} is converted
+ * to a {@link ConsumerRecord} and added to the returned {@link Fetch}.
+ *
+ * @param <K> Record key type
+ * @param <V> Record value type
+ */
+public class FetchCollector<K, V> {
+
+    private final Logger log;
+    private final ConsumerMetadata metadata;
+    private final SubscriptionState subscriptions;
+    private final FetchConfig<K, V> fetchConfig;
+    private final FetchMetricsManager metricsManager;
+    private final Time time;
+
+    public FetchCollector(final LogContext logContext,
+                          final ConsumerMetadata metadata,
+                          final SubscriptionState subscriptions,
+                          final FetchConfig<K, V> fetchConfig,
+                          final FetchMetricsManager metricsManager,
+                          final Time time) {
+        this.log = logContext.logger(FetchCollector.class);
+        this.metadata = metadata;
+        this.subscriptions = subscriptions;
+        this.fetchConfig = fetchConfig;
+        this.metricsManager = metricsManager;
+        this.time = time;
+    }
+
+    /**
+     * Return the fetched {@link ConsumerRecord records}, empty the {@link 
FetchBuffer record buffer}, and
+     * update the consumed position.
+     *
+     * </p>
+     *
+     * NOTE: returning an {@link Fetch#empty() empty} fetch guarantees the 
consumed position is not updated.
+     *
+     * @param fetchBuffer {@link FetchBuffer} from which to retrieve the 
{@link ConsumerRecord records}
+     *
+     * @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 Fetch<K, V> collectFetch(final FetchBuffer fetchBuffer) {
+        final Fetch<K, V> fetch = Fetch.empty();
+        final Queue<CompletedFetch> pausedCompletedFetches = new 
ArrayDeque<>();
+        int recordsRemaining = fetchConfig.maxPollRecords;
+
+        try {
+            while (recordsRemaining > 0) {
+                final CompletedFetch nextInLineFetch = 
fetchBuffer.nextInLineFetch();
+
+                if (nextInLineFetch == null || nextInLineFetch.isConsumed()) {
+                    final CompletedFetch records = fetchBuffer.peek();
+
+                    if (records == null)
+                        break;
+
+                    if (!records.isInitialized()) {
+                        try {
+                            
fetchBuffer.setNextInLineFetch(initialize(records));

Review Comment:
   I absolutely agree that it's awkward. This is essentially copy-and-paste 
from the existing implementation and I wanted to change things as little as 
possible. I'll take another look and see if I can make it more intuitive and 
easy to follow.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/FetchCollector.java:
##########
@@ -0,0 +1,372 @@
+/*
+ * 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.ConsumerConfig;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.OffsetOutOfRangeException;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.RecordTooLargeException;
+import org.apache.kafka.common.errors.TopicAuthorizationException;
+import org.apache.kafka.common.message.FetchResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.common.requests.FetchResponse;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.slf4j.Logger;
+
+import java.util.ArrayDeque;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+
+import static 
org.apache.kafka.clients.consumer.internals.AbstractFetch.requestMetadataUpdate;
+
+/**
+ * {@code FetchCollector} operates at the {@link RecordBatch} level, as that 
is what is stored in the
+ * {@link FetchBuffer}. Each {@link org.apache.kafka.common.record.Record} in 
the {@link RecordBatch} is converted
+ * to a {@link ConsumerRecord} and added to the returned {@link Fetch}.
+ *
+ * @param <K> Record key type
+ * @param <V> Record value type
+ */
+public class FetchCollector<K, V> {
+
+    private final Logger log;
+    private final ConsumerMetadata metadata;
+    private final SubscriptionState subscriptions;
+    private final FetchConfig<K, V> fetchConfig;
+    private final FetchMetricsManager metricsManager;
+    private final Time time;
+
+    public FetchCollector(final LogContext logContext,
+                          final ConsumerMetadata metadata,
+                          final SubscriptionState subscriptions,
+                          final FetchConfig<K, V> fetchConfig,
+                          final FetchMetricsManager metricsManager,
+                          final Time time) {
+        this.log = logContext.logger(FetchCollector.class);
+        this.metadata = metadata;
+        this.subscriptions = subscriptions;
+        this.fetchConfig = fetchConfig;
+        this.metricsManager = metricsManager;
+        this.time = time;
+    }
+
+    /**
+     * Return the fetched {@link ConsumerRecord records}, empty the {@link 
FetchBuffer record buffer}, and
+     * update the consumed position.
+     *
+     * </p>
+     *
+     * NOTE: returning an {@link Fetch#empty() empty} fetch guarantees the 
consumed position is not updated.
+     *
+     * @param fetchBuffer {@link FetchBuffer} from which to retrieve the 
{@link ConsumerRecord records}
+     *
+     * @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 Fetch<K, V> collectFetch(final FetchBuffer fetchBuffer) {
+        final Fetch<K, V> fetch = Fetch.empty();
+        final Queue<CompletedFetch> pausedCompletedFetches = new 
ArrayDeque<>();
+        int recordsRemaining = fetchConfig.maxPollRecords;
+
+        try {
+            while (recordsRemaining > 0) {
+                final CompletedFetch nextInLineFetch = 
fetchBuffer.nextInLineFetch();
+
+                if (nextInLineFetch == null || nextInLineFetch.isConsumed()) {
+                    final CompletedFetch records = fetchBuffer.peek();

Review Comment:
   Will do.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to