Skip to content

Commit b79d068

Browse files
SpikhalskiyHeartSaVioR
authored andcommitted
Cleanup cache state from failed attempts of cluster discovering
1 parent e85a9e4 commit b79d068

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/main/java/redis/clients/jedis/BinaryJedisCluster.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,7 @@ public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeo
5454
@Override
5555
public void close() {
5656
if (connectionHandler != null) {
57-
for (JedisPool pool : connectionHandler.getNodes().values()) {
58-
try {
59-
if (pool != null) {
60-
pool.destroy();
61-
}
62-
} catch (Exception e) {
63-
// pass
64-
}
65-
}
57+
connectionHandler.close();
6658
}
6759
}
6860

src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package redis.clients.jedis;
22

3+
import java.io.Closeable;
34
import java.util.ArrayList;
45
import java.util.Collections;
56
import java.util.List;
@@ -10,7 +11,7 @@
1011

1112
import redis.clients.jedis.exceptions.JedisConnectionException;
1213

13-
public abstract class JedisClusterConnectionHandler {
14+
public abstract class JedisClusterConnectionHandler implements Closeable {
1415
protected final JedisClusterInfoCache cache;
1516

1617
public JedisClusterConnectionHandler(Set<HostAndPort> nodes,
@@ -73,6 +74,11 @@ public void renewSlotCache(Jedis jedis) {
7374
}
7475
}
7576

77+
@Override
78+
public void close() {
79+
cache.reset();
80+
}
81+
7682
protected List<JedisPool> getShuffledNodesPool() {
7783
List<JedisPool> pools = new ArrayList<JedisPool>();
7884
pools.addAll(cache.getNodes().values());

src/main/java/redis/clients/jedis/JedisClusterInfoCache.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import redis.clients.util.SafeEncoder;
1717

1818
public class JedisClusterInfoCache {
19-
private Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
20-
private Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
19+
private final Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
20+
private final Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
2121

2222
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
2323
private final Lock r = rwl.readLock();
@@ -45,9 +45,7 @@ public void discoverClusterNodesAndSlots(Jedis jedis) {
4545
w.lock();
4646

4747
try {
48-
this.nodes.clear();
49-
this.slots.clear();
50-
48+
reset();
5149
List<Object> slots = jedis.clusterSlots();
5250

5351
for (Object slotInfoObj : slots) {
@@ -227,6 +225,28 @@ public Map<String, JedisPool> getNodes() {
227225
}
228226
}
229227

228+
/**
229+
* Clear discovered nodes collections and gently release allocated resources
230+
*/
231+
public void reset() {
232+
w.lock();
233+
try {
234+
for (JedisPool pool : nodes.values()) {
235+
try {
236+
if (pool != null) {
237+
pool.destroy();
238+
}
239+
} catch (Exception e) {
240+
// pass
241+
}
242+
}
243+
nodes.clear();
244+
slots.clear();
245+
} finally {
246+
w.unlock();
247+
}
248+
}
249+
230250
public static String getNodeKey(HostAndPort hnp) {
231251
return hnp.getHost() + ":" + hnp.getPort();
232252
}

0 commit comments

Comments
 (0)