GeorgeJahad commented on code in PR #3980:
URL: https://github.com/apache/ozone/pull/3980#discussion_r1140547044


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OMDBCheckpointServlet.java:
##########
@@ -82,4 +113,166 @@ public void init() throws ServletException {
         allowedGroups,
         om.isSpnegoEnabled());
   }
+
+  @Override
+  public void writeDbDataToStream(DBCheckpoint checkpoint,
+                                  HttpServletRequest request,
+                                  OutputStream destination)
+      throws IOException, InterruptedException {
+    // Map of inodes to path.
+    Map<Object, Path> copyFiles = new HashMap<>();
+    // Map of link to path.
+    Map<Path, Path> hardLinkFiles = new HashMap<>();
+
+    getFilesForArchive(checkpoint, copyFiles, hardLinkFiles,
+        includeSnapshotData(request));
+
+    try (TarArchiveOutputStream archiveOutputStream =
+            new TarArchiveOutputStream(destination)) {
+      archiveOutputStream
+          .setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
+      writeFilesToArchive(copyFiles, hardLinkFiles, archiveOutputStream);
+    }
+  }
+
+  private void getFilesForArchive(DBCheckpoint checkpoint,
+                                  Map<Object, Path> copyFiles,
+                                  Map<Path, Path> hardLinkFiles,
+                                  boolean includeSnapshotData)
+      throws IOException, InterruptedException {
+
+    // Get the active fs files.
+    Path dir = checkpoint.getCheckpointLocation();
+    processDir(dir, copyFiles, hardLinkFiles, new HashSet<>());
+
+    if (!includeSnapshotData) {
+      return;
+    }
+
+    // Get the snapshot files.
+    Set<Path> snapshotPaths = waitForSnapshotDirs(checkpoint);
+    Path snapshotDir = Paths.get(OMStorage.getOmDbDir(getConf()).toString(),
+        OM_SNAPSHOT_DIR);
+    processDir(snapshotDir, copyFiles, hardLinkFiles, snapshotPaths);
+  }
+
+  /**
+   * The snapshotInfo table may contain a snapshot that
+   * doesn't yet exist on the fs, so wait a few seconds for it.
+   * @param checkpoint Checkpoint containing snapshot entries expected.
+   * @return Set of expected snapshot dirs.
+   */
+  private Set<Path> waitForSnapshotDirs(DBCheckpoint checkpoint)
+      throws IOException, InterruptedException {
+
+    OzoneConfiguration conf = getConf();
+
+    Set<Path> snapshotPaths = new HashSet<>();
+
+    // get snapshotInfo entries
+    OmMetadataManagerImpl checkpointMetadataManager =
+        OmMetadataManagerImpl.createCheckpointMetadataManager(
+            conf, checkpoint);
+    try (TableIterator<String, ? extends Table.KeyValue<String, SnapshotInfo>>
+        iterator = checkpointMetadataManager
+        .getSnapshotInfoTable().iterator()) {
+
+      // For each entry, wait for corresponding directory.
+      while (iterator.hasNext()) {
+        Table.KeyValue<String, SnapshotInfo> entry = iterator.next();
+        Path path = Paths.get(getSnapshotPath(conf, entry.getValue()));
+        waitForDirToExist(path);
+        snapshotPaths.add(path);
+      }
+    }
+    return snapshotPaths;
+  }
+
+  private void waitForDirToExist(Path dir)
+      throws IOException, InterruptedException {
+    long endTime = System.currentTimeMillis() +
+        Duration.parse(DURATION_TO_WAIT_FOR_DIRECTORY).toMillis();
+    while (!dir.toFile().exists()) {
+      Thread.sleep(100);
+      if (System.currentTimeMillis() > endTime) {
+        throw new IOException("snapshot dir doesn't exist: " + dir);
+      }
+    }
+  }
+
+  @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
+  private void processDir(Path dir, Map<Object, Path> copyFiles,
+                          Map<Path, Path> hardLinkFiles,
+                          Set<Path> snapshotPaths)
+      throws IOException {
+    try (Stream<Path> files = Files.list(dir)) {
+      for (Path file : files.collect(Collectors.toList())) {
+        if (file.toFile().isDirectory()) {
+          // Skip any unexpected snapshot files.
+          if (file.getParent().toString().endsWith(OM_SNAPSHOT_CHECKPOINT_DIR)
+              && !snapshotPaths.contains(file)) {
+            continue;
+          }

Review Comment:
   These are only "unexpected" because they created after after the active fs 
was checkpointed and so shouldn't be copied over.  So they could exist without 
any error in the code, but I'll add a LOG.debug().



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