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


##########
server/src/main/java/org/apache/kafka/server/DefaultAutoTopicCreationManager.java:
##########
@@ -321,25 +321,19 @@ private void sendCreateTopicRequestWithErrorCaching(
     }
 
     private void cacheTopicCreationErrors(Set<String> topicNames, String 
errorMessage, long ttlMs) {
-        for (String topicName : topicNames) {
-            topicCreationErrorCache.put(topicName, errorMessage, ttlMs);
-        }
+        topicCreationErrorCache.put(topicNames.stream()
+            .collect(Collectors.toMap(topicName -> topicName, topicName -> 
errorMessage)), ttlMs);
     }
 
     private void cacheTopicCreationErrorsFromResponse(CreateTopicsResponse 
response, long ttlMs) {
-        response.data().topics().forEach(topicResult -> {
-            if (topicResult.errorCode() != Errors.NONE.code()) {
+        topicCreationErrorCache.put(response.data().topics().stream()
+            .filter(topicResult -> topicResult.errorCode() != 
Errors.NONE.code())
+            
.collect(Collectors.toMap(CreateTopicsResponseData.CreatableTopicResult::name, 
topicResult -> {

Review Comment:
   Oops, my previous comment was incorrect.
   
   Another approach is to provide two variants of the `put` method in 
`ExpiringErrorCache`. For example:
   ```java
       void put(Set<String> topicNames, String errorMessage, long ttlMs) {
           lock.lock();
           try {
               var currentTimeMs = time.milliseconds();
               var expirationTimeMs = currentTimeMs + ttlMs;
               topicNames.forEach(topicName -> {
                   var entry = new Entry(topicName, errorMessage, 
expirationTimeMs);
                   byTopic.put(topicName, entry);
                   expiryQueue.add(entry);
               });
               evictExpiredOrOverCapacity(currentTimeMs);
           } finally {
               lock.unlock();
           }
       }
   ```
   Since `ExpiringErrorCache` is only used by 
`DefaultAutoTopicCreationManager`, we can safely add specific methods tailored 
for it.



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