ivandika3 commented on code in PR #6960:
URL: https://github.com/apache/ozone/pull/6960#discussion_r1762252093


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java:
##########
@@ -2131,10 +2136,69 @@ public ContainerTokenGenerator 
getContainerTokenGenerator() {
   }
 
   @Override
-  public String getScmRatisRoles() {
+  public List<List<String>> getScmRatisRoles() {
     final SCMRatisServer server = getScmHAManager().getRatisServer();
-    return server != null ?
-        HddsUtils.format(server.getRatisRoles()) : "STANDALONE";
+
+    // If Ratis is disabled
+    if (server == null) {
+      return getRatisRolesException("Ratis is disabled");
+    }
+
+    // To attempt to find the SCM Leader,
+    // and if the Leader is not found
+    // return Leader is not found message.
+    RaftServer.Division division = server.getDivision();
+    RaftPeer leader = getLeader(division);
+    if (leader == null) {
+      return getRatisRolesException("No leader found");
+    }
+
+    // If the SCMRatisServer is stopped, return a service stopped message.
+    if (server.isStopped()) {
+      return getRatisRolesException("Server is shutting down");
+    }
+
+    // Attempt to retrieve role information.
+    try {
+      List<String> ratisRoles = server.getRatisRoles();
+      List<List<String>> result = new ArrayList<>();
+      for (String role : ratisRoles) {
+        String[] roleArr = role.split(":");
+        List<String> scmInfo = new ArrayList<>();
+        // Host Name
+        scmInfo.add(roleArr[0]);
+        // Node ID
+        scmInfo.add(roleArr[3]);
+        // Ratis Port
+        scmInfo.add(roleArr[1]);
+        // Role
+        scmInfo.add(roleArr[2]);
+        result.add(scmInfo);
+      }
+      return result;
+    } catch (Exception e) {
+      LOG.error("Failed to getRatisRoles.", e);
+      return getRatisRolesException("Exception Occurred, " + e.getMessage());
+    }
+  }
+
+  public RaftPeer getLeader(RaftServer.Division division) {
+    if (division.getInfo().isLeader()) {
+      return division.getPeer();
+    } else {
+      DivisionInfo info = division.getInfo();
+      RaftProtos.RoleInfoProto roleInfoProto = info.getRoleInfoProto();
+      RaftProtos.FollowerInfoProto followerInfo = 
roleInfoProto.getFollowerInfo();
+      RaftProtos.ServerRpcProto leaderInfo = followerInfo.getLeaderInfo();
+      RaftProtos.RaftPeerProto peerLeaderId = leaderInfo.getId();
+      ByteString leaderId = peerLeaderId.getId();
+      return leaderId.isEmpty() ? null :
+          division.getRaftConf().getPeer(RaftPeerId.valueOf(leaderId));
+    }
+  }

Review Comment:
   I don't think it's necessary to make different cases whether the current 
server is a leader or not. Maybe this can be simplified similar to 
`OzoneManagerRatisServer#getLeaderId` used in `OzoneManager#getRatisRoles`. 
   
   ```suggestion
     public RaftPeerId getLeaderId(RaftServer.Division division) {
       return division.getInfo().getLeaderId();
     }
   ```



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