Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HADOOP-18807. Close child file systems in ViewFileSystem when cache is disabled. #5847

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what difference between this method and getTargetFileSystem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some target FileSystem may not have be initialized. getTargetFileSystem initializes all target FileSystems (line1039 below). It's costly and unnecessary when we just try to close them.

public FileSystem[] getChildFileSystems() {
List<InodeTree.MountPoint<FileSystem>> mountPoints =
fsState.getMountPoints();
Map<String, FileSystem> fsMap = initializeMountedFileSystems(mountPoints);

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,46 @@ enum RenameStrategy {
SAME_FILESYSTEM_ACROSS_MOUNTPOINT
}

private void closeChildFileSystems(FileSystem fs) {
if (fs != null) {
FileSystem[] childFs = fs.getChildFileSystems();
for (FileSystem child : childFs) {
steveloughran marked this conversation as resolved.
Show resolved Hide resolved
if (child != null) {
String disableCacheName = String.format("fs.%s.impl.disable.cache",
child.getUri().getScheme());
if (config.getBoolean(disableCacheName, false)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check the child config rather than the viewfs one? would they ever be different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Child file system always use their parent's config. We can take a look at line 346 and line 350 of the code below:

protected Function<URI, FileSystem> initAndGetTargetFs() {
return new Function<URI, FileSystem>() {
@Override
public FileSystem apply(final URI uri) {
FileSystem fs;
try {
fs = ugi.doAs(new PrivilegedExceptionAction<FileSystem>() {
@Override
public FileSystem run() throws IOException {
if (enableInnerCache) {
synchronized (cache) {
return cache.get(uri, config);
}
} else {
return fsGetter().get(uri, config);
}
}
});
return new ChRootedFileSystem(fs, uri);
} catch (IOException | InterruptedException ex) {
LOG.error("Could not initialize the underlying FileSystem "
+ "object. Exception: " + ex.toString());
}
return null;
}
};
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveloughran means that we should only check viewfs config, rather than every child config here. As you mentioned code segment, it also shows that check viewfs only is enough IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me

try {
child.close();
} catch (IOException e) {
LOG.info("Fail closing ViewFileSystem's child filesystem " + fs,
e);
}
}
}
}
}
}

@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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about to invoke getTargetFileSystem directly?

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,61 @@
/**
* 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this test uses file:// as the childen fs then it could be added in hadoop common, which is where viewfs is implemented. Doing that means that yetus will run the test on every change to hadoop-common, which is needed to stop regressions in viewfs only being found later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't fully understand what you mean. Do you suggest moving this test file to hadoop-common?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and to use the localfs. yetus doesnt run the hadoop-hdfs tests when a patch just goes near hadoop-common, so the test needs to get there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the cache is disabled, LocalFileSystem#close() didn't do anything special. That is to say, when the child filesystem is a LocalFileSystem, whether it is closed or not has little effect. So I think using DistributedFileSystem in this UT is more appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and to use the localfs. yetus doesnt run the hadoop-hdfs tests when a patch just goes near hadoop-common, so the test needs to get there.

Hi @steveloughran this change can trigger hadoop-hdfs unit test, but it could not ensure to be launched later actually. I am also concerned it could not cover this case if use local FS only here as @zhangshuyan0 mentioned above 'LocalFileSystem#close() didn't do anything special'. Any suggestions? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, ok.


import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check import ordering

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsConstants;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.test.AbstractHadoopTestBase;
import org.junit.Test;

import static org.apache.hadoop.test.LambdaTestUtils.intercept;

public class TestViewFileSystemClose extends AbstractHadoopTestBase {

/**
* 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 Exception {

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use LambdaTestUtils.intercept() here

intercept(IOException.class, "Filesystem closed",
"Expect Filesystem closed IOException",
() -> fs.create(new Path(rootPath, "neverSuccess")));
}
}
}