Repository navigation
Bump commons-pool2 to 2.13.1 #605
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
Open
tonygermano
wants to merge
2
commits into
OpenIntegrationEngine:main
Choose a base branch
from
tonygermano:build/commons-pool2-2.13
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,14 @@ | ||
| /* | ||
| * Copyright (c) Mirth Corporation. All rights reserved. | ||
| * | ||
| * http://www.mirthcorp.com | ||
| * | ||
| * The software in this package is published under the terms of the MPL license a copy of which has | ||
| * been included with this distribution in the LICENSE.txt file. | ||
| */ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // SPDX-FileCopyrightText: Mirth Corporation | ||
| // SPDX-FileCopyrightText: 2026 Tony Germano | ||
|
|
||
| package com.mirth.connect.connectors.file; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
|
|
@@ -48,6 +44,7 @@ public class FileConnector { | |
| private boolean validateConnection; | ||
| private int maxTotalConnections = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL; | ||
| private boolean keepConnectionOpen = false; | ||
| private boolean closeConnectionsAfterUse = false; | ||
| private String maxIdleTime; | ||
|
|
||
| public FileConnector(String channelId, ConnectorProperties connectorProperties, Connector connector) { | ||
|
|
@@ -69,6 +66,7 @@ public FileConnector(String channelId, ConnectorProperties connectorProperties, | |
| this.validateConnection = fileDispatcherProperties.isValidateConnection(); | ||
| this.keepConnectionOpen = fileDispatcherProperties.isKeepConnectionOpen(); | ||
| this.maxIdleTime = fileDispatcherProperties.getMaxIdleTime(); | ||
| this.closeConnectionsAfterUse = !keepConnectionOpen; | ||
| } | ||
|
|
||
| if (connector instanceof DestinationConnector) { | ||
|
|
@@ -242,11 +240,23 @@ protected void disconnect() { | |
| */ | ||
| protected void destroyConnection(FileSystemConnection connection, FileSystemConnectionOptions fileSystemOptions) throws Exception { | ||
| if (connection != null) { | ||
| ObjectPool<FileSystemConnection> pool = getConnectionPool(fileSystemOptions); | ||
| pool.invalidateObject(connection); | ||
|
|
||
| synchronized (connections) { | ||
| connections.remove(connection); | ||
| try { | ||
| ObjectPool<FileSystemConnection> pool = getConnectionPool(fileSystemOptions); | ||
| if (closeConnectionsAfterUse) { | ||
| /* | ||
| * The pool keeps no idle connections in this mode, so returnObject destroys the | ||
| * connection and creates a replacement only for a thread waiting to borrow. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow what problem described. Consider editing for concise clarity. Without understanding: We should not have to special-case our way around a pool. Can this be abstracted away from us having to care about pooling in our app logic? |
||
| * invalidateObject would create none here, since addObject respects maxIdle, and | ||
| * a waiting thread could block forever. | ||
| */ | ||
| pool.returnObject(connection); | ||
| } else { | ||
| pool.invalidateObject(connection); | ||
| } | ||
| } finally { | ||
| synchronized (connections) { | ||
| connections.remove(connection); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -267,28 +277,34 @@ private synchronized ObjectPool<FileSystemConnection> getConnectionPool(FileSyst | |
| ObjectPool<FileSystemConnection> pool = pools.get(key); | ||
|
|
||
| if (pool == null) { | ||
| GenericObjectPoolConfig config = new GenericObjectPoolConfig(); | ||
| GenericObjectPoolConfig<FileSystemConnection> config = new GenericObjectPoolConfig<FileSystemConnection>(); | ||
| config.setMaxTotal(maxTotalConnections); | ||
| config.setMinIdle(0); // The makes the evictor thread run, if active, no matter how few objects are in the pool | ||
| config.setTimeBetweenEvictionRunsMillis(1000); // How often the evictor thread checks for objects to evict | ||
| config.setTimeBetweenEvictionRuns(Duration.ofSeconds(1)); // How often the evictor thread checks for objects to evict | ||
|
|
||
| if (keepConnectionOpen) { | ||
| int connectionMaxIdleTime = NumberUtils.toInt(maxIdleTime); | ||
|
|
||
| if (connectionMaxIdleTime == 0) { | ||
| // Disable the eviction thread. https://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/BaseGenericObjectPool.html#setTimeBetweenEvictionRunsMillis-long- | ||
| config.setTimeBetweenEvictionRunsMillis(0); | ||
| // Disable the eviction thread. A non-positive interval means no evictor runs. | ||
| config.setTimeBetweenEvictionRuns(Duration.ZERO); | ||
| } else { | ||
| config.setMinEvictableIdleTimeMillis(connectionMaxIdleTime); | ||
| config.setMinEvictableIdleDuration(Duration.ofMillis(connectionMaxIdleTime)); | ||
| } | ||
| } else { | ||
| // Always evict objects on the next check by the evictor thread | ||
| config.setMinEvictableIdleTimeMillis(1); | ||
| config.setMinEvictableIdleDuration(Duration.ofMillis(1)); | ||
| } | ||
|
|
||
| if (closeConnectionsAfterUse) { | ||
| // Never keep idle connections, so that destroyConnection can return them to be destroyed | ||
| config.setMaxIdle(0); | ||
| } | ||
|
|
||
| if (isValidateConnection()) { | ||
| config.setTestOnBorrow(true); | ||
| config.setTestOnReturn(true); | ||
| // Returned connections are always destroyed when they are not kept open | ||
| config.setTestOnReturn(!closeConnectionsAfterUse); | ||
| } | ||
| pool = new GenericObjectPool<FileSystemConnection>(fileSystemConnectionFactory, config); | ||
|
|
||
|
|
||
131 changes: 131 additions & 0 deletions
131
server/src/test/java/com/mirth/connect/connectors/file/FileConnectorConnectionPoolTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // SPDX-FileCopyrightText: 2026 Tony Germano | ||
|
|
||
| package com.mirth.connect.connectors.file; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertSame; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.commons.pool2.PooledObject; | ||
| import org.apache.commons.pool2.impl.DefaultPooledObject; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import com.mirth.connect.connectors.file.filesystems.FileSystemConnection; | ||
| import com.mirth.connect.connectors.file.filesystems.FileSystemConnectionFactory; | ||
|
|
||
| /** | ||
| * Checks how FileConnector uses its connection pool when a File Writer does not keep connections | ||
| * open, so that a commons-pool2 upgrade cannot change it unnoticed. | ||
| */ | ||
| public class FileConnectorConnectionPoolTest { | ||
|
|
||
| private CountingFactory factory; | ||
| private FileConnector fileConnector; | ||
| private FileSystemConnectionOptions options; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| options = new FileSystemConnectionOptions(false, "user", "password", null); | ||
| factory = new CountingFactory(options); | ||
|
|
||
| FileDispatcherProperties properties = new FileDispatcherProperties(); | ||
| properties.setKeepConnectionOpen(false); | ||
|
|
||
| fileConnector = new FileConnector("channelId", properties, null) { | ||
| @Override | ||
| protected FileSystemConnectionFactory getFileSystemConnectionFactory(FileSystemConnectionOptions fileSystemOptions) { | ||
| return factory; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| fileConnector.doStop(); | ||
| } | ||
|
|
||
| @Test | ||
| public void destroyConnectionDoesNotOpenAnotherConnection() throws Exception { | ||
| FileSystemConnection connection = fileConnector.getConnection(options); | ||
| fileConnector.destroyConnection(connection, options); | ||
|
|
||
| assertEquals(1, factory.created.get()); | ||
| verify(connection).destroy(); | ||
| } | ||
|
|
||
| @Test | ||
| public void destroyConnectionForgetsConnectionWhenFactoryFails() throws Exception { | ||
| FileSystemConnection connection = fileConnector.getConnection(options); | ||
| factory.fail.set(true); | ||
|
|
||
| // Creating a connection after destroying one would fail, as when the server is down | ||
| try { | ||
| fileConnector.destroyConnection(connection, options); | ||
| } catch (Exception e) { | ||
| // Only the state of the connector matters here | ||
| } | ||
|
|
||
| // A forgotten connection is not disconnected again | ||
| fileConnector.disconnect(); | ||
| verify(connection, never()).disconnect(); | ||
| } | ||
|
|
||
| @Test | ||
| public void getConnectionReplacesDisconnectedConnection() throws Exception { | ||
| factory.disconnectNext.set(true); | ||
|
|
||
| FileSystemConnection connection = fileConnector.getConnection(options); | ||
|
|
||
| assertEquals(2, factory.created.get()); | ||
| assertSame(factory.last, connection); | ||
| verify(factory.first).destroy(); | ||
|
|
||
| // Only the replacement is tracked as an open connection | ||
| fileConnector.disconnect(); | ||
| verify(factory.first, never()).disconnect(); | ||
| verify(connection).disconnect(); | ||
| } | ||
|
|
||
| private static class CountingFactory extends FileSystemConnectionFactory { | ||
| final AtomicInteger created = new AtomicInteger(); | ||
| final AtomicBoolean fail = new AtomicBoolean(); | ||
| final AtomicBoolean disconnectNext = new AtomicBoolean(); | ||
| volatile FileSystemConnection first; | ||
| volatile FileSystemConnection last; | ||
|
|
||
| CountingFactory(FileSystemConnectionOptions options) { | ||
| super(FileScheme.FTP, options, "localhost", 21, false, false, 0); | ||
| } | ||
|
|
||
| @Override | ||
| public String getPoolKey() { | ||
| return "test"; | ||
| } | ||
|
|
||
| @Override | ||
| public PooledObject<FileSystemConnection> makeObject() throws Exception { | ||
| if (fail.get()) { | ||
| throw new IOException("Connection refused"); | ||
| } | ||
| FileSystemConnection connection = mock(FileSystemConnection.class); | ||
| when(connection.isConnected()).thenReturn(!disconnectNext.getAndSet(false)); | ||
| when(connection.isValid()).thenReturn(true); | ||
| if (first == null) { | ||
| first = connection; | ||
| } | ||
| last = connection; | ||
| created.incrementAndGet(); | ||
| return new DefaultPooledObject<>(connection); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid keeping new state? I don't see the benefit of adding this.