chia7712 commented on code in PR #20383: URL: https://github.com/apache/kafka/pull/20383#discussion_r3203777746
########## server/src/main/java/org/apache/kafka/server/AutoTopicCreationManager.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.message.CreateTopicsRequestData.CreatableTopic; +import org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic; +import org.apache.kafka.common.requests.RequestContext; +import org.apache.kafka.server.quota.ControllerMutationQuota; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +public interface AutoTopicCreationManager { + + /** + * Initiate auto topic creation for the given topics. + * + * @param topics the topics to create + * @param controllerMutationQuota the controller mutation quota for topic creation + * @param metadataRequestContext defined when creating topics on behalf of the client. The goal here is to preserve + * original client principal for auditing, thus needing to wrap a plain CreateTopicsRequest + * inside Envelope to send to the controller when forwarding is enabled. + * @return auto created topic metadata responses + */ + List<MetadataResponseTopic> createTopics(Set<String> topics, ControllerMutationQuota controllerMutationQuota, Optional<RequestContext> metadataRequestContext); Review Comment: What do you think about using overloading to avoid passing Optional as an argument? ```java List<MetadataResponseTopic> createTopics(Set<String> topics, ControllerMutationQuota controllerMutationQuota, RequestContext metadataRequestContext); List<MetadataResponseTopic> createTopics(Set<String> topics, ControllerMutationQuota controllerMutationQuota); ``` ########## core/src/main/scala/kafka/server/KafkaApis.scala: ########## @@ -836,23 +836,27 @@ class KafkaApis(val requestChannel: RequestChannel, request: RequestChannel.Request, fetchAllTopics: Boolean, allowAutoTopicCreation: Boolean, - topics: Set[String], + topics: util.Set[String], listenerName: ListenerName, errorUnavailableEndpoints: Boolean, errorUnavailableListeners: Boolean ): Seq[MetadataResponseTopic] = { - val topicResponses = metadataCache.getTopicMetadata(topics.asJava, listenerName, + val topicResponses = metadataCache.getTopicMetadata(topics, listenerName, errorUnavailableEndpoints, errorUnavailableListeners) if (topics.isEmpty || topicResponses.size == topics.size || fetchAllTopics) { topicResponses.asScala } else { - val nonExistingTopics = topics.diff(topicResponses.asScala.map(_.name).toSet) + val existingTopics = topicResponses.stream().map(topic => topic.name).collect(Collectors.toSet()) + val nonExistingTopics = topics.stream(). Review Comment: ```scala val nonExistingTopics = new util.HashSet[String](topics) nonExistingTopics.removeAll(existingTopics) ``` ########## 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( Review Comment: ```java this( config, groupCoordinatorConfigsSupplier, transactionTopicConfigsSupplier, shareCoordinatorConfigsSupplier, topicCreator, time, // Hardcoded default capacity; can be overridden in tests via constructor param DEFAULT_TOPIC_ERROR_CACHE_CAPACITY ); ``` ########## core/src/main/scala/kafka/server/BrokerServer.scala: ########## @@ -405,7 +405,13 @@ class BrokerServer( val topicCreator = new KRaftTopicCreator(clientToControllerChannelManager) Review Comment: ```scala autoTopicCreationManager = new DefaultAutoTopicCreationManager( config, () => groupCoordinator.groupMetadataTopicConfigs, () => transactionCoordinator.transactionTopicConfigs, () => shareCoordinator.shareGroupStateTopicConfigs, new KRaftTopicCreator(clientToControllerChannelManager), time, ) ``` -- 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]
