Browse Source

HADOOP-13828. Implement getFileChecksum(path, length) for ViewFileSystem. Contributed by Manoj Govindassamy.

Andrew Wang 8 years ago
parent
commit
a2b1ff0257

+ 6 - 0
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ChRootedFileSystem.java

@@ -221,6 +221,12 @@ class ChRootedFileSystem extends FilterFileSystem {
     return super.getFileChecksum(fullPath(f));
   }
 
+  @Override
+  public FileChecksum getFileChecksum(final Path f, final long length)
+      throws IOException {
+    return super.getFileChecksum(fullPath(f), length);
+  }
+
   @Override
   public FileStatus getFileStatus(final Path f) 
       throws IOException {

+ 9 - 0
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java

@@ -350,6 +350,15 @@ public class ViewFileSystem extends FileSystem {
     return res.targetFileSystem.getFileChecksum(res.remainingPath);
   }
 
+  @Override
+  public FileChecksum getFileChecksum(final Path f, final long length)
+      throws AccessControlException, FileNotFoundException,
+      IOException {
+    InodeTree.ResolveResult<FileSystem> res =
+        fsState.resolve(getUriPath(f), true);
+    return res.targetFileSystem.getFileChecksum(res.remainingPath, length);
+  }
+
   private static FileStatus fixFileStatus(FileStatus orig,
       Path qualified) throws IOException {
     // FileStatus#getPath is a fully qualified path relative to the root of

+ 29 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemHdfs.java

@@ -31,6 +31,8 @@ import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.crypto.key.JavaKeyStoreProvider;
 import org.apache.hadoop.fs.CommonConfigurationKeys;
 import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
+import org.apache.hadoop.fs.FileChecksum;
+import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.FileSystemTestHelper;
 import org.apache.hadoop.fs.FsConstants;
@@ -218,4 +220,31 @@ public class TestViewFileSystemHdfs extends ViewFileSystemBaseTest {
     DFSTestUtil.FsShellRun("-df /", 0, null, newConf);
     DFSTestUtil.FsShellRun("-df", 0, null, newConf);
   }
+
+  @Test
+  public void testFileChecksum() throws IOException {
+    ViewFileSystem viewFs = (ViewFileSystem) fsView;
+    Path mountDataRootPath = new Path("/data");
+    String fsTargetFileName = "debug.log";
+    Path fsTargetFilePath = new Path(targetTestRoot, "data/debug.log");
+    Path mountDataFilePath = new Path(mountDataRootPath, fsTargetFileName);
+
+    fileSystemTestHelper.createFile(fsTarget, fsTargetFilePath);
+    FileStatus fileStatus = viewFs.getFileStatus(mountDataFilePath);
+    long fileLength = fileStatus.getLen();
+
+    FileChecksum fileChecksumViaViewFs =
+        viewFs.getFileChecksum(mountDataFilePath);
+    FileChecksum fileChecksumViaTargetFs =
+        fsTarget.getFileChecksum(fsTargetFilePath);
+    Assert.assertTrue("File checksum not matching!",
+        fileChecksumViaViewFs.equals(fileChecksumViaTargetFs));
+
+    fileChecksumViaViewFs =
+        viewFs.getFileChecksum(mountDataFilePath, fileLength / 2);
+    fileChecksumViaTargetFs =
+        fsTarget.getFileChecksum(fsTargetFilePath, fileLength / 2);
+    Assert.assertTrue("File checksum not matching!",
+        fileChecksumViaViewFs.equals(fileChecksumViaTargetFs));
+  }
 }