mumrah commented on a change in pull request #10018:
URL: https://github.com/apache/kafka/pull/10018#discussion_r568720185



##########
File path: core/src/main/scala/kafka/server/metadata/MetadataBrokers.scala
##########
@@ -0,0 +1,143 @@
+/**
+ * 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 kafka.server.metadata
+
+import java.util
+import java.util.Collections
+import java.util.concurrent.ThreadLocalRandom
+
+import kafka.cluster.BrokerEndPoint
+import kafka.common.BrokerEndPointNotAvailableException
+import org.apache.kafka.common.Node
+import org.apache.kafka.common.metadata.RegisterBrokerRecord
+import org.apache.kafka.common.network.ListenerName
+import org.slf4j.Logger
+
+import scala.jdk.CollectionConverters._
+
+object MetadataBroker {
+  def apply(record: RegisterBrokerRecord): MetadataBroker = {
+    new MetadataBroker(record.brokerId(), record.rack(),
+      record.endPoints().asScala.map {
+        case e => e.name() ->
+          new Node(record.brokerId(), e.host(), e.port(), record.rack())
+      }.toMap,
+      true)
+  }
+}
+
+case class MetadataBroker(id: Int,
+                          rack: String,
+                          endpoints: collection.Map[String, Node],
+                          fenced: Boolean) {
+  def brokerEndPoint(listenerName: ListenerName): BrokerEndPoint = {
+    endpoints.get(listenerName.value()) match {
+      case None => throw new BrokerEndPointNotAvailableException(
+        s"End point with listener name ${listenerName.value} not found for 
broker $id")
+      case Some(node) => new BrokerEndPoint(node.id(), node.host(), 
node.port())
+    }
+  }
+}
+
+class MetadataBrokersBuilder(log: Logger, prevBrokers: MetadataBrokers) {
+  private var newBrokerMap = prevBrokers.cloneBrokerMap()
+
+  def add(broker: MetadataBroker): Unit = {
+    newBrokerMap.put(broker.id, broker)
+  }
+
+  def changeFencing(id: Int, fenced: Boolean): Unit = {
+    val broker = newBrokerMap.get(id)
+    if (broker == null) {
+      throw new RuntimeException(s"Unknown broker id ${id}")
+    }
+    val newBroker = new MetadataBroker(broker.id, broker.rack, 
broker.endpoints, fenced)
+    newBrokerMap.put(id, newBroker)
+  }
+
+  def remove(id: Int): Unit = {
+    newBrokerMap.remove(id)
+  }
+
+  def get(brokerId: Int): Option[MetadataBroker] = 
Option(newBrokerMap.get(brokerId))
+
+  def build(): MetadataBrokers = {
+    val result = MetadataBrokers(log, newBrokerMap)
+    newBrokerMap = Collections.unmodifiableMap(newBrokerMap)
+    result
+  }
+}
+
+object MetadataBrokers {
+  def apply(log: Logger,
+            brokerMap: util.Map[Integer, MetadataBroker]): MetadataBrokers = {
+    var listenersIdenticalAcrossBrokers = true
+    var prevListeners: collection.Set[String] = null
+    val _aliveBrokers = new util.ArrayList[MetadataBroker](brokerMap.size())

Review comment:
       Looks like there's an `aliveBrokers` method defined below. 

##########
File path: core/src/main/scala/kafka/server/metadata/MetadataBrokers.scala
##########
@@ -0,0 +1,143 @@
+/**
+ * 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 kafka.server.metadata
+
+import java.util
+import java.util.Collections
+import java.util.concurrent.ThreadLocalRandom
+
+import kafka.cluster.BrokerEndPoint
+import kafka.common.BrokerEndPointNotAvailableException
+import org.apache.kafka.common.Node
+import org.apache.kafka.common.metadata.RegisterBrokerRecord
+import org.apache.kafka.common.network.ListenerName
+import org.slf4j.Logger
+
+import scala.jdk.CollectionConverters._
+
+object MetadataBroker {
+  def apply(record: RegisterBrokerRecord): MetadataBroker = {
+    new MetadataBroker(record.brokerId(), record.rack(),
+      record.endPoints().asScala.map {
+        case e => e.name() ->
+          new Node(record.brokerId(), e.host(), e.port(), record.rack())
+      }.toMap,
+      true)
+  }
+}
+
+case class MetadataBroker(id: Int,
+                          rack: String,
+                          endpoints: collection.Map[String, Node],
+                          fenced: Boolean) {
+  def brokerEndPoint(listenerName: ListenerName): BrokerEndPoint = {
+    endpoints.get(listenerName.value()) match {
+      case None => throw new BrokerEndPointNotAvailableException(
+        s"End point with listener name ${listenerName.value} not found for 
broker $id")
+      case Some(node) => new BrokerEndPoint(node.id(), node.host(), 
node.port())
+    }
+  }
+}
+
+class MetadataBrokersBuilder(log: Logger, prevBrokers: MetadataBrokers) {
+  private var newBrokerMap = prevBrokers.cloneBrokerMap()
+
+  def add(broker: MetadataBroker): Unit = {
+    newBrokerMap.put(broker.id, broker)
+  }
+
+  def changeFencing(id: Int, fenced: Boolean): Unit = {
+    val broker = newBrokerMap.get(id)
+    if (broker == null) {
+      throw new RuntimeException(s"Unknown broker id ${id}")
+    }
+    val newBroker = new MetadataBroker(broker.id, broker.rack, 
broker.endpoints, fenced)
+    newBrokerMap.put(id, newBroker)
+  }
+
+  def remove(id: Int): Unit = {
+    newBrokerMap.remove(id)
+  }
+
+  def get(brokerId: Int): Option[MetadataBroker] = 
Option(newBrokerMap.get(brokerId))
+
+  def build(): MetadataBrokers = {
+    val result = MetadataBrokers(log, newBrokerMap)
+    newBrokerMap = Collections.unmodifiableMap(newBrokerMap)
+    result
+  }
+}
+
+object MetadataBrokers {
+  def apply(log: Logger,
+            brokerMap: util.Map[Integer, MetadataBroker]): MetadataBrokers = {
+    var listenersIdenticalAcrossBrokers = true
+    var prevListeners: collection.Set[String] = null
+    val _aliveBrokers = new util.ArrayList[MetadataBroker](brokerMap.size())
+    brokerMap.values().iterator().asScala.foreach {
+      case broker => if (!broker.fenced) {
+        if (prevListeners == null) {
+          prevListeners = broker.endpoints.keySet
+        } else if (!prevListeners.equals(broker.endpoints.keySet)) {
+          listenersIdenticalAcrossBrokers = false
+        }
+        _aliveBrokers.add(broker)
+      }
+    }
+    if (!listenersIdenticalAcrossBrokers) {
+      log.error("Listeners are not identical across alive brokers. " +
+        _aliveBrokers.asScala.map(
+          broker => s"${broker.id}: ${broker.endpoints.keySet.mkString(", 
")}"))
+    }
+    new MetadataBrokers(_aliveBrokers, brokerMap)
+  }
+}
+
+case class MetadataBrokers(private val _aliveBrokers: 
util.List[MetadataBroker],
+                           private val brokerMap: util.Map[Integer, 
MetadataBroker]) {
+  def size(): Int = brokerMap.size()
+
+  def iterator(): Iterator[MetadataBroker] = 
brokerMap.values().iterator().asScala
+
+  def cloneBrokerMap(): util.Map[Integer, MetadataBroker] = {
+    val result = new util.HashMap[Integer, MetadataBroker]
+    result.putAll(brokerMap)
+    result
+  }
+
+  def getAlive(id: Int): Option[MetadataBroker] = {
+    val broker = get(id)

Review comment:
       How about simply `get(id).filter(!_.fenced)` ?

##########
File path: core/src/main/scala/kafka/server/metadata/MetadataPartitions.scala
##########
@@ -0,0 +1,282 @@
+/**
+ * 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 kafka.server.metadata
+
+import java.util
+import java.util.Collections
+import java.util.function.BiConsumer
+
+import org.apache.kafka.common.message.LeaderAndIsrRequestData
+import 
org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState
+import 
org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataPartitionState
+import org.apache.kafka.common.metadata.{IsrChangeRecord, PartitionRecord}
+import org.apache.kafka.common.{TopicPartition, Uuid}
+
+import scala.jdk.CollectionConverters._
+
+
+object MetadataPartition {
+  def apply(name: String, record: PartitionRecord): MetadataPartition = {
+    MetadataPartition(name,
+      record.partitionId(),
+      record.leader(),
+      record.leaderEpoch(),
+      record.replicas(),
+      record.isr(),
+      Collections.emptyList(), // TODO: handle offline replicas
+      Collections.emptyList(),
+      Collections.emptyList())
+  }
+
+  def apply(prevPartition: Option[MetadataPartition],
+            partition: UpdateMetadataPartitionState): MetadataPartition = {
+    new MetadataPartition(partition.topicName(),
+      partition.partitionIndex(),
+      partition.leader(),
+      partition.leaderEpoch(),
+      partition.replicas(),
+      partition.isr(),
+      partition.offlineReplicas(),
+      prevPartition.flatMap(p => 
Some(p.addingReplicas)).getOrElse(Collections.emptyList()),
+      prevPartition.flatMap(p => 
Some(p.removingReplicas)).getOrElse(Collections.emptyList())
+    )
+  }
+}
+
+case class MetadataPartition(topicName: String,
+                             partitionIndex: Int,
+                             leaderId: Int,
+                             leaderEpoch: Int,
+                             replicas: util.List[Integer],
+                             isr: util.List[Integer],
+                             offlineReplicas: util.List[Integer],
+                             addingReplicas: util.List[Integer],
+                             removingReplicas: util.List[Integer]) {
+  def toTopicPartition(): TopicPartition = new TopicPartition(topicName, 
partitionIndex)
+
+  def toLeaderAndIsrPartitionState(isNew: Boolean): 
LeaderAndIsrRequestData.LeaderAndIsrPartitionState = {
+    new LeaderAndIsrPartitionState().setTopicName(topicName).
+      setPartitionIndex(partitionIndex).
+      setLeader(leaderId).
+      setLeaderEpoch(leaderEpoch).
+      setReplicas(replicas).
+      setIsr(isr).
+      setAddingReplicas(addingReplicas).
+      setRemovingReplicas(removingReplicas).
+      setIsNew(isNew)
+    // Note: we don't set ZKVersion here.
+  }
+
+  def isReplicaFor(brokerId: Int): Boolean = 
replicas.contains(Integer.valueOf(brokerId))
+
+  def copyWithIsrChanges(record: IsrChangeRecord): MetadataPartition = {
+    MetadataPartition(topicName,
+      partitionIndex,
+      record.leader(),
+      record.leaderEpoch(),
+      replicas,
+      record.isr(),
+      offlineReplicas,
+      addingReplicas,
+      removingReplicas)
+  }
+}
+
+class MetadataPartitionsBuilder(val brokerId: Int,
+                                val prevPartitions: MetadataPartitions) {
+  private var newNameMap = prevPartitions.copyNameMap()
+  private var newIdMap = prevPartitions.copyIdMap()
+  private val changed = new util.IdentityHashMap[Any, Boolean]()
+  private val _localChanged = new util.HashSet[MetadataPartition]

Review comment:
       Seems to be due to methods with the same name defined on the class




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to