DATAREDIS-679 - Reconfigure Jedis cluster connection pools for discovered cluster nodes.
We now request a cluster node connection directly from the driver's connection handler and initiate reconfiguration if a cluster node is known through the topology but has no connection pool yet. This can happen if a cluster node is added to the cluster after the connection was initialized. The connection handler cannot be obtained directly although the field where it's held is protected which increases the amount of necessary test code and reflection access to the connection handler. Jedis discovers nodes during startup, on demand (full scan) or when requested (e.g. by a cluster redirection). Original Pull Request: #270
This commit is contained in:
committed by
Christoph Strobl
parent
00add116e9
commit
bbd4482f26
@@ -17,8 +17,10 @@ package org.springframework.data.redis.connection.jedis;
|
||||
|
||||
import redis.clients.jedis.BinaryJedis;
|
||||
import redis.clients.jedis.BinaryJedisPubSub;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.JedisClusterConnectionHandler;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -32,6 +34,7 @@ import java.util.Set;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.PropertyAccessor;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.redis.ClusterStateFailureException;
|
||||
@@ -43,6 +46,7 @@ import org.springframework.data.redis.connection.ClusterCommandExecutor.MultiKey
|
||||
import org.springframework.data.redis.connection.ClusterCommandExecutor.NodeResult;
|
||||
import org.springframework.data.redis.connection.RedisClusterNode.SlotRange;
|
||||
import org.springframework.data.redis.connection.convert.Converters;
|
||||
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -85,8 +89,8 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection {
|
||||
|
||||
closed = false;
|
||||
topologyProvider = new JedisClusterTopologyProvider(cluster);
|
||||
clusterCommandExecutor = new ClusterCommandExecutor(topologyProvider, new JedisClusterNodeResourceProvider(cluster),
|
||||
EXCEPTION_TRANSLATION);
|
||||
clusterCommandExecutor = new ClusterCommandExecutor(topologyProvider,
|
||||
new JedisClusterNodeResourceProvider(cluster, topologyProvider), EXCEPTION_TRANSLATION);
|
||||
disposeClusterCommandExecutorOnClose = true;
|
||||
|
||||
try {
|
||||
@@ -755,19 +759,35 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection {
|
||||
* Jedis specific implementation of {@link ClusterNodeResourceProvider}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 1.7
|
||||
*/
|
||||
static class JedisClusterNodeResourceProvider implements ClusterNodeResourceProvider {
|
||||
|
||||
private final JedisCluster cluster;
|
||||
private final ClusterTopologyProvider topologyProvider;
|
||||
private final JedisClusterConnectionHandler connectionHandler;
|
||||
|
||||
/**
|
||||
* Creates new {@link JedisClusterNodeResourceProvider}.
|
||||
*
|
||||
* @param cluster must not be {@literal null}.
|
||||
* @param topologyProvider must not be {@literal null}.
|
||||
*/
|
||||
public JedisClusterNodeResourceProvider(JedisCluster cluster) {
|
||||
JedisClusterNodeResourceProvider(JedisCluster cluster, ClusterTopologyProvider topologyProvider) {
|
||||
|
||||
this.cluster = cluster;
|
||||
this.topologyProvider = topologyProvider;
|
||||
|
||||
if (cluster != null) {
|
||||
PropertyAccessor accessor = new DirectFieldAccessFallbackBeanWrapper(cluster);
|
||||
|
||||
this.connectionHandler = accessor.isReadableProperty("connectionHandler")
|
||||
? (JedisClusterConnectionHandler) accessor.getPropertyValue("connectionHandler")
|
||||
: null;
|
||||
} else {
|
||||
this.connectionHandler = null;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -778,17 +798,23 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection {
|
||||
@SuppressWarnings("unchecked")
|
||||
public Jedis getResourceForSpecificNode(RedisClusterNode node) {
|
||||
|
||||
Assert.notNull(node, "Cannot get Pool for 'null' node!");
|
||||
|
||||
JedisPool pool = getResourcePoolForSpecificNode(node);
|
||||
if (pool != null) {
|
||||
return pool.getResource();
|
||||
}
|
||||
|
||||
Jedis connection = getConnectionForSpecificNode(node);
|
||||
|
||||
if (connection != null) {
|
||||
return connection;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node));
|
||||
}
|
||||
|
||||
protected JedisPool getResourcePoolForSpecificNode(RedisNode node) {
|
||||
|
||||
Assert.notNull(node, "Cannot get Pool for 'null' node!");
|
||||
private JedisPool getResourcePoolForSpecificNode(RedisClusterNode node) {
|
||||
|
||||
Map<String, JedisPool> clusterNodes = cluster.getClusterNodes();
|
||||
if (clusterNodes.containsKey(node.asString())) {
|
||||
@@ -798,6 +824,16 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Jedis getConnectionForSpecificNode(RedisClusterNode node) {
|
||||
|
||||
RedisClusterNode member = topologyProvider.getTopology().lookup(node);
|
||||
if (connectionHandler != null && member != null) {
|
||||
return connectionHandler.getConnectionFromNode(new HostAndPort(member.getHost(), member.getPort()));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.ClusterNodeResourceProvider#returnResourceForSpecificNode(org.springframework.data.redis.connection.RedisClusterNode, java.lang.Object)
|
||||
@@ -806,7 +842,6 @@ public class JedisClusterConnection implements DefaultedRedisClusterConnection {
|
||||
public void returnResourceForSpecificNode(RedisClusterNode node, Object client) {
|
||||
getResourcePoolForSpecificNode(node).returnResource((Jedis) client);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,6 +59,7 @@ import org.springframework.data.redis.connection.RedisPassword;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConnection;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisClusterConnection.JedisClusterTopologyProvider;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -389,9 +390,9 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
|
||||
private JedisCluster createCluster() {
|
||||
|
||||
JedisCluster cluster = createCluster(this.clusterConfig, getPoolConfig());
|
||||
this.clusterCommandExecutor = new ClusterCommandExecutor(
|
||||
new JedisClusterConnection.JedisClusterTopologyProvider(cluster),
|
||||
new JedisClusterConnection.JedisClusterNodeResourceProvider(cluster), EXCEPTION_TRANSLATION);
|
||||
JedisClusterTopologyProvider topologyProvider = new JedisClusterTopologyProvider(cluster);
|
||||
this.clusterCommandExecutor = new ClusterCommandExecutor(topologyProvider,
|
||||
new JedisClusterConnection.JedisClusterNodeResourceProvider(cluster, topologyProvider), EXCEPTION_TRANSLATION);
|
||||
return cluster;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user