urbandan commented on a change in pull request #9430: URL: https://github.com/apache/kafka/pull/9430#discussion_r572669925
########## File path: core/src/main/scala/kafka/tools/GetOffsetShell.scala ########## @@ -132,23 +161,85 @@ object GetOffsetShell { } } - partitionOffsets.toArray.sortBy { case (tp, _) => tp.partition }.foreach { case (tp, offset) => - println(s"$topic:${tp.partition}:${Option(offset).getOrElse("")}") + partitionOffsets.toSeq.sortWith((tp1, tp2) => compareTopicPartitions(tp1._1, tp2._1)).foreach { + case (tp, offset) => println(s"${tp.topic}:${tp.partition}:${Option(offset).getOrElse("")}") } + } + def compareTopicPartitions(a: TopicPartition, b: TopicPartition): Boolean = { + (a.topic(), a.partition()) < (b.topic(), b.partition()) } /** - * Return the partition infos for `topic`. If the topic does not exist, `None` is returned. + * Creates a topic-partition filter based on a list of patterns. + * Expected format: + * List: TopicPartitionPattern(, TopicPartitionPattern)* + * TopicPartitionPattern: TopicPattern(:PartitionPattern)? | :PartitionPattern + * TopicPattern: REGEX + * PartitionPattern: NUMBER | NUMBER-(NUMBER)? | -NUMBER */ - private def listPartitionInfos(consumer: KafkaConsumer[_, _], topic: String, partitionIds: Set[Int]): Option[Seq[PartitionInfo]] = { - val partitionInfos = consumer.listTopics.asScala.filter { case (k, _) => k == topic }.values.flatMap(_.asScala).toBuffer - if (partitionInfos.isEmpty) - None - else if (partitionIds.isEmpty) - Some(partitionInfos) - else - Some(partitionInfos.filter(p => partitionIds.contains(p.partition))) + def createTopicPartitionFilterWithPatternList(topicPartitions: String, excludeInternalTopics: Boolean): PartitionInfo => Boolean = { + val ruleSpecs = topicPartitions.split(",") + val rules = ruleSpecs.map { ruleSpec => parseRuleSpec(ruleSpec, excludeInternalTopics) } + tp => rules.exists(rule => rule.apply(tp)) } + def parseRuleSpec(ruleSpec: String, excludeInternalTopics: Boolean): PartitionInfo => Boolean = { + def wrapNullOrEmpty(s: String): Option[String] = { + if (s == null || s.isEmpty) + None + else + Some(s) + } + + val matcher = topicPartitionPattern.matcher(ruleSpec) + if (!matcher.matches() || matcher.groupCount() == 0) + throw new IllegalArgumentException(s"Invalid rule specification: $ruleSpec") + + val topicPattern = wrapNullOrEmpty(matcher.group(1)) + val lowerRange = if (matcher.groupCount() >= 2) wrapNullOrEmpty(matcher.group(2)) else None + val upperRangeSection = if (matcher.groupCount() >= 3) matcher.group(3) else null + val upperRange = wrapNullOrEmpty(upperRangeSection) + val isRange = upperRangeSection != null + + val includeList = IncludeList(topicPattern.getOrElse(".*")) + + val partitionFilter: Int => Boolean = if (lowerRange.isEmpty && upperRange.isEmpty) { + _ => true + } else if (lowerRange.isEmpty) { + val upperBound = upperRange.get.toInt + p => p < upperBound + } else if (upperRange.isEmpty) { + val lowerBound = lowerRange.get.toInt + if (isRange) { + p => p >= lowerBound + } + else { + p => p == lowerBound + } + } else { + val upperBound = upperRange.get.toInt + val lowerBound = lowerRange.get.toInt + p => p >= lowerBound && p < upperBound + } Review comment: Thank you, changed implementation to this ---------------------------------------------------------------- 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: us...@infra.apache.org