use of redis.clients.jedis.exceptions.JedisClusterOperationException in project jedis by xetorthio.
the class SSLACLJedisClusterTest method connectWithCustomHostNameVerifier.
@Test
public void connectWithCustomHostNameVerifier() {
HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
HostnameVerifier localhostVerifier = new LocalhostVerifier();
try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DefaultJedisClientConfig.builder().user("default").password("cluster").ssl(true).hostnameVerifier(hostnameVerifier).hostAndPortMapper(portMap).build(), DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc.get("foo");
Assert.fail("It should fail after all cluster attempts.");
// } catch (JedisClusterMaxAttemptsException e) {
} catch (JedisClusterOperationException e) {
// initial connection made with 'localhost' but subsequent connections to nodes use 127.0.0.1
// which causes custom hostname verification to fail
assertEquals("No more cluster attempts left.", e.getMessage());
}
try (JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DefaultJedisClientConfig.builder().user("default").password("cluster").ssl(true).hostnameVerifier(hostnameVerifier).hostAndPortMapper(portMap).build(), DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
// jc2.get("key");
// Assert.fail("There should be no reachable node in cluster.");
// // } catch (JedisNoReachableClusterNodeException e) {
} catch (JedisClusterOperationException e) {
// JedisNoReachableClusterNodeException exception occurs from not being able to connect since
// the socket factory fails the hostname verification
// assertEquals("No reachable node in cluster.", e.getMessage());
assertEquals("Could not initialize cluster slots cache.", e.getMessage());
}
try (JedisCluster jc3 = new JedisCluster(new HostAndPort("localhost", 8379), DefaultJedisClientConfig.builder().user("default").password("cluster").ssl(true).hostnameVerifier(localhostVerifier).hostAndPortMapper(portMap).build(), DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc3.get("foo");
}
}
use of redis.clients.jedis.exceptions.JedisClusterOperationException in project jedis by xetorthio.
the class ClusterConnectionProvider method getConnection.
@Override
public Connection getConnection() {
// In antirez's redis-rb-cluster implementation, getRandomConnection always
// return valid connection (able to ping-pong) or exception if all
// connections are invalid
List<ConnectionPool> pools = cache.getShuffledNodesPool();
JedisException suppressed = null;
for (ConnectionPool pool : pools) {
Connection jedis = null;
try {
jedis = pool.getResource();
if (jedis == null) {
continue;
}
jedis.ping();
return jedis;
} catch (JedisException ex) {
if (suppressed == null) {
// remembering first suppressed exception
suppressed = ex;
}
if (jedis != null) {
jedis.close();
}
}
}
JedisClusterOperationException noReachableNode = new JedisClusterOperationException("No reachable node in cluster.");
if (suppressed != null) {
noReachableNode.addSuppressed(suppressed);
}
throw noReachableNode;
}
use of redis.clients.jedis.exceptions.JedisClusterOperationException in project jedis by xetorthio.
the class ClusterConnectionProvider method initializeSlotsCache.
private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
ArrayList<HostAndPort> startNodeList = new ArrayList<>(startNodes);
Collections.shuffle(startNodeList);
for (HostAndPort hostAndPort : startNodeList) {
try (Connection jedis = new Connection(hostAndPort, clientConfig)) {
cache.discoverClusterNodesAndSlots(jedis);
return;
} catch (JedisConnectionException e) {
// try next nodes
}
}
throw new JedisClusterOperationException("Could not initialize cluster slots cache.");
}
Aggregations