stevenzwu commented on a change in pull request #13574: URL: https://github.com/apache/flink/pull/13574#discussion_r502608464
########## File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/enumerator/subscriber/TopicListSubscriber.java ########## @@ -0,0 +1,67 @@ +/* + 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.flink.connector.kafka.source.enumerator.subscriber; + +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.PartitionInfo; +import org.apache.kafka.common.TopicPartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.apache.flink.connector.kafka.source.enumerator.subscriber.KafkaSubscriberUtils.maybeLog; +import static org.apache.flink.connector.kafka.source.enumerator.subscriber.KafkaSubscriberUtils.updatePartitionChanges; + +/** + * A subscriber to a fixed list of topics. + */ +class TopicListSubscriber implements KafkaSubscriber { + private static final long serialVersionUID = -6917603843104947866L; + private static final Logger LOG = LoggerFactory.getLogger(TopicListSubscriber.class); + private final List<String> topics; + + TopicListSubscriber(List<String> topics) { + this.topics = topics; + } + + @Override + public PartitionChange getPartitionChanges( + KafkaConsumer<?, ?> consumer, + Set<TopicPartition> currentAssignment) { + Set<TopicPartition> newPartitions = new HashSet<>(); + Set<TopicPartition> removedPartitions = new HashSet<>(currentAssignment); + + // We cannot call partitionsFor() directly on each topic because this may accidentally create the topic + // if the topic does not exist. In Kafka 2.3+ we can use the "allow.auto.create.topics" configuration on Review comment: Since TopicListSubscriber (in particular single topic) is probably the more common case, it will be great if we can optimize for the common case by using `partitionsFor()`. - avoid listing all topics, which is significant for large clusters with a lot of partitions. - Since this module already depends on `2.4.1`, can we set the `allow.auto.create.topics` config to false - plus user / kafka admin can disable auto creation with broker config `auto.create.topics.enable`. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org