From 7cb50204fcef023e8854416094aa433e414dad9e Mon Sep 17 00:00:00 2001 From: "bodong.ybd" Date: Wed, 12 Apr 2023 18:59:49 +0800 Subject: [PATCH] Renew cluster slots strategy update backported from https://github.com/redis/jedis/pull/2643 --- .../jedis/JedisClusterConnectionHandler.java | 4 +-- .../clients/jedis/JedisClusterInfoCache.java | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index a533dbf8a2..a993e92e2a 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -96,13 +96,13 @@ public JedisClusterConnectionHandler(Set nodes, final JedisClientConfig seedNodesClientConfig, final GenericObjectPoolConfig poolConfig, final JedisClientConfig clusterNodesClientConfig) { - this.cache = new JedisClusterInfoCache(poolConfig, clusterNodesClientConfig); + this.cache = new JedisClusterInfoCache(poolConfig, clusterNodesClientConfig, nodes); initializeSlotsCache(nodes, seedNodesClientConfig); } public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, final JedisClientConfig clientConfig) { - this.cache = new JedisClusterInfoCache(poolConfig, clientConfig); + this.cache = new JedisClusterInfoCache(poolConfig, clientConfig, nodes); initializeSlotsCache(nodes, clientConfig); } diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 60ec9f86d2..aa9102ce0a 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -34,6 +34,7 @@ public class JedisClusterInfoCache { private final GenericObjectPoolConfig poolConfig; private final JedisClientConfig clientConfig; + private final Set startNodes; private static final int MASTER_NODE_INDEX = 2; @@ -134,13 +135,14 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, .blockingSocketTimeoutMillis(infiniteSoTimeout).user(user).password(password) .clientName(clientName).ssl(ssl).sslSocketFactory(sslSocketFactory) .sslParameters(sslParameters).hostnameVerifier(hostnameVerifier) - .hostAndPortMapper(hostAndPortMap).build()); + .hostAndPortMapper(hostAndPortMap).build(), null); } public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, - final JedisClientConfig clientConfig) { + final JedisClientConfig clientConfig, final Set startNodes) { this.poolConfig = poolConfig; this.clientConfig = clientConfig; + this.startNodes = startNodes; } public void discoverClusterNodesAndSlots(Jedis jedis) { @@ -181,6 +183,7 @@ public void renewClusterSlots(Jedis jedis) { // If rediscovering is already in process - no need to start one more same rediscovering, just return if (rediscoverLock.tryLock()) { try { + // First, if jedis is available, use jedis renew. if (jedis != null) { try { discoverClusterSlots(jedis); @@ -190,20 +193,34 @@ public void renewClusterSlots(Jedis jedis) { } } + // Then, we use startNodes to try, as long as startNodes is available, + // whether it is vip, domain, or physical ip, it will succeed. + if (startNodes != null) { + for (HostAndPort hostAndPort : startNodes) { + try (Jedis j = new Jedis(hostAndPort, clientConfig)) { + discoverClusterSlots(j); + return; + } catch (JedisConnectionException e) { + // try next nodes + } + } + } + + // Finally, we go back to the ShuffledNodesPool and try the remaining physical nodes. for (JedisPool jp : getShuffledNodesPool()) { - Jedis j = null; - try { - j = jp.getResource(); + try (Jedis j = jp.getResource()) { + // If already tried in startNodes, skip this node. + HostAndPort hostAndPort = new HostAndPort(j.getClient().getHost(), j.getClient().getPort()); + if (startNodes != null && startNodes.contains(hostAndPort)) { + continue; + } discoverClusterSlots(j); return; } catch (JedisConnectionException e) { // try next nodes - } finally { - if (j != null) { - j.close(); - } } } + } finally { rediscoverLock.unlock(); }