You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "fvaleri (via GitHub)" <gi...@apache.org> on 2023/01/30 10:43:44 UTC

[GitHub] [kafka] fvaleri opened a new pull request, #13171: KAFKA-7735: Move StateChangeLogMerger tool

fvaleri opened a new pull request, #13171:
URL: https://github.com/apache/kafka/pull/13171

   This also includes a new test. 


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090717453


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   That was my idea. I think we can drop CoreUtils.duplicates once the last command using it is migrated. I renamed from ToolsUtils.findDuplicates to ToolsUtils.duplicates, so the intention is clear. WDYT?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1526014730

   @mimaison I reverted these changes and added the deprecation note as stated in KIP-906 and suggested by @ijuma.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1420398056

   Given that clearly nobody is using this tool, I'm wondering if we should deprecate in 3.5 and remove in 4.0.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1094628589


##########
build.gradle:
##########
@@ -1763,6 +1763,7 @@ project(':tools') {
     implementation libs.jacksonJDK8Datatypes
     implementation libs.slf4jApi
     implementation libs.log4j
+    implementation libs.joptSimple

Review Comment:
   I believe we should stop adding this to pull requests 😄. It should have been merged in https://github.com/apache/kafka/pull/13136/files if not before.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090852796


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   We can create a KAFKA-14525 subtask where we list all pending cleanups/refactorings. You could even reference it in a comment on your PR.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1408759302

   Hi Omnia, thanks for the review.
   
   I've addressed some of your comments and answered the others.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-7735: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1408394028

   @mimaison @OmniaGM @tombentley this is ready for review if any of you have time. Thanks.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] mimaison merged pull request #13171: KAFKA-14584: Deprecate StateChangeLogMerger tool

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison merged PR #13171:
URL: https://github.com/apache/kafka/pull/13171


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] OmniaGM commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "OmniaGM (via GitHub)" <gi...@apache.org>.
OmniaGM commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090808339


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   sounds like a plan. Am working on other cli commands that use the `CoreUtils.duplicates` so keep a note to remove it with the last command. 



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090852796


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   We can create a cleanup subtask on KAFKA-14525 where we list all pending cleanups. You could even reference it in a comment on your PR.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090852796


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   We can create a KAFKA-14525 subtask where we list all pending cleanups. You could even reference it in a comment on your PR.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1416167959

   @mimaison thanks for spotting this! 
   
   I saw that ticket, but I started to test with an old log file for testing purpose, and then forgot to also test it on a recent log version. I agree that this is not much used, but maybe the logic can be reused to create something similar.
   
   With the latest commit, I make it work with the latest state change log format and addressed your comments. Before merging, we need to rebase with https://github.com/apache/kafka/pull/13195, so that I can also add this tool to the notable changes note.
   


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] OmniaGM commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "OmniaGM (via GitHub)" <gi...@apache.org>.
OmniaGM commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090471930


##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.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 joptsimple.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    private static List<String> files;
+    private static String topic;
+    private static List<Integer> partitions;
+    private static Date startDate;
+    private static Date endDate;
+
+    public static void main(String[] args) {

Review Comment:
   It may be better to unify the cli tool classes to use same pattern as `ClusterTools` and `MetadataQuorumCommand`to have `main`, `mainNoExit`, and `execute`. Example is here https://github.com/apache/kafka/blob/72cfc994f5675be349d4494ece3528efed290651/tools/src/main/java/org/apache/kafka/tools/MetadataQuorumCommand.java#L49



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090657703


##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.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 joptsimple.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    private static List<String> files;
+    private static String topic;
+    private static List<Integer> partitions;
+    private static Date startDate;
+    private static Date endDate;
+
+    public static void main(String[] args) {

Review Comment:
   Yeah, I thought about that, but there are shared util methods that call Exit.exit() directly, so we should change that first. I think we can address this in a separate PR once most commands are migrated.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1096043542


##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    public static void main(String[] args) {
+        try {
+            StateChangeLogMergerOptions options = new StateChangeLogMergerOptions(args);
+            if (printHelpOrVersion(options) || !validInput(options)) return;

Review Comment:
   Yes, that was the original intention. Thanks.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1413963412

   Rebased. Thanks @clolov and @mimaison.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1495942946

   Hi @mimaison, now that KIP-906 is accepted I would say this is ready for the final review. Thanks.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090717453


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   That was my idea. I think we can drop CoreUtils.duplicates once the last command using it is migrated. WDYT?



##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.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 joptsimple.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    private static List<String> files;
+    private static String topic;
+    private static List<Integer> partitions;
+    private static Date startDate;
+    private static Date endDate;
+
+    public static void main(String[] args) {

Review Comment:
   Yeah, I thought about that, but there are shared util methods that call Exit.exit() directly, so we should change that first. I think we can address this in a separate PR once most commands are migrated. WDYT?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090717453


##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   That was my idea. I think we can drop CoreUtils.duplicates once the last command using it is migrated. I renamed from findDuplicates to duplicates, so the intention is clear. WDYT?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] OmniaGM commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "OmniaGM (via GitHub)" <gi...@apache.org>.
OmniaGM commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1090495143


##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.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 joptsimple.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    private static List<String> files;
+    private static String topic;
+    private static List<Integer> partitions;
+    private static Date startDate;
+    private static Date endDate;
+
+    public static void main(String[] args) {
+        try {
+            StateChangeLogMergerOptions options = new StateChangeLogMergerOptions(args);
+            if (CommandLineUtils.isPrintHelpNeeded(options)) {
+                CommandLineUtils.printUsageAndExit(options.parser,
+                    "A tool for merging the log files from several brokers to reconstruct a unified history of what happened.");
+                return;
+            }
+            if (CommandLineUtils.isPrintVersionNeeded(options)) {
+                CommandLineUtils.printVersionAndExit();
+                return;
+            }
+
+            if ((!options.hasFiles() && !options.hasRegex()) || (options.hasFiles() && options.hasRegex())) {

Review Comment:
   I know the original code use this pattern, however, can't we use `CommandLineUtils.checkInvalidArgs` here instead?
   



##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.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 joptsimple.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    private static List<String> files;
+    private static String topic;
+    private static List<Integer> partitions;
+    private static Date startDate;
+    private static Date endDate;
+
+    public static void main(String[] args) {
+        try {
+            StateChangeLogMergerOptions options = new StateChangeLogMergerOptions(args);
+            if (CommandLineUtils.isPrintHelpNeeded(options)) {
+                CommandLineUtils.printUsageAndExit(options.parser,
+                    "A tool for merging the log files from several brokers to reconstruct a unified history of what happened.");
+                return;
+            }
+            if (CommandLineUtils.isPrintVersionNeeded(options)) {
+                CommandLineUtils.printVersionAndExit();
+                return;
+            }
+
+            if ((!options.hasFiles() && !options.hasRegex()) || (options.hasFiles() && options.hasRegex())) {
+                CommandLineUtils.printUsageAndExit(options.parser,
+                    String.format("Provide arguments to exactly one of the two options \"%s\" or \"%s\"", options.filesOpt, options.regexOpt));
+                return;
+            }
+            if (options.hasPartitions() && !options.hasTopic()) {

Review Comment:
   Similiar to the previous comment can't we use `CommandLineUtils.checkRequiredArgs`. 



##########
server-common/src/main/java/org/apache/kafka/server/util/ToolsUtils.java:
##########
@@ -100,4 +104,17 @@ public static void prettyPrintTable(
         printRow(columnLengths, headers, out);
         rows.forEach(row -> printRow(columnLengths, row, out));
     }
+
+    /**
+     * Returns a set of duplicated items.
+     */
+    public static <T> Set<T> findDuplicates(Collection<T> collection) {

Review Comment:
   Are we planning to drop `CoreUtils.duplicates` in favor of `ToolsUtils.findDuplicates` at some point as `CoreUtils.duplicates` are only used by cli tools at the moment



##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.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 joptsimple.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    private static List<String> files;
+    private static String topic;
+    private static List<Integer> partitions;
+    private static Date startDate;
+    private static Date endDate;
+
+    public static void main(String[] args) {

Review Comment:
   It may be better to unify the cli tool classes to use same pattern as @ `ClusterTools` and `MetadataQuorumCommand`to have `main`, `mainNoExit`, and `execute`. Example is here https://github.com/apache/kafka/blob/72cfc994f5675be349d4494ece3528efed290651/tools/src/main/java/org/apache/kafka/tools/MetadataQuorumCommand.java#L49



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] mimaison commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1094648508


##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    public static void main(String[] args) {
+        try {
+            StateChangeLogMergerOptions options = new StateChangeLogMergerOptions(args);
+            if (printHelpOrVersion(options) || !validInput(options)) return;

Review Comment:
   What about doing:
   ```
   CommandLineUtils.maybePrintHelpOrVersion(options,
                   "A tool for merging the log files from several brokers to reconstruct a unified history of what happened.");
   validInput(options);
   ```
   And remove the `printHelpOrVersion()` method and have `validInput()` return `void`?



##########
tools/src/test/java/org/apache/kafka/tools/StateChangeLogMergerTest.java:
##########
@@ -0,0 +1,193 @@
+/*
+ * 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.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class StateChangeLogMergerTest {
+    private final ToolsTestUtils.MockExitProcedure exitProcedure = new ToolsTestUtils.MockExitProcedure();
+
+    @TempDir
+    static Path tempDir;
+    static Path tempFile;
+
+    @BeforeAll
+    public static void beforeAll() throws IOException {
+        tempFile = Files.createFile(tempDir.resolve("state-change.log"));
+        try (PrintWriter output = new PrintWriter(new FileOutputStream(tempFile.toFile()))) {

Review Comment:
   We can use `Files.newOutputStream(tempFile.toFile().toPath())` instead of `new FileOutputStream`



##########
tools/src/main/java/org/apache/kafka/tools/StateChangeLogMerger.java:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.OptionSpec;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.apache.kafka.server.util.ToolsUtils;
+
+import java.io.File;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Arrays;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+
+/**
+ * A utility that merges the state change logs (possibly obtained from different brokers and over multiple days).
+ *
+ * This utility expects at least one of the following two arguments -
+ * 1. A list of state change log files
+ * 2. A regex to specify state change log file names.
+ *
+ * This utility optionally also accepts the following arguments -
+ * 1. The topic whose state change logs should be merged
+ * 2. A list of partitions whose state change logs should be merged (can be specified only when the topic argument
+ * is explicitly specified)
+ * 3. Start time from when the logs should be merged
+ * 4. End time until when the logs should be merged
+ */
+public class StateChangeLogMerger {
+    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
+    private static final Pattern DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}){1}");
+    private static final Pattern TOPIC_PART_PATTERN = Pattern.compile("\\[(" + Topic.LEGAL_CHARS + "+),([0-9]+)\\]");
+
+    public static void main(String[] args) {
+        try {
+            StateChangeLogMergerOptions options = new StateChangeLogMergerOptions(args);
+            if (printHelpOrVersion(options) || !validInput(options)) return;

Review Comment:
   And we could even put `CommandLineUtils.maybePrintHelpOrVersion()` in `validInput()`.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] mimaison commented on a diff in pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison commented on code in PR #13171:
URL: https://github.com/apache/kafka/pull/13171#discussion_r1094636765


##########
build.gradle:
##########
@@ -1763,6 +1763,7 @@ project(':tools') {
     implementation libs.jacksonJDK8Datatypes
     implementation libs.slf4jApi
     implementation libs.log4j
+    implementation libs.joptSimple

Review Comment:
   Yes this PR needs to be rebased on trunk to remove the extra changes like this and `ToolsTestUtils`/`TransactionCommandTest`.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] ijuma commented on pull request #13171: KAFKA-14584: Move StateChangeLogMerger tool

Posted by "ijuma (via GitHub)" <gi...@apache.org>.
ijuma commented on PR #13171:
URL: https://github.com/apache/kafka/pull/13171#issuecomment-1425808110

   Yeah, I think we should consider simply removing the tool if it doesn't work. But if it was exposed in ` bin`, we'd need a KIP for 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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org