This is an automated email from the ASF dual-hosted git repository.

siyao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new ec280f4168 HDDS-9157. [Snapshot] Print correct snapshot path in `fs 
-createSnapshot` (#5174)
ec280f4168 is described below

commit ec280f4168f8aa4034b440f0dc6a041041c81d83
Author: Siyao Meng <[email protected]>
AuthorDate: Tue Aug 22 11:23:00 2023 -0700

    HDDS-9157. [Snapshot] Print correct snapshot path in `fs -createSnapshot` 
(#5174)
---
 .../hadoop/ozone/om/helpers/OzoneFSUtils.java      |  8 +++
 .../hadoop/fs/ozone/TestOzoneFsSnapshot.java       | 34 +++++++++++++
 .../request/snapshot/OMSnapshotCreateRequest.java  |  2 +-
 .../hadoop/fs/ozone/BasicOzoneFileSystem.java      |  7 ++-
 .../fs/ozone/BasicRootedOzoneFileSystem.java       |  7 ++-
 .../hadoop/fs/ozone/TestBasicOzoneFileSystems.java | 59 +++++++++++++++++++++-
 6 files changed, 110 insertions(+), 7 deletions(-)

diff --git 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OzoneFSUtils.java
 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OzoneFSUtils.java
index c20f611473..624e479ce3 100644
--- 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OzoneFSUtils.java
+++ 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OzoneFSUtils.java
@@ -284,4 +284,12 @@ public final class OzoneFSUtils {
   public static String generateUniqueTempSnapshotName() {
     return "temp" + UUID.randomUUID() + SnapshotInfo.generateName(Time.now());
   }
+
+  public static Path trimPathToDepth(Path path, int maxDepth) {
+    Path res = path;
+    while (res.depth() > maxDepth) {
+      res = res.getParent();
+    }
+    return res;
+  }
 }
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsSnapshot.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsSnapshot.java
index 8a3be25b63..62ed9a800b 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsSnapshot.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsSnapshot.java
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Paths;
 import java.util.UUID;
 import java.util.stream.Stream;
 
@@ -138,6 +139,39 @@ public class TestOzoneFsSnapshot {
     Assertions.assertEquals(1, res);
   }
 
+  @Test
+  public void testCreateSnapshotWithSubDirInput() throws Exception {
+    // Test that:
+    // $ ozone fs -createSnapshot ofs://om/vol1/buck2/dir3/ snap1
+    //
+    // should print:
+    // Created snapshot ofs://om/vol1/buck2/.snapshot/snap1
+    //
+    // rather than:
+    // Created snapshot ofs://om/vol1/buck2/dir3/.snapshot/snap1
+
+    String snapshotName = "snap-" + RandomStringUtils.randomNumeric(5);
+
+    String dirPath = BUCKET_PATH + "/dir1/";
+
+    int res = ToolRunner.run(shell, new String[] {
+        "-mkdir", "-p", dirPath});
+    Assertions.assertEquals(0, res);
+
+    try (GenericTestUtils.SystemOutCapturer capture =
+             new GenericTestUtils.SystemOutCapturer()) {
+      res = ToolRunner.run(shell, new String[] {
+          "-createSnapshot", dirPath, snapshotName});
+      // Asserts that create request succeeded
+      Assertions.assertEquals(0, res);
+
+      String expectedSnapshotPath = Paths.get(
+          BUCKET_PATH, OM_SNAPSHOT_INDICATOR, snapshotName).toString();
+      String out = capture.getOutput().trim();
+      Assertions.assertTrue(out.endsWith(expectedSnapshotPath));
+    }
+  }
+
   /**
    * Create snapshot should succeed.
    * 1st case: valid snapshot name
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/snapshot/OMSnapshotCreateRequest.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/snapshot/OMSnapshotCreateRequest.java
index ebe1dbc80a..95c78e2be1 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/snapshot/OMSnapshotCreateRequest.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/snapshot/OMSnapshotCreateRequest.java
@@ -188,7 +188,7 @@ public class OMSnapshotCreateRequest extends 
OMClientRequest {
     // Performing audit logging outside the lock.
     auditLog(auditLogger, buildAuditMessage(OMAction.CREATE_SNAPSHOT,
         snapshotInfo.toAuditMap(), exception, userInfo));
-    
+
     if (exception == null) {
       LOG.info("Created snapshot: '{}' with snapshotId: '{}' under path '{}'",
           snapshotName, snapshotInfo.getSnapshotId(), snapshotPath);
diff --git 
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
 
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
index f095d42713..62fbfeac16 100644
--- 
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
+++ 
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
@@ -128,6 +128,8 @@ public class BasicOzoneFileSystem extends FileSystem {
       "o3fs://bucket.volume.om-host.example.com:5678/key  OR " +
       "o3fs://bucket.volume.omServiceId/key";
 
+  private static final int PATH_DEPTH_TO_BUCKET = 0;
+
   @Override
   public void initialize(URI name, Configuration conf) throws IOException {
     super.initialize(name, conf);
@@ -934,8 +936,9 @@ public class BasicOzoneFileSystem extends FileSystem {
   @Override
   public Path createSnapshot(Path path, String snapshotName)
           throws IOException {
-    String snapshot = adapter.createSnapshot(pathToKey(path), snapshotName);
-    return new Path(path,
+    String snapshot = getAdapter()
+        .createSnapshot(pathToKey(path), snapshotName);
+    return new Path(OzoneFSUtils.trimPathToDepth(path, PATH_DEPTH_TO_BUCKET),
         OM_SNAPSHOT_INDICATOR + OZONE_URI_DELIMITER + snapshot);
   }
 
diff --git 
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
 
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
index e560b47663..c3ce1ad0d8 100644
--- 
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
+++ 
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
@@ -127,6 +127,8 @@ public class BasicRootedOzoneFileSystem extends FileSystem {
       "ofs://om-host.example.com/path/to/key  OR " +
       "ofs://om-host.example.com:5678/path/to/key";
 
+  private static final int PATH_DEPTH_TO_BUCKET = 2;
+
   @Override
   public void initialize(URI name, Configuration conf) throws IOException {
     super.initialize(name, conf);
@@ -509,8 +511,9 @@ public class BasicRootedOzoneFileSystem extends FileSystem {
   @Override
   public Path createSnapshot(Path path, String snapshotName)
           throws IOException {
-    String snapshot = adapter.createSnapshot(pathToKey(path), snapshotName);
-    return new Path(path,
+    String snapshot = getAdapter()
+        .createSnapshot(pathToKey(path), snapshotName);
+    return new Path(OzoneFSUtils.trimPathToDepth(path, PATH_DEPTH_TO_BUCKET),
         OM_SNAPSHOT_INDICATOR + OZONE_URI_DELIMITER + snapshot);
   }
 
diff --git 
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneFileSystems.java
 
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneFileSystems.java
index 0414fe52cb..5a3f752865 100644
--- 
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneFileSystems.java
+++ 
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneFileSystems.java
@@ -26,13 +26,17 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
+import org.mockito.Mockito;
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
 
 import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SCM_BLOCK_SIZE;
 import static 
org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SCM_BLOCK_SIZE_DEFAULT;
+import static org.apache.hadoop.ozone.OzoneConsts.OM_SNAPSHOT_INDICATOR;
 import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
 
 /**
  * Unit test for Basic*OzoneFileSystem.
@@ -76,10 +80,61 @@ public class TestBasicOzoneFileSystems {
   // test for filesystem pseduo-posix symlink support
   @Test
   public void testFileSystemPosixSymlinkSupport() {
-    if (subject.getClass() == BasicRootedOzoneFileSystem.class) {
+    if (subject instanceof BasicRootedOzoneFileSystem) {
       Assert.assertTrue(subject.supportsSymlinks());
-    } else {
+    } else if (subject instanceof BasicOzoneFileSystem) {
       Assert.assertFalse(subject.supportsSymlinks());
+    } else {
+      Assert.fail("Test case not implemented for FileSystem: " +
+          subject.getClass().getSimpleName());
+    }
+  }
+
+  @Test
+  public void testCreateSnapshotReturnPath() throws IOException {
+    final String snapshotName = "snap1";
+
+    if (subject instanceof BasicRootedOzoneFileSystem) {
+      BasicRootedOzoneClientAdapterImpl adapter =
+          Mockito.mock(BasicRootedOzoneClientAdapterImpl.class);
+      Mockito.doReturn(snapshotName).when(adapter).createSnapshot(any(), 
any());
+
+      BasicRootedOzoneFileSystem ofs =
+          Mockito.spy((BasicRootedOzoneFileSystem) subject);
+      Mockito.when(ofs.getAdapter()).thenReturn(adapter);
+
+      Path ofsBucketStr = new Path("ofs://om/vol1/buck1/");
+      Path ofsDir1 = new Path(ofsBucketStr, "dir1");
+      Path res = ofs.createSnapshot(new Path(ofsDir1, snapshotName));
+
+      Path expectedSnapshotRoot = new Path(ofsBucketStr, 
OM_SNAPSHOT_INDICATOR);
+      Path expectedSnapshotPath = new Path(expectedSnapshotRoot, snapshotName);
+
+      // Return value path should be "ofs://om/vol1/buck1/.snapshot/snap1"
+      // without the subdirectory "dir1" in the Path.
+      Assert.assertEquals(expectedSnapshotPath, res);
+    } else if (subject instanceof BasicOzoneFileSystem) {
+      BasicOzoneClientAdapterImpl adapter =
+          Mockito.mock(BasicOzoneClientAdapterImpl.class);
+      Mockito.doReturn(snapshotName).when(adapter).createSnapshot(any(), 
any());
+
+      BasicOzoneFileSystem o3fs = Mockito.spy((BasicOzoneFileSystem) subject);
+      Mockito.when(o3fs.getAdapter()).thenReturn(adapter);
+
+      Path o3fsBucketStr = new Path("o3fs://buck1.vol1.om/");
+      Path o3fsDir1 = new Path(o3fsBucketStr, "dir1");
+      Path res = o3fs.createSnapshot(new Path(o3fsDir1, snapshotName));
+
+      Path expectedSnapshotRoot =
+          new Path(o3fsBucketStr, OM_SNAPSHOT_INDICATOR);
+      Path expectedSnapshotPath = new Path(expectedSnapshotRoot, snapshotName);
+
+      // Return value path should be "o3fs://buck1.vol1.om/.snapshot/snap1"
+      // without the subdirectory "dir1" in the Path.
+      Assert.assertEquals(expectedSnapshotPath, res);
+    } else {
+      Assert.fail("Test case not implemented for FileSystem: " +
+          subject.getClass().getSimpleName());
     }
   }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to