From 0762125a55ad79d6a6ffa05ad308921b838bccc5 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:27:11 +0600 Subject: [PATCH] Added a suppressed exception in cluster initialization --- .../providers/ClusterConnectionProvider.java | 14 +++++++++++-- .../jedis/JedisClusterWithoutSetupTest.java | 20 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java index 4ac0470ad1..57cf18bbc1 100644 --- a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java @@ -15,7 +15,6 @@ import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.JedisClusterInfoCache; import redis.clients.jedis.exceptions.JedisClusterOperationException; -import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; public class ClusterConnectionProvider implements ConnectionProvider { @@ -34,19 +33,30 @@ public ClusterConnectionProvider(Set clusterNodes, JedisClientConfi } private void initializeSlotsCache(Set startNodes, JedisClientConfig clientConfig) { + if (startNodes.isEmpty()) { + throw new JedisClusterOperationException("No nodes to initialize cluster slots cache."); + } + ArrayList startNodeList = new ArrayList<>(startNodes); Collections.shuffle(startNodeList); + JedisException firstException = null; for (HostAndPort hostAndPort : startNodeList) { try (Connection jedis = new Connection(hostAndPort, clientConfig)) { cache.discoverClusterNodesAndSlots(jedis); return; } catch (JedisException e) { + if (firstException == null) { + firstException = e; + } // try next nodes } } - throw new JedisClusterOperationException("Could not initialize cluster slots cache."); + JedisClusterOperationException uninitializedException + = new JedisClusterOperationException("Could not initialize cluster slots cache."); + uninitializedException.addSuppressed(firstException); + throw uninitializedException; } @Override diff --git a/src/test/java/redis/clients/jedis/JedisClusterWithoutSetupTest.java b/src/test/java/redis/clients/jedis/JedisClusterWithoutSetupTest.java index 4ae71eecd7..f683908653 100644 --- a/src/test/java/redis/clients/jedis/JedisClusterWithoutSetupTest.java +++ b/src/test/java/redis/clients/jedis/JedisClusterWithoutSetupTest.java @@ -1,13 +1,27 @@ package redis.clients.jedis; -import org.junit.Test; +import static java.util.Collections.emptySet; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import org.junit.Test; import redis.clients.jedis.exceptions.JedisClusterOperationException; public class JedisClusterWithoutSetupTest { - @Test(expected = JedisClusterOperationException.class) + @Test + public void noStartNodes() { + JedisClusterOperationException operationException = assertThrows( + JedisClusterOperationException.class, () -> new JedisCluster(emptySet())); + assertEquals("No nodes to initialize cluster slots cache.", operationException.getMessage()); + assertEquals(0, operationException.getSuppressed().length); + } + + @Test public void uselessStartNodes() { - new JedisCluster(new HostAndPort("localhost", 7378)); + JedisClusterOperationException operationException = assertThrows( + JedisClusterOperationException.class, () -> new JedisCluster(new HostAndPort("localhost", 7378))); + assertEquals("Could not initialize cluster slots cache.", operationException.getMessage()); + assertEquals(1, operationException.getSuppressed().length); } }