m1a2st commented on code in PR #24098:
URL: https://github.com/apache/kafka/pull/24098#discussion_r4547364867
##########
tools/src/main/java/org/apache/kafka/tools/GroupOffsetsResetter.java:
##########
@@ -459,17 +460,28 @@ public List<TopicPartition>
filterNoneLeaderPartitions(Collection<TopicPartition
// collect all topics
Set<String> topics =
topicPartitions.stream().map(TopicPartition::topic).collect(Collectors.toSet());
- try {
- return adminClient.describeTopics(topics, withTimeoutMs(new
DescribeTopicsOptions())).allTopicNames().get().entrySet()
- .stream()
- .flatMap(entry -> entry.getValue().partitions().stream()
- .filter(partitionInfo -> partitionInfo.leader() == null)
- .map(partitionInfo -> new TopicPartition(entry.getKey(),
partitionInfo.partition())))
- .filter(topicPartitions::contains)
- .toList();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
+ List<TopicPartition> noneLeaderPartitions = new ArrayList<>();
+ adminClient.describeTopics(topics, withTimeoutMs(new
DescribeTopicsOptions())).topicNameValues().forEach((topic, future) -> {
+ try {
+ Set<Integer> partitionsWithLeader =
future.get().partitions().stream()
+ .filter(partitionInfo -> partitionInfo.leader() != null)
+ .map(TopicPartitionInfo::partition)
+ .collect(Collectors.toSet());
+ // A partition that no longer exists has no leader either
+ topicPartitions.stream()
+ .filter(tp -> tp.topic().equals(topic) &&
!partitionsWithLeader.contains(tp.partition()))
+ .forEach(noneLeaderPartitions::add);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
Review Comment:
Could we group `topicPartitions` by topic before iterating over the futures?
Currently, each topic scans the entire collection. Grouping them once would
allow both the success and deleted-topic paths to process only the partitions
belonging to each topic.
```
public List<TopicPartition>
filterNoneLeaderPartitions(Collection<TopicPartition> topicPartitions) {
Map<String, List<TopicPartition>> partitionsByTopic =
topicPartitions.stream()
.collect(Collectors.groupingBy(TopicPartition::topic));
List<TopicPartition> noneLeaderPartitions = new ArrayList<>();
adminClient.describeTopics(partitionsByTopic.keySet(),
withTimeoutMs(new DescribeTopicsOptions())).topicNameValues().forEach((topic,
future) -> {
List<TopicPartition> partitions = partitionsByTopic.get(topic);
try {
Set<Integer> partitionsWithLeader =
future.get().partitions().stream()
.filter(partitionInfo -> partitionInfo.leader() !=
null)
.map(TopicPartitionInfo::partition)
.collect(Collectors.toSet());
// A partition that no longer exists has no leader either
partitions.stream()
.filter(tp ->
!partitionsWithLeader.contains(tp.partition()))
.forEach(noneLeaderPartitions::add);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
if (!(e.getCause() instanceof
UnknownTopicOrPartitionException))
throw new RuntimeException(e);
// A deleted topic has no leader for any of its partitions
noneLeaderPartitions.addAll(partitions);
}
});
return noneLeaderPartitions;
}
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]