Remove unused & buggy method / class (related to ThreadLocal<Random>)

This commit is contained in:
Jungtaek Lim
2014-09-19 07:51:53 +09:00
parent 45fce4368e
commit f8c69b105a
3 changed files with 43 additions and 40 deletions

View File

@@ -1,17 +1,16 @@
package redis.clients.jedis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;
import java.util.Map;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
public abstract class JedisClusterConnectionHandler {
protected final JedisClusterInfoCache cache;
private ThreadLocal<Random> random = new ThreadLocal<Random>();
abstract Jedis getConnection();
@@ -29,7 +28,6 @@ public abstract class JedisClusterConnectionHandler {
public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig) {
this.cache = new JedisClusterInfoCache(poolConfig);
this.random.set(new Random());
initializeSlotsCache(nodes, poolConfig);
}
@@ -80,9 +78,4 @@ public abstract class JedisClusterConnectionHandler {
}
}
protected JedisPool getRandomConnection() {
Object[] nodeArray = cache.getNodes().values().toArray();
return (JedisPool) (nodeArray[this.random.get().nextInt(nodeArray.length)]);
}
}

View File

@@ -1,26 +0,0 @@
package redis.clients.jedis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import java.util.Set;
public class JedisRandomConnectionHandler extends JedisClusterConnectionHandler {
public JedisRandomConnectionHandler(Set<HostAndPort> nodes) {
super(nodes, new GenericObjectPoolConfig());
}
public JedisRandomConnectionHandler(Set<HostAndPort> nodes,
final GenericObjectPoolConfig poolConfig) {
super(nodes, poolConfig);
}
public Jedis getConnection() {
return getRandomConnection().getResource();
}
@Override
Jedis getConnectionFromSlot(int slot) {
return getRandomConnection().getResource();
}
}

View File

@@ -1,8 +1,17 @@
package redis.clients.jedis.tests;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.After;
@@ -333,6 +342,33 @@ public class JedisClusterTest extends Assert {
}
}
@Test
public void testJedisClusterRunsWithMultithreaded() throws InterruptedException, ExecutionException {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
final JedisCluster jc = new JedisCluster(jedisClusterNode);
jc.set("foo", "bar");
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
List<Future<String>> futures = new ArrayList<Future<String>>();
for (int i = 0 ; i < 50 ; i++) {
executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
// FIXME : invalidate slot cache from JedisCluster to test random connection also does work
return jc.get("foo");
}
});
}
for (Future<String> future : futures) {
String value = future.get();
assertEquals("bar", value);
}
jc.close();
}
private static String getNodeServingSlotRange(String infoOutput) {
// f4f3dc4befda352a4e0beccf29f5e8828438705d 127.0.0.1:7380 master - 0 1394372400827 0 connected 5461-10922
for (String infoLine : infoOutput.split("\n")) {