Skip to content

Commit 6dc33b1

Browse files
authored
Added a suppressed exception in cluster initialization (#3274)
1 parent 79dc964 commit 6dc33b1

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import redis.clients.jedis.ConnectionPool;
1616
import redis.clients.jedis.JedisClusterInfoCache;
1717
import redis.clients.jedis.exceptions.JedisClusterOperationException;
18-
import redis.clients.jedis.exceptions.JedisConnectionException;
1918
import redis.clients.jedis.exceptions.JedisException;
2019

2120
public class ClusterConnectionProvider implements ConnectionProvider {
@@ -34,19 +33,30 @@ public ClusterConnectionProvider(Set<HostAndPort> clusterNodes, JedisClientConfi
3433
}
3534

3635
private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
36+
if (startNodes.isEmpty()) {
37+
throw new JedisClusterOperationException("No nodes to initialize cluster slots cache.");
38+
}
39+
3740
ArrayList<HostAndPort> startNodeList = new ArrayList<>(startNodes);
3841
Collections.shuffle(startNodeList);
3942

43+
JedisException firstException = null;
4044
for (HostAndPort hostAndPort : startNodeList) {
4145
try (Connection jedis = new Connection(hostAndPort, clientConfig)) {
4246
cache.discoverClusterNodesAndSlots(jedis);
4347
return;
4448
} catch (JedisException e) {
49+
if (firstException == null) {
50+
firstException = e;
51+
}
4552
// try next nodes
4653
}
4754
}
4855

49-
throw new JedisClusterOperationException("Could not initialize cluster slots cache.");
56+
JedisClusterOperationException uninitializedException
57+
= new JedisClusterOperationException("Could not initialize cluster slots cache.");
58+
uninitializedException.addSuppressed(firstException);
59+
throw uninitializedException;
5060
}
5161

5262
@Override
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package redis.clients.jedis;
22

3-
import org.junit.Test;
3+
import static java.util.Collections.emptySet;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThrows;
46

7+
import org.junit.Test;
58
import redis.clients.jedis.exceptions.JedisClusterOperationException;
69

710
public class JedisClusterWithoutSetupTest {
811

9-
@Test(expected = JedisClusterOperationException.class)
12+
@Test
13+
public void noStartNodes() {
14+
JedisClusterOperationException operationException = assertThrows(
15+
JedisClusterOperationException.class, () -> new JedisCluster(emptySet()));
16+
assertEquals("No nodes to initialize cluster slots cache.", operationException.getMessage());
17+
assertEquals(0, operationException.getSuppressed().length);
18+
}
19+
20+
@Test
1021
public void uselessStartNodes() {
11-
new JedisCluster(new HostAndPort("localhost", 7378));
22+
JedisClusterOperationException operationException = assertThrows(
23+
JedisClusterOperationException.class, () -> new JedisCluster(new HostAndPort("localhost", 7378)));
24+
assertEquals("Could not initialize cluster slots cache.", operationException.getMessage());
25+
assertEquals(1, operationException.getSuppressed().length);
1226
}
1327
}

0 commit comments

Comments
 (0)