Skip to content

Commit

Permalink
Added a suppressed exception in cluster initialization (#3274)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 authored Jan 24, 2023
1 parent 79dc964 commit 6dc33b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,19 +33,30 @@ public ClusterConnectionProvider(Set<HostAndPort> clusterNodes, JedisClientConfi
}

private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
if (startNodes.isEmpty()) {
throw new JedisClusterOperationException("No nodes to initialize cluster slots cache.");
}

ArrayList<HostAndPort> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6dc33b1

Please sign in to comment.