chungen0126 commented on code in PR #11505:
URL: https://github.com/apache/ozone/pull/11505#discussion_r4540652682


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServerImpl.java:
##########
@@ -147,6 +150,40 @@ public static void initialize(String clusterId, String 
scmId,
     }
   }
 
+  /**
+   * Fail if the SCM Ratis storage directory contains any Raft group directory.
+   * A new cluster has none, so any existing one is stale.
+   */
+  public static void verifyNoRaftGroup(ConfigurationSource conf) throws 
IOException {
+    verifyNoOtherRaftGroup(conf, null);
+  }
+
+  /**
+   * Fail if the SCM Ratis storage directory contains a Raft group directory 
other than the given one.
+   * SCM has a single Raft group derived from the cluster ID.
+   *
+   * @param groupId the only group allowed in the directory, or null to allow 
none
+   */
+  private static void verifyNoOtherRaftGroup(ConfigurationSource conf, 
RaftGroupId groupId) throws IOException {
+    final File ratisDir = new File(SCMHAUtils.getSCMRatisDirectory(conf));
+    final File[] children = ratisDir.listFiles(File::isDirectory);
+    if (children == null) {
+      return;
+    }
+    for (File child : children) {
+      final String name = child.getName();
+      try {
+        if (!RaftGroupId.valueOf(UUID.fromString(name)).equals(groupId)) {
+          throw new IOException("Found stal Raft group directory " + child + " 
in the SCM Ratis storage directory "

Review Comment:
   typo
   
   ```suggestion
             throw new IOException("Found stale Raft group directory " + child 
+ " in the SCM Ratis storage directory "
   ```



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServerImpl.java:
##########
@@ -147,6 +150,40 @@ public static void initialize(String clusterId, String 
scmId,
     }
   }
 
+  /**
+   * Fail if the SCM Ratis storage directory contains any Raft group directory.
+   * A new cluster has none, so any existing one is stale.
+   */
+  public static void verifyNoRaftGroup(ConfigurationSource conf) throws 
IOException {
+    verifyNoOtherRaftGroup(conf, null);
+  }
+
+  /**
+   * Fail if the SCM Ratis storage directory contains a Raft group directory 
other than the given one.
+   * SCM has a single Raft group derived from the cluster ID.
+   *
+   * @param groupId the only group allowed in the directory, or null to allow 
none
+   */
+  private static void verifyNoOtherRaftGroup(ConfigurationSource conf, 
RaftGroupId groupId) throws IOException {
+    final File ratisDir = new File(SCMHAUtils.getSCMRatisDirectory(conf));
+    final File[] children = ratisDir.listFiles(File::isDirectory);
+    if (children == null) {
+      return;
+    }
+    for (File child : children) {
+      final String name = child.getName();
+      try {
+        if (!RaftGroupId.valueOf(UUID.fromString(name)).equals(groupId)) {

Review Comment:
   I suggest separating `RaftGroupId.valueOf(UUID.fromString(name))` and 
`.equals(groupId)` into three distinct steps. First, parse the `UUID` to check 
if it's not a Raft group directory skip it. Second, if groupId is null, we can 
skip it. Third, compare it with groupId and throw an exception if they don't 
match.
   
   This will make the logic much clearer.



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