jojochuang commented on code in PR #9141:
URL: https://github.com/apache/ozone/pull/9141#discussion_r2430876869


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/OmSnapshotLocalDataManager.java:
##########
@@ -95,14 +123,100 @@ public void createNewOmSnapshotLocalDataFile(RDBStore 
snapshotStore, SnapshotInf
   }
 
   public OmSnapshotLocalData getOmSnapshotLocalData(SnapshotInfo snapshotInfo) 
throws IOException {
-    Path snapshotLocalDataPath = 
Paths.get(getSnapshotLocalPropertyYamlPath(snapshotInfo));
-    return snapshotLocalDataSerializer.load(snapshotLocalDataPath.toFile());
+    return getOmSnapshotLocalData(snapshotInfo.getSnapshotId());
+  }
+
+  public OmSnapshotLocalData getOmSnapshotLocalData(UUID snapshotId) throws 
IOException {
+    Path snapshotLocalDataPath = 
Paths.get(getSnapshotLocalPropertyYamlPath(snapshotId));
+    OmSnapshotLocalData snapshotLocalData = 
snapshotLocalDataSerializer.load(snapshotLocalDataPath.toFile());
+    if (!Objects.equals(snapshotLocalData.getSnapshotId(), snapshotId)) {
+      throw new IOException("SnapshotId in path : " + snapshotLocalDataPath + 
" contains snapshotLocalData " +
+          "corresponding to snapshotId " + snapshotLocalData.getSnapshotId() + 
". Expected snapshotId " + snapshotId);
+    }
+    return snapshotLocalData;
   }
 
   public OmSnapshotLocalData getOmSnapshotLocalData(File snapshotDataPath) 
throws IOException {
     return snapshotLocalDataSerializer.load(snapshotDataPath);
   }
 
+  private LocalDataVersionNode getVersionNode(UUID snapshotId, int version) {
+    if (!versionNodeMap.containsKey(snapshotId)) {
+      return null;
+    }
+    return versionNodeMap.get(snapshotId).getVersionNode(version);
+  }
+
+  private void addSnapshotVersionMeta(UUID snapshotId, SnapshotVersionsMeta 
snapshotVersionsMeta)
+      throws IOException {
+    if (!versionNodeMap.containsKey(snapshotId)) {
+      for (LocalDataVersionNode versionNode : 
snapshotVersionsMeta.getSnapshotVersions().values()) {
+        if (getVersionNode(versionNode.snapshotId, versionNode.version) != 
null) {
+          throw new IOException("Unable to add " + versionNode + " since it 
already exists");
+        }
+        LocalDataVersionNode previousVersionNode = 
versionNode.previousSnapshotId == null ? null :
+            getVersionNode(versionNode.previousSnapshotId, 
versionNode.previousSnapshotVersion);
+        if (versionNode.previousSnapshotId != null && previousVersionNode == 
null) {
+          throw new IOException("Unable to add " + versionNode + " since 
previous snapshot with version hasn't been " +
+              "loaded");
+        }
+        localDataGraph.addNode(versionNode);
+        if (previousVersionNode != null) {
+          localDataGraph.putEdge(versionNode, previousVersionNode);
+        }
+      }
+      versionNodeMap.put(snapshotId, snapshotVersionsMeta);
+    }
+  }
+
+  public void addVersionNodeWithDependents(OmSnapshotLocalData 
snapshotLocalData) throws IOException {
+    if (versionNodeMap.containsKey(snapshotLocalData.getSnapshotId())) {
+      return;
+    }
+    Set<UUID> visitedSnapshotIds = new HashSet<>();
+    Stack<Pair<UUID, SnapshotVersionsMeta>> stack = new Stack<>();
+    stack.push(Pair.of(snapshotLocalData.getSnapshotId(), new 
SnapshotVersionsMeta(snapshotLocalData)));
+    while (!stack.isEmpty()) {
+      Pair<UUID, SnapshotVersionsMeta> versionNodeToProcess = stack.peek();
+      UUID snapId = versionNodeToProcess.getLeft();
+      SnapshotVersionsMeta snapshotVersionsMeta = 
versionNodeToProcess.getRight();
+      if (visitedSnapshotIds.contains(snapId)) {
+        addSnapshotVersionMeta(snapId, snapshotVersionsMeta);
+        stack.pop();
+      } else {
+        UUID prevSnapId = snapshotVersionsMeta.getPreviousSnapshotId();
+        if (prevSnapId != null && !versionNodeMap.containsKey(prevSnapId)) {
+          OmSnapshotLocalData prevSnapshotLocalData = 
getOmSnapshotLocalData(prevSnapId);
+          stack.push(Pair.of(prevSnapshotLocalData.getSnapshotId(), new 
SnapshotVersionsMeta(prevSnapshotLocalData)));
+        }
+        visitedSnapshotIds.add(snapId);
+      }
+    }
+  }
+
+  private void init() throws IOException {
+    RDBStore store = (RDBStore) omMetadataManager.getStore();
+    String checkpointPrefix = store.getDbLocation().getName();
+    File snapshotDir = new File(store.getSnapshotsParentDir());
+    File[] localDataFiles = snapshotDir.listFiles(
+        (dir, name) -> name.startsWith(checkpointPrefix) && 
name.endsWith(YAML_FILE_EXTENSION));
+    if (localDataFiles == null) {
+      throw new IOException("Error while listing yaml files inside directory: 
" + snapshotDir.getAbsolutePath());
+    }
+    Arrays.sort(localDataFiles, Comparator.comparing(File::getName));
+    for (File localDataFile : localDataFiles) {

Review Comment:
   process snapshot metadata in the order of snapshot UUID which follows 
creation time. Older/earlier snapshots processed first.



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