From dab95f99c59cf7fc5d0c2f8a22ece4ea02a5d71e Mon Sep 17 00:00:00 2001 From: Thach Le Date: Fri, 5 Apr 2024 22:48:01 +0700 Subject: [PATCH 1/8] Add support for configuring connection pool to MultiClusterPooledConnectionProvider --- .../redis/clients/jedis/MultiClusterClientConfig.java | 9 +++++++++ .../providers/MultiClusterPooledConnectionProvider.java | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java index 10dff9ef64..8f1e8b2a30 100644 --- a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java +++ b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java @@ -177,6 +177,7 @@ public static class ClusterConfig { private int priority; private HostAndPort hostAndPort; private JedisClientConfig clientConfig; + private ConnectionPoolConfig connectionPoolConfig; public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this.hostAndPort = hostAndPort; @@ -198,6 +199,14 @@ public HostAndPort getHostAndPort() { public JedisClientConfig getJedisClientConfig() { return clientConfig; } + + public ConnectionPoolConfig getConnectionPoolConfig() { + return connectionPoolConfig; + } + + public void setConnectionPoolConfig(ConnectionPoolConfig connectionPoolConfig) { + this.connectionPoolConfig = connectionPoolConfig; + } } public static class Builder { diff --git a/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java index f26716086e..047c59d268 100644 --- a/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java @@ -112,6 +112,10 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste ClusterConfig[] clusterConfigs = multiClusterClientConfig.getClusterConfigs(); for (ClusterConfig config : clusterConfigs) { + ConnectionPoolConfig poolConfig = config.getConnectionPoolConfig(); + if (poolConfig == null) { + poolConfig = new ConnectionPoolConfig(); + } String clusterId = "cluster:" + config.getPriority() + ":" + config.getHostAndPort(); @@ -132,7 +136,7 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste multiClusterMap.put(config.getPriority(), new Cluster(new ConnectionPool(config.getHostAndPort(), - config.getJedisClientConfig()), retry, circuitBreaker)); + config.getJedisClientConfig(), poolConfig), retry, circuitBreaker)); } /// --- /// From 7e236031266756ab27e410450abbf964a78d1bfa Mon Sep 17 00:00:00 2001 From: Thach Le Date: Sat, 6 Apr 2024 13:31:45 +0700 Subject: [PATCH 2/8] Add ClusterConfig constructor to include ConnectionPoolConfig, add test --- .../clients/jedis/MultiClusterClientConfig.java | 12 ++++++++---- .../MultiClusterPooledConnectionProvider.java | 3 --- ...ultiClusterPooledConnectionProviderTest.java | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java index 8f1e8b2a30..1436d56444 100644 --- a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java +++ b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java @@ -182,6 +182,14 @@ public static class ClusterConfig { public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this.hostAndPort = hostAndPort; this.clientConfig = clientConfig; + connectionPoolConfig = new ConnectionPoolConfig(); + } + + public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig, + ConnectionPoolConfig connectionPoolConfig) { + this.hostAndPort = hostAndPort; + this.clientConfig = clientConfig; + this.connectionPoolConfig = connectionPoolConfig; } public int getPriority() { @@ -203,10 +211,6 @@ public JedisClientConfig getJedisClientConfig() { public ConnectionPoolConfig getConnectionPoolConfig() { return connectionPoolConfig; } - - public void setConnectionPoolConfig(ConnectionPoolConfig connectionPoolConfig) { - this.connectionPoolConfig = connectionPoolConfig; - } } public static class Builder { diff --git a/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java index 047c59d268..4762987f35 100644 --- a/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java @@ -113,9 +113,6 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste ClusterConfig[] clusterConfigs = multiClusterClientConfig.getClusterConfigs(); for (ClusterConfig config : clusterConfigs) { ConnectionPoolConfig poolConfig = config.getConnectionPoolConfig(); - if (poolConfig == null) { - poolConfig = new ConnectionPoolConfig(); - } String clusterId = "cluster:" + config.getPriority() + ":" + config.getHostAndPort(); diff --git a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java index 7cf7d3323b..359748631f 100644 --- a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java @@ -24,10 +24,14 @@ public class MultiClusterPooledConnectionProviderTest { @Before public void setUp() { + ConnectionPoolConfig poolConfig = new ConnectionPoolConfig(); + poolConfig.setMaxTotal(8); + poolConfig.setMaxIdle(4); + poolConfig.setMinIdle(1); ClusterConfig[] clusterConfigs = new ClusterConfig[2]; - clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build()); - clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build()); + clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig); + clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig); provider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build()); } @@ -131,4 +135,13 @@ public void testSetActiveMultiClusterIndexOutOfRange() { provider.setActiveMultiClusterIndex(3); // Should throw an exception } + @Test + public void testConnectionPoolConfigApplied() { + MultiClusterPooledConnectionProvider.Cluster activeCluster = provider.getCluster(); + ConnectionPool connectionPool = activeCluster.getConnectionPool(); + + assertEquals(8, connectionPool.getMaxTotal()); + assertEquals(4, connectionPool.getMaxIdle()); + assertEquals(1, connectionPool.getMinIdle()); + } } \ No newline at end of file From f0426e44550210d5c40dae397de7ca0bb9e62f18 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Wed, 17 Apr 2024 17:43:26 +0700 Subject: [PATCH 3/8] Update src/main/java/redis/clients/jedis/MultiClusterClientConfig.java Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- src/main/java/redis/clients/jedis/MultiClusterClientConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java index 1436d56444..58d7c5574b 100644 --- a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java +++ b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java @@ -182,7 +182,6 @@ public static class ClusterConfig { public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this.hostAndPort = hostAndPort; this.clientConfig = clientConfig; - connectionPoolConfig = new ConnectionPoolConfig(); } public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig, From faf957a408d0d050100b21dcf93799e661207035 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Wed, 17 Apr 2024 17:45:43 +0700 Subject: [PATCH 4/8] Update src/main/java/redis/clients/jedis/MultiClusterClientConfig.java Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- src/main/java/redis/clients/jedis/MultiClusterClientConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java index 58d7c5574b..0e476a76fb 100644 --- a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java +++ b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java @@ -177,7 +177,7 @@ public static class ClusterConfig { private int priority; private HostAndPort hostAndPort; private JedisClientConfig clientConfig; - private ConnectionPoolConfig connectionPoolConfig; + private GenericObjectPoolConfig connectionPoolConfig; public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this.hostAndPort = hostAndPort; From c9ed879279bc676ca46476aca878e232c054bff7 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Wed, 17 Apr 2024 18:12:09 +0700 Subject: [PATCH 5/8] Update test --- .../jedis/MultiClusterClientConfig.java | 3 +- .../MultiClusterPooledConnectionProvider.java | 16 ++++++--- ...tiClusterPooledConnectionProviderTest.java | 33 ++++++++++++------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java index 0e476a76fb..1337c293b2 100644 --- a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java +++ b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java @@ -7,6 +7,7 @@ import java.util.Arrays; import java.util.List; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.annots.Experimental; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisValidationException; @@ -207,7 +208,7 @@ public JedisClientConfig getJedisClientConfig() { return clientConfig; } - public ConnectionPoolConfig getConnectionPoolConfig() { + public GenericObjectPoolConfig getConnectionPoolConfig() { return connectionPoolConfig; } } diff --git a/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java index 4762987f35..47b03c7773 100644 --- a/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java @@ -14,6 +14,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; + +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -112,7 +114,7 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste ClusterConfig[] clusterConfigs = multiClusterClientConfig.getClusterConfigs(); for (ClusterConfig config : clusterConfigs) { - ConnectionPoolConfig poolConfig = config.getConnectionPoolConfig(); + GenericObjectPoolConfig poolConfig = config.getConnectionPoolConfig(); String clusterId = "cluster:" + config.getPriority() + ":" + config.getHostAndPort(); @@ -131,9 +133,15 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste circuitBreakerEventPublisher.onSlowCallRateExceeded(event -> log.error(String.valueOf(event))); circuitBreakerEventPublisher.onStateTransition(event -> log.warn(String.valueOf(event))); - multiClusterMap.put(config.getPriority(), - new Cluster(new ConnectionPool(config.getHostAndPort(), - config.getJedisClientConfig(), poolConfig), retry, circuitBreaker)); + if (poolConfig != null) { + multiClusterMap.put(config.getPriority(), + new Cluster(new ConnectionPool(config.getHostAndPort(), + config.getJedisClientConfig(), poolConfig), retry, circuitBreaker)); + } else { + multiClusterMap.put(config.getPriority(), + new Cluster(new ConnectionPool(config.getHostAndPort(), + config.getJedisClientConfig()), retry, circuitBreaker)); + } } /// --- /// diff --git a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java index 359748631f..e16904a9f5 100644 --- a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java @@ -24,14 +24,10 @@ public class MultiClusterPooledConnectionProviderTest { @Before public void setUp() { - ConnectionPoolConfig poolConfig = new ConnectionPoolConfig(); - poolConfig.setMaxTotal(8); - poolConfig.setMaxIdle(4); - poolConfig.setMinIdle(1); ClusterConfig[] clusterConfigs = new ClusterConfig[2]; - clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig); - clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig); + clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build()); + clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build()); provider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build()); } @@ -137,11 +133,24 @@ public void testSetActiveMultiClusterIndexOutOfRange() { @Test public void testConnectionPoolConfigApplied() { - MultiClusterPooledConnectionProvider.Cluster activeCluster = provider.getCluster(); - ConnectionPool connectionPool = activeCluster.getConnectionPool(); - - assertEquals(8, connectionPool.getMaxTotal()); - assertEquals(4, connectionPool.getMaxIdle()); - assertEquals(1, connectionPool.getMinIdle()); + ConnectionPoolConfig poolConfig = new ConnectionPoolConfig(); + poolConfig.setMaxTotal(8); + poolConfig.setMaxIdle(4); + poolConfig.setMinIdle(1); + ClusterConfig[] clusterConfigs = new ClusterConfig[2]; + clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig); + clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig); + try { + provider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build()); + MultiClusterPooledConnectionProvider.Cluster activeCluster = provider.getCluster(); + ConnectionPool connectionPool = activeCluster.getConnectionPool(); + assertEquals(8, connectionPool.getMaxTotal()); + assertEquals(4, connectionPool.getMaxIdle()); + assertEquals(1, connectionPool.getMinIdle()); + } finally { + if (provider != null) { + provider.close(); + } + } } } \ No newline at end of file From 7abe9006299f32eb4ef26f1fccd7dad9d00c84a8 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Wed, 17 Apr 2024 20:32:02 +0700 Subject: [PATCH 6/8] Update src/main/java/redis/clients/jedis/MultiClusterClientConfig.java Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- src/main/java/redis/clients/jedis/MultiClusterClientConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java index 1337c293b2..247ffb7ae8 100644 --- a/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java +++ b/src/main/java/redis/clients/jedis/MultiClusterClientConfig.java @@ -186,7 +186,7 @@ public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) { } public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig, - ConnectionPoolConfig connectionPoolConfig) { + GenericObjectPoolConfig connectionPoolConfig) { this.hostAndPort = hostAndPort; this.clientConfig = clientConfig; this.connectionPoolConfig = connectionPoolConfig; From 09afb9f45b29800a72f40a96dfed4d441a8842d9 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Wed, 17 Apr 2024 20:32:18 +0700 Subject: [PATCH 7/8] Update src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../MultiClusterPooledConnectionProviderTest.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java index e16904a9f5..3366a1becd 100644 --- a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java @@ -140,17 +140,12 @@ public void testConnectionPoolConfigApplied() { ClusterConfig[] clusterConfigs = new ClusterConfig[2]; clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig); clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig); - try { - provider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build()); - MultiClusterPooledConnectionProvider.Cluster activeCluster = provider.getCluster(); + try (MultiClusterPooledConnectionProvider customProvider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build())) { + MultiClusterPooledConnectionProvider.Cluster activeCluster = customProvider.getCluster(); ConnectionPool connectionPool = activeCluster.getConnectionPool(); assertEquals(8, connectionPool.getMaxTotal()); assertEquals(4, connectionPool.getMaxIdle()); assertEquals(1, connectionPool.getMinIdle()); - } finally { - if (provider != null) { - provider.close(); - } } } } \ No newline at end of file From 68e8080cbd981464c12b0971bbb7cf1424426e02 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:44:42 +0600 Subject: [PATCH 8/8] Format testConnectionPoolConfigApplied --- .../providers/MultiClusterPooledConnectionProviderTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java index 3366a1becd..d094d26b4f 100644 --- a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java @@ -140,7 +140,8 @@ public void testConnectionPoolConfigApplied() { ClusterConfig[] clusterConfigs = new ClusterConfig[2]; clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig); clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig); - try (MultiClusterPooledConnectionProvider customProvider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build())) { + try (MultiClusterPooledConnectionProvider customProvider = new MultiClusterPooledConnectionProvider( + new MultiClusterClientConfig.Builder(clusterConfigs).build())) { MultiClusterPooledConnectionProvider.Cluster activeCluster = customProvider.getCluster(); ConnectionPool connectionPool = activeCluster.getConnectionPool(); assertEquals(8, connectionPool.getMaxTotal()); @@ -148,4 +149,4 @@ public void testConnectionPoolConfigApplied() { assertEquals(1, connectionPool.getMinIdle()); } } -} \ No newline at end of file +}