dengziming commented on a change in pull request #11173:
URL: https://github.com/apache/kafka/pull/11173#discussion_r816447498



##########
File path: core/src/test/scala/kafka/tools/GetOffsetShellParsingTest.scala
##########
@@ -17,191 +17,231 @@
 
 package kafka.tools
 
-import org.apache.kafka.common.PartitionInfo
-import org.junit.jupiter.api.Assertions.{assertEquals, assertFalse, 
assertThrows, assertTrue}
+import org.apache.kafka.common.TopicPartition
+import org.junit.jupiter.api.Assertions.{assertFalse, assertThrows, assertTrue}
 import org.junit.jupiter.api.Test
-import org.junit.jupiter.params.ParameterizedTest
-import org.junit.jupiter.params.provider.ValueSource
 
 class GetOffsetShellParsingTest {
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForTopicName(excludeInternal: Boolean): Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList("test", 
excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 0)))
-    assertTrue(filter.apply(partitionInfo("test", 1)))
-    assertFalse(filter.apply(partitionInfo("test1", 0)))
-    assertFalse(filter.apply(partitionInfo("__consumer_offsets", 0)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForInternalTopicName(excludeInternal: Boolean): 
Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList("__consumer_offsets", 
excludeInternal)
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 0)))
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 1)))
-    assertFalse(filter.apply(partitionInfo("test1", 0)))
-    assertFalse(filter.apply(partitionInfo("test2", 0)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForTopicNameList(excludeInternal: Boolean): Unit 
= {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList("test,test1,__consumer_offsets",
 excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 0)))
-    assertTrue(filter.apply(partitionInfo("test1", 1)))
-    assertFalse(filter.apply(partitionInfo("test2", 0)))
-
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 0)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForRegex(excludeInternal: Boolean): Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList("test.*", 
excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 0)))
-    assertTrue(filter.apply(partitionInfo("test1", 1)))
-    assertTrue(filter.apply(partitionInfo("test2", 0)))
-    assertFalse(filter.apply(partitionInfo("__consumer_offsets", 0)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForPartitionIndexSpec(excludeInternal: Boolean): 
Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList(":0", excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 0)))
-    assertTrue(filter.apply(partitionInfo("test1", 0)))
-    assertFalse(filter.apply(partitionInfo("test2", 1)))
-
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 0)))
-    assertFalse(filter.apply(partitionInfo("__consumer_offsets", 1)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForPartitionRangeSpec(excludeInternal: Boolean): 
Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList(":1-3", 
excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 1)))
-    assertTrue(filter.apply(partitionInfo("test1", 2)))
-    assertFalse(filter.apply(partitionInfo("test2", 0)))
-    assertFalse(filter.apply(partitionInfo("test2", 3)))
-
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 2)))
-    assertFalse(filter.apply(partitionInfo("__consumer_offsets", 3)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForPartitionLowerBoundSpec(excludeInternal: 
Boolean): Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList(":1-", excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 1)))
-    assertTrue(filter.apply(partitionInfo("test1", 2)))
-    assertFalse(filter.apply(partitionInfo("test2", 0)))
-
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 2)))
-    assertFalse(filter.apply(partitionInfo("__consumer_offsets", 0)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))
-  def testTopicPartitionFilterForPartitionUpperBoundSpec(excludeInternal: 
Boolean): Unit = {
-    val filter = 
GetOffsetShell.createTopicPartitionFilterWithPatternList(":-3", excludeInternal)
-    assertTrue(filter.apply(partitionInfo("test", 0)))
-    assertTrue(filter.apply(partitionInfo("test1", 1)))
-    assertTrue(filter.apply(partitionInfo("test2", 2)))
-    assertFalse(filter.apply(partitionInfo("test3", 3)))
-
-    assertEquals(!excludeInternal, 
filter.apply(partitionInfo("__consumer_offsets", 2)))
-    assertFalse(filter.apply(partitionInfo("__consumer_offsets", 3)))
-  }
-
-  @ParameterizedTest
-  @ValueSource(booleans = Array(true, false))

Review comment:
       We no longer need to test "excludeInternal" flag since we already 
filtered them in `client.describeTopics`, so   excludeInternal is always false 
when filtering topicPartitions.

##########
File path: core/src/main/scala/kafka/tools/GetOffsetShell.scala
##########
@@ -224,9 +239,81 @@ object GetOffsetShell {
   /**
    * Return the partition infos. Filter them with topicPartitionFilter.
    */
-  private def listPartitionInfos(consumer: KafkaConsumer[_, _], 
topicPartitionFilter: PartitionInfo => Boolean): Seq[PartitionInfo] = {
-    consumer.listTopics.asScala.values.flatMap { partitions =>
-      partitions.asScala.filter(topicPartitionFilter)
+  private def listPartitionInfos(
+    client: Admin,
+    topicPartitionFilter: TopicPartitionFilter,
+    excludeInternalTopics: Boolean
+  ): Seq[TopicPartition] = {
+    val listTopicsOptions = new 
ListTopicsOptions().listInternal(!excludeInternalTopics)
+    val topics = client.listTopics(listTopicsOptions).names.get
+    val filteredTopics = 
topics.asScala.filter(topicPartitionFilter.isTopicAllowed)
+
+    
client.describeTopics(filteredTopics.asJava).allTopicNames.get.asScala.flatMap 
{ case (topic, description) =>
+      description
+        .partitions
+        .asScala
+        .map(tp => new TopicPartition(topic, tp.partition))
+        .filter(topicPartitionFilter.isPartitionAllowed)

Review comment:
       We still have to create `TopicPartition` since the 
`AdminClient.listOffsets` accepts ` Map<TopicPartition, OffsetSpec>`, so it's 
convenient to convert it in advence.

##########
File path: core/src/test/scala/kafka/tools/TopicPartitionFilterTest.scala
##########
@@ -0,0 +1,95 @@
+/**
+ * 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.tools
+
+import kafka.utils.IncludeList
+import org.apache.kafka.common.TopicPartition
+import org.junit.jupiter.api.Assertions.{assertFalse, assertTrue}
+import org.junit.jupiter.api.Test
+
+class TopicPartitionFilterTest {

Review comment:
       Currently, `GetOffsetShellParsingTest` only tested "--topic-partitions", 
and "--topic " + "--partitions" is ignored.
   And your suggestions are right here, I removed this test case and added a 
case in `GetOffsetShellParsingTest`

##########
File path: core/src/test/scala/kafka/tools/TopicPartitionFilterTest.scala
##########
@@ -0,0 +1,95 @@
+/**
+ * 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.tools
+
+import kafka.utils.IncludeList
+import org.apache.kafka.common.TopicPartition
+import org.junit.jupiter.api.Assertions.{assertFalse, assertTrue}
+import org.junit.jupiter.api.Test
+
+class TopicPartitionFilterTest {

Review comment:
       Currently, `GetOffsetShellParsingTest` only tested "--topic-partitions", 
and "--topic " + "--partitions" is ignored.
   And your suggestions are right here, I removed this test case and added a 
case in `GetOffsetShellParsingTest`




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

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

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


Reply via email to