Skip to content

Instantly share code, notes, and snippets.

@ib84
Created July 11, 2011 18:32
Show Gist options
  • Save ib84/1076472 to your computer and use it in GitHub Desktop.
Save ib84/1076472 to your computer and use it in GitHub Desktop.
Simple Jedis Round Robin
import org.apache.commons.pool.impl.GenericObjectPool;
import redis.clients.jedis.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RoundRobinJedis {
private String masterIP;
private int masterPort;
private JedisPool maPool;
private Jedis master;
private Jedis readingJedis;
private List<JedisShardInfo> shards;
private ShardedJedisPool shardedPool;
private Iterator slaveIterator;
public RoundRobinJedis(String masterIP, int masterPort, List<JedisShardInfo> shards)
{
this.shards = shards;
this.slaveIterator = this.shards.iterator();
this.masterIP = masterIP;
this.masterPort = masterPort;
this.maPool = new JedisPool(new JedisPoolConfig(), this.masterIP, this.masterPort);
this.master = maPool.getResource();
this.shardedPool = new ShardedJedisPool (new GenericObjectPool.Config(), this.shards);
for(JedisShardInfo jsi: shards)
makeSlaveOfMaster(jsi);
this.slaveIterator = shardedPool.getResource().getAllShards().iterator();
}
public RoundRobinJedis(String masterIP, int masterPort)
{
List<JedisShardInfo> shardsss = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo(masterIP, masterPort);
shardsss.add(si);
this.shards = shardsss;
this.slaveIterator = this.shards.iterator();
this.masterIP = masterIP;
this.masterPort = masterPort;
this.maPool = new JedisPool(new JedisPoolConfig(), this.masterIP, this.masterPort);
this.master = maPool.getResource();
this.shardedPool = new ShardedJedisPool (new GenericObjectPool.Config(), this.shards);
this.slaveIterator = shardedPool.getResource().getAllShards().iterator();
}
public List<JedisShardInfo> getShards() { return shards;}
public void addShardSet(List<JedisShardInfo> shards)
{
for(JedisShardInfo jsi: shards)
makeSlaveOfMaster(jsi);
shards.addAll(shards);
reinitialize();
}
public void setShards(List<JedisShardInfo> shardss)
{
for(JedisShardInfo jsi: shardss)
makeSlaveOfMaster(jsi);
this.shards = shardss;
reinitialize();
}
private void reinitialize()
{
this.shardedPool.destroy();
this.shardedPool = new ShardedJedisPool (new GenericObjectPool.Config(), this.shards);
this.slaveIterator = shardedPool.getResource().getAllShards().iterator();
}
public Jedis getMaster() {return master;}
public Jedis getNextSlave()
{
if(this.slaveIterator.hasNext())
{
return (Jedis) this.slaveIterator.next();
}
else
{
slaveIterator = shardedPool.getResource().getAllShards().iterator();
return (Jedis) this.slaveIterator.next();
}
}
public void addSlave(String ip, int port)
{
JedisShardInfo newSlave = new JedisShardInfo(ip, port);
makeSlaveOfMaster(newSlave);
this.shards.add(newSlave);
this.shardedPool.destroy();
this.shardedPool = new ShardedJedisPool (new GenericObjectPool.Config(), this.shards);
this.slaveIterator = shardedPool.getResource().getAllShards().iterator();
}
public void removeSlave(String ip, int port)
{
JedisShardInfo removeThatSlave = new JedisShardInfo(ip, port);
try{ Jedis temp = removeThatSlave.createResource(); temp.slaveofNoOne(); temp.disconnect(); }
catch (Exception ex) {System.out.println("Unable to run slaveofNoOne");}
JedisShardInfo current;
for (int i = 1; i< shards.size(); i++)
{
current = (JedisShardInfo) shards.get(i);
if (current.equals(removeThatSlave))
shards.remove(i);
}
this.slaveIterator = shardedPool.getResource().getAllShards().iterator();
}
private void makeSlaveOfMaster(JedisShardInfo s)
{
Jedis temp = s.createResource();
temp.slaveof(masterIP, masterPort);
temp.disconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment