Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a suppressed exception in cluster initialization #3274

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}