aryangupta1998 commented on code in PR #8415: URL: https://github.com/apache/ozone/pull/8415#discussion_r2080504912
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/logs/container/ListContainersByHealth.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.logs.container; + +import java.nio.file.Path; +import java.util.concurrent.Callable; +import org.apache.hadoop.hdds.cli.AbstractSubcommand; +import org.apache.hadoop.hdds.scm.container.ReplicationManagerReport; +import org.apache.hadoop.ozone.debug.logs.container.utils.ContainerDatanodeDatabase; +import org.apache.hadoop.ozone.shell.ListOptions; +import picocli.CommandLine; + +/** + * Lists containers based on their health state. + */ [email protected]( + name = "list-health", + description = "Lists containers based on health state like UNDER_REPLICATED, " + + "OVER_REPLICATED , UNHEALTHY, QUASI_CLOSED stuck." +) +public class ListContainersByHealth extends AbstractSubcommand implements Callable<Void> { + + @CommandLine.Option(names = {"--issue"}, + required = true, + description = "Health state of the container.") + private ReplicationManagerReport.HealthState healthState; + + @CommandLine.Mixin + private ListOptions listOptions; + + @CommandLine.ParentCommand + private ContainerLogController parent; + + @Override + public Void call() throws Exception { + + Path dbPath = parent.resolveDbPath(); + if (dbPath == null) { + return null; + } + + ContainerDatanodeDatabase cdd = new ContainerDatanodeDatabase(dbPath.toString()); + + switch (healthState) { + case UNDER_REPLICATED: + cdd.listReplicatedContainers("UNDER_REPLICATED", listOptions.getLimit()); + break; + case OVER_REPLICATED: + cdd.listReplicatedContainers("OVER_REPLICATED", listOptions.getLimit()); + break; Review Comment: Something like, ```suggestion switch (healthState) { case UNDER_REPLICATED: case OVER_REPLICATED: cdd.listReplicatedContainers(healthState, listOptions.getLimit()); break; ``` ########## 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; Review Comment: ```suggestion boolean limitProvided = (limit < 1 && limit != Integer.MAX_VALUE); ``` ########## 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; Review Comment: ```suggestion boolean limitProvided = (limit < 1 && limit != Integer.MAX_VALUE); ``` -- 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]
