philipnee commented on code in PR #13265: URL: https://github.com/apache/kafka/pull/13265#discussion_r1109286591
########## 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"); + } + Review Comment: we still need subscriptions.subscribe 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