This is an automated email from the ASF dual-hosted git repository.

dengziming pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 7ef5e8b022c MINOR: Avoid a couple of map copies in 
`KRaftMetadataCache.getPartitionReplicaEndpoints` (#14660)
7ef5e8b022c is described below

commit 7ef5e8b022ce27dd30012a3fdd4a12a076facade
Author: Ismael Juma <[email protected]>
AuthorDate: Tue Oct 31 19:34:30 2023 -0700

    MINOR: Avoid a couple of map copies in 
`KRaftMetadataCache.getPartitionReplicaEndpoints` (#14660)
    
    Neither the `toMap` or `filter` seem to be necessary.
    
    Reviewers: Ziming Deng<[email protected]>.
---
 core/src/main/scala/kafka/server/MetadataCache.scala   |  1 +
 .../kafka/server/metadata/KRaftMetadataCache.scala     | 18 +++++++++---------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/core/src/main/scala/kafka/server/MetadataCache.scala 
b/core/src/main/scala/kafka/server/MetadataCache.scala
index 2e60ce2ba7b..414e4fab3ca 100755
--- a/core/src/main/scala/kafka/server/MetadataCache.scala
+++ b/core/src/main/scala/kafka/server/MetadataCache.scala
@@ -25,6 +25,7 @@ import org.apache.kafka.common.{Cluster, Node, 
TopicPartition, Uuid}
 import org.apache.kafka.server.common.{Features, MetadataVersion}
 
 import java.util
+import scala.collection._
 
 /**
  * Used to represent the controller id cached in the metadata cache of the 
broker. This trait is
diff --git a/core/src/main/scala/kafka/server/metadata/KRaftMetadataCache.scala 
b/core/src/main/scala/kafka/server/metadata/KRaftMetadataCache.scala
index f2924ea2140..0d998ed84c5 100644
--- a/core/src/main/scala/kafka/server/metadata/KRaftMetadataCache.scala
+++ b/core/src/main/scala/kafka/server/metadata/KRaftMetadataCache.scala
@@ -39,7 +39,7 @@ import 
org.apache.kafka.common.message.{DescribeUserScramCredentialsRequestData,
 import org.apache.kafka.metadata.{PartitionRegistration, Replicas}
 import org.apache.kafka.server.common.{Features, MetadataVersion}
 
-import scala.collection.{Seq, Set, mutable}
+import scala.collection.{Map, Seq, Set, mutable}
 import scala.jdk.CollectionConverters._
 import scala.compat.java8.OptionConverters._
 
@@ -280,17 +280,17 @@ class KRaftMetadataCache(val brokerId: Int) extends 
MetadataCache with Logging w
     Option(image.topics().getTopic(tp.topic())).foreach { topic =>
       topic.partitions().values().forEach { partition =>
         partition.replicas.foreach { replicaId =>
-          result.put(replicaId, Option(image.cluster().broker(replicaId)) 
match {
-            case None => Node.noNode()
-            case Some(broker) if broker.fenced() => Node.noNode()
-            case Some(broker) => 
broker.node(listenerName.value()).asScala.getOrElse(Node.noNode())
-          })
+          val broker = image.cluster().broker(replicaId)
+          if (broker != null && !broker.fenced()) {
+            broker.node(listenerName.value).ifPresent { node =>
+              if (!node.isEmpty)
+                result.put(replicaId, node)
+            }
+          }
         }
       }
     }
-    result.toMap.filter(pair => pair match {
-      case (_, node) => !node.isEmpty
-    })
+    result
   }
 
   /**

Reply via email to