chia7712 commented on code in PR #20383:
URL: https://github.com/apache/kafka/pull/20383#discussion_r3231669454


##########
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) {

Review Comment:
   What do you think about adding a `put(List)` variant? It could be a nice 
micro-optimization to avoid repeated lock acquisition.
   
   This could be addressed in next PR



-- 
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]

Reply via email to