Skip to content

Commit

Permalink
Remove unused & buggy method / class (related to ThreadLocal<Random>)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeartSaVioR committed Sep 18, 2014
1 parent 87f9938 commit bc0fbf8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package redis.clients.jedis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;

import java.util.Map;
import java.util.Random;
import java.util.Set;

import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;
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();

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

public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig) {
this.cache = new JedisClusterInfoCache(poolConfig);
this.random.set(new Random());
initializeSlotsCache(nodes, poolConfig);
}

Expand Down Expand Up @@ -80,9 +78,4 @@ public void renewSlotCache() {
}
}

protected JedisPool getRandomConnection() {
Object[] nodeArray = cache.getNodes().values().toArray();
return (JedisPool) (nodeArray[this.random.get().nextInt(nodeArray.length)]);
}

}

This file was deleted.

34 changes: 34 additions & 0 deletions src/test/java/redis/clients/jedis/tests/JedisClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
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;
Expand Down Expand Up @@ -335,6 +342,33 @@ public void testCloseable() {
}
}

@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")) {
Expand Down

0 comments on commit bc0fbf8

Please sign in to comment.