Skip to content

Instantly share code, notes, and snippets.

@yaourt
Created September 12, 2011 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaourt/1211016 to your computer and use it in GitHub Desktop.
Save yaourt/1211016 to your computer and use it in GitHub Desktop.
Getting started JedisDynamicShardsProvider sample code
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.util.JedisDynamicShardsProvider;
public class Main {
public static void main(final String[] argv) {
// We want the following Redis to be used at startup ...
// This could be optional if we are sure that the "shards manager"
// will have a ready to go configuration at startup ....
final JedisShardInfo initialShard = new JedisShardInfo("localhost", 6379);
// Build the bridge between Jedis internals and our dummy shards manager ...
final JedisDynamicShardsProvider provider = new JedisDynamicShardsProvider(Arrays.asList(initialShard));
// Dumb shards manager that will add a new instance to the cluster every 10 secs once started
final ShardsManager shardsManager = new ShardsManager(provider);
// The usual ShardedJedis ...
final ShardedJedis shardedJedis = new ShardedJedis(provider);
// Ask the current cluster conf ...
printAllShards(shardedJedis.getAllShardInfo());
// Start the dumb manager ...
shardsManager.startShardsAdding();
for(int i=0; i < 20; i++) {
// Let's see if overtime the cluster configuration is propagated ...
printAllShards(shardedJedis.getAllShardInfo());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void printAllShards(final Collection<JedisShardInfo> list) {
final StringBuilder strb = new StringBuilder();
strb.append(new Date().toString());
for(final JedisShardInfo info : list) {
strb
.append(" ")
.append(info.getHost())
.append(':')
.append(info.getPort());
}
System.out.println(strb.toString());
}
}
import redis.clients.jedis.JedisShardInfo;
import redis.clients.util.JedisDynamicShardsProvider;
/**
* Really dumb shard manager that add a new Redis instance to the shards cluster
* every 10 sec ... once started ...
*/
public class ShardsManager implements Runnable {
private final Thread updateThread;
private final JedisDynamicShardsProvider provider;
public ShardsManager(final JedisDynamicShardsProvider provider) {
this.provider = provider;
updateThread = new Thread(this);
}
public void startShardsAdding() {
updateThread.start();
}
public void run() {
for(int currentPort = 6380; currentPort < 6389; currentPort++) {
final JedisShardInfo shard = new JedisShardInfo("localhost", currentPort);
provider.addShard(shard);
try {
Thread.sleep(10000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment