virajith commented on a change in pull request #1988: URL: https://github.com/apache/hadoop/pull/1988#discussion_r419042354
########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", + FILE), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + FILE), + LocalFileSystem.class.getName()); + fsTarget = new LocalFileSystem(); + fsTarget.initialize(new URI("file:///"), conf); + fileSystemTestHelper = new FileSystemTestHelper(); + // create the test root on local_fs + targetTestRoot = fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget); + fsTarget.delete(targetTestRoot, true); + fsTarget.mkdirs(targetTestRoot); + } + + /** + * Tests write file and read file with ViewFSOverloadScheme. + */ + @Test + public void testLocalTargetLinkWriteSimple() throws IOException { + LOG.info("Starting testLocalTargetLinkWriteSimple"); + final String testString = "Hello Local!..."; + final Path lfsRoot = new Path("/lfsRoot"); + ConfigUtil.addLink(conf, lfsRoot.toString(), + URI.create(targetTestRoot + "/local")); + final FileSystem lViewFs = FileSystem.get(URI.create("file:///"), conf); + + final Path testPath = new Path(lfsRoot, "test.txt"); + final FSDataOutputStream fsDos = lViewFs.create(testPath); Review comment: `try (FSDataOutputStream fsDos = lViewFs.create(testPath)) { }` ########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", + FILE), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + FILE), + LocalFileSystem.class.getName()); + fsTarget = new LocalFileSystem(); + fsTarget.initialize(new URI("file:///"), conf); + fileSystemTestHelper = new FileSystemTestHelper(); + // create the test root on local_fs + targetTestRoot = fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget); + fsTarget.delete(targetTestRoot, true); + fsTarget.mkdirs(targetTestRoot); + } + + /** + * Tests write file and read file with ViewFSOverloadScheme. + */ + @Test + public void testLocalTargetLinkWriteSimple() throws IOException { + LOG.info("Starting testLocalTargetLinkWriteSimple"); + final String testString = "Hello Local!..."; + final Path lfsRoot = new Path("/lfsRoot"); + ConfigUtil.addLink(conf, lfsRoot.toString(), + URI.create(targetTestRoot + "/local")); + final FileSystem lViewFs = FileSystem.get(URI.create("file:///"), conf); + + final Path testPath = new Path(lfsRoot, "test.txt"); + final FSDataOutputStream fsDos = lViewFs.create(testPath); + try { + fsDos.writeUTF(testString); + } finally { + fsDos.close(); + } + + FSDataInputStream lViewIs = lViewFs.open(testPath); + try { Review comment: `try(FSDataInputStream lViewIs = lViewFs.open(testPath)) {}` ########## File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFsOverloadScheme.java ########## @@ -0,0 +1,175 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.UnsupportedFileSystemException; + +/****************************************************************************** Review comment: Thanks for adding these Uma. A couple of comments: (a) "The objective here is to handle multiple mounted file systems transparently.", "Unlike ViewFileSystem scheme (viewfs://), the users would be able to use any scheme." --> These are not functions of this class. `ViewFileSystem` already does this. (b) Configuring `fs.SCHEME.impl = ViewFsOverloadScheme` is not explained in these examples. That should also be brought up. ########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", + FILE), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + FILE), + LocalFileSystem.class.getName()); + fsTarget = new LocalFileSystem(); + fsTarget.initialize(new URI("file:///"), conf); + fileSystemTestHelper = new FileSystemTestHelper(); + // create the test root on local_fs + targetTestRoot = fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget); + fsTarget.delete(targetTestRoot, true); + fsTarget.mkdirs(targetTestRoot); + } + + /** + * Tests write file and read file with ViewFSOverloadScheme. + */ + @Test + public void testLocalTargetLinkWriteSimple() throws IOException { + LOG.info("Starting testLocalTargetLinkWriteSimple"); + final String testString = "Hello Local!..."; + final Path lfsRoot = new Path("/lfsRoot"); + ConfigUtil.addLink(conf, lfsRoot.toString(), + URI.create(targetTestRoot + "/local")); + final FileSystem lViewFs = FileSystem.get(URI.create("file:///"), conf); + + final Path testPath = new Path(lfsRoot, "test.txt"); + final FSDataOutputStream fsDos = lViewFs.create(testPath); + try { + fsDos.writeUTF(testString); + } finally { + fsDos.close(); + } + + FSDataInputStream lViewIs = lViewFs.open(testPath); + try { + Assert.assertEquals(testString, lViewIs.readUTF()); + } finally { + lViewIs.close(); + } + } + + /** + * Tests create file and delete file with ViewFSOverloadScheme. + */ + @Test + public void testLocalFsCreateAndDelete() throws Exception { + LOG.info("Starting testLocalFsCreateAndDelete"); + ConfigUtil.addLink(conf, "mt", "/lfsroot", + URI.create(targetTestRoot + "/wd2")); + final URI mountURI = URI.create("file://mt/"); + final FileSystem lViewFS = FileSystem.get(mountURI, conf); + try { + Path testPath = new Path(mountURI.toString() + "/lfsroot/test"); + lViewFS.create(testPath); Review comment: stream is not closed. may be use `createNewFile`? ########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", Review comment: indentation of the `conf.set` lines is confusing. can you make this better? ########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", + FILE), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + FILE), + LocalFileSystem.class.getName()); + fsTarget = new LocalFileSystem(); + fsTarget.initialize(new URI("file:///"), conf); + fileSystemTestHelper = new FileSystemTestHelper(); + // create the test root on local_fs + targetTestRoot = fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget); + fsTarget.delete(targetTestRoot, true); + fsTarget.mkdirs(targetTestRoot); + } + + /** + * Tests write file and read file with ViewFSOverloadScheme. + */ + @Test + public void testLocalTargetLinkWriteSimple() throws IOException { + LOG.info("Starting testLocalTargetLinkWriteSimple"); + final String testString = "Hello Local!..."; + final Path lfsRoot = new Path("/lfsRoot"); + ConfigUtil.addLink(conf, lfsRoot.toString(), + URI.create(targetTestRoot + "/local")); + final FileSystem lViewFs = FileSystem.get(URI.create("file:///"), conf); + + final Path testPath = new Path(lfsRoot, "test.txt"); + final FSDataOutputStream fsDos = lViewFs.create(testPath); + try { + fsDos.writeUTF(testString); + } finally { + fsDos.close(); + } + + FSDataInputStream lViewIs = lViewFs.open(testPath); + try { + Assert.assertEquals(testString, lViewIs.readUTF()); + } finally { + lViewIs.close(); + } + } + + /** + * Tests create file and delete file with ViewFSOverloadScheme. + */ + @Test + public void testLocalFsCreateAndDelete() throws Exception { + LOG.info("Starting testLocalFsCreateAndDelete"); + ConfigUtil.addLink(conf, "mt", "/lfsroot", + URI.create(targetTestRoot + "/wd2")); + final URI mountURI = URI.create("file://mt/"); + final FileSystem lViewFS = FileSystem.get(mountURI, conf); + try { + Path testPath = new Path(mountURI.toString() + "/lfsroot/test"); + lViewFS.create(testPath); + Assert.assertTrue(lViewFS.exists(testPath)); + lViewFS.delete(testPath, true); + Assert.assertFalse(lViewFS.exists(testPath)); + } finally { + lViewFS.close(); + } + } + + /** + * Tests root level file with linkMergeSlash with ViewFSOverloadScheme. + */ + @Test + public void testLocalFsLinkSlashMerge() throws Exception { + LOG.info("Starting testLocalFsLinkSlashMerge"); + ConfigUtil.addLinkMergeSlash(conf, "mt", + URI.create(targetTestRoot + "/wd2")); + final URI mountURI = URI.create("file://mt/"); + final FileSystem lViewFS = FileSystem.get(mountURI, conf); + try { + Path fileOnRoot = new Path(mountURI.toString() + "/NewFile"); + lViewFS.create(fileOnRoot); Review comment: same comment here -- use `createNewFile`? ########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", + FILE), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + FILE), + LocalFileSystem.class.getName()); + fsTarget = new LocalFileSystem(); + fsTarget.initialize(new URI("file:///"), conf); + fileSystemTestHelper = new FileSystemTestHelper(); + // create the test root on local_fs + targetTestRoot = fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget); + fsTarget.delete(targetTestRoot, true); + fsTarget.mkdirs(targetTestRoot); + } + + /** + * Tests write file and read file with ViewFSOverloadScheme. + */ + @Test + public void testLocalTargetLinkWriteSimple() throws IOException { + LOG.info("Starting testLocalTargetLinkWriteSimple"); + final String testString = "Hello Local!..."; + final Path lfsRoot = new Path("/lfsRoot"); + ConfigUtil.addLink(conf, lfsRoot.toString(), + URI.create(targetTestRoot + "/local")); + final FileSystem lViewFs = FileSystem.get(URI.create("file:///"), conf); + + final Path testPath = new Path(lfsRoot, "test.txt"); + final FSDataOutputStream fsDos = lViewFs.create(testPath); + try { + fsDos.writeUTF(testString); + } finally { + fsDos.close(); + } + + FSDataInputStream lViewIs = lViewFs.open(testPath); + try { + Assert.assertEquals(testString, lViewIs.readUTF()); + } finally { + lViewIs.close(); + } + } + + /** + * Tests create file and delete file with ViewFSOverloadScheme. + */ + @Test + public void testLocalFsCreateAndDelete() throws Exception { + LOG.info("Starting testLocalFsCreateAndDelete"); + ConfigUtil.addLink(conf, "mt", "/lfsroot", + URI.create(targetTestRoot + "/wd2")); + final URI mountURI = URI.create("file://mt/"); + final FileSystem lViewFS = FileSystem.get(mountURI, conf); + try { + Path testPath = new Path(mountURI.toString() + "/lfsroot/test"); + lViewFS.create(testPath); + Assert.assertTrue(lViewFS.exists(testPath)); + lViewFS.delete(testPath, true); + Assert.assertFalse(lViewFS.exists(testPath)); + } finally { + lViewFS.close(); + } + } + + /** + * Tests root level file with linkMergeSlash with ViewFSOverloadScheme. + */ + @Test + public void testLocalFsLinkSlashMerge() throws Exception { + LOG.info("Starting testLocalFsLinkSlashMerge"); + ConfigUtil.addLinkMergeSlash(conf, "mt", + URI.create(targetTestRoot + "/wd2")); + final URI mountURI = URI.create("file://mt/"); + final FileSystem lViewFS = FileSystem.get(mountURI, conf); + try { + Path fileOnRoot = new Path(mountURI.toString() + "/NewFile"); + lViewFS.create(fileOnRoot); + Assert.assertTrue(lViewFS.exists(fileOnRoot)); + } finally { + lViewFS.close(); + } + } + + /** + * Tests with linkMergeSlash and other mounts in ViewFSOverloadScheme. + */ + @Test(expected = IOException.class) + public void testLocalFsLinkSlashMergeWithOtherMountLinks() throws Exception { + LOG.info("Starting testLocalFsLinkSlashMergeWithOtherMountLinks"); + ConfigUtil.addLink(conf, "mt", "/lfsroot", + URI.create(targetTestRoot + "/wd2")); + ConfigUtil.addLinkMergeSlash(conf, "mt", + URI.create(targetTestRoot + "/wd2")); + final URI mountURI = URI.create("file://mt/"); + FileSystem.get(mountURI, conf); + Assert.fail("A merge slash cannot be configured with other mount links."); + } + + @After + public void tearDown() throws Exception { + fsTarget.delete(fileSystemTestHelper.getTestRootPath(fsTarget), true); Review comment: `fsTarget.close()` as well? ########## File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFsOverloadScheme.java ########## @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.net.URI; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.UnsupportedFileSystemException; + +/****************************************************************************** + * This class is extended from the ViewFileSystem for the overloaded scheme file + * system. The objective here is to handle multiple mounted file systems + * transparently. Mount link configurations and in-memory mount table + * building behaviors are inherited from ViewFileSystem. Unlike ViewFileSystem + * scheme (viewfs://), the users would be able to use any scheme. + * + * Example 1: + * If users want some of their existing cluster (hdfs://Cluster) + * data to mount with other hdfs and object store clusters(hdfs://NN1, + * o3fs://bucket1.volume1/, s3a://bucket1/) + * + * fs.viewfs.mounttable.Cluster./user = hdfs://NN1/user + * fs.viewfs.mounttable.Cluster./data = o3fs://bucket1.volume1/data + * fs.viewfs.mounttable.Cluster./backup = s3a://bucket1/backup/ + * + * Op1: Create file hdfs://Cluster/user/fileA will go to hdfs://NN1/user/fileA + * Op2: Create file hdfs://Cluster/data/datafile will go to + * o3fs://bucket1.volume1/data/datafile + * Op3: Create file hdfs://Cluster/backup/data.zip will go to + * s3a://bucket1/backup/data.zip + * + * Example 2: + * If users want some of their existing cluster (s3a://bucketA/) + * data to mount with other hdfs and object store clusters + * (hdfs://NN1, o3fs://bucket1.volume1/) + * + * fs.viewfs.mounttable.bucketA./user = hdfs://NN1/user + * fs.viewfs.mounttable.bucketA./data = o3fs://bucket1.volume1/data + * fs.viewfs.mounttable.bucketA./salesDB = s3a://bucketA/salesDB/ + * + * Op1: Create file s3a://bucketA/user/fileA will go to hdfs://NN1/user/fileA + * Op2: Create file s3a://bucketA/data/datafile will go to + * o3fs://bucket1.volume1/data/datafile + * Op3: Create file s3a://bucketA/salesDB/dbfile will go to + * s3a://bucketA/salesDB/dbfile + *****************************************************************************/ [email protected]({ "MapReduce", "HBase", "Hive" }) [email protected] +public class ViewFsOverloadScheme extends ViewFileSystem { Review comment: I think `ViewOverloadSchemeFilesystem` is a better name. Also do we need an implementation extending `ViewFs` class (which extends `AbstractFileSystem`)? ########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeLocalFileSystem.java ########## @@ -0,0 +1,158 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.IOException; +import java.net.URI; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemTestHelper; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.LocalFileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * Test the TestViewFsOverloadSchemeLocalFS using a file with authority: + * file://mountTableName/ i.e, the authority is used to load a mount table. + */ +public class TestViewFsOverloadSchemeLocalFileSystem { + private static final String FILE = "file"; + private static final Log LOG = + LogFactory.getLog(TestViewFsOverloadSchemeLocalFileSystem.class); + private FileSystem fsTarget; + private Configuration conf; + private Path targetTestRoot; + private FileSystemTestHelper fileSystemTestHelper; + + @Before + public void setUp() throws Exception { + conf = new Configuration(); + conf.set(String.format("fs.%s.impl", + FILE), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + FILE), + LocalFileSystem.class.getName()); + fsTarget = new LocalFileSystem(); + fsTarget.initialize(new URI("file:///"), conf); + fileSystemTestHelper = new FileSystemTestHelper(); + // create the test root on local_fs + targetTestRoot = fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget); + fsTarget.delete(targetTestRoot, true); Review comment: Except for these two, can the rest be done `@BeforeClass`? ########## File path: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeHdfsFileSystemContract.java ########## @@ -0,0 +1,122 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import static org.junit.Assume.assumeTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URI; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemContractBaseTest; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hdfs.AppendTestUtil; +import org.apache.hadoop.hdfs.DistributedFileSystem; +import org.apache.hadoop.hdfs.HdfsConfiguration; +import org.apache.hadoop.hdfs.MiniDFSCluster; +import org.apache.hadoop.hdfs.TestHDFSFileSystemContract; +import org.apache.hadoop.security.AccessControlException; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.hadoop.test.GenericTestUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Tests ViewFsOverloadScheme with file system contract tests. + */ +public class TestViewFsOverloadSchemeHdfsFileSystemContract + extends TestHDFSFileSystemContract { + + private MiniDFSCluster cluster; + private String defaultWorkingDirectory; + + @Before + public void setUp() throws Exception { + final Configuration conf = new HdfsConfiguration(); + conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, + FileSystemContractBaseTest.TEST_UMASK); + final File basedir = GenericTestUtils.getRandomizedTestDir(); + cluster = new MiniDFSCluster.Builder(conf, basedir) + .numDataNodes(2) + .build(); + defaultWorkingDirectory = + "/user/" + UserGroupInformation.getCurrentUser().getShortUserName(); + conf.set(String.format("fs.%s.impl", "hdfs"), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + "hdfs"), + DistributedFileSystem.class.getName()); + URI defaultFSURI = + URI.create(conf.get(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY)); + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), "/user", + defaultFSURI); + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), "/append", + defaultFSURI); + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), + "/FileSystemContractBaseTest/", + new URI(defaultFSURI.toString() + "/FileSystemContractBaseTest/")); + fs = (ViewFsOverloadScheme) FileSystem.get(conf); Review comment: cast not required. ########## File path: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeHdfsFileSystemContract.java ########## @@ -0,0 +1,122 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import static org.junit.Assume.assumeTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URI; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileSystemContractBaseTest; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hdfs.AppendTestUtil; +import org.apache.hadoop.hdfs.DistributedFileSystem; +import org.apache.hadoop.hdfs.HdfsConfiguration; +import org.apache.hadoop.hdfs.MiniDFSCluster; +import org.apache.hadoop.hdfs.TestHDFSFileSystemContract; +import org.apache.hadoop.security.AccessControlException; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.hadoop.test.GenericTestUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Tests ViewFsOverloadScheme with file system contract tests. + */ +public class TestViewFsOverloadSchemeHdfsFileSystemContract + extends TestHDFSFileSystemContract { + + private MiniDFSCluster cluster; + private String defaultWorkingDirectory; + + @Before + public void setUp() throws Exception { + final Configuration conf = new HdfsConfiguration(); + conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, + FileSystemContractBaseTest.TEST_UMASK); + final File basedir = GenericTestUtils.getRandomizedTestDir(); + cluster = new MiniDFSCluster.Builder(conf, basedir) Review comment: do we need the `cluster` created for every test? seems heavy :| Also, can we set `FS_DEFAULT_NAME_KEY` explicitly here? ########## File path: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeWithHdfsScheme.java ########## @@ -0,0 +1,351 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.File; +import java.io.IOException; +import java.net.URI; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RawLocalFileSystem; +import org.apache.hadoop.fs.UnsupportedFileSystemException; +import org.apache.hadoop.hdfs.DFSConfigKeys; +import org.apache.hadoop.hdfs.DistributedFileSystem; +import org.apache.hadoop.hdfs.MiniDFSCluster; +import org.apache.hadoop.security.AccessControlException; +import org.apache.hadoop.test.PathUtils; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests ViewFsOverloadScheme with configured mount links. + */ +public class TestViewFsOverloadSchemeWithHdfsScheme { + private static final String FS_IMPL_PATTERN_KEY = "fs.%s.impl"; + private static final String HDFS_SCHEME = "hdfs"; + private Configuration conf = null; + private MiniDFSCluster cluster = null; + private URI defaultFSURI; + private File localTargetDir; + private static final String TEST_ROOT_DIR = + PathUtils.getTestDirName(TestViewFsOverloadSchemeWithHdfsScheme.class); + private static final String HDFS_USER_FOLDER = "/HDFSUser"; + private static final String LOCAL_FOLDER = "/local"; + + @Before + public void startCluster() throws IOException { + conf = new Configuration(); + conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_DELEGATION_TOKEN_ALWAYS_USE_KEY, + true); + conf.set(String.format(FS_IMPL_PATTERN_KEY, HDFS_SCHEME), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + HDFS_SCHEME), DistributedFileSystem.class.getName()); + cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build(); + cluster.waitClusterUp(); + defaultFSURI = + URI.create(conf.get(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY)); + localTargetDir = new File(TEST_ROOT_DIR, "/root/"); + Assert.assertEquals(HDFS_SCHEME, defaultFSURI.getScheme()); // hdfs scheme. + } + + @After + public void tearDown() throws IOException { + if (cluster != null) { + FileSystem.closeAll(); + cluster.shutdown(); + } + } + + private void createLinks(boolean needFalbackLink, Path hdfsTargetPath, + Path localTragetPath) { + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), HDFS_USER_FOLDER, + hdfsTargetPath.toUri()); + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), LOCAL_FOLDER, + localTragetPath.toUri()); + if (needFalbackLink) { + ConfigUtil.addLinkFallback(conf, defaultFSURI.getAuthority(), + hdfsTargetPath.toUri()); + } + } + + /** + * Create mount links as follows. + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * + * create file /HDFSUser/testfile should create in hdfs + * create file /local/test should create directory in local fs + */ + @Test(timeout = 30000) + public void testMountLinkWithLocalAndHDFS() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + Assert.assertEquals(2, fs.getMountPoints().length); + + // /HDFSUser/testfile + Path hdfsFile = new Path(HDFS_USER_FOLDER + "/testfile"); + // /local/test + Path localDir = new Path(LOCAL_FOLDER + "/test"); + + fs.create(hdfsFile); // /HDFSUser/testfile + fs.mkdirs(localDir); // /local/test + + // Initialize HDFS and test files exist in ls or not + DistributedFileSystem dfs = new DistributedFileSystem(); + dfs.initialize(defaultFSURI, conf); + try { + Assert.assertTrue(dfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(hdfsTargetPath), + hdfsFile.getName()))); // should be in hdfs. + Assert.assertFalse(dfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(localTragetPath), + localDir.getName()))); // should not be in local fs. + } finally { + dfs.close(); + } + + RawLocalFileSystem lfs = new RawLocalFileSystem(); + lfs.initialize(localTragetPath.toUri(), conf); + try { + Assert.assertFalse(lfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(hdfsTargetPath), + hdfsFile.getName()))); // should not be in hdfs. + Assert.assertTrue(lfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(localTragetPath), + localDir.getName()))); // should be in local fs. + } finally { + lfs.close(); + } + } + + /** + * Create mount links as follows. + * hdfs://localhost:xxx/HDFSUser --> nonexistent://NonExistent/User/ + * It should fail to add non existent fs link. + */ + @Test(expected = IOException.class, timeout = 30000) + public void testMountLinkWithNonExistentLink() throws Exception { + final String userFolder = "/User"; + final Path nonExistTargetPath = + new Path("nonexistent://NonExistent" + userFolder); + + /** + * Below addLink will create following mount points + * hdfs://localhost:xxx/User --> nonexistent://NonExistent/User/ + */ + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), userFolder, + nonExistTargetPath.toUri()); + FileSystem.get(conf); + Assert.fail("Expected to fail with non existent link"); + } + + /** + * Create mount links as follows. + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * ListStatus on / should list the mount links. + */ + @Test(timeout = 30000) + public void testListStatusOnRootShouldListAllMountLinks() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + FileStatus[] ls = fs.listStatus(new Path("/")); + Assert.assertEquals(2, ls.length); + String lsPath1 = + Path.getPathWithoutSchemeAndAuthority(ls[0].getPath()).toString(); + String lsPath2 = + Path.getPathWithoutSchemeAndAuthority(ls[1].getPath()).toString(); + Assert.assertTrue( + HDFS_USER_FOLDER.equals(lsPath1) || LOCAL_FOLDER.equals(lsPath1)); + Assert.assertTrue( + HDFS_USER_FOLDER.equals(lsPath2) || LOCAL_FOLDER.equals(lsPath2)); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * ListStatus non mount directory should fail. + */ + @Test(expected = IOException.class, timeout = 30000) + public void testListStatusOnNonMountedPath() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.listStatus(new Path("/nonMount")); + Assert.fail("It should fail as no mount link with /nonMount"); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows hdfs://localhost:xxx/HDFSUser --> + * hdfs://localhost:xxx/HDFSUser/ hdfs://localhost:xxx/local --> + * file://TEST_ROOT_DIR/root/ fallback --> hdfs://localhost:xxx/HDFSUser/ + * Creating file or directory at non root level should succeed with fallback + * links. + */ + @Test(timeout = 30000) + public void testWithLinkFallBack() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(true, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/nonMount/myfile")); + FileStatus[] ls = fs.listStatus(new Path("/nonMount")); + Assert.assertEquals(1, ls.length); + Assert.assertEquals( + Path.getPathWithoutSchemeAndAuthority(ls[0].getPath()).getName(), + "myfile"); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * + * It can not find any mount link. ViewFS expects a mount point from root. + */ + @Test(expected = NotInMountpointException.class, timeout = 30000) + public void testCreateOnRootShouldFailWhenMountLinkConfigured() + throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/newFileOnRoot")); + Assert.fail("It should fail as root is read only in viewFS."); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * fallback --> hdfs://localhost:xxx/HDFSUser/ + * + * It will find fallback link, but root is not accessible and read only. + */ + @Test(expected = AccessControlException.class, timeout = 30000) + public void testCreateOnRootShouldFailEvenFallBackMountLinkConfigured() + throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(true, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/onRootWhenFallBack")); + Assert.fail( + "It should fail as root is read only in viewFS, even when configured" + + " with fallback."); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * fallback --> hdfs://localhost:xxx/HDFSUser/ + * + * It will find fallback link, but root is not accessible and read only. + */ + @Test(expected = UnsupportedFileSystemException.class, timeout = 30000) + public void testInvalidOverloadSchemeTargetFS() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + conf = new Configuration(); + createLinks(true, hdfsTargetPath, localTragetPath); Review comment: calling createLinks isn't even needed here right? ########## File path: hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeWithHdfsScheme.java ########## @@ -0,0 +1,351 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.viewfs; + +import java.io.File; +import java.io.IOException; +import java.net.URI; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FsConstants; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RawLocalFileSystem; +import org.apache.hadoop.fs.UnsupportedFileSystemException; +import org.apache.hadoop.hdfs.DFSConfigKeys; +import org.apache.hadoop.hdfs.DistributedFileSystem; +import org.apache.hadoop.hdfs.MiniDFSCluster; +import org.apache.hadoop.security.AccessControlException; +import org.apache.hadoop.test.PathUtils; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests ViewFsOverloadScheme with configured mount links. + */ +public class TestViewFsOverloadSchemeWithHdfsScheme { + private static final String FS_IMPL_PATTERN_KEY = "fs.%s.impl"; + private static final String HDFS_SCHEME = "hdfs"; + private Configuration conf = null; + private MiniDFSCluster cluster = null; + private URI defaultFSURI; + private File localTargetDir; + private static final String TEST_ROOT_DIR = + PathUtils.getTestDirName(TestViewFsOverloadSchemeWithHdfsScheme.class); + private static final String HDFS_USER_FOLDER = "/HDFSUser"; + private static final String LOCAL_FOLDER = "/local"; + + @Before + public void startCluster() throws IOException { + conf = new Configuration(); + conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_DELEGATION_TOKEN_ALWAYS_USE_KEY, + true); + conf.set(String.format(FS_IMPL_PATTERN_KEY, HDFS_SCHEME), + ViewFsOverloadScheme.class.getName()); + conf.set(String.format( + FsConstants.FS_VIEWFS_OVERLOAD_SCHEME_TARGET_FS_IMPL_PATTERN, + HDFS_SCHEME), DistributedFileSystem.class.getName()); + cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build(); + cluster.waitClusterUp(); + defaultFSURI = + URI.create(conf.get(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY)); + localTargetDir = new File(TEST_ROOT_DIR, "/root/"); + Assert.assertEquals(HDFS_SCHEME, defaultFSURI.getScheme()); // hdfs scheme. + } + + @After + public void tearDown() throws IOException { + if (cluster != null) { + FileSystem.closeAll(); + cluster.shutdown(); + } + } + + private void createLinks(boolean needFalbackLink, Path hdfsTargetPath, + Path localTragetPath) { + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), HDFS_USER_FOLDER, + hdfsTargetPath.toUri()); + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), LOCAL_FOLDER, + localTragetPath.toUri()); + if (needFalbackLink) { + ConfigUtil.addLinkFallback(conf, defaultFSURI.getAuthority(), + hdfsTargetPath.toUri()); + } + } + + /** + * Create mount links as follows. + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * + * create file /HDFSUser/testfile should create in hdfs + * create file /local/test should create directory in local fs + */ + @Test(timeout = 30000) + public void testMountLinkWithLocalAndHDFS() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + Assert.assertEquals(2, fs.getMountPoints().length); + + // /HDFSUser/testfile + Path hdfsFile = new Path(HDFS_USER_FOLDER + "/testfile"); + // /local/test + Path localDir = new Path(LOCAL_FOLDER + "/test"); + + fs.create(hdfsFile); // /HDFSUser/testfile + fs.mkdirs(localDir); // /local/test + + // Initialize HDFS and test files exist in ls or not + DistributedFileSystem dfs = new DistributedFileSystem(); + dfs.initialize(defaultFSURI, conf); + try { + Assert.assertTrue(dfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(hdfsTargetPath), + hdfsFile.getName()))); // should be in hdfs. + Assert.assertFalse(dfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(localTragetPath), + localDir.getName()))); // should not be in local fs. + } finally { + dfs.close(); + } + + RawLocalFileSystem lfs = new RawLocalFileSystem(); + lfs.initialize(localTragetPath.toUri(), conf); + try { + Assert.assertFalse(lfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(hdfsTargetPath), + hdfsFile.getName()))); // should not be in hdfs. + Assert.assertTrue(lfs.exists( + new Path(Path.getPathWithoutSchemeAndAuthority(localTragetPath), + localDir.getName()))); // should be in local fs. + } finally { + lfs.close(); + } + } + + /** + * Create mount links as follows. + * hdfs://localhost:xxx/HDFSUser --> nonexistent://NonExistent/User/ + * It should fail to add non existent fs link. + */ + @Test(expected = IOException.class, timeout = 30000) + public void testMountLinkWithNonExistentLink() throws Exception { + final String userFolder = "/User"; + final Path nonExistTargetPath = + new Path("nonexistent://NonExistent" + userFolder); + + /** + * Below addLink will create following mount points + * hdfs://localhost:xxx/User --> nonexistent://NonExistent/User/ + */ + ConfigUtil.addLink(conf, defaultFSURI.getAuthority(), userFolder, + nonExistTargetPath.toUri()); + FileSystem.get(conf); + Assert.fail("Expected to fail with non existent link"); + } + + /** + * Create mount links as follows. + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * ListStatus on / should list the mount links. + */ + @Test(timeout = 30000) + public void testListStatusOnRootShouldListAllMountLinks() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + FileStatus[] ls = fs.listStatus(new Path("/")); + Assert.assertEquals(2, ls.length); + String lsPath1 = + Path.getPathWithoutSchemeAndAuthority(ls[0].getPath()).toString(); + String lsPath2 = + Path.getPathWithoutSchemeAndAuthority(ls[1].getPath()).toString(); + Assert.assertTrue( + HDFS_USER_FOLDER.equals(lsPath1) || LOCAL_FOLDER.equals(lsPath1)); + Assert.assertTrue( + HDFS_USER_FOLDER.equals(lsPath2) || LOCAL_FOLDER.equals(lsPath2)); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * ListStatus non mount directory should fail. + */ + @Test(expected = IOException.class, timeout = 30000) + public void testListStatusOnNonMountedPath() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.listStatus(new Path("/nonMount")); + Assert.fail("It should fail as no mount link with /nonMount"); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows hdfs://localhost:xxx/HDFSUser --> + * hdfs://localhost:xxx/HDFSUser/ hdfs://localhost:xxx/local --> + * file://TEST_ROOT_DIR/root/ fallback --> hdfs://localhost:xxx/HDFSUser/ + * Creating file or directory at non root level should succeed with fallback + * links. + */ + @Test(timeout = 30000) + public void testWithLinkFallBack() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(true, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/nonMount/myfile")); + FileStatus[] ls = fs.listStatus(new Path("/nonMount")); + Assert.assertEquals(1, ls.length); + Assert.assertEquals( + Path.getPathWithoutSchemeAndAuthority(ls[0].getPath()).getName(), + "myfile"); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * + * It can not find any mount link. ViewFS expects a mount point from root. + */ + @Test(expected = NotInMountpointException.class, timeout = 30000) + public void testCreateOnRootShouldFailWhenMountLinkConfigured() + throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(false, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/newFileOnRoot")); + Assert.fail("It should fail as root is read only in viewFS."); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * fallback --> hdfs://localhost:xxx/HDFSUser/ + * + * It will find fallback link, but root is not accessible and read only. + */ + @Test(expected = AccessControlException.class, timeout = 30000) + public void testCreateOnRootShouldFailEvenFallBackMountLinkConfigured() + throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + + createLinks(true, hdfsTargetPath, localTragetPath); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/onRootWhenFallBack")); + Assert.fail( + "It should fail as root is read only in viewFS, even when configured" + + " with fallback."); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * fallback --> hdfs://localhost:xxx/HDFSUser/ + * + * It will find fallback link, but root is not accessible and read only. + */ + @Test(expected = UnsupportedFileSystemException.class, timeout = 30000) + public void testInvalidOverloadSchemeTargetFS() throws Exception { + final Path hdfsTargetPath = new Path(defaultFSURI + HDFS_USER_FOLDER); + final Path localTragetPath = new Path(localTargetDir.toURI()); + conf = new Configuration(); + createLinks(true, hdfsTargetPath, localTragetPath); + conf.set(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY, + defaultFSURI.toString()); + conf.set(String.format(FS_IMPL_PATTERN_KEY, HDFS_SCHEME), + ViewFsOverloadScheme.class.getName()); + + ViewFsOverloadScheme fs = (ViewFsOverloadScheme) FileSystem.get(conf); + try { + fs.create(new Path("/onRootWhenFallBack")); + Assert.fail("OverloadScheme target fs should be valid."); + } finally { + fs.close(); + } + } + + /** + * Create mount links as follows + * hdfs://localhost:xxx/HDFSUser --> hdfs://localhost:xxx/HDFSUser/ + * hdfs://localhost:xxx/local --> file://TEST_ROOT_DIR/root/ + * + * It should be able to create file using ViewFsOverloadScheme. + */ + @Test(timeout = 30000) + public void testViewFsOverloadSchemeWhenInnerCacheDisabled() Review comment: Can we add a test that cache actually works? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
