From 2dc341cfe51caeb6ddc262c5b35849ec11a5de0c Mon Sep 17 00:00:00 2001 From: Chentao Yu Date: Wed, 28 Oct 2020 14:37:26 -0700 Subject: [PATCH] HADOOP-18119. ViewFileSystem#getUri should return a URI with a empty path component --- .../org/apache/hadoop/fs/viewfs/ViewFileSystem.java | 4 +++- ...tViewFileSystemWithAuthorityLocalFileSystem.java | 2 +- .../hadoop/fs/viewfs/ViewFileSystemBaseTest.java | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java index 538f03a1300a3..1dd094ec97db0 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java @@ -314,7 +314,9 @@ public void initialize(final URI theUri, final Configuration conf) tableName = theUri.getHost(); } try { - myUri = new URI(getScheme(), authority, "/", null, null); + myUri = (authority != null) ? + new URI(getScheme(), authority, null, null, null) : + new URI(getScheme(), null, "/", null, null); boolean initingUriAsFallbackOnNoMounts = supportAutoAddingFallbackOnNoMounts(); fsState = new InodeTree(conf, tableName, myUri, diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java index 877c2228c1eea..b28a416d16d33 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java @@ -52,7 +52,7 @@ public void setUp() throws Exception { // Now create a viewfs using a mount table called "default" // hence viewfs://default/ schemeWithAuthority = - new URI(FsConstants.VIEWFS_SCHEME, "default", "/", null, null); + new URI(FsConstants.VIEWFS_SCHEME, "default", null, null, null); fsView = FileSystem.get(schemeWithAuthority, conf); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java index 434eff0bef32e..8a3b67a04bf23 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java @@ -1515,4 +1515,17 @@ public void testTargetFileSystemLazyInitializationForChecksumMethods() // viewfs inner cache is disabled assertEquals(cacheSize + 1, TestFileUtil.getCacheSize()); } + + @Test + public void testGetURI() throws Exception { + // test that URIs without authority return the path component. + URI viewFsWithoutAuthority = URI.create("viewfs:///"); + FileSystem fs = FileSystem.get(viewFsWithoutAuthority, conf); + assertEquals(viewFsWithoutAuthority, fs.getUri()); + + // test that URIs with authority do not return the path component. + URI viewFsWithAuthority = URI.create("viewfs://default"); + fs = FileSystem.get(viewFsWithAuthority, conf); + assertEquals(viewFsWithAuthority, fs.getUri()); + } }