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


##########
hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/SafeModeCheckSubcommand.java:
##########
@@ -45,12 +71,106 @@ public void execute(ScmClient scmClient) throws 
IOException {
       System.out.println("SCM is out of safe mode.");
     }
     if (isVerbose()) {
-      for (Map.Entry<String, Pair<Boolean, String>> entry :
-          scmClient.getSafeModeRuleStatuses().entrySet()) {
-        Pair<Boolean, String> value = entry.getValue();
-        System.out.printf("validated:%s, %s, %s%n",
-            value.getLeft(), entry.getKey(), value.getRight());
+      printSafeModeRules(scmClient.getSafeModeRuleStatuses());
+    }
+  }
+
+  private void executeForSpecificNodeInHA(ScmClient scmClient, String 
serviceId) throws IOException {
+    String scmAddress = getScmOption().getScm();
+
+    System.out.println("Service ID: " + serviceId);
+
+    final OzoneConfiguration conf = getOzoneConf();
+
+    List<SCMNodeInfo> nodes = SCMNodeInfo.buildNodeInfo(conf);
+    
+    // Find the node matching the --scm address
+    List<SCMNodeInfo> matchedNodes = nodes.stream()
+        .filter(node -> matchesAddress(node, scmAddress))
+        .collect(Collectors.toList());
+
+    if (matchedNodes.isEmpty()) {
+      throw new IOException("Specified --scm address " + scmAddress +
+          " does not match any node in service " + serviceId +
+          ". Available nodes: " + nodes.stream()
+              .map(n -> n.getScmClientAddress() + " [" + n.getNodeId() + "]")
+              .collect(Collectors.joining(", ")));
+    }
+    
+    queryNode(scmClient, matchedNodes.get(0));
+  }
+
+  private void executeForAllNodes(ScmClient scmClient) throws IOException {
+    final OzoneConfiguration conf = getOzoneConf();
+    String serviceId = HddsUtils.getScmServiceId(conf);
+
+    if (serviceId == null) {
+      executeForSingleNode(scmClient);
+      return;
+    }
+
+    System.out.println("Service ID: " + serviceId);
+    List<SCMNodeInfo> nodes = SCMNodeInfo.buildNodeInfo(conf);
+
+    for (SCMNodeInfo node : nodes) {
+      queryNode(scmClient, node);
+    }
+  }
+
+  private void queryNode(ScmClient scmClient, SCMNodeInfo node) {
+    String nodeId = node.getNodeId();
+    
+    try {
+      boolean inSafeMode = scmClient.inSafeModeForNode(nodeId);
+
+      System.out.printf("%s [%s]: %s%n",
+          node.getScmClientAddress(),
+          nodeId,
+          inSafeMode ? "IN SAFE MODE" : "OUT OF SAFE MODE");
+
+      if (isVerbose()) {
+        Map<String, Pair<Boolean, String>> rules = 
scmClient.getSafeModeRuleStatusesForNode(nodeId);
+        if (rules != null && !rules.isEmpty()) {
+          printSafeModeRules(rules);
+        }
       }
+    } catch (Exception e) {
+      System.out.printf("%s [%s]: ERROR: Failed to get safe mode status - 
%s%n",
+          node.getScmClientAddress(), nodeId, e.getMessage());
+    }
+  }
+
+  /**
+   * Check if the given SCMNodeInfo matches the target address.
+   * Tries to match by direct string comparison and by resolved address.
+   */
+  private boolean matchesAddress(SCMNodeInfo node, String targetAddress) {
+    String nodeAddress = node.getScmClientAddress();
+
+    // Direct match
+    if (nodeAddress.equals(targetAddress)) {
+      return true;
+    }
+
+    // Try normalizing both addresses and comparing
+    try {
+      InetSocketAddress target = NetUtils.createSocketAddr(targetAddress);
+      InetSocketAddress nodeAddr = NetUtils.createSocketAddr(nodeAddress);
+
+      // Match by resolved IP and port
+      return target.getPort() == nodeAddr.getPort() &&
+          target.getAddress().equals(nodeAddr.getAddress());
+    } catch (Exception e) {
+      // If address resolution fails, no match
+      return false;

Review Comment:
   I have removed the logging here because it creates unwanted noise in the CLI 
output. Instead, I have ensured that actual errors are properly surfaced when 
no leader can be determined or when the node specified in --scm option doesn't 
match, clear error messages are thrown to the user.



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