Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic sharding implementation #174

Closed
wants to merge 10 commits into from
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,45 @@
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import redis.clients.util.JedisDynamicShardsProvider;

public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
implements BinaryJedisCommands {
public BinaryShardedJedis(List<JedisShardInfo> shards) {
super(shards);
}

public BinaryShardedJedis(final JedisDynamicShardsProvider provider) {
super(provider);
}

public BinaryShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
super(shards, algo);
}

public BinaryShardedJedis(final JedisDynamicShardsProvider provider, final Hashing algo) {
super(provider, algo);
}

public BinaryShardedJedis(List<JedisShardInfo> shards, Pattern keyTagPattern) {
super(shards, keyTagPattern);
}

public BinaryShardedJedis(final JedisDynamicShardsProvider provider, final Pattern keyTagPattern) {
super(provider, keyTagPattern);
}

public BinaryShardedJedis(List<JedisShardInfo> shards, Hashing algo,
Pattern keyTagPattern) {
super(shards, algo, keyTagPattern);
}

public BinaryShardedJedis( final JedisDynamicShardsProvider provider, //
final Hashing algo, //
final Pattern keyTagPattern) {
super(provider, algo, keyTagPattern);
}

public void disconnect() throws IOException {
for (Jedis jedis : getAllShards()) {
jedis.disconnect();
Expand Down Expand Up @@ -155,6 +174,11 @@ public Boolean hexists(byte[] key, byte[] field) {
return j.hexists(key, field);
}

public Long del(byte[] key) {
Jedis j = getShard(key);
return j.del(key);
}

public Long hdel(byte[] key, byte[] field) {
Jedis j = getShard(key);
return j.hdel(key, field);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,46 @@
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import redis.clients.util.JedisDynamicShardsProvider;

public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
public ShardedJedis(List<JedisShardInfo> shards) {
super(shards);
}

public ShardedJedis(final JedisDynamicShardsProvider provider) {
super(provider);
}

public ShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
super(shards, algo);
}

public ShardedJedis(final JedisDynamicShardsProvider provider, //
final Hashing algo) {
super(provider, algo);
}

public ShardedJedis(List<JedisShardInfo> shards, Pattern keyTagPattern) {
super(shards, keyTagPattern);
}

public ShardedJedis(final JedisDynamicShardsProvider provider, //
final Pattern keyTagPattern) {
super(provider, keyTagPattern);
}

public ShardedJedis(List<JedisShardInfo> shards, Hashing algo,
Pattern keyTagPattern) {
super(shards, algo, keyTagPattern);
}

public ShardedJedis(final JedisDynamicShardsProvider provider, //
final Hashing algo, //
final Pattern keyTagPattern) {
super(provider, algo, keyTagPattern);
}

public void disconnect() {
for (Jedis jedis : getAllShards()) {
jedis.quit();
Expand Down
40 changes: 37 additions & 3 deletions src/main/java/redis/clients/jedis/ShardedJedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
import org.apache.commons.pool.impl.GenericObjectPool;

import redis.clients.util.Hashing;
import redis.clients.util.JedisDynamicShardsProvider;
import redis.clients.util.Pool;

public class ShardedJedisPool extends Pool<ShardedJedis> {
public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
public ShardedJedisPool (final GenericObjectPool.Config poolConfig, final JedisDynamicShardsProvider shardProvider) {
this(poolConfig, shardProvider, Hashing.MURMUR_HASH, null);
}

public ShardedJedisPool (final GenericObjectPool.Config poolConfig, final JedisDynamicShardsProvider shardProvider, final Pattern keyTagPattern) {
this(poolConfig, shardProvider, Hashing.MURMUR_HASH, keyTagPattern);
}

public ShardedJedisPool (final GenericObjectPool.Config poolConfig, final JedisDynamicShardsProvider shardProvider, final Hashing algo, final Pattern keyTagPattern) {
super(poolConfig, new ShardedJedisFactory(shardProvider, algo, keyTagPattern));
}

public ShardedJedisPool(final GenericObjectPool.Config poolConfig,
List<JedisShardInfo> shards) {
this(poolConfig, shards, Hashing.MURMUR_HASH);
}
Expand All @@ -37,22 +50,43 @@ private static class ShardedJedisFactory extends BasePoolableObjectFactory {
private List<JedisShardInfo> shards;
private Hashing algo;
private Pattern keyTagPattern;

private final JedisDynamicShardsProvider provider;
private final boolean isDynamic;

public ShardedJedisFactory(final JedisDynamicShardsProvider provider, final Hashing algo, final Pattern keyTagPattern) {
this.isDynamic = true;
this.algo = algo;
this.provider = provider;
this.shards = null;
this.keyTagPattern = keyTagPattern;
}

public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo,
Pattern keyTagPattern) {
this.isDynamic = false;
this.provider = null;
this.shards = shards;
this.algo = algo;
this.keyTagPattern = keyTagPattern;
}

public Object makeObject() throws Exception {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
return jedis;
if(isDynamic) {
ShardedJedis jedis = new ShardedJedis(provider, algo, keyTagPattern);
return jedis;
} else {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
return jedis;
}
}

public void destroyObject(final Object obj) throws Exception {
if ((obj != null) && (obj instanceof ShardedJedis)) {
ShardedJedis shardedJedis = (ShardedJedis) obj;
if(isDynamic) {
provider.unregister(shardedJedis);
}
for (Jedis jedis : shardedJedis.getAllShards()) {
try {
try {
Expand Down
Loading