You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/02/02 16:00:21 UTC

[GitHub] [kafka] mumrah commented on a change in pull request #10018: Upstream MetadataImage and related classes

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. 




----------------------------------------------------------------
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:
users@infra.apache.org