Skip to content

Commit fed4adf

Browse files
committed
HADOOP-18807. Close child file systems in ViewFileSystem when cache is disabled. (apache#5847)
Contributed by Shuyan Zhang
1 parent 8ee580a commit fed4adf

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/InodeTree.java

+5
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ public T getTargetFileSystem() throws IOException {
349349
}
350350
return targetFileSystem;
351351
}
352+
353+
T getTargetFileSystemForClose() throws IOException {
354+
return targetFileSystem;
355+
}
356+
352357
}
353358

354359
private void createLink(final String src, final String target,

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

+34
Original file line numberDiff line numberDiff line change
@@ -1929,12 +1929,46 @@ enum RenameStrategy {
19291929
SAME_FILESYSTEM_ACROSS_MOUNTPOINT
19301930
}
19311931

1932+
private void closeChildFileSystems(FileSystem fs) {
1933+
if (fs != null) {
1934+
FileSystem[] childFs = fs.getChildFileSystems();
1935+
for (FileSystem child : childFs) {
1936+
if (child != null) {
1937+
String disableCacheName = String.format("fs.%s.impl.disable.cache",
1938+
child.getUri().getScheme());
1939+
if (config.getBoolean(disableCacheName, false)) {
1940+
try {
1941+
child.close();
1942+
} catch (IOException e) {
1943+
LOG.info("Fail closing ViewFileSystem's child filesystem " + fs,
1944+
e);
1945+
}
1946+
}
1947+
}
1948+
}
1949+
}
1950+
}
1951+
19321952
@Override
19331953
public void close() throws IOException {
19341954
super.close();
19351955
if (enableInnerCache && cache != null) {
19361956
cache.closeAll();
19371957
cache.clear();
19381958
}
1959+
1960+
if (!enableInnerCache) {
1961+
for (InodeTree.MountPoint<FileSystem> mountPoint :
1962+
fsState.getMountPoints()) {
1963+
FileSystem targetFs = mountPoint.target.getTargetFileSystemForClose();
1964+
closeChildFileSystems(targetFs);
1965+
}
1966+
1967+
if (fsState.isRootInternalDir() &&
1968+
fsState.getRootFallbackLink() != null) {
1969+
closeChildFileSystems(
1970+
fsState.getRootFallbackLink().getTargetFileSystem());
1971+
}
1972+
}
19391973
}
19401974
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.fs.viewfs;
19+
20+
import java.io.IOException;
21+
22+
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.hadoop.fs.FileSystem;
24+
import org.apache.hadoop.fs.FsConstants;
25+
import org.apache.hadoop.fs.Path;
26+
import org.apache.hadoop.test.AbstractHadoopTestBase;
27+
import org.junit.Test;
28+
29+
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
30+
31+
public class TestViewFileSystemClose extends AbstractHadoopTestBase {
32+
33+
/**
34+
* Verify that all child file systems of a ViewFileSystem will be shut down
35+
* when the cache is disabled.
36+
* @throws IOException
37+
*/
38+
@Test
39+
public void testFileSystemLeak() throws Exception {
40+
41+
Configuration conf = new Configuration();
42+
conf.set("fs.viewfs.impl", ViewFileSystem.class.getName());
43+
conf.setBoolean("fs.viewfs.enable.inner.cache", false);
44+
conf.setBoolean("fs.viewfs.impl.disable.cache", true);
45+
conf.setBoolean("fs.hdfs.impl.disable.cache", true);
46+
47+
String rootPath = "hdfs://localhost/tmp";
48+
ConfigUtil.addLink(conf, "/data", new Path(rootPath, "data").toUri());
49+
ViewFileSystem viewFs =
50+
(ViewFileSystem) FileSystem.get(FsConstants.VIEWFS_URI, conf);
51+
52+
FileSystem[] children = viewFs.getChildFileSystems();
53+
viewFs.close();
54+
FileSystem.closeAll();
55+
for (FileSystem fs : children) {
56+
intercept(IOException.class, "Filesystem closed",
57+
"Expect Filesystem closed IOException",
58+
() -> fs.create(new Path(rootPath, "neverSuccess")));
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)