Skip to content

Commit bc0fbf8

Browse files
committed
Remove unused & buggy method / class (related to ThreadLocal<Random>)
1 parent 87f9938 commit bc0fbf8

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

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

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package redis.clients.jedis;
22

3-
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
4-
import redis.clients.jedis.exceptions.JedisConnectionException;
3+
import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;
54

65
import java.util.Map;
7-
import java.util.Random;
86
import java.util.Set;
97

10-
import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;
8+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
9+
10+
import redis.clients.jedis.exceptions.JedisConnectionException;
1111

1212
public abstract class JedisClusterConnectionHandler {
1313
protected final JedisClusterInfoCache cache;
14-
private ThreadLocal<Random> random = new ThreadLocal<Random>();
1514

1615
abstract Jedis getConnection();
1716

@@ -29,7 +28,6 @@ public void returnBrokenConnection(Jedis connection) {
2928

3029
public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig) {
3130
this.cache = new JedisClusterInfoCache(poolConfig);
32-
this.random.set(new Random());
3331
initializeSlotsCache(nodes, poolConfig);
3432
}
3533

@@ -80,9 +78,4 @@ public void renewSlotCache() {
8078
}
8179
}
8280

83-
protected JedisPool getRandomConnection() {
84-
Object[] nodeArray = cache.getNodes().values().toArray();
85-
return (JedisPool) (nodeArray[this.random.get().nextInt(nodeArray.length)]);
86-
}
87-
8881
}

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

-26
This file was deleted.

src/test/java/redis/clients/jedis/tests/JedisClusterTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
import java.util.Iterator;
66
import java.util.List;
77
import java.util.Set;
8+
import java.util.concurrent.ArrayBlockingQueue;
9+
import java.util.concurrent.BlockingQueue;
10+
import java.util.concurrent.Callable;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.Future;
13+
import java.util.concurrent.ThreadPoolExecutor;
14+
import java.util.concurrent.TimeUnit;
815

916
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
1017
import org.junit.After;
@@ -335,6 +342,33 @@ public void testCloseable() {
335342
}
336343
}
337344

345+
@Test
346+
public void testJedisClusterRunsWithMultithreaded() throws InterruptedException, ExecutionException {
347+
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
348+
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
349+
final JedisCluster jc = new JedisCluster(jedisClusterNode);
350+
jc.set("foo", "bar");
351+
352+
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
353+
List<Future<String>> futures = new ArrayList<Future<String>>();
354+
for (int i = 0 ; i < 50 ; i++) {
355+
executor.submit(new Callable<String>() {
356+
@Override
357+
public String call() throws Exception {
358+
// FIXME : invalidate slot cache from JedisCluster to test random connection also does work
359+
return jc.get("foo");
360+
}
361+
});
362+
}
363+
364+
for (Future<String> future : futures) {
365+
String value = future.get();
366+
assertEquals("bar", value);
367+
}
368+
369+
jc.close();
370+
}
371+
338372
private static String getNodeServingSlotRange(String infoOutput) {
339373
// f4f3dc4befda352a4e0beccf29f5e8828438705d 127.0.0.1:7380 master - 0 1394372400827 0 connected 5461-10922
340374
for (String infoLine : infoOutput.split("\n")) {

0 commit comments

Comments
 (0)