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

VFS-824 HttpFileSystem free Unused Resources lead to HttpClient Conn… #300

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -148,6 +148,21 @@ public void freeUnusedResources() {
.forEach(AbstractFileSystem::closeCommunicationLink);
}

/**
* get snapshot of all fileSystem
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
*
* @return FileSystem Array
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
*/
public AbstractFileSystem[] getAllFileSystemSnapshot() {
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
final AbstractFileSystem[] abstractFileSystems;
synchronized (fileSystemMap) {
// create snapshot under lock
abstractFileSystems = fileSystemMap.values().toArray(EMPTY_ABSTRACT_FILE_SYSTEMS);
}
return abstractFileSystems;
}


/**
* Gets the FileSystemConfigBuilder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Stream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.vfs2.Capability;
Expand All @@ -28,6 +29,7 @@
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.UserAuthenticationData;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
import org.apache.commons.vfs2.provider.GenericFileName;
import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
Expand Down Expand Up @@ -93,4 +95,19 @@ public Collection<Capability> getCapabilities() {
public FileSystemConfigBuilder getConfigBuilder() {
return HttpFileSystemConfigBuilder.getInstance();
}


/**
* Frees unused resources and close HttpFileSystem.
*/
@Override
public void freeUnusedResources() {
final AbstractFileSystem[] abstractFileSystems = getAllFileSystemSnapshot();
// process snapshot outside lock
Stream.of(abstractFileSystems).filter(AbstractFileSystem::isReleaseable)
.forEach(system -> {
system.closeCommunicationLink();
closeFileSystem(system);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.UserAuthenticationData;
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
import org.apache.commons.vfs2.provider.GenericFileName;
import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
Expand Down Expand Up @@ -378,4 +379,18 @@ private HttpHost getProxyHttpHost(final Http4FileSystemConfigBuilder builder,
return null;
}


/**
* Frees unused resources and close HttpFileSystem.
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
*/
@Override
public void freeUnusedResources() {
final AbstractFileSystem[] abstractFileSystems = getAllFileSystemSnapshot();
// process snapshot outside lock
Stream.of(abstractFileSystems).filter(AbstractFileSystem::isReleaseable)
.forEach(system -> {
system.closeCommunicationLink();
closeFileSystem(system);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.UserAuthenticationData;
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
import org.apache.commons.vfs2.provider.GenericFileName;
import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
Expand Down Expand Up @@ -375,4 +376,19 @@ private HttpHost getProxyHttpHost(final Http5FileSystemConfigBuilder builder,
return null;
}


/**
* Frees unused resources and close HttpFileSystem.
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
*/
@Override
public void freeUnusedResources() {
final AbstractFileSystem[] abstractFileSystems = getAllFileSystemSnapshot();
// process snapshot outside lock
Stream.of(abstractFileSystems).filter(AbstractFileSystem::isReleaseable)
.forEach(system -> {
system.closeCommunicationLink();
closeFileSystem(system);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.ProviderTestSuite;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.cache.WeakRefFilesCache;
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.util.NHttpFileServer;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -212,4 +215,36 @@ public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
testResolveFolderSlash(ConnectionUri + "/read-tests/", true);
}

@Test
public void testHttpFileSystemFreeUnusedResources() throws Exception {
try (StandardFileSystemManager fileSystemManager = new StandardFileSystemManager()) {
fileSystemManager.setConfiguration(StandardFileSystemManager.class.getResource("providers.xml"));
// use WeakRef
fileSystemManager.setFilesCache(new WeakRefFilesCache());
fileSystemManager.init();

String path = "http://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL";
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
AbstractFileSystem http4FileSystem = getFile(fileSystemManager, path);
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
http4FileSystem.isReleaseable();

while (!http4FileSystem.isReleaseable()) {
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
// Try GC
System.gc();
}
// free resource
// http4FileSystem.httpclient is closed
fileSystemManager.freeUnusedResources();

// get file again
getFile(fileSystemManager, path);
}
}

private static AbstractFileSystem getFile(FileSystemManager fileSystemManager, String path) throws FileSystemException {
FileObject fileObject = fileSystemManager.resolveFile(path);
// send
fileObject.getType();
return (AbstractFileSystem) fileObject.getFileSystem();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.ProviderTestSuite;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.cache.WeakRefFilesCache;
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.util.NHttpFileServer;
import org.junit.jupiter.api.Assertions;
import org.junit.Test;
Expand Down Expand Up @@ -216,4 +219,35 @@ public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
testResolveFolderSlash(ConnectionUri + "/read-tests/", true);
}

@Test
public void testHttp4FileSystemFreeUnusedResources() throws Exception {
try (StandardFileSystemManager fileSystemManager = new StandardFileSystemManager()) {
fileSystemManager.setConfiguration(StandardFileSystemManager.class.getResource("providers.xml"));
// use WeakRef
fileSystemManager.setFilesCache(new WeakRefFilesCache());
fileSystemManager.init();

String path = "http4://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL";
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
AbstractFileSystem http4FileSystem = getFile(fileSystemManager, path);
http4FileSystem.isReleaseable();

while (!http4FileSystem.isReleaseable()) {
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
// Try GC
System.gc();
}
// free resource
// http4FileSystem.httpclient is closed
fileSystemManager.freeUnusedResources();

// get file again
getFile(fileSystemManager, path);
}
}

private static AbstractFileSystem getFile(FileSystemManager fileSystemManager, String path) throws FileSystemException {
FileObject fileObject = fileSystemManager.resolveFile(path);
// send
fileObject.getType();
return (AbstractFileSystem) fileObject.getFileSystem();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.ProviderTestSuite;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.cache.WeakRefFilesCache;
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.util.NHttpFileServer;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -210,4 +213,36 @@ public void testResolveFolderSlashYesRedirectOn() throws FileSystemException {
testResolveFolderSlash(ConnectionUri + "/read-tests/", true);
}

@Test
public void testHttp5FileSystemFreeUnusedResources() throws Exception {
try (StandardFileSystemManager fileSystemManager = new StandardFileSystemManager()) {
fileSystemManager.setConfiguration(StandardFileSystemManager.class.getResource("providers.xml"));
// use WeakRef
fileSystemManager.setFilesCache(new WeakRefFilesCache());
fileSystemManager.init();

String path = "http5://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL";
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
AbstractFileSystem http4FileSystem = getFile(fileSystemManager, path);
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
http4FileSystem.isReleaseable();

while (!http4FileSystem.isReleaseable()) {
mind-echo marked this conversation as resolved.
Show resolved Hide resolved
// Try GC
System.gc();
}
// free resource
// http4FileSystem.httpclient is closed
fileSystemManager.freeUnusedResources();

// get file again
getFile(fileSystemManager, path);
}
}

private static AbstractFileSystem getFile(FileSystemManager fileSystemManager, String path) throws FileSystemException {
FileObject fileObject = fileSystemManager.resolveFile(path);
// send
fileObject.getType();
return (AbstractFileSystem) fileObject.getFileSystem();
}

}