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
@@ -15,19 +15,22 @@
|
||||
*/
|
||||
package org.springframework.data.redis.connection.jedis;
|
||||
|
||||
import static org.hamcrest.core.Is.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.redis.connection.ClusterTestVariables.*;
|
||||
import static org.springframework.data.redis.test.util.MockitoUtils.*;
|
||||
|
||||
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 redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -38,7 +41,10 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.data.redis.ClusterStateFailureException;
|
||||
import org.springframework.data.redis.connection.ClusterInfo;
|
||||
import org.springframework.data.redis.connection.RedisClusterCommands.AddSlots;
|
||||
@@ -72,7 +78,8 @@ public class JedisClusterConnectionUnitTests {
|
||||
|
||||
JedisClusterConnection connection;
|
||||
|
||||
@Mock JedisCluster clusterMock;
|
||||
@Spy StubJedisCluster clusterMock;
|
||||
@Mock JedisClusterConnectionHandler connectionHandlerMock;
|
||||
|
||||
@Mock JedisPool node1PoolMock;
|
||||
@Mock JedisPool node2PoolMock;
|
||||
@@ -82,12 +89,13 @@ public class JedisClusterConnectionUnitTests {
|
||||
@Mock Jedis con2Mock;
|
||||
@Mock Jedis con3Mock;
|
||||
|
||||
Map<String, JedisPool> nodes = new LinkedHashMap<>();
|
||||
|
||||
public @Rule ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
Map<String, JedisPool> nodes = new LinkedHashMap<>(3);
|
||||
nodes.put(CLUSTER_HOST + ":" + MASTER_NODE_1_PORT, node1PoolMock);
|
||||
nodes.put(CLUSTER_HOST + ":" + MASTER_NODE_2_PORT, node2PoolMock);
|
||||
nodes.put(CLUSTER_HOST + ":" + MASTER_NODE_3_PORT, node3PoolMock);
|
||||
@@ -98,6 +106,7 @@ public class JedisClusterConnectionUnitTests {
|
||||
when(node3PoolMock.getResource()).thenReturn(con3Mock);
|
||||
|
||||
when(con1Mock.clusterNodes()).thenReturn(CLUSTER_NODES_RESPONSE);
|
||||
clusterMock.setConnectionHandler(connectionHandlerMock);
|
||||
|
||||
connection = new JedisClusterConnection(clusterMock);
|
||||
}
|
||||
@@ -251,6 +260,53 @@ public class JedisClusterConnectionUnitTests {
|
||||
verifyInvocationsAcross("time", times(1), con1Mock, con2Mock, con3Mock);
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-679
|
||||
public void shouldFailWithUnknownNode() {
|
||||
|
||||
try {
|
||||
connection.serverCommands().dbSize(new RedisClusterNode(CLUSTER_HOST, SLAVEOF_NODE_1_PORT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(),
|
||||
containsString("Node " + CLUSTER_HOST + ":" + SLAVEOF_NODE_1_PORT + " is unknown to cluster"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-679
|
||||
public void shouldFailWithAbsentConnection() {
|
||||
|
||||
nodes.remove(CLUSTER_HOST + ":" + MASTER_NODE_3_PORT);
|
||||
|
||||
try {
|
||||
connection.serverCommands().dbSize(new RedisClusterNode(CLUSTER_HOST, MASTER_NODE_3_PORT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(),
|
||||
containsString("Node " + CLUSTER_HOST + ":" + MASTER_NODE_3_PORT + " is unknown to cluster"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-679
|
||||
public void shouldReconfigureJedisWithDiscoveredNode() {
|
||||
|
||||
nodes.remove(CLUSTER_HOST + ":" + MASTER_NODE_3_PORT);
|
||||
|
||||
when(connectionHandlerMock.getConnectionFromNode(new HostAndPort(CLUSTER_HOST, MASTER_NODE_3_PORT)))
|
||||
.thenReturn(con3Mock);
|
||||
when(con3Mock.dbSize()).thenAnswer(new Answer<Long>() {
|
||||
@Override
|
||||
public Long answer(InvocationOnMock invocation) throws Throwable {
|
||||
|
||||
// Required to return the resource properly after invocation.
|
||||
nodes.put(CLUSTER_HOST + ":" + MASTER_NODE_3_PORT, node3PoolMock);
|
||||
|
||||
return 42L;
|
||||
}
|
||||
});
|
||||
|
||||
Long result = connection.serverCommands().dbSize(new RedisClusterNode(CLUSTER_HOST, MASTER_NODE_3_PORT));
|
||||
|
||||
assertThat(result, is(42L));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-315
|
||||
public void timeShouldBeExecutedOnSingleNode() {
|
||||
|
||||
@@ -297,4 +353,21 @@ public class JedisClusterConnectionUnitTests {
|
||||
|
||||
new JedisClusterTopologyProvider(clusterMock).getTopology();
|
||||
}
|
||||
|
||||
static class StubJedisCluster extends JedisCluster {
|
||||
|
||||
JedisClusterConnectionHandler connectionHandler;
|
||||
|
||||
public StubJedisCluster() {
|
||||
super(Collections.emptySet());
|
||||
}
|
||||
|
||||
JedisClusterConnectionHandler getConnectionHandler() {
|
||||
return connectionHandler;
|
||||
}
|
||||
|
||||
void setConnectionHandler(JedisClusterConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user