You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ij...@apache.org on 2020/04/26 02:32:16 UTC

[kafka] branch trunk updated: MINOR: Partition is under reassignment when adding and removing (#8364)

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

ijuma 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 d63eaaa  MINOR: Partition is under reassignment when adding and removing (#8364)
d63eaaa is described below

commit d63eaaaa0181bb7b9b4f5ed088abc00d7b32aeb0
Author: José Armando García Sancio <js...@users.noreply.github.com>
AuthorDate: Sat Apr 25 19:31:41 2020 -0700

    MINOR: Partition is under reassignment when adding and removing (#8364)
    
    A partition is under reassignment if the either the set of adding
    replicas or set removing replicas is non-empty.
    
    Fix the test assertion such that it prints stdout on failure.
    
    Reviewers: Ismael Juma <is...@juma.me.uk>
---
 core/src/main/scala/kafka/admin/TopicCommand.scala |  6 +--
 .../scala/unit/kafka/admin/TopicCommandTest.scala  | 56 ++++++++++++++++++++++
 .../admin/TopicCommandWithAdminClientTest.scala    |  2 +-
 .../kafka/admin/TopicCommandWithZKClientTest.scala |  2 +-
 4 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/core/src/main/scala/kafka/admin/TopicCommand.scala b/core/src/main/scala/kafka/admin/TopicCommand.scala
index 9c2c223..86dc84e 100755
--- a/core/src/main/scala/kafka/admin/TopicCommand.scala
+++ b/core/src/main/scala/kafka/admin/TopicCommand.scala
@@ -563,10 +563,10 @@ object TopicCommand extends Logging {
     // It is possible for a reassignment to complete between the time we have fetched its state and the time
     // we fetch partition metadata. In ths case, we ignore the reassignment when determining replication factor.
     def isReassignmentInProgress(ra: PartitionReassignment): Boolean = {
-      // Reassignment is still in progress as long as the removing replicas are still present
+      // Reassignment is still in progress as long as the removing and adding replicas are still present
       val allReplicaIds = tpi.replicas.asScala.map(_.id).toSet
-      val removingReplicaIds = ra.removingReplicas.asScala.map(Int.unbox).toSet
-      allReplicaIds.exists(removingReplicaIds.contains)
+      val changingReplicaIds = ra.removingReplicas.asScala.map(_.intValue).toSet ++ ra.addingReplicas.asScala.map(_.intValue).toSet
+      allReplicaIds.exists(changingReplicaIds.contains)
     }
 
     reassignment match {
diff --git a/core/src/test/scala/unit/kafka/admin/TopicCommandTest.scala b/core/src/test/scala/unit/kafka/admin/TopicCommandTest.scala
new file mode 100644
index 0000000..9ea8a80
--- /dev/null
+++ b/core/src/test/scala/unit/kafka/admin/TopicCommandTest.scala
@@ -0,0 +1,56 @@
+/**
+ * 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.admin
+
+import kafka.admin.TopicCommand.PartitionDescription
+import org.apache.kafka.clients.admin.PartitionReassignment
+import org.apache.kafka.common.Node
+import org.apache.kafka.common.TopicPartitionInfo
+import org.junit.Assert._
+import org.junit.Test
+import scala.jdk.CollectionConverters._
+
+class TopicCommandTest {
+  @Test
+  def testIsNotUnderReplicatedWhenAdding(): Unit = {
+    val replicaIds = List(1, 2)
+    val replicas = replicaIds.map { id =>
+      new Node(id, "localhost", 9090 + id)
+    }
+
+    val partitionDescription = PartitionDescription(
+      "test-topic",
+      new TopicPartitionInfo(
+        0,
+        new Node(1, "localhost", 9091),
+        replicas.asJava,
+        List(new Node(1, "localhost", 9091)).asJava
+      ),
+      None,
+      false,
+      Some(
+        new PartitionReassignment(
+          replicaIds.map(id => id: java.lang.Integer).asJava,
+          List(2: java.lang.Integer).asJava,
+          List.empty.asJava
+        )
+      )
+    )
+
+    assertFalse(partitionDescription.isUnderReplicated)
+  }
+}
diff --git a/core/src/test/scala/unit/kafka/admin/TopicCommandWithAdminClientTest.scala b/core/src/test/scala/unit/kafka/admin/TopicCommandWithAdminClientTest.scala
index 78a4a6f..409b76b 100644
--- a/core/src/test/scala/unit/kafka/admin/TopicCommandWithAdminClientTest.scala
+++ b/core/src/test/scala/unit/kafka/admin/TopicCommandWithAdminClientTest.scala
@@ -682,7 +682,7 @@ class TopicCommandWithAdminClientTest extends KafkaServerTestHarness with Loggin
 
     val underReplicatedOutput = TestUtils.grabConsoleOutput(
       topicService.describeTopic(new TopicCommandOptions(Array("--under-replicated-partitions"))))
-    assertEquals("--under-replicated-partitions shouldn't return anything", "", underReplicatedOutput)
+    assertEquals(s"--under-replicated-partitions shouldn't return anything: '$underReplicatedOutput'", "", underReplicatedOutput)
 
     TestUtils.removeReplicationThrottleForPartitions(adminClient, brokerIds, Set(tp))
     TestUtils.waitForAllReassignmentsToComplete(adminClient)
diff --git a/core/src/test/scala/unit/kafka/admin/TopicCommandWithZKClientTest.scala b/core/src/test/scala/unit/kafka/admin/TopicCommandWithZKClientTest.scala
index db9eaed..64233b5 100644
--- a/core/src/test/scala/unit/kafka/admin/TopicCommandWithZKClientTest.scala
+++ b/core/src/test/scala/unit/kafka/admin/TopicCommandWithZKClientTest.scala
@@ -31,7 +31,7 @@ import org.scalatest.Assertions.intercept
 
 import scala.util.Random
 
-class TopicCommandTest extends ZooKeeperTestHarness with Logging with RackAwareTest {
+class TopicCommandWithZKClientTest extends ZooKeeperTestHarness with Logging with RackAwareTest {
 
   private var topicService: ZookeeperTopicService = _
   private var testTopicName: String = _