Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions core/src/main/scala/kafka/admin/ReassignPartitionsCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,26 @@ object ReassignPartitionsCommand extends Logging {
proposedAssignments
}

private def describeTopics(adminClient: Admin,
topics: Set[String])
: Map[String, TopicDescription] = {
adminClient.describeTopics(topics.asJava).values.asScala.map {
case (topicName, topicDescriptionFuture) =>
try {
topicName -> topicDescriptionFuture.get
}
catch {
case t: ExecutionException if t.getCause != null =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using classOf[UnknownTopicOrPartitionException].isInstance(t.getCause)? That includes null check.

if (t.getCause.isInstanceOf[UnknownTopicOrPartitionException]) {
throw new ExecutionException(
new UnknownTopicOrPartitionException(s"Topic $topicName not found."))
} else {
throw t
}
}
}
}

/**
* Get the current replica assignments for some topics.
*
Expand All @@ -823,12 +843,10 @@ object ReassignPartitionsCommand extends Logging {
def getReplicaAssignmentForTopics(adminClient: Admin,
topics: Seq[String])
: Map[TopicPartition, Seq[Int]] = {
adminClient.describeTopics(topics.asJava).all().get().asScala.flatMap {
case (topicName, topicDescription) =>
topicDescription.partitions.asScala.map {
info => (new TopicPartition(topicName, info.partition),
info.replicas.asScala.map(_.id))
}
describeTopics(adminClient, topics.toSet).flatMap {
case (topicName, topicDescription) => topicDescription.partitions.asScala.map { info =>
(new TopicPartition(topicName, info.partition), info.replicas.asScala.map(_.id))
}
}
}

Expand All @@ -843,15 +861,14 @@ object ReassignPartitionsCommand extends Logging {
def getReplicaAssignmentForPartitions(adminClient: Admin,
partitions: Set[TopicPartition])
: Map[TopicPartition, Seq[Int]] = {
adminClient.describeTopics(partitions.map(_.topic).asJava).all().get().asScala.flatMap {
case (topicName, topicDescription) =>
topicDescription.partitions.asScala.flatMap {
info => if (partitions.contains(new TopicPartition(topicName, info.partition))) {
Some(new TopicPartition(topicName, info.partition()),
info.replicas.asScala.map(_.id))
} else {
None
}
describeTopics(adminClient, partitions.map(_.topic)).flatMap {
case (topicName, topicDescription) => topicDescription.partitions.asScala.flatMap { info =>
val tp = new TopicPartition(topicName, info.partition)
if (partitions.contains(tp)) {
Some(tp, info.replicas.asScala.map(_.id))
} else {
None
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ class ReassignPartitionsUnitTest {
}
}

@Test
def testGenerateAssignmentWithInvalidPartitionsFails(): Unit = {
val adminClient = new MockAdminClient.Builder().numBrokers(5).build()
try {
addTopics(adminClient)
assertStartsWith("Topic quux not found",
assertThrows(classOf[ExecutionException],
() => generateAssignment(adminClient, """{"topics":[{"topic":"foo"},{"topic":"quux"}]}""", "0,1", false),
() => "Expected generateAssignment to fail").getCause.getMessage)
} finally {
adminClient.close()
}
}

@Test
def testGenerateAssignmentWithInconsistentRacks(): Unit = {
val adminClient = new MockAdminClient.Builder().
Expand Down