chia7712 commented on code in PR #23990:
URL: https://github.com/apache/kafka/pull/23990#discussion_r4449462798


##########
clients/src/testFixtures/java/org/apache/kafka/clients/admin/AdminClientTestUtils.java:
##########
@@ -217,4 +221,19 @@ public static ListOffsetsResult 
createListOffsetsResult(Map<TopicPartition, Offs
     public static Admin create(Map<String, Object> conf, HostResolver 
hostResolver) {
         return KafkaAdminClient.createInternal(new AdminClientConfig(conf, 
true), null, hostResolver);
     }
+
+    /**
+     * Returns the current leader of the given partition, or empty if the 
partition has no leader.
+     * This does not wait for a leader to be elected; wrap it in {@link 
org.apache.kafka.test.TestUtils#waitForCondition}
+     * when the test needs to wait for a specific leader.
+     */
+    public static Optional<Integer> currentLeader(Admin admin, TopicPartition 
topicPartition) throws ExecutionException, InterruptedException {
+        return 
admin.describeTopics(List.of(topicPartition.topic())).allTopicNames().get()
+                .get(topicPartition.topic()).partitions().stream()
+                .filter(p -> p.partition() == topicPartition.partition())
+                .findFirst()
+                .map(TopicPartitionInfo::leader)
+                .filter(leader -> leader != null && leader.id() != 
Node.noNode().id())

Review Comment:
   `leader != null` this condition is redundant since `Optional#map` already 
returns empty for a null leader.



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

Reply via email to