HCFSMountTableConfigLoader implements MountTableConfigLoader { @Override public void load(String mountTableConfigPath, Configuration conf) throws IOException { this.mountTable = new Path(mountTableConfigPath); String scheme = mountTable.toUri().getScheme(); FsGetter fsGetter = new ViewFileSystemOverloadScheme.ChildFsGetter(scheme); try (FileSystem fs = fsGetter.getNewInstance(mountTable.toUri(), conf)) { RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(mountTable, false); LocatedFileStatus lfs = null; int higherVersion = -1; while (listFiles.hasNext()) { LocatedFileStatus curLfs = listFiles.next(); String cur = curLfs.getPath().getName(); String[] nameParts = cur.split(REGEX_DOT); if (nameParts.length < 2) { logInvalidFileNameFormat(cur); continue; } int curVersion = higherVersion; try { curVersion = Integer.parseInt(nameParts[nameParts.length - 2]); } catch (NumberFormatException nfe) { logInvalidFileNameFormat(cur); continue; } if (curVersion > higherVersion) { higherVersion = curVersion; lfs = curLfs; } } if (lfs == null) { LOGGER.warn("No valid mount-table file exist at: {}. At least one " + "mount-table file should present with the name format: " + "mount-table.<versionNumber>.xml", mountTableConfigPath); return; } Path latestVersionMountTable = lfs.getPath(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Loading the mount-table {} into configuration.", latestVersionMountTable); } try (FSDataInputStream open = fs.open(latestVersionMountTable)) { Configuration newConf = new Configuration(false); newConf.addResource(open); conf.addResource(newConf); } } }  @Override void load(String mountTableConfigPath, Configuration conf);  }
@Test public void testMountTableFileLoadingWhenMultipleFilesExist() throws Exception { ViewFsTestSetup.addMountLinksToFile(TABLE_NAME, new String[] {SRC_ONE, SRC_TWO }, new String[] {TARGET_ONE, TARGET_TWO }, new Path(newVersionMountTableFile.toURI()), conf); loader.load(targetTestRoot.toString(), conf); Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_TWO), TARGET_TWO); Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_ONE), TARGET_ONE); }
@Test public void testMountTableFileWithInvalidFormat() throws Exception { Path path = new Path(new URI( targetTestRoot.toString() + "/testMountTableFileWithInvalidFormat/")); fsTarget.mkdirs(path); File invalidMountFileName = new File(new URI(path.toString() + "/table.InvalidVersion.xml")); invalidMountFileName.createNewFile(); ViewFsTestSetup.addMountLinksToFile(TABLE_NAME, new String[] {SRC_ONE, SRC_TWO }, new String[] {TARGET_ONE, TARGET_TWO }, new Path(invalidMountFileName.toURI()), conf); loader.load(path.toString(), conf); Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_TWO)); Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_ONE)); invalidMountFileName.delete(); }
@Test public void testMountTableFileWithInvalidFormatWithNoDotsInName() throws Exception { Path path = new Path(new URI(targetTestRoot.toString() + "/testMountTableFileWithInvalidFormatWithNoDots/")); fsTarget.mkdirs(path); File invalidMountFileName = new File(new URI(path.toString() + "/tableInvalidVersionxml")); invalidMountFileName.createNewFile(); loader.load(path.toString(), conf); Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_TWO)); Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_ONE)); invalidMountFileName.delete(); }
@Test(expected = FileNotFoundException.class) public void testLoadWithMountFile() throws Exception { loader.load(new URI(targetTestRoot.toString() + "/Non-Existent-File.xml") .toString(), conf); }
@Test public void testLoadWithNonExistentMountFile() throws Exception { ViewFsTestSetup.addMountLinksToFile(TABLE_NAME, new String[] {SRC_ONE, SRC_TWO }, new String[] {TARGET_ONE, TARGET_TWO }, new Path(oldVersionMountTableFile.toURI()), conf); loader.load(oldVersionMountTableFile.toURI().toString(), conf); Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_TWO), TARGET_TWO); Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_ONE), TARGET_ONE); }