Skip to content

Commit 83aaf12

Browse files
committed
Redis Cluster: check nodes hosts and ports at the moment of connection creation
1 parent f961984 commit 83aaf12

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

driver/src/main/java/jdbc/client/impl/RedisJedisURIBase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class RedisJedisURIBase implements JedisClientConfig {
4444
private SSLSocketFactory sslSocketFactory;
4545

4646
// host and port mapper
47-
private HostAndPortMapper hostAndPortMapper;
47+
private CompleteHostAndPortMapper hostAndPortMapper;
4848

4949

5050
protected RedisJedisURIBase(String url, Properties info) throws SQLException {
@@ -203,7 +203,7 @@ protected boolean getBoolean(Map<String, String> parameters, Properties info, St
203203

204204
private void setHostAndPortMapping(Properties info) {
205205
Map<HostAndPort, HostAndPort> mapping = getMap(info, HOST_AND_PORT_MAPPING, this::parseHostAndPort, this::parseHostAndPort);
206-
this.hostAndPortMapper = mapping == null ? null : new ClientHostAndPortMapper(mapping);
206+
this.hostAndPortMapper = mapping == null ? null : new CompleteHostAndPortMapper(mapping);
207207
}
208208

209209
// TODO (cluster): improvements + tests
@@ -262,15 +262,15 @@ public SSLSocketFactory getSslSocketFactory() {
262262
}
263263

264264
@Override
265-
public HostAndPortMapper getHostAndPortMapper() {
265+
public CompleteHostAndPortMapper getHostAndPortMapper() {
266266
return hostAndPortMapper;
267267
}
268268

269269

270-
protected static class ClientHostAndPortMapper implements HostAndPortMapper {
270+
public static class CompleteHostAndPortMapper implements HostAndPortMapper {
271271
private final Map<HostAndPort, HostAndPort> mapping;
272272

273-
public ClientHostAndPortMapper(@NotNull Map<HostAndPort, HostAndPort> mapping) {
273+
public CompleteHostAndPortMapper(@NotNull Map<HostAndPort, HostAndPort> mapping) {
274274
this.mapping = mapping;
275275
}
276276

driver/src/main/java/jdbc/client/impl/cluster/RedisJedisClusterClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import jdbc.client.RedisMode;
44
import jdbc.client.impl.RedisClientBase;
5+
import jdbc.client.impl.RedisJedisURIBase.CompleteHostAndPortMapper;
56
import jdbc.client.impl.standalone.RedisJedisClient;
67
import jdbc.client.structures.query.NodeHint;
78
import jdbc.client.structures.query.RedisKeysPatternQuery;
89
import jdbc.client.structures.query.RedisQuery;
910
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
1012
import redis.clients.jedis.*;
1113
import redis.clients.jedis.Protocol.Command;
14+
import redis.clients.jedis.exceptions.JedisConnectionException;
1215
import redis.clients.jedis.exceptions.JedisDataException;
1316
import redis.clients.jedis.exceptions.JedisException;
1417
import redis.clients.jedis.util.JedisClusterHashTag;
@@ -29,11 +32,22 @@ public class RedisJedisClusterClient extends RedisClientBase {
2932
public RedisJedisClusterClient(@NotNull RedisJedisClusterURI uri) throws SQLException {
3033
try {
3134
jedisCluster = new JedisCluster(uri.getNodes(), uri, uri.getMaxAttempts(), new SingleConnectionPoolConfig());
35+
checkClusterNodes(jedisCluster, uri.getHostAndPortMapper());
3236
} catch (JedisException e) {
3337
throw sqlWrap(e);
3438
}
3539
}
3640

41+
private static void checkClusterNodes(@NotNull JedisCluster cluster,
42+
@Nullable CompleteHostAndPortMapper hostAndPortMapper) throws JedisConnectionException {
43+
if (hostAndPortMapper == null) return;
44+
Iterable<String> nodeKeys = cluster.getClusterNodes().keySet();
45+
for (String nodeKey : nodeKeys) {
46+
HostAndPort nodeHostAndPort = HostAndPort.from(nodeKey);
47+
hostAndPortMapper.getHostAndPort(nodeHostAndPort);
48+
}
49+
}
50+
3751

3852
@Override
3953
public Object execute(@NotNull RedisQuery query) throws SQLException {

0 commit comments

Comments
 (0)