You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2024/07/05 13:36:47 UTC

(plc4x) branch develop updated: feat: Made the cached connection manager closeable.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new e7fb5274e0 feat: Made the cached connection manager closeable.
e7fb5274e0 is described below

commit e7fb5274e0a014bbbf07507ebf1557b29440835e
Author: Christofer Dutz <cd...@apache.org>
AuthorDate: Fri Jul 5 15:36:40 2024 +0200

    feat: Made the cached connection manager closeable.
---
 .../utils/cache/CachedPlcConnectionManager.java    | 26 +++++-
 .../java/utils/cache/ConnectionContainer.java      | 20 +++++
 .../java/utils/cache/LeasedPlcConnection.java      | 11 +++
 .../PlcConnectionManagerClosedException.java       | 30 +++++++
 .../cache/CachedPlcConnectionManagerTest.java      | 97 ++++++++++++++++++++++
 5 files changed, 182 insertions(+), 2 deletions(-)

diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java
index e0ac0ad4c3..c3f10a13f4 100644
--- a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java
@@ -23,17 +23,20 @@ import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.PlcConnectionManager;
 import org.apache.plc4x.java.api.authentication.PlcAuthentication;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.utils.cache.exceptions.PlcConnectionManagerClosedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.time.Duration;
 import java.util.*;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
 
-public class CachedPlcConnectionManager implements PlcConnectionManager {
+public class CachedPlcConnectionManager implements PlcConnectionManager, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(CachedPlcConnectionManager.class);
 
@@ -43,6 +46,8 @@ public class CachedPlcConnectionManager implements PlcConnectionManager {
 
     private final Map<String, ConnectionContainer> connectionContainers;
 
+    private final AtomicBoolean closed = new AtomicBoolean(false);
+
     public static Builder getBuilder() {
         return new Builder(new DefaultPlcDriverManager());
     }
@@ -78,10 +83,16 @@ public class CachedPlcConnectionManager implements PlcConnectionManager {
     }
 
     public PlcConnection getConnection(String url) throws PlcConnectionException {
+        // If the connection manager is already closed, abort.
+        if(closed.get()) {
+            throw new PlcConnectionManagerClosedException();
+        }
+
+        // Get a connection container for the given url.
         ConnectionContainer connectionContainer;
         synchronized (connectionContainers) {
             connectionContainer = connectionContainers.get(url);
-            if (connectionContainers.get(url) == null) {
+            if (connectionContainer == null) {
                 LOG.debug("Creating new connection");
 
                 // Crate a connection container to manage handling this connection
@@ -105,6 +116,17 @@ public class CachedPlcConnectionManager implements PlcConnectionManager {
         throw new PlcConnectionException("the cached driver manager currently doesn't support authentication");
     }
 
+    @Override
+    public void close() throws Exception {
+        // Set the cache to "closed" so no new connections can be requested.
+        closed.set(true);
+
+        // Tell all connections to close themselves.
+        connectionContainers.forEach((connectionString, connectionContainer) -> {
+            connectionContainer.close();
+        });
+    }
+
     public static class Builder {
 
         private final PlcConnectionManager connectionManager;
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/ConnectionContainer.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/ConnectionContainer.java
index e4d67288b5..13c6a19f4b 100644
--- a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/ConnectionContainer.java
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/ConnectionContainer.java
@@ -22,6 +22,7 @@ import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.PlcConnectionManager;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+import org.apache.plc4x.java.utils.cache.exceptions.PlcConnectionManagerClosedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -50,6 +51,25 @@ class ConnectionContainer {
         this.leasedConnection = null;
     }
 
+    public synchronized void close() {
+        // Close all waiting clients exceptionally.
+        queue.forEach(plcConnectionCompletableFuture ->
+            plcConnectionCompletableFuture.completeExceptionally(new PlcConnectionManagerClosedException()));
+
+        // Clear the queue.
+        queue.clear();
+
+        // If the connection is currently used, close it.
+        if(leasedConnection != null) {
+            try {
+                leasedConnection.closeConnection();
+                leasedConnection = null;
+            } catch (Exception e) {
+                // Ignore this ...
+            }
+        }
+    }
+
     public synchronized Future<PlcConnection> lease() {
         CompletableFuture<PlcConnection> connectionFuture = new CompletableFuture<>();
 
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/LeasedPlcConnection.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/LeasedPlcConnection.java
index 866365620e..563d50d857 100644
--- a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/LeasedPlcConnection.java
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/LeasedPlcConnection.java
@@ -63,6 +63,17 @@ public class LeasedPlcConnection implements PlcConnection {
         }, Date.from(LocalDateTime.now().plusNanos(maxUseTime.toNanos()).atZone(ZoneId.systemDefault()).toInstant()));
     }
 
+    public synchronized void closeConnection() throws Exception {
+        // Get the real connection and close it.
+        PlcConnection plcConnection = connection.get();
+        if(plcConnection != null) {
+            plcConnection.close();
+        }
+
+        // Close the LeasedPlcConnection.
+        close();
+    }
+
     @Override
     public synchronized void close() {
         // In this case the connection was already closed (possibly by the timer)
diff --git a/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/exceptions/PlcConnectionManagerClosedException.java b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/exceptions/PlcConnectionManagerClosedException.java
new file mode 100644
index 0000000000..6f48dd4736
--- /dev/null
+++ b/plc4j/tools/connection-cache/src/main/java/org/apache/plc4x/java/utils/cache/exceptions/PlcConnectionManagerClosedException.java
@@ -0,0 +1,30 @@
+/*
+ * 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.plc4x.java.utils.cache.exceptions;
+
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+
+public class PlcConnectionManagerClosedException extends PlcConnectionException {
+
+    public PlcConnectionManagerClosedException() {
+        super("CachedPlcConnectionManager is closed");
+    }
+
+}
diff --git a/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java b/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java
index 3515fb40b3..c6df8fb1c4 100644
--- a/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java
+++ b/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java
@@ -22,12 +22,15 @@ import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.PlcConnectionManager;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+import org.apache.plc4x.java.utils.cache.exceptions.PlcConnectionManagerClosedException;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 
 import java.time.Duration;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
 
 public class CachedPlcConnectionManagerTest {
 
@@ -176,4 +179,98 @@ public class CachedPlcConnectionManagerTest {
         Mockito.verify(mockConnectionManager, Mockito.times(1)).getConnection("test");
     }
 
+    @Test
+    public void testClosingConnectionCache() throws Exception {
+        PlcConnection mockConnection = Mockito.mock(PlcConnection.class);
+        PlcConnectionManager mockConnectionManager = Mockito.mock(PlcConnectionManager.class);
+        Mockito.when(mockConnectionManager.getConnection("test")).thenReturn(mockConnection);
+
+        CachedPlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder(mockConnectionManager).withMaxLeaseTime(Duration.ofMillis(20)).build();
+
+        // Have multiple leases borrowed.
+        // The first should get the lease directly but will hang on to it for some time.
+        CompletableFuture<Void> firstFuture = new CompletableFuture<>();
+        Thread thread = new Thread(() -> {
+            try {
+                PlcConnection connection = connectionManager.getConnection("test");
+                Thread.sleep(1000L);
+                connection.close();
+                firstFuture.completeExceptionally(new Exception("First connection should have failed"));
+            } catch (InterruptedException e) {
+                // The thread will be blocking longer than the test execution, so we're sending an interrupt
+                // and this is the way we find out we got it.
+                firstFuture.completeExceptionally(e);
+            } catch (Exception e) {
+                firstFuture.completeExceptionally(e);
+            }
+        });
+        thread.start();
+
+        // Give the thread some time to start up.
+        Thread.sleep(2L);
+
+        // The second will wait in the queue.
+        CompletableFuture<Void> secondFuture = new CompletableFuture<>();
+        new Thread(() -> {
+            try {
+                connectionManager.getConnection("test");
+                secondFuture.completeExceptionally(new Exception("Second connection should have failed"));
+            } catch (PlcConnectionException e) {
+                // This getConnection() call was waiting in line in order to get the connection
+                // when the cache was closed, so we expect this to fail and to contain a reference to the closed
+                // connection manager.
+                secondFuture.completeExceptionally(e);
+            }
+        }).start();
+
+        // Give the thread some time to start up.
+        Thread.sleep(2L);
+
+        // Check that only one connection has been requested from the PlcConnectionManager.
+        Mockito.verify(mockConnectionManager, Mockito.times(1)).getConnection("test");
+        // Check that the connection borrowed from the Mocked ConnectionManager has not been closed yet.
+        Mockito.verify(mockConnection, Mockito.times(0)).close();
+
+        // Close the connection manager.
+        connectionManager.close();
+        // Interrupt the first thread, that is just wasting our time waiting.
+        thread.interrupt();
+
+        // Borrowing after the connectionManager is closed, should result in exceptions.
+        try {
+            connectionManager.getConnection("test");
+            Assertions.fail("This should have failed");
+        } catch (PlcConnectionException e) {
+            // This should fail and give a reference to the closed connection cache.
+            Assertions.assertInstanceOf(PlcConnectionManagerClosedException.class, e);
+        }
+
+        // Check that the connection borrowed from the Mocked ConnectionManager has been closed.
+        Mockito.verify(mockConnection, Mockito.times(1)).close();
+
+        try {
+            firstFuture.get();
+            Assertions.fail("This should have failed");
+        }
+        catch (Exception e) {
+            // In the case of the first thread, the thread was stuck waiting in the one-second pause
+            // when we intentionally interrupted it after closing the cache. So we expect to see that
+            // interrupt exception here.
+            Assertions.assertInstanceOf(ExecutionException.class, e);
+            Assertions.assertInstanceOf(InterruptedException.class, e.getCause());
+        }
+
+        try {
+            secondFuture.get();
+            Assertions.fail("This should have failed");
+        } catch (Exception e) {
+            // In this case the process was waiting for getting the connection thread 1 was hogging.
+            // When closing the cache, all waiting connection requests were instantly finished with
+            // an exception.
+            Assertions.assertInstanceOf(ExecutionException.class, e);
+            Assertions.assertInstanceOf(PlcConnectionException.class, e.getCause());
+            Assertions.assertInstanceOf(PlcConnectionManagerClosedException.class, e.getCause().getCause().getCause());
+        }
+    }
+
 }