[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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


##
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 assignment = Arrays.asList(broker2, broker3);
+
+cluster.waitForReadyBrokers();
+Admin client = cluster.createAdminClient();
+Map> 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()
+);

Review Comment:
   updated with the suggested comment



##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,376 @@
+/*
+ * 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 

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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


##
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 assignment = Arrays.asList(broker2, broker3);
+
+cluster.waitForReadyBrokers();
+Admin client = cluster.createAdminClient();
+Map> partitionAssignment = new HashMap<>();
+partitionAssignment.put(partition, assignment);
+
+createTopic(client, topic, partitionAssignment);

Review Comment:
   updated with the suggested comment



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



[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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 assignment = Arrays.asList(broker2, broker3);
+
+cluster.waitForReadyBrokers();
+Admin client = cluster.createAdminClient();
+Map> 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);
+   

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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


##
tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandErrorTest.java:
##
@@ -0,0 +1,90 @@
+/*
+ * 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 org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * For some error cases, we can save a little build time by avoiding the 
overhead for
+ * cluster creation and cleanup because the command is expected to fail 
immediately.
+ */
+public class LeaderElectionCommandErrorTest {
+@Test
+public void testTopicWithoutPartition() {
+String out = ToolsTestUtils.captureStandardErr(() -> 
LeaderElectionCommand.main(
+"--bootstrap-server", "nohost:9092",
+"--election-type", "unclean",
+"--topic", "some-topic"
+));
+assertTrue(out.startsWith("Missing required option(s)"));
+assertTrue(out.contains(" partition"));
+}
+
+@Test
+public void testPartitionWithoutTopic() {
+String out = ToolsTestUtils.captureStandardErr(() -> 
LeaderElectionCommand.main(
+"--bootstrap-server", "nohost:9092",
+"--election-type", "unclean",
+"--all-topic-partitions",
+"--partition", "0"
+));
+String[] rows = out.split("\n");

Review Comment:
   Updated to use the suggested assertion. 



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



[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,376 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonMappingException;
+import joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.apache.kafka.server.common.AdminOperationException;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger LOG = 
LoggerFactory.getLogger(LeaderElectionCommand.class);
+private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+
+public static void main(String... args) {
+try {
+run(Duration.ofMillis(3), args);
+} catch (Exception e) {
+System.err.println(e.getMessage());
+System.err.println(Utils.stackTrace(e));
+}
+}
+
+static void run(Duration timeoutMs, String... args) throws Exception {
+LeaderElectionCommandOptions commandOptions = new 
LeaderElectionCommandOptions(args);
+
+commandOptions.maybePrintHelpOrVersion();
+
+commandOptions.validate();
+ElectionType electionType = commandOptions.getElectionType();
+Optional> jsonFileTopicPartitions =
+Optional.ofNullable(commandOptions.getPathToJsonFile())
+.map(path -> parseReplicaElectionData(path));
+
+Optional topicOption = 
Optional.ofNullable(commandOptions.getTopic());
+Optional partitionOption = 
Optional.ofNullable(commandOptions.getPartition());
+final Optional> singleTopicPartition =
+(topicOption.isPresent() && partitionOption.isPresent()) ?
+Optional.of(Collections.singleton(new 
TopicPartition(topicOption.get(), partitionOption.get( :
+Optional.empty();
+
+/* Note: No need to look at --all-topic-partitions as we want this to 
be null if it is use.
+ * The validate function should be checking that this option is 
required if the --topic and --path-to-json-file
+ * are not specified.
+ */
+Optional> topicPartitions = 
jsonFileTopicPartitions.map(Optional::of).orElse(singleTopicPartition);
+
+Properties props = new Properties();
+if (commandOptions.hasAdminClientConfig()) {
+
props.putAll(Utils.loadProps(commandOptions.getAdminClientConfig()));
+}
+props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
commandOptions.getBootstrapServer());
+props.setProperty(AdminClientConfig.DEFAULT_API_TIMEOUT_MS_CONFIG, 
Long.t

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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


##
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 assignment = Arrays.asList(broker2, broker3);
+
+cluster.waitForReadyBrokers();
+Admin client = cluster.createAdminClient();
+Map> 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[] {

Review Comment:
   Updated this 



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

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-15 Thread via GitHub


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


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

Review Comment:
   Removed it. 



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



[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-09 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,378 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonMappingException;
+import joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.apache.kafka.server.common.AdminOperationException;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger LOG = 
LoggerFactory.getLogger(LeaderElectionCommand.class);
+private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+
+public static void main(String... args) {
+try {
+run(Duration.ofMillis(3), args);
+} catch (TerseException e) {
+System.err.println(e.getMessage());
+} catch (Exception e) {
+System.err.println(e.getMessage());
+System.err.println(Utils.stackTrace(e));
+}
+}
+
+static void run(Duration timeoutMs, String... args) throws Exception {
+LeaderElectionCommandOptions commandOptions = new 
LeaderElectionCommandOptions(args);
+
+commandOptions.maybePrintHelpOrVersion();
+
+commandOptions.validate();
+ElectionType electionType = commandOptions.getElectionType();
+Optional> jsonFileTopicPartitions =
+Optional.ofNullable(commandOptions.getPathToJsonFile())
+.map(path -> parseReplicaElectionData(path));
+
+Optional topicOption = 
Optional.ofNullable(commandOptions.getTopic());
+Optional partitionOption = 
Optional.ofNullable(commandOptions.getPartition());
+final Optional> singleTopicPartition =
+(topicOption.isPresent() && partitionOption.isPresent()) ?
+Optional.of(Collections.singleton(new 
TopicPartition(topicOption.get(), partitionOption.get( :
+Optional.empty();
+
+/* Note: No need to look at --all-topic-partitions as we want this to 
be null if it is use.
+ * The validate function should be checking that this option is 
required if the --topic and --path-to-json-file
+ * are not specified.
+ */
+Optional> topicPartitions = 
jsonFileTopicPartitions.map(Optional::of).orElse(singleTopicPartition);
+
+Properties props = new Properties();
+if (commandOptions.hasAdminClientConfig()) {
+
props.putAll(Utils.loadProps(commandOptions.getAdminClientConfig()));
+}
+props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
commandOptions.getBootstrapServer(

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-09 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,378 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonMappingException;
+import joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.apache.kafka.server.common.AdminOperationException;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger LOG = 
LoggerFactory.getLogger(LeaderElectionCommand.class);
+private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+
+public static void main(String... args) {
+try {
+run(Duration.ofMillis(3), args);
+} catch (TerseException e) {
+System.err.println(e.getMessage());
+} catch (Exception e) {
+System.err.println(e.getMessage());
+System.err.println(Utils.stackTrace(e));
+}
+}
+
+static void run(Duration timeoutMs, String... args) throws Exception {
+LeaderElectionCommandOptions commandOptions = new 
LeaderElectionCommandOptions(args);
+
+commandOptions.maybePrintHelpOrVersion();
+
+commandOptions.validate();
+ElectionType electionType = commandOptions.getElectionType();
+Optional> jsonFileTopicPartitions =
+Optional.ofNullable(commandOptions.getPathToJsonFile())
+.map(path -> parseReplicaElectionData(path));
+
+Optional topicOption = 
Optional.ofNullable(commandOptions.getTopic());
+Optional partitionOption = 
Optional.ofNullable(commandOptions.getPartition());
+final Optional> singleTopicPartition =
+(topicOption.isPresent() && partitionOption.isPresent()) ?
+Optional.of(Collections.singleton(new 
TopicPartition(topicOption.get(), partitionOption.get( :
+Optional.empty();
+
+/* Note: No need to look at --all-topic-partitions as we want this to 
be null if it is use.
+ * The validate function should be checking that this option is 
required if the --topic and --path-to-json-file
+ * are not specified.
+ */
+Optional> topicPartitions = 
jsonFileTopicPartitions.map(Optional::of).orElse(singleTopicPartition);
+
+Properties props = new Properties();
+if (commandOptions.hasAdminClientConfig()) {
+
props.putAll(Utils.loadProps(commandOptions.getAdminClientConfig()));
+}
+props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
commandOptions.getBootstrapServer(

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-09 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,378 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonMappingException;
+import joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.apache.kafka.server.common.AdminOperationException;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger LOG = 
LoggerFactory.getLogger(LeaderElectionCommand.class);
+private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+
+public static void main(String... args) {
+try {
+run(Duration.ofMillis(3), args);
+} catch (TerseException e) {
+System.err.println(e.getMessage());
+} catch (Exception e) {
+System.err.println(e.getMessage());
+System.err.println(Utils.stackTrace(e));
+}
+}
+
+static void run(Duration timeoutMs, String... args) throws Exception {
+LeaderElectionCommandOptions commandOptions = new 
LeaderElectionCommandOptions(args);
+
+commandOptions.maybePrintHelpOrVersion();
+
+commandOptions.validate();
+ElectionType electionType = commandOptions.getElectionType();
+Optional> jsonFileTopicPartitions =
+Optional.ofNullable(commandOptions.getPathToJsonFile())
+.map(path -> parseReplicaElectionData(path));
+
+Optional topicOption = 
Optional.ofNullable(commandOptions.getTopic());
+Optional partitionOption = 
Optional.ofNullable(commandOptions.getPartition());
+final Optional> singleTopicPartition =
+(topicOption.isPresent() && partitionOption.isPresent()) ?
+Optional.of(Collections.singleton(new 
TopicPartition(topicOption.get(), partitionOption.get( :
+Optional.empty();
+
+/* Note: No need to look at --all-topic-partitions as we want this to 
be null if it is use.
+ * The validate function should be checking that this option is 
required if the --topic and --path-to-json-file
+ * are not specified.
+ */
+Optional> topicPartitions = 
jsonFileTopicPartitions.map(Optional::of).orElse(singleTopicPartition);
+
+Properties props = new Properties();
+if (commandOptions.hasAdminClientConfig()) {
+
props.putAll(Utils.loadProps(commandOptions.getAdminClientConfig()));
+}
+props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
commandOptions.getBootstrapServer(

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-09 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,378 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonMappingException;
+import joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.apache.kafka.server.common.AdminOperationException;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger LOG = 
LoggerFactory.getLogger(LeaderElectionCommand.class);
+private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+
+public static void main(String... args) {
+try {
+run(Duration.ofMillis(3), args);
+} catch (TerseException e) {

Review Comment:
   Removed the `TerseException`



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



[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-09 Thread via GitHub


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


##
tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandErrorTest.java:
##
@@ -0,0 +1,100 @@
+/*
+ * 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 org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * For some error cases, we can save a little build time by avoiding the 
overhead for
+ * cluster creation and cleanup because the command is expected to fail 
immediately.
+ */
+public class LeaderElectionCommandErrorTest {
+@Test
+public void testTopicWithoutPartition() {
+String out = ToolsTestUtils.captureStandardErr(() -> 
LeaderElectionCommand.main(
+new String[] {

Review Comment:
   Remove it



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



[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-08-09 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,378 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonMappingException;
+import joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.common.AdminCommandFailedException;
+import org.apache.kafka.server.common.AdminOperationException;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger LOG = 
LoggerFactory.getLogger(LeaderElectionCommand.class);
+private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+
+public static void main(String... args) {
+try {
+run(Duration.ofMillis(3), args);
+} catch (TerseException e) {
+System.err.println(e.getMessage());
+} catch (Exception e) {
+System.err.println(e.getMessage());
+System.err.println(Utils.stackTrace(e));
+}
+}
+
+static void run(Duration timeoutMs, String... args) throws Exception {
+LeaderElectionCommandOptions commandOptions = new 
LeaderElectionCommandOptions(args);
+
+commandOptions.maybePrintHelpOrVersion();
+
+commandOptions.validate();
+ElectionType electionType = commandOptions.getElectionType();
+Optional> jsonFileTopicPartitions =
+Optional.ofNullable(commandOptions.getPathToJsonFile())
+.map(path -> parseReplicaElectionData(path));
+
+Optional topicOption = 
Optional.ofNullable(commandOptions.getTopic());
+Optional partitionOption = 
Optional.ofNullable(commandOptions.getPartition());
+final Optional> singleTopicPartition =
+(topicOption.isPresent() && partitionOption.isPresent()) ?
+Optional.of(Collections.singleton(new 
TopicPartition(topicOption.get(), partitionOption.get( :
+Optional.empty();
+
+/* Note: No need to look at --all-topic-partitions as we want this to 
be null if it is use.
+ * The validate function should be checking that this option is 
required if the --topic and --path-to-json-file
+ * are not specified.
+ */
+Optional> topicPartitions = 
jsonFileTopicPartitions.map(Optional::of).orElse(singleTopicPartition);
+
+Properties props = new Properties();
+if (commandOptions.hasAdminClientConfig()) {
+
props.putAll(Utils.loadProps(commandOptions.getAdminClientConfig()));
+}
+props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
commandOptions.getBootstrapServer(

[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-02-12 Thread via GitHub


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


##
tools/src/main/java/org/apache/kafka/tools/LeaderElectionCommand.java:
##
@@ -0,0 +1,335 @@
+/*
+ * 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 joptsimple.AbstractOptionSpec;
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpecBuilder;
+import joptsimple.util.EnumConverter;
+import kafka.admin.AdminOperationException;
+import kafka.common.AdminCommandFailedException;
+import kafka.utils.CoreUtils;
+import kafka.utils.Json;
+import kafka.utils.json.DecodeJson;
+import kafka.utils.json.JsonObject;
+import kafka.utils.json.JsonValue;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.common.ElectionType;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.errors.ElectionNotNeededException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import scala.Option;
+import scala.collection.Iterable;
+import scala.collection.Iterator;
+import scala.collection.JavaConverters;
+import scala.collection.mutable.Seq;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+public class LeaderElectionCommand {
+private static final Logger log = 
LoggerFactory.getLogger(LeaderElectionCommand.class);

Review Comment:
   Removed it



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



[GitHub] [kafka] OmniaGM commented on a diff in pull request #13204: KAFKA-14593: Move LeaderElectionCommand to tools

2023-02-12 Thread via GitHub


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


##
build.gradle:
##
@@ -1756,6 +1756,7 @@ project(':tools') {
 
   dependencies {
 implementation project(':clients')
+implementation project(':core')

Review Comment:
   It would be bit harder to move them to tools. For example the command uses 
`kafka.utils.json` which is used by `kafka.server`, 
`kafka.security.authorizer`. And `kafka.admin.AdminOperationException` is used 
by `kafka.admin`, `kafka.controller`, `kafka.zk`, etc. So not sure it's easy to 
move them out. 



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