Now Sharded will handle connections instead of Info, so connection won't be shared between instances of ShardedJedis

This commit is contained in:
Jonathan Leibiusky
2010-11-22 12:49:18 -03:00
parent ad149e8252
commit a1815f3881
7 changed files with 58 additions and 47 deletions

View File

@@ -31,8 +31,8 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
}
public void disconnect() throws IOException {
for (JedisShardInfo jedis : getAllShards()) {
jedis.getResource().disconnect();
for (Jedis jedis : getAllShards()) {
jedis.disconnect();
}
}

View File

@@ -29,9 +29,9 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
}
public void disconnect() throws IOException {
for (JedisShardInfo jedis : getAllShards()) {
jedis.getResource().quit();
jedis.getResource().disconnect();
for (Jedis jedis : getAllShards()) {
jedis.quit();
jedis.disconnect();
}
}

View File

@@ -51,9 +51,9 @@ public class ShardedJedisPool extends Pool<ShardedJedis> {
boolean done = false;
while (!done) {
try {
for (JedisShardInfo shard : jedis.getAllShards()) {
if (!shard.getResource().isConnected()) {
shard.getResource().connect();
for (Jedis shard : jedis.getAllShards()) {
if (!shard.isConnected()) {
shard.connect();
}
}
done = true;
@@ -82,9 +82,8 @@ public class ShardedJedisPool extends Pool<ShardedJedis> {
public boolean validateObject(final Object obj) {
try {
ShardedJedis jedis = (ShardedJedis) obj;
for (JedisShardInfo shard : jedis.getAllShards()) {
if (!shard.getResource().isConnected()
|| !shard.getResource().ping().equals("PONG")) {
for (Jedis shard : jedis.getAllShards()) {
if (!shard.isConnected() || !shard.ping().equals("PONG")) {
return false;
}
}

View File

@@ -1,8 +1,6 @@
package redis.clients.util;
public abstract class ShardInfo<T> {
private T resource;
private int weight;
public ShardInfo() {
@@ -16,13 +14,5 @@ public abstract class ShardInfo<T> {
return this.weight;
}
public T getResource() {
return resource;
}
public void initResource () {
resource = createResource();
}
protected abstract T createResource();
}

View File

@@ -2,8 +2,10 @@ package redis.clients.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.regex.Matcher;
@@ -14,6 +16,7 @@ public class Sharded<R, S extends ShardInfo<R>> {
public static final int DEFAULT_WEIGHT = 1;
private TreeMap<Long, S> nodes;
private final Hashing algo;
private final Map<ShardInfo<R>, R> resources = new HashMap<ShardInfo<R>, R>();
/**
* The default pattern used for extracting a key tag. The pattern must have
@@ -63,7 +66,7 @@ public class Sharded<R, S extends ShardInfo<R>> {
long floor = Long.MIN_VALUE;
for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i);
shardInfo.initResource();
resources.put(shardInfo, shardInfo.createResource());
nodes.put(floor, shardInfo);
floor += 4 * oneForthOfStep * shardInfo.getWeight(); // *4 to
// compensate
@@ -72,11 +75,11 @@ public class Sharded<R, S extends ShardInfo<R>> {
}
public R getShard(byte[] key) {
return getShardInfo(key).getResource();
return resources.get(getShardInfo(key));
}
public R getShard(String key) {
return getShardInfo(key).getResource();
return resources.get(getShardInfo(key));
}
private S getShardInfo(byte[] key) {
@@ -111,7 +114,11 @@ public class Sharded<R, S extends ShardInfo<R>> {
return key;
}
public Collection<S> getAllShards() {
public Collection<S> getAllShardInfo() {
return Collections.unmodifiableCollection(nodes.values());
}
public Collection<R> getAllShards() {
return Collections.unmodifiableCollection(resources.values());
}
}