sreejasahithi commented on code in PR #8415:
URL: https://github.com/apache/ozone/pull/8415#discussion_r2081204405


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/logs/container/utils/ContainerDatanodeDatabase.java:
##########
@@ -609,5 +615,179 @@ private List<DatanodeContainerInfo> 
getContainerLogDataForOpenContainers(Long co
 
     return logEntries;
   }
+
+  /**
+   * Lists container replicas with key details such as Container ID, Datanode 
ID, State, BCSID for over- 
+   * or under-replicated containers.
+   */
+  
+  public void listReplicatedContainers(String overOrUnder, Integer limit) 
throws SQLException {
+    String operator;
+    if ("OVER_REPLICATED".equalsIgnoreCase(overOrUnder)) {
+      operator = ">";
+    } else if ("UNDER_REPLICATED".equalsIgnoreCase(overOrUnder)) {
+      operator = "<";
+    } else {
+      out.println("Invalid type. Use OVER_REPLICATED or UNDER_REPLICATED.");
+      return;
+    }
+    
+    String rawQuery = SQLDBConstants.SELECT_REPLICATED_CONTAINERS;
+
+    if (!rawQuery.contains("{operator}")) {
+      out.println("Query not defined correctly.");
+      return;
+    }
+
+    String finalQuery = rawQuery.replace("{operator}", operator);
+
+    boolean limitProvided = limit != Integer.MAX_VALUE;
+    if (limitProvided) {
+      finalQuery += " LIMIT ?";
+    }
+
+    try (Connection connection = getConnection();
+         PreparedStatement pstmt = connection.prepareStatement(finalQuery)) {
+
+      pstmt.setInt(1, DEFAULT_REPLICATION_FACTOR);
+      
+      if (limitProvided) {
+        pstmt.setInt(2, limit + 1);
+      }
+
+      try (ResultSet rs = pstmt.executeQuery()) {
+        int count = 0;
+
+        out.printf("| %-15s | %-35s | %-15s | %-15s %n", "Container ID", 
"Datanode ID", "State", "BCSID");
+        
out.println("+-----------------+-------------------------------------+-----------------+-----------------+");
+
+        while (rs.next()) {
+          if (limitProvided && count >= limit) {
+            out.println("Note: There might be more containers. Use -all option 
to list all entries.");
+            break;
+          }
+          out.printf("| %-15d | %-35s | %-15s | %-15d %n",
+              rs.getLong("container_id"),
+              rs.getString("datanode_id"),
+              rs.getString("latest_state"),
+              rs.getLong("latest_bcsid"));
+          count++;
+        }
+
+        out.println("Number of containers listed: " + count);
+
+      }
+
+    } catch (SQLException e) {
+      throw new SQLException("Error while retrieving containers." + 
e.getMessage(), e);
+    } catch (Exception e) {
+      throw new RuntimeException("Unexpected error: "  + e);
+    }
+  }
+
+  /**
+   * Lists container replicas with key details such as Container ID, Datanode 
ID, Timestamp, BCSID for 
+   * containers that are UNHEALTHY.
+   */
+
+  public void listUnhealthyContainers(Integer limit) throws SQLException {
+    
+    String query = SQLDBConstants.SELECT_UNHEALTHY_CONTAINERS;
+
+    boolean limitProvided = limit != Integer.MAX_VALUE;
+    if (limitProvided) {
+      query += " LIMIT ?";
+    }
+
+    try (Connection connection = getConnection();
+         PreparedStatement stmt = connection.prepareStatement(query)) {
+
+      if (limitProvided) {
+        stmt.setInt(1, limit + 1);
+      }
+
+      try (ResultSet rs = stmt.executeQuery()) {
+        int count = 0;
+
+        out.printf("| %-15s | %-35s | %-25s | %-10s %n", "Container ID", 
"Datanode ID", "Timestamp", "BCSID");
+        
out.println("+-----------------+-------------------------------------+---------------------------+----------");
+
+        while (rs.next()) {
+          if (limitProvided && count >= limit) {
+            out.println("Note: There might be more containers. Use -all option 
to list all entries.");
+            break;
+          }
+
+          String containerId = rs.getString("container_id");
+          String datanodeId = rs.getString("datanode_id");
+          String timestamp = rs.getString("latest_unhealthy_timestamp");
+          long bcsid = rs.getLong("bcsid");
+
+          out.printf("| %-15s | %-35s | %-25s | %-10d %n", containerId, 
datanodeId, timestamp, bcsid);

Review Comment:
   I have changed the output of the command , so a table structure is not 
needed anymore.
   I have also added the new output to the PR description.



-- 
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: issues-unsubscr...@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org
For additional commands, e-mail: issues-h...@ozone.apache.org

Reply via email to