Skip to content

Commit

Permalink
HDFS-17089. Close child file systems in ViewFileSystem when cache is …
Browse files Browse the repository at this point in the history
…disabled.
  • Loading branch information
zhangshuyan0 committed Jul 16, 2023
1 parent c44823d commit ce58823
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ public T getTargetFileSystem() throws IOException {
}
return targetFileSystem;
}

T getTargetFileSystemForClose() throws IOException {
return targetFileSystem;
}

}

private void createLink(final String src, final String target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1926,12 +1926,41 @@ enum RenameStrategy {
SAME_FILESYSTEM_ACROSS_MOUNTPOINT
}

private void closeChildFileSystems(FileSystem fs) throws IOException {
if (fs != null) {
FileSystem[] childFs = fs.getChildFileSystems();
for (FileSystem child : childFs) {
if (child != null) {
String disableCacheName = String.format("fs.%s.impl.disable.cache",
child.getUri().getScheme());
if (config.getBoolean(disableCacheName, false)) {
child.close();
}
}
}
}
}

@Override
public void close() throws IOException {
super.close();
if (enableInnerCache && cache != null) {
cache.closeAll();
cache.clear();
}

if (!enableInnerCache) {
for (InodeTree.MountPoint<FileSystem> mountPoint :
fsState.getMountPoints()) {
FileSystem targetFs = mountPoint.target.getTargetFileSystemForClose();
closeChildFileSystems(targetFs);
}

if (fsState.isRootInternalDir() &&
fsState.getRootFallbackLink() != null) {
closeChildFileSystems(
fsState.getRootFallbackLink().getTargetFileSystem());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.apache.hadoop.fs.viewfs;

Check failure on line 1 in hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemClose.java

View check run for this annotation

ASF Cloudbees Jenkins ci-hadoop / Apache Yetus

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemClose.java#L1

asflicense: Missing Apache License

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsConstants;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class TestViewFileSystemClose {

/**
* Verify that all child file systems of a ViewFileSystem will be shut down
* when the cache is disabled.
* @throws IOException
*/
@Test
public void testFileSystemLeak() throws IOException {

Configuration conf = new Configuration();
conf.set("fs.viewfs.impl", ViewFileSystem.class.getName());
conf.setBoolean("fs.viewfs.enable.inner.cache", false);
conf.setBoolean("fs.viewfs.impl.disable.cache", true);
conf.setBoolean("fs.hdfs.impl.disable.cache", true);

String rootPath = "hdfs://localhost/tmp";
ConfigUtil.addLink(conf, "/data", new Path(rootPath, "data").toUri());
ViewFileSystem viewFs =
(ViewFileSystem) FileSystem.get(FsConstants.VIEWFS_URI, conf);

FileSystem[] children = viewFs.getChildFileSystems();
viewFs.close();
FileSystem.closeAll();
for (FileSystem fs : children) {
try {
fs.create(new Path(rootPath, "neverSuccess"));
fail();
} catch (IOException ioe) {
assertTrue(ioe.getMessage().contains("Filesystem closed"));
}
}
}
}

0 comments on commit ce58823

Please sign in to comment.