Search in sources :

Example 1 with MountPoint

use of org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint in project hadoop by apache.

the class ViewFileSystemBaseTest method testViewFileSystemUtil.

@Test(expected = NotInMountpointException.class)
public void testViewFileSystemUtil() throws Exception {
    Configuration newConf = new Configuration(conf);
    FileSystem fileSystem = FileSystem.get(FsConstants.LOCAL_FS_URI, newConf);
    Assert.assertFalse("Unexpected FileSystem: " + fileSystem, ViewFileSystemUtil.isViewFileSystem(fileSystem));
    fileSystem = FileSystem.get(FsConstants.VIEWFS_URI, newConf);
    Assert.assertTrue("Unexpected FileSystem: " + fileSystem, ViewFileSystemUtil.isViewFileSystem(fileSystem));
    // Case 1: Verify FsStatus of root path returns all MountPoints status.
    Map<MountPoint, FsStatus> mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, InodeTree.SlashPath);
    Assert.assertEquals(getExpectedMountPoints(), mountPointFsStatusMap.size());
    // Case 2: Verify FsStatus of an internal dir returns all
    // MountPoints status.
    mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/internalDir"));
    Assert.assertEquals(getExpectedMountPoints(), mountPointFsStatusMap.size());
    // Case 3: Verify FsStatus of a matching MountPoint returns exactly
    // the corresponding MountPoint status.
    mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/user"));
    Assert.assertEquals(1, mountPointFsStatusMap.size());
    for (Entry<MountPoint, FsStatus> entry : mountPointFsStatusMap.entrySet()) {
        Assert.assertEquals(entry.getKey().getMountedOnPath().toString(), "/user");
    }
    // Case 4: Verify FsStatus of a path over a MountPoint returns the
    // corresponding MountPoint status.
    mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/user/cloud"));
    Assert.assertEquals(1, mountPointFsStatusMap.size());
    for (Entry<MountPoint, FsStatus> entry : mountPointFsStatusMap.entrySet()) {
        Assert.assertEquals(entry.getKey().getMountedOnPath().toString(), "/user");
    }
    // Case 5: Verify FsStatus of any level of an internal dir
    // returns all MountPoints status.
    mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/internalDir/internalDir2"));
    Assert.assertEquals(getExpectedMountPoints(), mountPointFsStatusMap.size());
    // Case 6: Verify FsStatus of a MountPoint URI returns
    // the MountPoint status.
    mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("viewfs:/user/"));
    Assert.assertEquals(1, mountPointFsStatusMap.size());
    for (Entry<MountPoint, FsStatus> entry : mountPointFsStatusMap.entrySet()) {
        Assert.assertEquals(entry.getKey().getMountedOnPath().toString(), "/user");
    }
    // Case 7: Verify FsStatus of a non MountPoint path throws exception
    ViewFileSystemUtil.getStatus(fileSystem, new Path("/non-existing"));
}
Also used : MountPoint(org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint) Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) FsStatus(org.apache.hadoop.fs.FsStatus) Test(org.junit.Test)

Example 2 with MountPoint

use of org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint in project hadoop by apache.

the class ViewFileSystemUtil method getStatus.

/**
   * Get FsStatus for all ViewFsMountPoints matching path for the given
   * ViewFileSystem.
   *
   * Say ViewFileSystem has following mount points configured
   *  (1) hdfs://NN0_host:port/sales mounted on /dept/sales
   *  (2) hdfs://NN1_host:port/marketing mounted on /dept/marketing
   *  (3) hdfs://NN2_host:port/eng_usa mounted on /dept/eng/usa
   *  (4) hdfs://NN3_host:port/eng_asia mounted on /dept/eng/asia
   *
   * For the above config, here is a sample list of paths and their matching
   * mount points while getting FsStatus
   *
   *  Path                  Description                      Matching MountPoint
   *
   *  "/"                   Root ViewFileSystem lists all    (1), (2), (3), (4)
   *                         mount points.
   *
   *  "/dept"               Not a mount point, but a valid   (1), (2), (3), (4)
   *                         internal dir in the mount tree
   *                         and resolved down to "/" path.
   *
   *  "/dept/sales"         Matches a mount point            (1)
   *
   *  "/dept/sales/india"   Path is over a valid mount point (1)
   *                         and resolved down to
   *                         "/dept/sales"
   *
   *  "/dept/eng"           Not a mount point, but a valid   (1), (2), (3), (4)
   *                         internal dir in the mount tree
   *                         and resolved down to "/" path.
   *
   *  "/erp"                Doesn't match or leads to or
   *                         over any valid mount points     None
   *
   *
   * @param fileSystem - ViewFileSystem on which mount point exists
   * @param path - URI for which FsStatus is requested
   * @return Map of ViewFsMountPoint and FsStatus
   */
public static Map<MountPoint, FsStatus> getStatus(FileSystem fileSystem, Path path) throws IOException {
    if (!isViewFileSystem(fileSystem)) {
        throw new UnsupportedFileSystemException("FileSystem '" + fileSystem.getUri() + "'is not a ViewFileSystem.");
    }
    ViewFileSystem viewFileSystem = (ViewFileSystem) fileSystem;
    String viewFsUriPath = viewFileSystem.getUriPath(path);
    boolean isPathOverMountPoint = false;
    boolean isPathLeadingToMountPoint = false;
    boolean isPathIncludesAllMountPoint = false;
    Map<MountPoint, FsStatus> mountPointMap = new HashMap<>();
    for (MountPoint mountPoint : viewFileSystem.getMountPoints()) {
        String[] mountPointPathComponents = InodeTree.breakIntoPathComponents(mountPoint.getMountedOnPath().toString());
        String[] incomingPathComponents = InodeTree.breakIntoPathComponents(viewFsUriPath);
        int pathCompIndex;
        for (pathCompIndex = 0; pathCompIndex < mountPointPathComponents.length && pathCompIndex < incomingPathComponents.length; pathCompIndex++) {
            if (!mountPointPathComponents[pathCompIndex].equals(incomingPathComponents[pathCompIndex])) {
                break;
            }
        }
        if (pathCompIndex >= mountPointPathComponents.length) {
            // Patch matches or over a valid mount point
            isPathOverMountPoint = true;
            mountPointMap.clear();
            updateMountPointFsStatus(viewFileSystem, mountPointMap, mountPoint, new Path(viewFsUriPath));
            break;
        } else {
            if (pathCompIndex > 1) {
                // Path is in the mount tree
                isPathLeadingToMountPoint = true;
            } else if (incomingPathComponents.length <= 1) {
                // Special case of "/" path
                isPathIncludesAllMountPoint = true;
            }
            updateMountPointFsStatus(viewFileSystem, mountPointMap, mountPoint, mountPoint.getMountedOnPath());
        }
    }
    if (!isPathOverMountPoint && !isPathLeadingToMountPoint && !isPathIncludesAllMountPoint) {
        throw new NotInMountpointException(path, "getStatus");
    }
    return mountPointMap;
}
Also used : MountPoint(org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint) Path(org.apache.hadoop.fs.Path) HashMap(java.util.HashMap) UnsupportedFileSystemException(org.apache.hadoop.fs.UnsupportedFileSystemException) FsStatus(org.apache.hadoop.fs.FsStatus) MountPoint(org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint)

Example 3 with MountPoint

use of org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint in project hadoop by apache.

the class ViewFileSystemBaseTest method testGetMountPoints.

@Test
public void testGetMountPoints() {
    ViewFileSystem viewfs = (ViewFileSystem) fsView;
    MountPoint[] mountPoints = viewfs.getMountPoints();
    for (MountPoint mountPoint : mountPoints) {
        LOG.info("MountPoint: " + mountPoint.getMountedOnPath() + " => " + mountPoint.getTargetFileSystemURIs()[0]);
    }
    Assert.assertEquals(getExpectedMountPoints(), mountPoints.length);
}
Also used : MountPoint(org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint) Test(org.junit.Test)

Aggregations

MountPoint (org.apache.hadoop.fs.viewfs.ViewFileSystem.MountPoint)3 FsStatus (org.apache.hadoop.fs.FsStatus)2 Path (org.apache.hadoop.fs.Path)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Configuration (org.apache.hadoop.conf.Configuration)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 UnsupportedFileSystemException (org.apache.hadoop.fs.UnsupportedFileSystemException)1