This is an automated email from the ASF dual-hosted git repository.
dominikriemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git
The following commit(s) were added to refs/heads/dev by this push:
new 6ebc0fb1ca fix: Improve cleanup of stale connections (#4548)
6ebc0fb1ca is described below
commit 6ebc0fb1ca15f8e9b6b8901a16f7dbb69afefaf8
Author: Dominik Riemer <[email protected]>
AuthorDate: Mon Jun 15 21:36:26 2026 +0200
fix: Improve cleanup of stale connections (#4548)
---
.../management/connect/PullAdapterScheduler.java | 3 +-
.../connect/PullAdapterSchedulerTest.java | 70 +++++++++
.../plc/cache/SpCachedPlcConnectionManager.java | 31 ++--
.../plc/cache/SpConnectionContainer.java | 50 ++++--
.../plc/adapter/ConnectionContainerReproTest.java | 170 +++++++++++++++++++++
5 files changed, 302 insertions(+), 22 deletions(-)
diff --git
a/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/PullAdapterScheduler.java
b/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/PullAdapterScheduler.java
index 1c501fe288..38693d065f 100644
---
a/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/PullAdapterScheduler.java
+++
b/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/PullAdapterScheduler.java
@@ -26,7 +26,6 @@ import org.apache.streampipes.model.monitoring.SpLogMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -48,7 +47,7 @@ public class PullAdapterScheduler {
final Runnable task = () -> {
try {
pullAdapter.pullData();
- } catch (ExecutionException | InterruptedException | TimeoutException |
CompletionException e) {
+ } catch (ExecutionException | InterruptedException | TimeoutException |
RuntimeException e) {
LOG.error("Error while pulling data: {}", e.getMessage());
SpMonitoringManager.INSTANCE.addErrorMessage(
adapterElementId,
diff --git
a/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/connect/PullAdapterSchedulerTest.java
b/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/connect/PullAdapterSchedulerTest.java
new file mode 100644
index 0000000000..c6ef7df54f
--- /dev/null
+++
b/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/connect/PullAdapterSchedulerTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.streampipes.extensions.management.connect;
+
+import org.apache.streampipes.extensions.api.connect.IPollingSettings;
+import org.apache.streampipes.extensions.api.connect.IPullAdapter;
+import
org.apache.streampipes.extensions.management.connect.adapter.util.PollingSettings;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class PullAdapterSchedulerTest {
+
+ @Test
+ void continuesSchedulingAfterRuntimeException() throws InterruptedException {
+ var scheduler = new PullAdapterScheduler();
+ var secondInvocation = new CountDownLatch(1);
+
+ try {
+ scheduler.schedule(new FailingOncePullAdapter(secondInvocation),
"adapter-id");
+ assertTrue(secondInvocation.await(2, TimeUnit.SECONDS));
+ } finally {
+ scheduler.shutdown();
+ }
+ }
+
+ private static class FailingOncePullAdapter implements IPullAdapter {
+
+ private final AtomicInteger invocations = new AtomicInteger();
+ private final CountDownLatch secondInvocation;
+
+ private FailingOncePullAdapter(CountDownLatch secondInvocation) {
+ this.secondInvocation = secondInvocation;
+ }
+
+ @Override
+ public void pullData() {
+ if (invocations.incrementAndGet() == 1) {
+ throw new IllegalStateException("first poll failed");
+ }
+ secondInvocation.countDown();
+ }
+
+ @Override
+ public IPollingSettings getPollingInterval() {
+ return PollingSettings.from(TimeUnit.MILLISECONDS, 10);
+ }
+ }
+}
diff --git
a/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpCachedPlcConnectionManager.java
b/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpCachedPlcConnectionManager.java
index e3702aec10..1f702d0913 100644
---
a/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpCachedPlcConnectionManager.java
+++
b/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpCachedPlcConnectionManager.java
@@ -81,12 +81,14 @@ public class SpCachedPlcConnectionManager implements
PlcConnectionManager, AutoC
* @param url url of the connection that should be removed.
*/
public void removeCachedConnection(String url) {
+ SpConnectionContainer connectionContainer;
synchronized (connectionContainers) {
- // Make sure the connection is closed before removing it.
- if (connectionContainers.containsKey(url)) {
- connectionContainers.get(url).close();
- }
- connectionContainers.remove(url);
+ connectionContainer = connectionContainers.remove(url);
+ }
+
+ // Make sure the connection is closed before removing it.
+ if (connectionContainer != null) {
+ connectionContainer.close();
}
}
@@ -119,8 +121,13 @@ public class SpCachedPlcConnectionManager implements
PlcConnectionManager, AutoC
Future<PlcConnection> leaseFuture = connectionContainer.lease();
try {
return leaseFuture.get(this.maxWaitTime.toMillis(),
TimeUnit.MILLISECONDS);
- } catch (ExecutionException | InterruptedException | TimeoutException e) {
- throw new PlcConnectionException("Error acquiring lease for connection");
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new PlcConnectionException("Interrupted while acquiring lease for
connection", e);
+ } catch (TimeoutException e) {
+ throw new PlcConnectionException("Timed out acquiring lease for
connection", e);
+ } catch (ExecutionException e) {
+ throw new PlcConnectionException("Error acquiring lease for connection",
e.getCause());
}
}
@@ -134,9 +141,12 @@ public class SpCachedPlcConnectionManager implements
PlcConnectionManager, AutoC
closed.set(true);
// Tell all connections to close themselves.
- connectionContainers.forEach((connectionString, connectionContainer) -> {
- connectionContainer.close();
- });
+ Map<String, SpConnectionContainer> containersToClose;
+ synchronized (connectionContainers) {
+ containersToClose = new HashMap<>(connectionContainers);
+ connectionContainers.clear();
+ }
+ containersToClose.forEach((connectionString, connectionContainer) ->
connectionContainer.close());
}
public static class Builder {
@@ -175,4 +185,3 @@ public class SpCachedPlcConnectionManager implements
PlcConnectionManager, AutoC
}
}
-
diff --git
a/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpConnectionContainer.java
b/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpConnectionContainer.java
index 6d9e324889..a3a329fc54 100644
---
a/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpConnectionContainer.java
+++
b/streampipes-extensions/streampipes-connectors-plc/src/main/java/org/apache/streampipes/extensions/connectors/plc/cache/SpConnectionContainer.java
@@ -50,6 +50,7 @@ public class SpConnectionContainer {
private PlcConnection connection;
private SpLeasedPlcConnection leasedConnection;
private Timer idleTimer;
+ private boolean closed;
public SpConnectionContainer(PlcConnectionManager connectionManager, String
connectionUrl,
Duration maxLeaseTime, Duration maxIdleTime,
@@ -62,9 +63,12 @@ public class SpConnectionContainer {
this.queue = new LinkedList<>();
this.connection = null;
this.leasedConnection = null;
+ this.closed = false;
}
public synchronized void close() {
+ closed = true;
+
// Close all waiting clients exceptionally.
queue.forEach(plcConnectionCompletableFuture ->
plcConnectionCompletableFuture.completeExceptionally(new
PlcConnectionManagerClosedException()));
@@ -100,6 +104,11 @@ public class SpConnectionContainer {
public synchronized Future<PlcConnection> lease() {
CompletableFuture<PlcConnection> connectionFuture = new
CompletableFuture<>();
+ if (closed) {
+ connectionFuture.completeExceptionally(new
PlcConnectionManagerClosedException());
+ return connectionFuture;
+ }
+
// Try to get a new connection, if we haven't got one yet.
if (connection == null) {
try {
@@ -147,6 +156,14 @@ public class SpConnectionContainer {
throw new PlcRuntimeException("Error trying to return lease from invalid
connection");
}
+ if (closed) {
+ leasedConnection = null;
+ connection = null;
+ queue.forEach(future -> future.completeExceptionally(new
PlcConnectionManagerClosedException()));
+ queue.clear();
+ return;
+ }
+
// If something happened while using the connection, invalidate this one
and create a new connection.
if (invalidateConnection) {
// Close the old connection.
@@ -180,14 +197,7 @@ public class SpConnectionContainer {
idleTimer.schedule(new TimerTask() {
@Override
public void run() {
- if (connection != null) {
- try {
- connection.close();
- } catch (Exception e) {
- // Ignore ...
- }
- }
- closeConnectionHandler.apply(connectionUrl);
+ closeIdleConnection();
}
}, maxIdleTime.toMillis());
return;
@@ -209,6 +219,29 @@ public class SpConnectionContainer {
}
}
+ private void closeIdleConnection() {
+ PlcConnection connectionToClose;
+ synchronized (this) {
+ if (closed || leasedConnection != null || connection == null) {
+ return;
+ }
+
+ closed = true;
+ connectionToClose = connection;
+ connection = null;
+ idleTimer = null;
+ queue.forEach(future -> future.completeExceptionally(new
PlcConnectionManagerClosedException()));
+ queue.clear();
+ }
+
+ closeConnectionHandler.apply(connectionUrl);
+ try {
+ connectionToClose.close();
+ } catch (Exception e) {
+ // Ignore ...
+ }
+ }
+
public void addEventListener(EventListener listener) {
if ((connection != null) && (connection instanceof EventPlcConnection)) {
@@ -232,4 +265,3 @@ public class SpConnectionContainer {
}
-
diff --git
a/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/ConnectionContainerReproTest.java
b/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/ConnectionContainerReproTest.java
index aa96155be1..c2326fb121 100644
---
a/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/ConnectionContainerReproTest.java
+++
b/streampipes-extensions/streampipes-connectors-plc/src/test/java/org/apache/streampipes/extensions/connectors/plc/adapter/ConnectionContainerReproTest.java
@@ -18,6 +18,7 @@
package org.apache.streampipes.extensions.connectors.plc.adapter;
+import
org.apache.streampipes.extensions.connectors.plc.cache.SpCachedPlcConnectionManager;
import
org.apache.streampipes.extensions.connectors.plc.cache.SpConnectionContainer;
import
org.apache.streampipes.extensions.connectors.plc.cache.SpLeasedPlcConnection;
@@ -34,12 +35,16 @@ import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.api.value.PlcValue;
+import
org.apache.plc4x.java.utils.cache.exceptions.PlcConnectionManagerClosedException;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -47,6 +52,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class ConnectionContainerReproTest {
@@ -134,6 +140,27 @@ class ConnectionContainerReproTest {
// implement other methods as no-ops if your interface requires them
}
+ static class BlockingCloseConnection extends DummyConnection {
+ private final CountDownLatch closeStarted;
+ private final CountDownLatch releaseClose;
+
+ BlockingCloseConnection(CountDownLatch closeStarted,
+ CountDownLatch releaseClose) {
+ this.closeStarted = closeStarted;
+ this.releaseClose = releaseClose;
+ }
+
+ @Override
+ public void close() {
+ closeStarted.countDown();
+ try {
+ releaseClose.await(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
@Test
void recoversAfterFailedReconnectAndServesNewLeases() throws Exception {
FlakyManager mgr = new FlakyManager();
@@ -166,4 +193,147 @@ class ConnectionContainerReproTest {
PlcConnection lease3 = cc.lease().get(500, TimeUnit.MILLISECONDS);
assertNotNull(lease3);
}
+
+ @Test
+ void removingSlowConnectionDoesNotBlockLeasesForOtherUrls() throws Exception
{
+ var closeStarted = new CountDownLatch(1);
+ var releaseClose = new CountDownLatch(1);
+ PlcConnectionManager manager = new PlcConnectionManager() {
+ @Override
+ public PlcConnection getConnection(String url) {
+ if ("mock://slow".equals(url)) {
+ return new BlockingCloseConnection(closeStarted, releaseClose);
+ }
+ return new DummyConnection();
+ }
+
+ @Override
+ public PlcConnection getConnection(String url,
+ PlcAuthentication authentication) {
+ return null;
+ }
+ };
+
+ var cachedConnectionManager = new SpCachedPlcConnectionManager(
+ manager,
+ Duration.ofSeconds(30),
+ Duration.ofSeconds(30),
+ Duration.ofSeconds(30)
+ );
+
+ cachedConnectionManager.getConnection("mock://slow");
+
+ ExecutorService removeExecutor = Executors.newSingleThreadExecutor();
+ ExecutorService leaseExecutor = Executors.newSingleThreadExecutor();
+ try {
+ Future<?> removeFuture = removeExecutor.submit(
+ () -> cachedConnectionManager.removeCachedConnection("mock://slow"));
+ assertTrue(closeStarted.await(500, TimeUnit.MILLISECONDS));
+
+ Future<PlcConnection> otherLease = leaseExecutor.submit(
+ () -> cachedConnectionManager.getConnection("mock://other"));
+ assertNotNull(otherLease.get(500, TimeUnit.MILLISECONDS));
+
+ releaseClose.countDown();
+ removeFuture.get(500, TimeUnit.MILLISECONDS);
+ } finally {
+ releaseClose.countDown();
+ removeExecutor.shutdownNow();
+ leaseExecutor.shutdownNow();
+ }
+ }
+
+ @Test
+ void doesNotLeaseConnectionWhileIdleConnectionIsClosing() throws Exception {
+ var closeStarted = new CountDownLatch(1);
+ var releaseClose = new CountDownLatch(1);
+ var removeCalled = new CountDownLatch(1);
+ PlcConnectionManager manager = new PlcConnectionManager() {
+ @Override
+ public PlcConnection getConnection(String url) {
+ return new BlockingCloseConnection(closeStarted, releaseClose);
+ }
+
+ @Override
+ public PlcConnection getConnection(String url,
+ PlcAuthentication authentication) {
+ return null;
+ }
+ };
+ var connectionContainer = new SpConnectionContainer(
+ manager,
+ "mock://idle",
+ Duration.ofSeconds(30),
+ Duration.ofMillis(10),
+ url -> {
+ removeCalled.countDown();
+ return null;
+ }
+ );
+
+ SpLeasedPlcConnection lease =
+ (SpLeasedPlcConnection) connectionContainer.lease().get(500,
TimeUnit.MILLISECONDS);
+ connectionContainer.returnConnection(lease, false);
+
+ try {
+ assertTrue(closeStarted.await(500, TimeUnit.MILLISECONDS));
+
+ ExecutionException exception = assertThrows(
+ ExecutionException.class,
+ () -> connectionContainer.lease().get(500, TimeUnit.MILLISECONDS)
+ );
+ assertTrue(exception.getCause() instanceof
PlcConnectionManagerClosedException);
+
+ releaseClose.countDown();
+ assertTrue(removeCalled.await(500, TimeUnit.MILLISECONDS));
+ } finally {
+ releaseClose.countDown();
+ }
+ }
+
+ @Test
+ void idleCloseDoesNotRemoveReplacementContainer() throws Exception {
+ var closeStarted = new CountDownLatch(1);
+ var releaseClose = new CountDownLatch(1);
+ var connectionAttempts = new AtomicInteger();
+ PlcConnectionManager manager = new PlcConnectionManager() {
+ @Override
+ public PlcConnection getConnection(String url) {
+ if (connectionAttempts.incrementAndGet() == 1) {
+ return new BlockingCloseConnection(closeStarted, releaseClose);
+ }
+ return new DummyConnection();
+ }
+
+ @Override
+ public PlcConnection getConnection(String url,
+ PlcAuthentication authentication) {
+ return null;
+ }
+ };
+
+ var cachedConnectionManager = new SpCachedPlcConnectionManager(
+ manager,
+ Duration.ofSeconds(30),
+ Duration.ofSeconds(30),
+ Duration.ofMillis(10)
+ );
+ PlcConnection firstLease =
cachedConnectionManager.getConnection("mock://idle");
+ firstLease.close();
+
+ try {
+ assertTrue(closeStarted.await(500, TimeUnit.MILLISECONDS));
+
+ PlcConnection replacementLease =
cachedConnectionManager.getConnection("mock://idle");
+ assertNotNull(replacementLease);
+
assertTrue(cachedConnectionManager.getCachedConnections().contains("mock://idle"));
+
+ releaseClose.countDown();
+
assertTrue(cachedConnectionManager.getCachedConnections().contains("mock://idle"));
+ replacementLease.close();
+ } finally {
+ releaseClose.countDown();
+ cachedConnectionManager.removeCachedConnection("mock://idle");
+ }
+ }
}