Skip to content
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

Custom connection pool to MultiClusterPooledConnectionProvider #3801

13 changes: 13 additions & 0 deletions src/main/java/redis/clients/jedis/MultiClusterClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,12 +178,20 @@ public static class ClusterConfig {
private int priority;
private HostAndPort hostAndPort;
private JedisClientConfig clientConfig;
private GenericObjectPoolConfig<Connection> connectionPoolConfig;

public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
this.hostAndPort = hostAndPort;
this.clientConfig = clientConfig;
}

public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig,
GenericObjectPoolConfig<Connection> connectionPoolConfig) {
this.hostAndPort = hostAndPort;
this.clientConfig = clientConfig;
this.connectionPoolConfig = connectionPoolConfig;
}

public int getPriority() {
return priority;
}
Expand All @@ -198,6 +207,10 @@ public HostAndPort getHostAndPort() {
public JedisClientConfig getJedisClientConfig() {
return clientConfig;
}

public GenericObjectPoolConfig<Connection> getConnectionPoolConfig() {
return connectionPoolConfig;
}
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -112,6 +114,7 @@ public MultiClusterPooledConnectionProvider(MultiClusterClientConfig multiCluste

ClusterConfig[] clusterConfigs = multiClusterClientConfig.getClusterConfigs();
for (ClusterConfig config : clusterConfigs) {
GenericObjectPoolConfig<Connection> poolConfig = config.getConnectionPoolConfig();

String clusterId = "cluster:" + config.getPriority() + ":" + config.getHostAndPort();

Expand All @@ -130,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()), 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));
}
}

/// --- ///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,22 @@ public void testSetActiveMultiClusterIndexOutOfRange() {
provider.setActiveMultiClusterIndex(3); // Should throw an exception
}

}
@Test
public void testConnectionPoolConfigApplied() {
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 (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());
}
}
}
Loading