OmniaGM commented on code in PR #13204:
URL: https://github.com/apache/kafka/pull/13204#discussion_r1294460465


##########
tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandTest.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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 org.apache.kafka.tools;
+
+import kafka.server.KafkaConfig;
+import kafka.server.KafkaServer;
+import kafka.test.ClusterConfig;
+import kafka.test.ClusterInstance;
+import kafka.test.annotation.ClusterTest;
+import kafka.test.annotation.ClusterTestDefaults;
+import kafka.test.annotation.Type;
+import kafka.test.junit.ClusterTestExtensions;
+import kafka.utils.TestUtils;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.CreateTopicsResult;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.extension.ExtendWith;
+import scala.collection.JavaConverters;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+
+@SuppressWarnings("deprecation")
+@ExtendWith(value = ClusterTestExtensions.class)
+@ClusterTestDefaults(clusterType = Type.ALL, brokers = 3)
+@Tag("integration")
+public class LeaderElectionCommandTest {
+    private final ClusterInstance cluster;
+    int broker1 = 0;
+    int broker2 = 1;
+    int broker3 = 2;
+
+    public LeaderElectionCommandTest(ClusterInstance cluster) {
+        this.cluster = cluster;
+    }
+
+    @BeforeEach
+    void setup(ClusterConfig clusterConfig) {
+        TestUtils.verifyNoUnexpectedThreads("@BeforeEach");
+        
clusterConfig.serverProperties().put(KafkaConfig.AutoLeaderRebalanceEnableProp(),
 "false");
+        
clusterConfig.serverProperties().put(KafkaConfig.ControlledShutdownEnableProp(),
 "true");
+        
clusterConfig.serverProperties().put(KafkaConfig.ControlledShutdownMaxRetriesProp(),
 "1");
+        
clusterConfig.serverProperties().put(KafkaConfig.ControlledShutdownRetryBackoffMsProp(),
 "1000");
+        
clusterConfig.serverProperties().put(KafkaConfig.OffsetsTopicReplicationFactorProp(),
 "2");
+    }
+
+    @ClusterTest
+    public void testAllTopicPartition() throws InterruptedException, 
ExecutionException {
+        String topic = "unclean-topic";
+        int partition = 0;
+        List<Integer> assignment = Arrays.asList(broker2, broker3);
+
+        cluster.waitForReadyBrokers();
+        Admin client = cluster.createAdminClient();
+        Map<Integer, List<Integer>> partitionAssignment = new HashMap<>();
+        partitionAssignment.put(partition, assignment);
+
+        createTopic(client, topic, partitionAssignment);
+
+        TopicPartition topicPartition = new TopicPartition(topic, partition);
+
+        TestUtils.assertLeader(client, topicPartition, broker2);
+        cluster.shutdownBroker(broker3);
+        TestUtils.waitForBrokersOutOfIsr(client,
+            
JavaConverters.asScalaBuffer(Collections.singletonList(topicPartition)).toSet(),
+            
JavaConverters.asScalaBuffer(Collections.singletonList(broker3)).toSet()
+        );
+        cluster.shutdownBroker(broker2);
+        TestUtils.assertNoLeader(client, topicPartition);
+        cluster.startBroker(broker3);
+        TestUtils.waitForOnlineBroker(client, broker3);
+
+        LeaderElectionCommand.main(
+            new String[] {
+                "--bootstrap-server", cluster.bootstrapServers(),
+                "--election-type", "unclean",
+                "--all-topic-partitions"
+            }
+        );
+
+        TestUtils.assertLeader(client, topicPartition, broker3);
+    }
+
+    @ClusterTest
+    public void testTopicPartition() throws InterruptedException, 
ExecutionException {
+        String topic = "unclean-topic";
+        int partition = 0;
+        List<Integer> assignment = Arrays.asList(broker2, broker3);
+
+        cluster.waitForReadyBrokers();
+        Map<Integer, List<Integer>> partitionAssignment = new HashMap<>();
+        partitionAssignment.put(partition, assignment);
+        Admin client = cluster.createAdminClient();
+        createTopic(client, topic, partitionAssignment);
+
+        TopicPartition topicPartition = new TopicPartition(topic, partition);
+
+        TestUtils.assertLeader(client, topicPartition, broker2);
+
+        cluster.shutdownBroker(broker3);
+        TestUtils.waitForBrokersOutOfIsr(client,
+            
JavaConverters.asScalaBuffer(Collections.singletonList(topicPartition)).toSet(),
+            
JavaConverters.asScalaBuffer(Collections.singletonList(broker3)).toSet()
+        );
+        cluster.shutdownBroker(broker2);
+        TestUtils.assertNoLeader(client, topicPartition);
+        cluster.startBroker(broker3);
+        TestUtils.waitForOnlineBroker(client, broker3);
+
+        LeaderElectionCommand.main(
+            new String[] {
+                "--bootstrap-server", cluster.bootstrapServers(),
+                "--election-type", "unclean",
+                "--topic", topic,
+                "--partition", Integer.toString(partition)
+            }
+        );
+
+        TestUtils.assertLeader(client, topicPartition, broker3);
+    }
+
+    @ClusterTest
+    public void testPathToJsonFile() throws Exception {
+        String topic = "unclean-topic";
+        int partition = 0;
+        List<Integer> assignment = Arrays.asList(broker2, broker3);
+
+        cluster.waitForReadyBrokers();
+        Map<Integer, List<Integer>> partitionAssignment = new HashMap<>();
+        partitionAssignment.put(partition, assignment);
+
+        Admin client = cluster.createAdminClient();
+        createTopic(client, topic, partitionAssignment);
+
+        TopicPartition topicPartition = new TopicPartition(topic, partition);
+
+        TestUtils.assertLeader(client, topicPartition, broker2);
+
+        cluster.shutdownBroker(broker3);
+        TestUtils.waitForBrokersOutOfIsr(client,
+            
JavaConverters.asScalaBuffer(Collections.singletonList(topicPartition)).toSet(),
+            
JavaConverters.asScalaBuffer(Collections.singletonList(broker3)).toSet()
+        );
+        cluster.shutdownBroker(broker2);
+        TestUtils.assertNoLeader(client, topicPartition);
+        cluster.startBroker(broker3);
+        TestUtils.waitForOnlineBroker(client, broker3);
+
+        Path topicPartitionPath = 
tempTopicPartitionFile(Collections.singletonList(topicPartition));
+
+        LeaderElectionCommand.main(
+            new String[] {
+                "--bootstrap-server", cluster.bootstrapServers(),
+                "--election-type", "unclean",
+                "--path-to-json-file", topicPartitionPath.toString()
+            }
+        );
+
+        TestUtils.assertLeader(client, topicPartition, broker3);
+    }
+
+    @ClusterTest
+    public void testPreferredReplicaElection() throws InterruptedException, 
ExecutionException {
+        String topic = "preferred-topic";
+        int partition = 0;
+        List<Integer> assignment = Arrays.asList(broker2, broker3);
+
+        cluster.waitForReadyBrokers();
+        Admin client = cluster.createAdminClient();
+        Map<Integer, List<Integer>> partitionAssignment = new HashMap<>();
+        partitionAssignment.put(partition, assignment);
+
+        createTopic(client, topic, partitionAssignment);
+
+        TopicPartition topicPartition = new TopicPartition(topic, partition);
+
+        TestUtils.assertLeader(client, topicPartition, broker2);
+
+        cluster.shutdownBroker(broker2);
+        TestUtils.assertLeader(client, topicPartition, broker3);
+        cluster.startBroker(broker2);
+        TestUtils.waitForBrokersInIsr(client, topicPartition,
+            
JavaConverters.asScalaBuffer(Collections.singletonList(broker2)).toSet()
+        );
+
+        LeaderElectionCommand.main(
+            new String[] {
+                "--bootstrap-server", cluster.bootstrapServers(),
+                "--election-type", "preferred",
+                "--all-topic-partitions"
+            }
+        );
+
+        TestUtils.assertLeader(client, topicPartition, broker2);
+    }
+
+    @ClusterTest
+    public void testTopicDoesNotExist() {
+        Throwable e =  assertThrows(AdminCommandFailedException.class, () -> 
LeaderElectionCommand.run(
+            Duration.ofSeconds(30),
+            new String[] {
+                "--bootstrap-server", cluster.bootstrapServers(),
+                "--election-type", "preferred",
+                "--topic", "unknown-topic-name",
+                "--partition", "0"
+            }));
+        assertTrue(e.getSuppressed()[0] instanceof 
UnknownTopicOrPartitionException);
+    }
+
+    @ClusterTest
+    public void testElectionResultOutput() throws Exception {
+        String topic = "non-preferred-topic";
+        int partition0 = 0;
+        int partition1 = 1;
+        List<Integer> assignment0 = Arrays.asList(broker2, broker3);
+        List<Integer> assignment1 = Arrays.asList(broker3, broker2);
+
+        cluster.waitForReadyBrokers();
+        Admin client = cluster.createAdminClient();
+        Map<Integer, List<Integer>> partitionAssignment = new HashMap<Integer, 
List<Integer>>();
+        partitionAssignment.put(partition0, assignment0);
+        partitionAssignment.put(partition1, assignment1);
+
+        createTopic(client, topic, partitionAssignment);
+
+        TopicPartition topicPartition0 = new TopicPartition(topic, partition0);
+        TopicPartition topicPartition1 = new TopicPartition(topic, partition1);
+
+        TestUtils.assertLeader(client, topicPartition0, broker2);
+        TestUtils.assertLeader(client, topicPartition1, broker3);
+
+        cluster.shutdownBroker(broker2);
+        TestUtils.assertLeader(client, topicPartition0, broker3);
+        cluster.startBroker(broker2);
+        TestUtils.waitForBrokersInIsr(client, topicPartition0,
+            
JavaConverters.asScalaBuffer(Collections.singletonList(broker2)).toSet()
+        );
+        TestUtils.waitForBrokersInIsr(client, topicPartition1,
+            
JavaConverters.asScalaBuffer(Collections.singletonList(broker2)).toSet()
+        );
+
+        Path topicPartitionPath = 
tempTopicPartitionFile(Arrays.asList(topicPartition0, topicPartition1));
+        String output = ToolsTestUtils.captureStandardOut(() ->
+            LeaderElectionCommand.main(
+                new String[] {
+                    "--bootstrap-server", cluster.bootstrapServers(),
+                    "--election-type", "preferred",
+                    "--path-to-json-file", topicPartitionPath.toString()
+                }
+            )
+        );
+
+        Iterator<String> electionResultOutputIter = 
Arrays.stream(output.split("\n")).iterator();
+
+        assertTrue(electionResultOutputIter.hasNext());
+        String firstLine = electionResultOutputIter.next();
+        assertTrue(firstLine.contains(String.format(
+            "Successfully completed leader election (PREFERRED) for partitions 
%s", topicPartition0)),
+            String.format("Unexpected output: %s", firstLine));
+
+        assertTrue(electionResultOutputIter.hasNext());
+        String secondLine = electionResultOutputIter.next();
+        assertTrue(secondLine.contains(String.format("Valid replica already 
elected for partitions %s", topicPartition1)),
+            String.format("Unexpected output: %s", secondLine));
+    }
+    private static Map<String, Object> createConfig(List<KafkaServer> servers) 
{

Review Comment:
   You are right we don't need it anymore. I also removed the dependency on 
`kafka.server`



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