chia7712 commented on code in PR #20383: URL: https://github.com/apache/kafka/pull/20383#discussion_r3231694463
########## server/src/main/java/org/apache/kafka/server/DefaultAutoTopicCreationManager.java: ########## @@ -0,0 +1,348 @@ +/* + * 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.server; + +import org.apache.kafka.common.errors.AuthenticationException; +import org.apache.kafka.common.errors.InvalidTopicException; +import org.apache.kafka.common.errors.TimeoutException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.internals.Topic; +import org.apache.kafka.common.message.CreateTopicsRequestData; +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic; +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicConfig; +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicConfigCollection; +import org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.CreateTopicsRequest; +import org.apache.kafka.common.requests.CreateTopicsResponse; +import org.apache.kafka.common.requests.RequestContext; +import org.apache.kafka.common.utils.Time; +import org.apache.kafka.coordinator.group.GroupCoordinatorConfig; +import org.apache.kafka.coordinator.share.ShareCoordinatorConfig; +import org.apache.kafka.coordinator.transaction.TransactionLogConfig; +import org.apache.kafka.server.config.AbstractKafkaConfig; +import org.apache.kafka.server.config.ReplicationConfigs; +import org.apache.kafka.server.config.ServerLogConfigs; +import org.apache.kafka.server.quota.ControllerMutationQuota; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; +import java.util.stream.Stream; + +public class DefaultAutoTopicCreationManager implements AutoTopicCreationManager { + + private static final int DEFAULT_TOPIC_ERROR_CACHE_CAPACITY = 1000; + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAutoTopicCreationManager.class); + + private final AbstractKafkaConfig config; + private final TopicCreator topicCreator; + private final Supplier<Properties> groupCoordinator; + private final Supplier<Properties> shareCoordinator; + private final Supplier<Properties> transactionTopicConfigsSupplier; Review Comment: Would you mind aligning the naming for the consistency? ########## server/src/main/java/org/apache/kafka/server/DefaultAutoTopicCreationManager.java: ########## @@ -0,0 +1,348 @@ +/* + * 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.server; + +import org.apache.kafka.common.errors.AuthenticationException; +import org.apache.kafka.common.errors.InvalidTopicException; +import org.apache.kafka.common.errors.TimeoutException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.internals.Topic; +import org.apache.kafka.common.message.CreateTopicsRequestData; +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic; +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicConfig; +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicConfigCollection; +import org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.CreateTopicsRequest; +import org.apache.kafka.common.requests.CreateTopicsResponse; +import org.apache.kafka.common.requests.RequestContext; +import org.apache.kafka.common.utils.Time; +import org.apache.kafka.coordinator.group.GroupCoordinatorConfig; +import org.apache.kafka.coordinator.share.ShareCoordinatorConfig; +import org.apache.kafka.coordinator.transaction.TransactionLogConfig; +import org.apache.kafka.server.config.AbstractKafkaConfig; +import org.apache.kafka.server.config.ReplicationConfigs; +import org.apache.kafka.server.config.ServerLogConfigs; +import org.apache.kafka.server.quota.ControllerMutationQuota; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; +import java.util.stream.Stream; + +public class DefaultAutoTopicCreationManager implements AutoTopicCreationManager { + + private static final int DEFAULT_TOPIC_ERROR_CACHE_CAPACITY = 1000; + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAutoTopicCreationManager.class); + + private final AbstractKafkaConfig config; + private final TopicCreator topicCreator; + private final Supplier<Properties> groupCoordinator; + private final Supplier<Properties> shareCoordinator; + private final Supplier<Properties> transactionTopicConfigsSupplier; + private final Time time; + private final Set<String> inflightTopics = ConcurrentHashMap.newKeySet(); + private final ExpiringErrorCache topicCreationErrorCache; + + public DefaultAutoTopicCreationManager( + AbstractKafkaConfig config, + Supplier<Properties> groupCoordinatorConfigsSupplier, + Supplier<Properties> transactionTopicConfigsSupplier, + Supplier<Properties> shareCoordinatorConfigsSupplier, + TopicCreator topicCreator, + Time time + ) { + this( + config, + groupCoordinatorConfigsSupplier, + transactionTopicConfigsSupplier, + shareCoordinatorConfigsSupplier, + topicCreator, + time, + // Hardcoded default capacity; can be overridden in tests via constructor param + DEFAULT_TOPIC_ERROR_CACHE_CAPACITY + ); + } + + // VisibleForTesting + DefaultAutoTopicCreationManager( + AbstractKafkaConfig config, + Supplier<Properties> groupCoordinatorConfigsSupplier, + Supplier<Properties> transactionTopicConfigsSupplier, + Supplier<Properties> shareCoordinatorConfigsSupplier, + TopicCreator topicCreator, + Time time, + int topicErrorCacheCapacity + ) { + this.config = config; + this.groupCoordinator = groupCoordinatorConfigsSupplier; + this.shareCoordinator = shareCoordinatorConfigsSupplier; + this.transactionTopicConfigsSupplier = transactionTopicConfigsSupplier; + this.time = time; + this.topicCreator = topicCreator; + this.topicCreationErrorCache = new ExpiringErrorCache(topicErrorCacheCapacity, time); + } + + @Override + public List<MetadataResponseTopic> createTopics( + Set<String> topics, + ControllerMutationQuota controllerMutationQuota, + RequestContext metadataRequestContext + ) { + var creatableTopics = new HashMap<String, CreatableTopic>(); + var uncreatableTopicResponses = new ArrayList<MetadataResponseTopic>(); + topics.forEach(topic -> { + // Attempt basic topic validation before sending any requests to the controller. + Optional<Errors> validationError = Optional.empty(); + if (!isValidTopicName(topic)) { + validationError = Optional.of(Errors.INVALID_TOPIC_EXCEPTION); + } else if (!inflightTopics.add(topic)) { + validationError = Optional.of(Errors.UNKNOWN_TOPIC_OR_PARTITION); + } + + validationError.ifPresentOrElse( Review Comment: Maybe we could streamline those code ```java if (!isValidTopicName(topic)) { uncreatableTopicResponses.add(new MetadataResponseTopic() .setErrorCode(Errors.INVALID_TOPIC_EXCEPTION.code()) .setName(topic) .setIsInternal(Topic.isInternal(topic))); } else if (!inflightTopics.add(topic)) { uncreatableTopicResponses.add(new MetadataResponseTopic() .setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code()) .setName(topic) .setIsInternal(Topic.isInternal(topic))); } else { creatableTopics.put(topic, creatableTopic(topic)); } ``` -- 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]
