Skip to content

Commit 29dfac9

Browse files
committed
HDFS-17089. Close child files systems in ViewFileSystem when cache is disabled.
1 parent c44823d commit 29dfac9

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-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
@@ -413,6 +413,11 @@ public T getTargetFileSystem() throws IOException {
413413
}
414414
return targetFileSystem;
415415
}
416+
417+
T getTargetFileSystemForClose() throws IOException {
418+
return targetFileSystem;
419+
}
420+
416421
}
417422

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

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

+29
Original file line numberDiff line numberDiff line change
@@ -1926,12 +1926,41 @@ enum RenameStrategy {
19261926
SAME_FILESYSTEM_ACROSS_MOUNTPOINT
19271927
}
19281928

1929+
private void closeChildFileSystems(FileSystem fs) throws IOException {
1930+
if (fs != null) {
1931+
FileSystem[] childFs = fs.getChildFileSystems();
1932+
for (FileSystem child : childFs) {
1933+
if (child != null) {
1934+
String disableCacheName = String.format("fs.%s.impl.disable.cache",
1935+
child.getUri().getScheme());
1936+
if (config.getBoolean(disableCacheName, false)) {
1937+
child.close();
1938+
}
1939+
}
1940+
}
1941+
}
1942+
}
1943+
19291944
@Override
19301945
public void close() throws IOException {
19311946
super.close();
19321947
if (enableInnerCache && cache != null) {
19331948
cache.closeAll();
19341949
cache.clear();
19351950
}
1951+
1952+
if (!enableInnerCache) {
1953+
for (InodeTree.MountPoint<FileSystem> mountPoint :
1954+
fsState.getMountPoints()) {
1955+
FileSystem targetFs = mountPoint.target.getTargetFileSystemForClose();
1956+
closeChildFileSystems(targetFs);
1957+
}
1958+
1959+
if (fsState.isRootInternalDir() &&
1960+
fsState.getRootFallbackLink() != null) {
1961+
closeChildFileSystems(
1962+
fsState.getRootFallbackLink().getTargetFileSystem());
1963+
}
1964+
}
19361965
}
19371966
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.apache.hadoop.fs.viewfs;
2+
3+
import org.apache.hadoop.conf.Configuration;
4+
import org.apache.hadoop.fs.FileSystem;
5+
import org.apache.hadoop.fs.FsConstants;
6+
import org.apache.hadoop.fs.Path;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
11+
import static org.junit.Assert.assertTrue;
12+
import static org.junit.Assert.fail;
13+
14+
public class TestViewFileSystemClose {
15+
16+
/**
17+
* Verify that all child file systems of a ViewFileSystem will be shut down
18+
* when the cache is disabled.
19+
* @throws IOException
20+
*/
21+
@Test
22+
public void testFileSystemLeak() throws IOException {
23+
24+
Configuration conf = new Configuration();
25+
conf.set("fs.viewfs.impl", ViewFileSystem.class.getName());
26+
conf.setBoolean("fs.viewfs.enable.inner.cache", false);
27+
conf.setBoolean("fs.viewfs.impl.disable.cache", true);
28+
conf.setBoolean("fs.hdfs.impl.disable.cache", true);
29+
30+
String rootPath = "hdfs://localhost/tmp";
31+
ConfigUtil.addLink(conf, "/data", new Path(rootPath, "data").toUri());
32+
ViewFileSystem viewFs =
33+
(ViewFileSystem) FileSystem.get(FsConstants.VIEWFS_URI, conf);
34+
35+
FileSystem[] children = viewFs.getChildFileSystems();
36+
viewFs.close();
37+
FileSystem.closeAll();
38+
for (FileSystem fs : children) {
39+
try {
40+
fs.create(new Path(rootPath, "neverSuccess"));
41+
fail();
42+
} catch (IOException ioe) {
43+
assertTrue(ioe.getMessage().contains("Filesystem closed"));
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)