chia7712 commented on code in PR #20383: URL: https://github.com/apache/kafka/pull/20383#discussion_r3279890642
########## server/src/main/java/org/apache/kafka/server/ExpiringErrorCache.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.utils.Time; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; + +/** + * Thread-safe cache that stores topic creation errors with per-entry expiration. + * - Expiration: maintained by a min-heap (priority queue) on expiration time + * - Capacity: enforced by evicting entries with earliest expiration time (not LRU) + * - Updates: old entries remain in queue but are ignored via reference equality check + */ +class ExpiringErrorCache { + + private record Entry(String topicName, String errorMessage, long expirationTimeMs) { + } + + private final int maxSize; + private final Time time; + private final ConcurrentHashMap<String, Entry> byTopic = new ConcurrentHashMap<>(); + private final PriorityQueue<Entry> expiryQueue = + new PriorityQueue<>(11, Comparator.comparingLong(e -> e.expirationTimeMs)); + private final ReentrantLock lock = new ReentrantLock(); + + ExpiringErrorCache(int maxSize, Time time) { + this.maxSize = maxSize; + this.time = time; + } + + void put(String topicName, String errorMessage, long ttlMs) { + lock.lock(); + try { + var currentTimeMs = time.milliseconds(); + var expirationTimeMs = currentTimeMs + ttlMs; + var entry = new Entry(topicName, errorMessage, expirationTimeMs); + byTopic.put(topicName, entry); + expiryQueue.add(entry); + + // Clean up expired entries and enforce capacity + while (!expiryQueue.isEmpty() && + (expiryQueue.peek().expirationTimeMs <= currentTimeMs || byTopic.size() > maxSize)) { + var evicted = expiryQueue.poll(); + var current = byTopic.get(evicted.topicName); + if (current != null && current == evicted) { + byTopic.remove(evicted.topicName); + } + } + } finally { + lock.unlock(); + } + } + + boolean hasError(String topicName, long currentTimeMs) { + var entry = byTopic.get(topicName); + return entry != null && entry.expirationTimeMs > currentTimeMs; + } + + Map<String, String> getErrorsForTopics(Set<String> topicNames, long currentTimeMs) { + var result = new HashMap<String, String>(); + for (var topicName : topicNames) { + var entry = byTopic.get(topicName); + if (entry != null && entry.expirationTimeMs > currentTimeMs) { + result.put(topicName, entry.errorMessage); + } + } + return result; + } + + void clear() { Review Comment: It is used to clear the queues, but the only use is during server shutdown. It looks like defensive code. Maybe we could consider removing it in a follow-up PR ########## server/src/main/java/org/apache/kafka/server/DefaultAutoTopicCreationManager.java: ########## @@ -0,0 +1,347 @@ +/* + * 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> groupCoordinatorConfigsSupplier; Review Comment: We should avoid using Properties in this modern world. `Map<String, String>` is way better. This can be addressed (or discussed) in a follow-up -- 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]
