aryangupta1998 commented on code in PR #8282:
URL: https://github.com/apache/ozone/pull/8282#discussion_r2044014784
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/ContainerDatanodeDatabase.java:
##########
@@ -233,5 +238,65 @@ private void dropTable(String tableName, Statement stmt)
throws SQLException {
stmt.executeUpdate(dropTableSQL);
}
+ private void createContainerLogIndex(Statement stmt) throws SQLException {
+ String createIndexSQL = queries.get("CREATE_INDEX_LATEST_STATE");
+ stmt.execute(createIndexSQL);
+ }
+
+ /**
+ * Lists containers filtered by the specified state and writes their details
to a file.
+ * <p>
+ * The output includes timestamp, datanode ID, container ID, BCSID, error
message, and index value,
+ * written in a human-readable table format to a file named {@code
<state>_containers_by_state.txt}.
+ *
+ * @param state the container state to filter by (e.g., "OPEN", "CLOSED")
+ */
+
+ public void listContainersByState(String state) throws SQLException {
+ int count = 0;
+ String query = queries.get("SELECT_LATEST_CONTAINER_LOGS_BY_STATE");
+
+ Path outputPath = Paths.get(state + "_containers_by_state.txt");
Review Comment:
```suggestion
Path outputPath = Paths.get(state + "_containers_by_state.txt");
if (Files.exists(outputPath)) {
System.out.println(
"Warning: Output file already exists and will be overwritten: "
+ outputPath.toAbsolutePath());
}
```
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/ContainerDatanodeDatabase.java:
##########
@@ -233,5 +238,65 @@ private void dropTable(String tableName, Statement stmt)
throws SQLException {
stmt.executeUpdate(dropTableSQL);
}
+ private void createContainerLogIndex(Statement stmt) throws SQLException {
+ String createIndexSQL = queries.get("CREATE_INDEX_LATEST_STATE");
+ stmt.execute(createIndexSQL);
+ }
+
+ /**
+ * Lists containers filtered by the specified state and writes their details
to a file.
+ * <p>
+ * The output includes timestamp, datanode ID, container ID, BCSID, error
message, and index value,
+ * written in a human-readable table format to a file named {@code
<state>_containers_by_state.txt}.
+ *
+ * @param state the container state to filter by (e.g., "OPEN", "CLOSED")
+ */
+
+ public void listContainersByState(String state) throws SQLException {
+ int count = 0;
+ String query = queries.get("SELECT_LATEST_CONTAINER_LOGS_BY_STATE");
+
+ Path outputPath = Paths.get(state + "_containers_by_state.txt");
+
+ try (Connection connection = getConnection();
+ Statement stmt = connection.createStatement();
+ BufferedWriter writer = Files.newBufferedWriter(outputPath,
StandardCharsets.UTF_8)) {
+
+ createContainerLogIndex(stmt);
+
+ try (PreparedStatement pstmt = connection.prepareStatement(query)) {
+ pstmt.setString(1, state);
+ try (ResultSet rs = pstmt.executeQuery()) {
+ writer.write(String.format("%-25s | %-35s | %-15s | %-15s | %-40s |
%-12s%n",
+ "Timestamp", "Datanode ID", "Container ID", "BCSID", "Message",
"Index Value"));
+
writer.write("-------------------------------------------------------------------------------------"
+
+
"---------------------------------------------------------------------------------------\n");
+
+ while (rs.next()) {
+ String timestamp = rs.getString("timestamp");
+ String datanodeId = rs.getString("datanode_id");
+ long containerId = rs.getLong("container_id");
+ String latestState = rs.getString("latest_state");
+ long latestBcsid = rs.getLong("latest_bcsid");
+ String errorMessage = rs.getString("error_message");
+ int indexValue = rs.getInt("index_value");
+ count++;
+
+ writer.write(String.format("%-25s | %-15s | %-15d | %-15d | %-40s
| %-12d%n",
+ timestamp, datanodeId, containerId, latestBcsid, errorMessage,
indexValue));
Review Comment:
The above data node ID header uses %-35s, but here we are using %-15s. Is
this intended?
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/container/ListContainers.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.hadoop.ozone.debug.container;
+
+import java.sql.SQLException;
+import java.util.concurrent.Callable;
+import org.apache.hadoop.ozone.containerlog.parser.ContainerDatanodeDatabase;
+import picocli.CommandLine;
+
+
+/**
+ * List containers based on the parameter given.
+ */
+
[email protected](
+ name = "list-containers",
+ description = "Finds containers from the database based on the option
provided."
+)
+public class ListContainers implements Callable<Void> {
+
+
+ @CommandLine.Option(names = {"--state"},
+ description = "state of the container")
+ private String state;
Review Comment:
Let's use an enum instead.
Maybe something like,
```
private ContainerState state;
public enum ContainerState {
OPEN, CLOSED, QUASI_CLOSED
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]