philipnee commented on code in PR #13265:
URL: https://github.com/apache/kafka/pull/13265#discussion_r1114851962


##########
clients/src/main/java/org/apache/kafka/clients/consumer/StubbedAsyncKafkaConsumer.java:
##########
@@ -0,0 +1,548 @@
+/*
+ * 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;
+
+import org.apache.kafka.clients.consumer.internals.Fetcher;
+import org.apache.kafka.clients.consumer.internals.SubscriptionState;
+import 
org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener;
+import 
org.apache.kafka.clients.consumer.internals.events.ApplicationEventProcessor;
+import 
org.apache.kafka.clients.consumer.internals.events.AssignPartitionsEvent;
+import org.apache.kafka.clients.consumer.internals.events.CommitAsyncEvent;
+import org.apache.kafka.clients.consumer.internals.events.CommitSyncEvent;
+import org.apache.kafka.clients.consumer.internals.events.EventHandler;
+import 
org.apache.kafka.clients.consumer.internals.events.FetchCommittedOffsetsEvent;
+import org.apache.kafka.clients.consumer.internals.events.FetchOffsetsEvent;
+import org.apache.kafka.clients.consumer.internals.events.FetchRecordsEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.RequestRebalanceEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.SubscribePatternEvent;
+import org.apache.kafka.clients.consumer.internals.events.SubscribeTopicsEvent;
+import org.apache.kafka.clients.consumer.internals.events.UnsubscribeEvent;
+import org.apache.kafka.common.Metric;
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.PartitionInfo;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.InvalidGroupIdException;
+import org.apache.kafka.common.errors.RecordDeserializationException;
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.record.Record;
+import org.apache.kafka.common.record.TimestampType;
+import org.apache.kafka.common.requests.ListOffsetsRequest;
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Timer;
+import org.apache.kafka.common.utils.Utils;
+
+import java.nio.ByteBuffer;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalLong;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * This {@link StubbedAsyncKafkaConsumer stub} implementation of {@link 
Consumer} serves as a <i>wireframe</i> or
+ * <i>sketch</i>, showing the interaction between the foreground and 
background threads. Each of the main API methods
+ * will need to answer the following questions:
+ *
+ * <ol>
+ *     <li>Does this method block?</li>
+ *     <li>Does this method interact with the background thread?</li>
+ *     <li>If yes, what data is passed as input to the background thread?</li>
+ *     <li>If yes, what data is returned as output from the background 
thread?</li>
+ * </ol>
+ *
+ * @param <K> Key
+ * @param <V> Value
+ * @see ApplicationEventProcessor for the logic of the background event handler
+ */
+public class StubbedAsyncKafkaConsumer<K, V> implements Consumer<K, V> {
+
+    /**
+     * These instance variables are intentionally left unassigned, to avoid 
clutter...
+     */
+    private Time time;
+
+    private EventHandler eventHandler;
+
+    private SubscriptionState subscriptions;
+
+    private Deserializer<K> keyDeserializer;
+
+    private Deserializer<V> valueDeserializer;
+
+    private long defaultApiTimeoutMs;
+
+    private List<ConsumerPartitionAssignor> assignors;
+
+    private Optional<String> groupId;
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>No</li>
+     *     <li>No</li>
+     *     <li><i>n/a</i></li>
+     *     <li><i>n/a</i></li>
+     * </ol>
+     */
+    @Override
+    public Set<TopicPartition> assignment() {
+        return Collections.unmodifiableSet(subscriptions.assignedPartitions());
+    }
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>No</li>
+     *     <li>No</li>
+     *     <li><i>n/a</i></li>
+     *     <li><i>n/a</i></li>
+     * </ol>
+     */
+    @Override
+    public Set<String> subscription() {
+        return Collections.unmodifiableSet(subscriptions.subscription());
+    }
+
+    /**
+     * @see #subscribe(Collection, ConsumerRebalanceListener)
+     */
+    @Override
+    public void subscribe(Collection<String> topics) {
+        subscribe(topics, new NoOpConsumerRebalanceListener());
+    }
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>No</li>
+     *     <li>Yes, except in cases of bad input data</li>
+     *     <li>Topic list and {@link ConsumerRebalanceListener}</li>
+     *     <li><i>n/a</i></li>
+     * </ol>
+     */
+    @Override
+    public void subscribe(Collection<String> topics, ConsumerRebalanceListener 
callback) {
+        maybeThrowInvalidGroupIdException();
+        if (topics == null)
+            throw new IllegalArgumentException("Topic collection to subscribe 
to cannot be null");
+        if (topics.isEmpty()) {
+            // treat subscribing to empty topic list as the same as 
unsubscribing
+            this.unsubscribe();
+        } else {
+            for (String topic : topics) {
+                if (Utils.isBlank(topic))
+                    throw new IllegalArgumentException("Topic collection to 
subscribe to cannot contain null or empty topic");
+            }
+
+            throwIfNoAssignorsConfigured();
+            eventHandler.add(new SubscribeTopicsEvent(topics, callback));
+        }
+    }
+
+    /**
+     * @see #subscribe(Pattern, ConsumerRebalanceListener)
+     */    @Override
+    public void subscribe(Pattern pattern) {
+        subscribe(pattern, new NoOpConsumerRebalanceListener());
+    }
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>No</li>
+     *     <li>Yes, except in cases of bad input data</li>
+     *     <li>{@link Pattern} and {@link ConsumerRebalanceListener}</li>
+     *     <li><i>n/a</i></li>
+     * </ol>
+     */
+    @Override
+    public void subscribe(Pattern pattern, ConsumerRebalanceListener callback) 
{
+        maybeThrowInvalidGroupIdException();
+        if (pattern == null || pattern.toString().equals(""))
+            throw new IllegalArgumentException("Topic pattern to subscribe to 
cannot be " + (pattern == null ?
+                    "null" : "empty"));
+
+        throwIfNoAssignorsConfigured();
+        eventHandler.add(new SubscribePatternEvent(pattern, callback));
+    }
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>No</li>
+     *     <li>Yes</li>
+     *     <li>None</li>
+     *     <li>None</li>
+     * </ol>
+     */
+    @Override
+    public void unsubscribe() {
+        eventHandler.add(new UnsubscribeEvent());
+    }
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>No</li>
+     *     <li>Yes</li>
+     *     <li>List of {@link TopicPartition partitions}</li>
+     *     <li>None</li>
+     * </ol>
+     */
+    @Override
+    public void assign(Collection<TopicPartition> partitions) {
+        if (partitions == null) {
+            throw new IllegalArgumentException("Topic partition collection to 
assign to cannot be null");
+        } else if (partitions.isEmpty()) {
+            this.unsubscribe();
+        } else {
+            for (TopicPartition tp : partitions) {
+                String topic = (tp != null) ? tp.topic() : null;
+                if (Utils.isBlank(topic))
+                    throw new IllegalArgumentException("Topic partitions to 
assign to cannot have null or empty topic");
+            }
+
+            eventHandler.add(new AssignPartitionsEvent(partitions));
+        }
+    }
+
+    @Deprecated
+    @Override
+    public ConsumerRecords<K, V> poll(final long timeoutMs) {
+        return poll(time.timer(timeoutMs), false);
+    }
+
+    @Override
+    public ConsumerRecords<K, V> poll(Duration timeout) {
+        return poll(time.timer(timeout), true);
+    }
+
+    /**
+     * Answers to the above questions:
+     *
+     * <ol>
+     *     <li>Yes</li>
+     *     <li>Yes</li>
+     *     <li>List of {@link TopicPartition partitions}</li>
+     *     <li>None</li>
+     * </ol>
+     */
+    private ConsumerRecords<K, V> poll(final Timer timer, final boolean 
includeMetadataInTimeout) {
+        Map<TopicPartition, List<ConsumerRecord<K, V>>> records = new 
HashMap<>();
+
+        // The background thread will do the following when it receives this 
event:
+        //
+        // 1. Execute the 'update assignment metadata' logic (which I don't 
really understand at all, yet...)

Review Comment:
   sorry, i think i'm not getting this part `for the former, the source of 
truth is on the background thread` (I assume you mean by subscription).  The 
current expectation is, as previously discussed, subscribe() -> subscriptions() 
should yield the most up to date subscription.  Changing the ownership will 
break this assumption no?



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to