You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by da...@apache.org on 2021/07/20 18:39:44 UTC

[kafka] branch 3.0 updated: MINOR: Handle some null cases in BrokerMetadataPublisher (#11029)

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

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


The following commit(s) were added to refs/heads/3.0 by this push:
     new 89c9c94  MINOR: Handle some null cases in BrokerMetadataPublisher (#11029)
89c9c94 is described below

commit 89c9c94f87791cc8349a3d3dec7029b36003574c
Author: David Arthur <mu...@gmail.com>
AuthorDate: Tue Jul 20 14:10:14 2021 -0400

    MINOR: Handle some null cases in BrokerMetadataPublisher (#11029)
    
    Reviewers: Chia-Ping Tsai <ch...@gmail.com>, Colin P. McCabe <cm...@apache.org>
---
 .../server/metadata/BrokerMetadataPublisher.scala  |  6 ++--
 .../metadata/BrokerMetadataPublisherTest.scala     | 42 ++++++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/core/src/main/scala/kafka/server/metadata/BrokerMetadataPublisher.scala b/core/src/main/scala/kafka/server/metadata/BrokerMetadataPublisher.scala
index b270bb9..0389d67 100644
--- a/core/src/main/scala/kafka/server/metadata/BrokerMetadataPublisher.scala
+++ b/core/src/main/scala/kafka/server/metadata/BrokerMetadataPublisher.scala
@@ -46,8 +46,10 @@ object BrokerMetadataPublisher {
   def getTopicDelta(topicName: String,
                     newImage: MetadataImage,
                     delta: MetadataDelta): Option[TopicDelta] = {
-    Option(newImage.topics().getTopic(topicName)).map {
-      topicImage => delta.topicsDelta().changedTopic(topicImage.id())
+    Option(newImage.topics().getTopic(topicName)).flatMap {
+      topicImage => Option(delta.topicsDelta()).flatMap {
+        topicDelta => Option(topicDelta.changedTopic(topicImage.id()))
+      }
     }
   }
 
diff --git a/core/src/test/scala/unit/kafka/server/metadata/BrokerMetadataPublisherTest.scala b/core/src/test/scala/unit/kafka/server/metadata/BrokerMetadataPublisherTest.scala
new file mode 100644
index 0000000..eac1571
--- /dev/null
+++ b/core/src/test/scala/unit/kafka/server/metadata/BrokerMetadataPublisherTest.scala
@@ -0,0 +1,42 @@
+/**
+ * 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 unit.kafka.server.metadata
+
+import kafka.server.metadata.BrokerMetadataPublisher
+import org.apache.kafka.image.MetadataImageTest
+import org.junit.jupiter.api.Test
+
+class BrokerMetadataPublisherTest {
+  @Test
+  def testGetTopicDelta(): Unit = {
+    assert(BrokerMetadataPublisher.getTopicDelta(
+      "not-a-topic",
+      MetadataImageTest.IMAGE1,
+      MetadataImageTest.DELTA1).isEmpty, "Expected no delta for unknown topic")
+
+    assert(BrokerMetadataPublisher.getTopicDelta(
+      "foo",
+      MetadataImageTest.IMAGE1,
+      MetadataImageTest.DELTA1).isEmpty, "Expected no delta for deleted topic")
+
+    assert(BrokerMetadataPublisher.getTopicDelta(
+      "bar",
+      MetadataImageTest.IMAGE1,
+      MetadataImageTest.DELTA1).isDefined, "Expected to see delta for changed topic")
+  }
+}