Skip to content

Commit

Permalink
throw exception if JedisCluster cannot connect to any host-port (#2721)
Browse files Browse the repository at this point in the history
Original PR: #2275 
Original author: @chinafzy 

If none of the `startNodes`, given in JedisCluster constructor, works:
- JedisCluster does not work.
- No message/exception comes up.
- JedisCluster cannot not retry later.

User would be instantly aware if an exception is thrown.

Co-authored-by: fangzeyu <zeyu.fang@chelaile.net.cn>
  • Loading branch information
sazzad16 and fangzeyu1 authored Dec 9, 2021
1 parent ecdc50d commit e56e46b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig
// try next nodes
}
}

throw new JedisClusterOperationException("Could not initialize cluster slots cache.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package redis.clients.jedis;

import org.junit.Test;

import redis.clients.jedis.exceptions.JedisClusterOperationException;

public class JedisClusterWithoutSetupTest {

@Test(expected = JedisClusterOperationException.class)
public void uselessStartNodes() {
new JedisCluster(new HostAndPort("localhost", 7378));
}
}
27 changes: 15 additions & 12 deletions src/test/java/redis/clients/jedis/SSLACLJedisClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ public void connectByIpAddressFailsWithSSLParameters() {
DefaultJedisClientConfig.builder().user("default").password("cluster").ssl(true)
.sslParameters(sslParameters).hostAndPortMapper(hostAndPortMap).build(),
DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc.get("key");
Assert.fail("There should be no reachable node in cluster.");
// } catch (JedisNoReachableClusterNodeException e) {
// jc.get("key");
// Assert.fail("There should be no reachable node in cluster.");
//// } catch (JedisNoReachableClusterNodeException e) {
} catch (JedisClusterOperationException e) {
assertEquals("No reachable node in cluster.", e.getMessage());
// assertEquals("No reachable node in cluster.", e.getMessage());
assertEquals("Could not initialize cluster slots cache.", e.getMessage());
}
}

Expand All @@ -169,13 +170,14 @@ public void connectWithCustomHostNameVerifier() {
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) {
// 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("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),
Expand Down Expand Up @@ -205,11 +207,12 @@ public void connectWithEmptyTrustStore() throws Exception {
try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379),
DefaultJedisClientConfig.builder().user("default").password("cluster").ssl(true)
.sslSocketFactory(sslSocketFactory).build(), DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc.get("key");
Assert.fail("There should be no reachable node in cluster.");
// } catch (JedisNoReachableClusterNodeException e) {
// jc.get("key");
// Assert.fail("There should be no reachable node in cluster.");
//// } catch (JedisNoReachableClusterNodeException e) {
} catch (JedisClusterOperationException e) {
assertEquals("No reachable node in cluster.", e.getMessage());
// assertEquals("No reachable node in cluster.", e.getMessage());
assertEquals("Could not initialize cluster slots cache.", e.getMessage());
}
}

Expand Down
27 changes: 15 additions & 12 deletions src/test/java/redis/clients/jedis/SSLJedisClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ public void connectByIpAddressFailsWithSSLParameters() {
DefaultJedisClientConfig.builder().password("cluster").ssl(true)
.sslParameters(sslParameters).hostAndPortMapper(hostAndPortMap).build(),
DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc.get("key");
Assert.fail("There should be no reachable node in cluster.");
// } catch (JedisNoReachableClusterNodeException e) {
// jc.get("key");
// Assert.fail("There should be no reachable node in cluster.");
//// } catch (JedisNoReachableClusterNodeException e) {
} catch (JedisClusterOperationException e) {
assertEquals("No reachable node in cluster.", e.getMessage());
// assertEquals("No reachable node in cluster.", e.getMessage());
assertEquals("Could not initialize cluster slots cache.", e.getMessage());
}
}

Expand All @@ -168,13 +169,14 @@ public void connectWithCustomHostNameVerifier() {
DefaultJedisClientConfig.builder().password("cluster").ssl(true)
.hostnameVerifier(hostnameVerifier).hostAndPortMapper(portMap).build(),
DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc2.get("foo");
Assert.fail("There should be no reachable node in cluster.");
// } catch (JedisNoReachableClusterNodeException e) {
// jc2.get("foo");
// 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("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),
Expand Down Expand Up @@ -204,11 +206,12 @@ public void connectWithEmptyTrustStore() throws Exception {
try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379),
DefaultJedisClientConfig.builder().password("cluster").ssl(true)
.sslSocketFactory(sslSocketFactory).build(), DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) {
jc.get("key");
Assert.fail("There should be no reachable node in cluster.");
// } catch (JedisNoReachableClusterNodeException e) {
// jc.get("key");
// Assert.fail("There should be no reachable node in cluster.");
//// } catch (JedisNoReachableClusterNodeException e) {
} catch (JedisClusterOperationException e) {
assertEquals("No reachable node in cluster.", e.getMessage());
// assertEquals("No reachable node in cluster.", e.getMessage());
assertEquals("Could not initialize cluster slots cache.", e.getMessage());
}
}

Expand Down

0 comments on commit e56e46b

Please sign in to comment.