Efrat19 commented on code in PR #287: URL: https://github.com/apache/flink-connector-kafka/pull/287#discussion_r3698712743
########## flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/source/enumerator/metadata/TopicIntegrityProvider.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.metadata; + +import org.apache.flink.connector.kafka.util.AdminUtils; +import org.apache.flink.util.ExceptionUtils; + +import org.apache.kafka.clients.admin.AdminClient; +import org.apache.kafka.clients.admin.TopicDescription; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.UnknownTopicOrPartitionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Provider of topic integrity related functionalities for {@link + * org.apache.flink.connector.kafka.source.enumerator.KafkaSourceEnumerator}. + */ +public class TopicIntegrityProvider implements TopicMetadataProvider { + + private static final Logger LOG = LoggerFactory.getLogger(TopicIntegrityProvider.class); + private final Map<String, String> trackedTopicIdsByName; + + public TopicIntegrityProvider(Map<String, String> trackedTopicIdsByNameFromContext) { + trackedTopicIdsByName = new ConcurrentHashMap<>(trackedTopicIdsByNameFromContext); + } + + @Override + public Map<String, TopicDescription> getTopicMetadata( + AdminClient adminClient, Pattern pattern) { + final Collection<String> topicsToVerifyInPatternMode = + trackedTopicIdsByName.keySet().stream() + .filter(pattern.asPredicate()) + .collect(Collectors.toCollection(HashSet::new)); + topicsToVerifyInPatternMode.addAll(AdminUtils.getTopicsByPattern(adminClient, pattern)); + return getTopicMetadata(adminClient, topicsToVerifyInPatternMode); + } + + @Override + public Map<String, TopicDescription> getTopicMetadata( + AdminClient adminClient, Collection<String> subscribedTopicNames) { + Map<String, TopicDescription> topicMetadata; + try { + topicMetadata = AdminUtils.getTopicMetadata(adminClient, subscribedTopicNames); + failIfRecreated(subscribedTopicNames, topicMetadata); + } catch (RuntimeException original) { + if (ExceptionUtils.findThrowable(original, UnknownTopicOrPartitionException.class) + .isPresent()) { + // UnknownTopicOrPartitionException can be transient due to broker timeout + // or permanent due to topic/partition loss. + // Determine if the exception is caused by a missing topic + // and if yes, trigger a TopicIntegrity failure instead + try { + failIfMissing(subscribedTopicNames, adminClient.listTopics().names().get()); + } catch (TopicIntegrityException missingTopicException) { + throw missingTopicException; + } catch (Exception ignored) { + // ignored so we fallback to the original error + } + } + throw original; + } + refreshTrackedTopicIds(subscribedTopicNames, topicMetadata); + return topicMetadata; + } + + private void refreshTrackedTopicIds( + Collection<String> subscribedTopicNames, Map<String, TopicDescription> topicMetadata) { + + // Add new subscribed topic to trackedTopicIdsByName + for (String subscribedTopicName : subscribedTopicNames) { + if (!trackedTopicIdsByName.keySet().contains(subscribedTopicName)) { + trackedTopicIdsByName.put( Review Comment: I didn't really think about it as it wouldn't make sense to enable the feature if your broker doesn't support topicIds But added the check anyway, thanks -- 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]
