adoroszlai commented on code in PR #8264:
URL: https://github.com/apache/ozone/pull/8264#discussion_r2047125685


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/container/ContainerInfoCommand.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;
+
+/**
+ * Command to display detailed information of a single container by ID.
+ */
+
[email protected](
+    name = "container-info",

Review Comment:
   ```suggestion
       name = "info",
   ```



##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/container/ContainerInfoCommand.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;
+
+/**
+ * Command to display detailed information of a single container by ID.
+ */
+
[email protected](
+    name = "container-info",
+    description = "provides details of a single container"
+)
+public class ContainerInfoCommand implements Callable<Void> {
+
+  @CommandLine.Option(names = {"--containerId"},
+      description = "container Id", 
+          required = true)
+  private Long containerId;
+
+  @CommandLine.ParentCommand
+  private ContainerLogController parent;
+  
+  @Override
+  public Void call() throws Exception {
+    if (containerId != null) {
+
+      ContainerDatanodeDatabase cdd = new ContainerDatanodeDatabase();
+      try {
+        cdd.showContainerDetails(containerId);
+      } catch (SQLException e) {
+        System.err.println("SQL error while fetching container details: " + 
e.getMessage());

Review Comment:
   Same comment as in #8282:
   
   ```suggestion
   ```



##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/container/ContainerInfoCommand.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;
+
+/**
+ * Command to display detailed information of a single container by ID.
+ */
+
[email protected](
+    name = "container-info",
+    description = "provides details of a single container"
+)
+public class ContainerInfoCommand implements Callable<Void> {
+
+  @CommandLine.Option(names = {"--containerId"},
+      description = "container Id", 
+          required = true)
+  private Long containerId;

Review Comment:
   Should this be a parameter and accept multiple values?  Like:
   
   
https://github.com/apache/ozone/blob/ef8e0da2c1180d2de6e1ce53d4379e1527ecd615/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/InfoSubcommand.java#L61-L62
   
   Or simply be a parameter, similar to:
   
   
https://github.com/apache/ozone/blob/ef8e0da2c1180d2de6e1ce53d4379e1527ecd615/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CloseSubcommand.java#L37-L38
   
   If you prefer it as an option, then please at least rename to `--id`.



##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/ContainerDatanodeDatabase.java:
##########
@@ -233,5 +236,189 @@ private void dropTable(String tableName, Statement stmt) 
throws SQLException {
     stmt.executeUpdate(dropTableSQL);
   }
 
+  /**
+   * Displays detailed information about a container based on its ID, 
including its state, BCSID, 
+   * timestamp, message, and index value. It also checks for issues such as 
UNHEALTHY 
+   * replicas, under-replication, over-replication, OPEN_UNHEALTHY, 
OUASI_CLOSED_STUCK, mismatched replication
+   * and duplicate open.
+   *
+   * @param containerID The ID of the container to display details for.
+   */
+  
+  public void showContainerDetails(Long containerID) throws SQLException {
+    String query = queries.get("CONTAINER_SELECT_QUERY");
+
+    try (Connection connection = getConnection()) {
+      List<DatanodeContainerInfo> logEntries = 
getContainerLogData(containerID, connection);
+      
+      System.out.printf("%-25s | %-15s | %-35s | %-20s | %-10s | %-30s | 
%-12s%n",
+          "Timestamp", "Container ID", "Datanode ID", "Container State", 
"BCSID", "Message", "Index Value");
+      
System.out.println("-----------------------------------------------------------------------------------"
 +
+          
"-------------------------------------------------------------------------------------------------");
+
+      for (DatanodeContainerInfo entry : logEntries) {
+        System.out.printf("%-25s | %-15d | %-35s | %-20s | %-10d | %-30s | 
%-12d%n",
+            entry.getTimestamp(),
+            entry.getContainerId(),
+            entry.getDatanodeId(),
+            entry.getState(),
+            entry.getBcsid(),
+            entry.getErrorMessage(),
+            entry.getIndexValue());
+      }
+
+      if (checkForMultipleOpenStates(logEntries)) {
+        System.out.println("Container " + containerID + " might have duplicate 
OPEN state.");
+        return;
+      }
+
+      try (PreparedStatement statement = connection.prepareStatement(query)) {
+        statement.setLong(1, containerID);
+
+        try (ResultSet resultSet = statement.executeQuery()) {
+
+          boolean foundit = false;
+          Set<String> datanodeIds = new HashSet<>();
+          Set<String> unhealthyReplicas = new HashSet<>();
+          Set<String> closedReplicas = new HashSet<>();
+          Set<String> openReplicas = new HashSet<>();
+          Set<String> quasiclosedReplicas = new HashSet<>();
+          Set<Long> bcsids = new HashSet<>();
+          Set<String> deletedReplicas = new HashSet<>();
+
+          while (resultSet.next()) {
+            foundit = true;
+
+            String datanodeId = resultSet.getString("datanode_id");
+            String latestState = resultSet.getString("latest_state");
+            long latestBcsid = resultSet.getLong("latest_bcsid");
+            String timestamp = resultSet.getString("timestamp");
+            String errorMessage = resultSet.getString("error_message");
+            int indexValue = resultSet.getInt("index_value");
+
+            datanodeIds.add(datanodeId);
+
+            if ("UNHEALTHY".equalsIgnoreCase(latestState)) {
+              unhealthyReplicas.add(datanodeId);
+            } else if ("CLOSED".equalsIgnoreCase(latestState)) {
+              closedReplicas.add(datanodeId);
+              bcsids.add(latestBcsid);
+            } else if ("OPEN".equalsIgnoreCase(latestState)) {
+              openReplicas.add(datanodeId);
+            } else if ("QUASI_CLOSED".equalsIgnoreCase(latestState)) {
+              quasiclosedReplicas.add(datanodeId);
+            } else if ("DELETED".equalsIgnoreCase(latestState)) {
+              deletedReplicas.add(datanodeId);
+              bcsids.add(latestBcsid);
+            }
+          }
+
+          if (!foundit) {
+            System.out.println("Missing container with ID: " + containerID);
+            return;
+          }
+
+          if (!deletedReplicas.isEmpty() && !closedReplicas.isEmpty() && 
bcsids.size() > 1) {
+            System.out.println("Container " + containerID + " has MISMATCHED 
REPLICATION.");
+            return;
+          }
+
+          int unhealthyCount = unhealthyReplicas.size();
+          int replicaCount = datanodeIds.size();
+
+          if (unhealthyCount == replicaCount) {
+            System.out.println("Container " + containerID + " is UNHEALTHY 
across all datanodes.");
+          } else if (unhealthyCount >= 2 && closedReplicas.size() == 
replicaCount - unhealthyCount) {
+            System.out.println("Container " + containerID + " is both 
UNHEALTHY and UNDER-REPLICATED.");
+          } else if (unhealthyCount == 1 && closedReplicas.size() == 
replicaCount - unhealthyCount) {
+            System.out.println("Container " + containerID + " is 
UNDER-REPLICATED.");
+          } else if ((!openReplicas.isEmpty() && openReplicas.size() < 3) &&
+              (closedReplicas.size() == replicaCount - openReplicas.size() ||
+                  unhealthyCount == replicaCount - openReplicas.size())) {
+            System.out.println("Container " + containerID + " is 
OPEN_UNHEALTHY.");
+          } else if (quasiclosedReplicas.size() >= 3) {
+            System.out.println("Container " + containerID + " is 
OUASI_CLOSED_STUCK.");
+          } else {
+            int expectedReplicationFactor = 3;
+            if (replicaCount - deletedReplicas.size() < 
expectedReplicationFactor) {
+              System.out.println("Container " + containerID + " is 
UNDER-REPLICATED.");
+            } else if (replicaCount - deletedReplicas.size() > 
expectedReplicationFactor) {
+              System.out.println("Container " + containerID + " is 
OVER-REPLICATED.");
+            } else {
+              System.out.println("Container " + containerID + " has enough 
replicas.");
+            }
+          }
+        }
+      }
+    } catch (SQLException e) {
+      LOG.error("Failed to get details of container: {}", containerID, e);

Review Comment:
   Error/warning messages should go to stderr (`System.err`).



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

Reply via email to