|
| 1 | +package redis.clients.jedis.mcf; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | +import java.util.concurrent.CompletableFuture; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | +import java.util.concurrent.CountDownLatch; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.function.Supplier; |
| 11 | + |
| 12 | +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import redis.clients.jedis.Connection; |
| 17 | +import redis.clients.jedis.ConnectionFactory; |
| 18 | +import redis.clients.jedis.ConnectionPool; |
| 19 | +import redis.clients.jedis.HostAndPort; |
| 20 | +import redis.clients.jedis.JedisClientConfig; |
| 21 | +import redis.clients.jedis.exceptions.JedisConnectionException; |
| 22 | + |
| 23 | +public class TrackingConnectionPool extends ConnectionPool { |
| 24 | + private static final Logger log = LoggerFactory.getLogger(TrackingConnectionPool.class); |
| 25 | + |
| 26 | + private final Set<Connection> allCreatedObjects = ConcurrentHashMap.newKeySet(); |
| 27 | + private volatile boolean forcingDisconnect; |
| 28 | + private ConnectionFactory factory; |
| 29 | + |
| 30 | + // Executor for running connection creation subtasks |
| 31 | + private final ExecutorService connectionCreationExecutor = Executors.newCachedThreadPool(r -> { |
| 32 | + Thread t = new Thread(r, "connection-creator"); |
| 33 | + t.setDaemon(true); |
| 34 | + return t; |
| 35 | + }); |
| 36 | + |
| 37 | + // Simple latch for external unblocking of all connection creation threads |
| 38 | + private volatile CountDownLatch unblockLatch = new CountDownLatch(1); |
| 39 | + |
| 40 | + public TrackingConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, |
| 41 | + GenericObjectPoolConfig<Connection> poolConfig) { |
| 42 | + super(hostAndPort, clientConfig, poolConfig); |
| 43 | + this.factory = (ConnectionFactory) this.getFactory(); |
| 44 | + factory.injectMaker(this::injector); |
| 45 | + } |
| 46 | + |
| 47 | + private Supplier<Connection> injector(Supplier<Connection> supplier) { |
| 48 | + return () -> make(supplier); |
| 49 | + } |
| 50 | + |
| 51 | + private Connection make(Supplier<Connection> supplier) { |
| 52 | + // Create CompletableFutures for both the connection task and unblock signal |
| 53 | + CompletableFuture<Connection> connectionFuture = CompletableFuture.supplyAsync(() -> supplier.get(), |
| 54 | + connectionCreationExecutor); |
| 55 | + |
| 56 | + CompletableFuture<Void> unblockFuture = CompletableFuture.runAsync(() -> { |
| 57 | + try { |
| 58 | + unblockLatch.await(); |
| 59 | + } catch (InterruptedException e) { |
| 60 | + Thread.currentThread().interrupt(); |
| 61 | + } |
| 62 | + }, connectionCreationExecutor); |
| 63 | + |
| 64 | + try { |
| 65 | + // Wait for whichever completes first |
| 66 | + CompletableFuture.anyOf(connectionFuture, unblockFuture).join(); |
| 67 | + |
| 68 | + if (connectionFuture.isDone() && !connectionFuture.isCompletedExceptionally()) { |
| 69 | + return connectionFuture.join(); |
| 70 | + } else { |
| 71 | + connectionFuture.cancel(true); |
| 72 | + throw new JedisConnectionException("Connection creation was cancelled due to forced disconnect!"); |
| 73 | + } |
| 74 | + } catch (JedisConnectionException e) { |
| 75 | + connectionFuture.cancel(true); |
| 76 | + unblockFuture.cancel(true); |
| 77 | + throw e; |
| 78 | + } catch (Exception e) { |
| 79 | + connectionFuture.cancel(true); |
| 80 | + unblockFuture.cancel(true); |
| 81 | + throw new JedisConnectionException("Connection creation failed", e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public TrackingConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig) { |
| 86 | + super(hostAndPort, clientConfig); |
| 87 | + this.factory = (ConnectionFactory) this.getFactory(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Connection getResource() { |
| 92 | + if (forcingDisconnect) { |
| 93 | + throw new JedisConnectionException("Forced disconnect in progress!"); |
| 94 | + } |
| 95 | + |
| 96 | + Connection conn = super.getResource(); |
| 97 | + allCreatedObjects.add(conn); |
| 98 | + return conn; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void returnResource(final Connection resource) { |
| 103 | + super.returnResource(resource); |
| 104 | + allCreatedObjects.remove(resource); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void returnBrokenResource(final Connection resource) { |
| 109 | + if (forcingDisconnect) { |
| 110 | + super.returnResource(resource); |
| 111 | + } else { |
| 112 | + super.returnBrokenResource(resource); |
| 113 | + } |
| 114 | + allCreatedObjects.remove(resource); |
| 115 | + } |
| 116 | + |
| 117 | + public void forceDisconnect() { |
| 118 | + this.forcingDisconnect = true; |
| 119 | + |
| 120 | + // First, unblock any pending connection creation |
| 121 | + unblockConnectionCreation(); |
| 122 | + |
| 123 | + this.clear(); |
| 124 | + for (Connection connection : allCreatedObjects) { |
| 125 | + try { |
| 126 | + connection.forceDisconnect(); |
| 127 | + } catch (Exception e) { |
| 128 | + log.warn("Error while force disconnecting connection: " + connection.toIdentityString()); |
| 129 | + } |
| 130 | + } |
| 131 | + this.clear(); |
| 132 | + this.forcingDisconnect = false; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Externally unblock ALL waiting connection creation threads. |
| 137 | + */ |
| 138 | + public void unblockConnectionCreation() { |
| 139 | + CountDownLatch oldLatch = unblockLatch; |
| 140 | + unblockLatch = new CountDownLatch(1); // Reset first |
| 141 | + oldLatch.countDown(); // Then signal old one |
| 142 | + log.info("Externally unblocked waiting connection creation threads"); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void close() { |
| 147 | + // Shutdown the connection creation executor |
| 148 | + connectionCreationExecutor.shutdown(); |
| 149 | + try { |
| 150 | + if (!connectionCreationExecutor.awaitTermination(1, TimeUnit.SECONDS)) { |
| 151 | + connectionCreationExecutor.shutdownNow(); |
| 152 | + } |
| 153 | + } catch (InterruptedException e) { |
| 154 | + connectionCreationExecutor.shutdownNow(); |
| 155 | + Thread.currentThread().interrupt(); |
| 156 | + } |
| 157 | + |
| 158 | + super.close(); |
| 159 | + } |
| 160 | +} |
0 commit comments