ChenSammi commented on code in PR #9479:
URL: https://github.com/apache/ozone/pull/9479#discussion_r2650375664


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/helpers/ContainerUtils.java:
##########
@@ -166,25 +169,71 @@ public static synchronized void writeDatanodeDetailsTo(
    * @return {@link DatanodeDetails}
    * @throws IOException If the id file is malformed or other I/O exceptions
    */
-  public static synchronized DatanodeDetails readDatanodeDetailsFrom(File path)
-      throws IOException {
+  public static synchronized DatanodeDetails readDatanodeDetailsFrom(
+      File path, ConfigurationSource conf) throws IOException {
     if (!path.exists()) {
       throw new IOException("Datanode ID file not found.");
     }
     try {
       return DatanodeIdYaml.readDatanodeIdFile(path);
     } catch (IOException e) {
-      LOG.warn("Error loading DatanodeDetails yaml from {}",
-          path.getAbsolutePath(), e);
-      // Try to load as protobuf before giving up
-      try (InputStream in = Files.newInputStream(path.toPath())) {
-        return DatanodeDetails.getFromProtoBuf(
-            HddsProtos.DatanodeDetailsProto.parseFrom(in));
-      } catch (IOException io) {
-        throw new IOException("Failed to parse DatanodeDetails from "
-            + path.getAbsolutePath(), io);
+      LOG.warn("Failed to read Datanode ID file as YAML. Reason: {}. " +
+          "Attempting recovery.", e.getMessage());
+      try {
+        return recoverDatanodeDetailsFromVersionFile(path, conf);
+      } catch (IOException recoveryEx) {
+        LOG.warn("Datanode ID recovery from VERSION file failed. Reason: {}. " 
+
+            "Falling back to reading as Protobuf.", recoveryEx.getMessage());
+        try {
+          return readDatanodeDetailsFromProto(path);
+        } catch (IOException io) {
+          throw new IOException("Failed to parse DatanodeDetails from "
+              + path.getAbsolutePath(), io);
+        }
+      }
+    }
+  }
+
+  /**
+   * Recover DatanodeDetails from VERSION file.
+   */
+  private static DatanodeDetails recoverDatanodeDetailsFromVersionFile(
+      File path, ConfigurationSource conf) throws IOException {
+    LOG.info("Attempting to recover Datanode ID from VERSION file.");
+    String dnUuid = null;
+    Collection<String> dataNodeDirs =
+        HddsServerUtil.getDatanodeStorageDirs(conf);
+    if (dataNodeDirs.isEmpty()) {

Review Comment:
   HddsServerUtil.getDatanodeStorageDirs checks whether return list is empty 
before return, so the double check here can be omitted.  



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