org.craft.atom.redis.DefaultMasterSlaveRedis Maven / Gradle / Ivy
package org.craft.atom.redis;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.ToString;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.craft.atom.redis.api.MasterSlaveRedis;
import org.craft.atom.redis.api.Redis;
import org.craft.atom.redis.api.RedisConnectionException;
import org.craft.atom.redis.api.RedisPubSub;
import org.craft.atom.redis.api.RedisTransaction;
import org.craft.atom.redis.api.Slowlog;
import org.craft.atom.redis.api.handler.RedisMonitorHandler;
import org.craft.atom.redis.api.handler.RedisPsubscribeHandler;
import org.craft.atom.redis.api.handler.RedisSubscribeHandler;
/**
* @author mindwind
* @version 1.0, Jun 26, 2013
*/
@ToString
public class DefaultMasterSlaveRedis implements MasterSlaveRedis {
private static final Log LOG = LogFactory.getLog(DefaultMasterSlaveRedis.class);
private List chain;
private volatile int index;
// ~ ---------------------------------------------------------------------------------------------------------
public DefaultMasterSlaveRedis(List chain) {
this(chain, 0);
}
public DefaultMasterSlaveRedis(List chain, int index) {
if (chain == null) {
throw new IllegalArgumentException("Master-slave chain list is null.");
}
int size = chain.size();
if (size < 2) {
throw new IllegalArgumentException("Master-slave chain list must have 2 redis node at lease.");
}
Set set = new HashSet();
set.addAll(chain);
if (set.size() != size) {
throw new IllegalArgumentException("Invalid master-slave chain, because repeated redis node, chain=" + chain);
}
check(index, size);
this.chain = chain;
this.index = index;
rebuild();
}
private void check(int index, int size) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Master index should be in [0," + (size - 1) + "]");
}
}
// ~ ---------------------------------------------------------------------------------------------------------
@Override
public void master(int index) {
master(index, true);
}
private void master(int index, boolean rebuild) {
check(index, chain.size());
this.index = index;
if (rebuild) {
rebuild();
}
}
@Override
public Redis master() {
return chain.get(index);
}
@Override
public int index() {
return index;
}
@Override
public List chain() {
List l = new ArrayList(chain.size());
int c = index;
for (int i = 0; i < chain.size(); i++) {
l.add(chain.get(c));
c++;
if (c == chain.size()) {
c = 0;
}
}
return l;
}
private void rebuild() {
Redis master = chain.get(index);
master.slaveofnoone();
// after master
for (int i = index + 1; i < chain.size(); i++) {
Redis m = chain.get(i - 1);
Redis s = chain.get(i);
link(m, s);
}
// before master except index 0
for (int i = 1; i < index; i++) {
Redis m = chain.get(i - 1);
Redis s = chain.get(i);
link(m, s);
}
// index 0
if (index != 0) {
Redis m = chain.get(chain.size() - 1);
Redis s = chain.get(0);
link(m, s);
}
}
@Override
public void reset() {
index = 0;
rebuild();
}
private void link(Redis m, Redis s) {
try {
s.slaveof(m.host(), m.port());
} catch (RedisConnectionException e) {
LOG.warn(String.format("%s slaveof %s failed", s.toString(), m.toString()));
}
}
// ~ --------------------------------------------------------------------------------------------------------- Keys
@Override
public Long del(String... keys) {
return master().del(keys);
}
@Override
public byte[] dump(String key) {
return master().dump(key);
}
@Override
public Boolean exists(String key) {
return master().exists(key);
}
@Override
public Long expire(String key, int seconds) {
return master().expire(key, seconds);
}
@Override
public Long expireat(String key, long timestamp) {
return master().expireat(key, timestamp);
}
@Override
public Set keys(String pattern) {
return master().keys(pattern);
}
@Override
public String migrate(String host, int port, String key, int destinationdb, int timeout) {
return master().migrate(host, port, key, destinationdb, timeout);
}
@Override
public Long move(String key, int db) {
return master().move(key, db);
}
@Override
public Long objectrefcount(String key) {
return master().objectrefcount(key);
}
@Override
public String objectencoding(String key) {
return master().objectencoding(key);
}
@Override
public Long objectidletime(String key) {
return master().objectidletime(key);
}
@Override
public Long persist(String key) {
return master().persist(key);
}
@Override
public Long pexpire(String key, int milliseconds) {
return master().pexpire(key, milliseconds);
}
@Override
public Long pexpireat(String key, long millisecondstimestamp) {
return master().pexpireat(key, millisecondstimestamp);
}
@Override
public Long pttl(String key) {
return master().pttl(key);
}
@Override
public String randomkey() {
return master().randomkey();
}
@Override
public String rename(String key, String newkey) {
return master().rename(key, newkey);
}
@Override
public Long renamenx(String key, String newkey) {
return master().renamenx(key, newkey);
}
@Override
public String restore(String key, int ttl, byte[] serializedvalue) {
return master().restore(key, ttl, serializedvalue);
}
@Override
public List sort(String key) {
return master().sort(key);
}
@Override
public List sort(String key, boolean desc) {
return master().sort(key, desc);
}
@Override
public List sort(String key, boolean alpha, boolean desc) {
return master().sort(key, alpha, desc);
}
@Override
public List sort(String key, int offset, int count) {
return master().sort(key, offset, count);
}
@Override
public List sort(String key, int offset, int count, boolean alpha, boolean desc) {
return master().sort(key, offset, count, alpha, desc);
}
@Override
public List sort(String key, String bypattern, String... getpatterns) {
return master().sort(key, bypattern, getpatterns);
}
@Override
public List sort(String key, String bypattern, boolean desc, String... getpatterns) {
return master().sort(key, bypattern, desc, getpatterns);
}
@Override
public List sort(String key, String bypattern, boolean alpha, boolean desc, String... getpatterns) {
return master().sort(key, bypattern, alpha, desc, getpatterns);
}
@Override
public List sort(String key, String bypattern, int offset, int count, String... getpatterns) {
return master().sort(key, bypattern, offset, count, getpatterns);
}
@Override
public List sort(String key, String bypattern, int offset, int count, boolean alpha, boolean desc, String... getpatterns) {
return master().sort(key, bypattern, offset, count, alpha, desc, getpatterns);
}
@Override
public Long sort(String key, String destination) {
return master().sort(key, destination);
}
@Override
public Long sort(String key, boolean desc, String destination) {
return master().sort(key, desc, destination);
}
@Override
public Long sort(String key, boolean alpha, boolean desc, String destination) {
return master().sort(key, alpha, desc, destination);
}
@Override
public Long sort(String key, int offset, int count, String destination) {
return master().sort(key, offset, count, destination);
}
@Override
public Long sort(String key, int offset, int count, boolean alpha, boolean desc, String destination) {
return master().sort(key, offset, count, alpha, desc, destination);
}
@Override
public Long sort(String key, String bypattern, String destination, String... getpatterns) {
return master().sort(key, bypattern, destination, getpatterns);
}
@Override
public Long sort(String key, String bypattern, boolean desc, String destination, String... getpatterns) {
return master().sort(key, bypattern, desc, destination, getpatterns);
}
@Override
public Long sort(String key, String bypattern, boolean alpha, boolean desc, String destination, String... getpatterns) {
return master().sort(key, bypattern, alpha, desc, destination, getpatterns);
}
@Override
public Long sort(String key, String bypattern, int offset, int count, String destination, String... getpatterns) {
return master().sort(key, bypattern, offset, count, destination, getpatterns);
}
@Override
public Long sort(String key, String bypattern, int offset, int count, boolean alpha, boolean desc, String destination, String... getpatterns) {
return master().sort(key, bypattern, offset, count, alpha, desc, destination, getpatterns);
}
@Override
public Long ttl(String key) {
return master().ttl(key);
}
@Override
public String type(String key) {
return master().type(key);
}
// ~ ------------------------------------------------------------------------------------------------------ Strings
@Override
public Long append(String key, String value) {
return master().append(key, value);
}
@Override
public Long bitcount(String key) {
return master().bitcount(key);
}
@Override
public Long bitcount(String key, long start, long end) {
return master().bitcount(key, start, end);
}
@Override
public Long bitnot(String destkey, String key) {
return master().bitnot(destkey, key);
}
@Override
public Long bitand(String destkey, String... keys) {
return master().bitand(destkey, keys);
}
@Override
public Long bitor(String destkey, String... keys) {
return master().bitor(destkey, keys);
}
@Override
public Long bitxor(String destkey, String... keys) {
return master().bitxor(destkey, keys);
}
@Override
public Long decr(String key) {
return master().decr(key);
}
@Override
public Long decrby(String key, long decrement) {
return master().decrby(key, decrement);
}
@Override
public String get(String key) {
return master().get(key);
}
@Override
public Boolean getbit(String key, long offset) {
return master().getbit(key, offset);
}
@Override
public String getrange(String key, long start, long end) {
return master().getrange(key, start, end);
}
@Override
public String getset(String key, String value) {
return master().getset(key, value);
}
@Override
public Long incr(String key) {
return master().incr(key);
}
@Override
public Long incrby(String key, long increment) {
return master().incrby(key, increment);
}
@Override
public Double incrbyfloat(String key, double increment) {
return master().incrbyfloat(key, increment);
}
@Override
public List mget(String... keys) {
return master().mget(keys);
}
@Override
public String mset(String... keysvalues) {
return master().mset(keysvalues);
}
@Override
public Long msetnx(String... keysvalues) {
return master().msetnx(keysvalues);
}
@Override
public String psetex(String key, int milliseconds, String value) {
return master().psetex(key, milliseconds, value);
}
@Override
public String set(String key, String value) {
return master().set(key, value);
}
@Override
public String setxx(String key, String value) {
return master().setxx(key, value);
}
@Override
public String setnxex(String key, String value, int seconds) {
return master().setnxex(key, value, seconds);
}
@Override
public String setnxpx(String key, String value, int milliseconds) {
return master().setnxpx(key, value, milliseconds);
}
@Override
public String setxxex(String key, String value, int seconds) {
return master().setxxex(key, value, seconds);
}
@Override
public String setxxpx(String key, String value, int milliseconds) {
return master().setxxpx(key, value, milliseconds);
}
@Override
public Boolean setbit(String key, long offset, boolean value) {
return master().setbit(key, offset, value);
}
@Override
public String setex(String key, int seconds, String value) {
return master().setex(key, seconds, value);
}
@Override
public Long setnx(String key, String value) {
return master().setnx(key, value);
}
@Override
public Long setrange(String key, long offset, String value) {
return master().setrange(key, offset, value);
}
@Override
public Long strlen(String key) {
return master().strlen(key);
}
// ~ ------------------------------------------------------------------------------------------------------ Hashes
@Override
public Long hdel(String key, String... fields) {
return master().hdel(key, fields);
}
@Override
public Boolean hexists(String key, String field) {
return master().hexists(key, field);
}
@Override
public String hget(String key, String field) {
return master().hget(key, field);
}
@Override
public Map hgetall(String key) {
return master().hgetall(key);
}
@Override
public Long hincrby(String key, String field, long increment) {
return master().hincrby(key, field, increment);
}
@Override
public Double hincrbyfloat(String key, String field, double increment) {
return master().hincrbyfloat(key, field, increment);
}
@Override
public Set hkeys(String key) {
return master().hkeys(key);
}
@Override
public Long hlen(String key) {
return master().hlen(key);
}
@Override
public List hmget(String key, String... fields) {
return master().hmget(key, fields);
}
@Override
public String hmset(String key, Map fieldvalues) {
return master().hmset(key, fieldvalues);
}
@Override
public Long hset(String key, String field, String value) {
return master().hset(key, field, value);
}
@Override
public Long hsetnx(String key, String field, String value) {
return master().hsetnx(key, field, value);
}
@Override
public List hvals(String key) {
return master().hvals(key);
}
// ~ ------------------------------------------------------------------------------------------------------- Lists
@Override
public String blpop(String key) {
return master().blpop(key);
}
@Override
public String blpop(String key, int timeout) {
return master().blpop(key, timeout);
}
@Override
public Map blpop(String... keys) {
return master().blpop(keys);
}
@Override
public Map blpop(int timeout, String... keys) {
return master().blpop(timeout, keys);
}
@Override
public String brpop(String key) {
return master().brpop(key);
}
@Override
public String brpop(String key, int timeout) {
return master().brpop(key, timeout);
}
@Override
public Map brpop(String... keys) {
return master().brpop(keys);
}
@Override
public Map brpop(int timeout, String... keys) {
return master().brpop(timeout, keys);
}
@Override
public String brpoplpush(String source, String destination, int timeout) {
return master().brpoplpush(source, destination, timeout);
}
@Override
public String lindex(String key, long index) {
return master().lindex(key, index);
}
@Override
public Long linsertbefore(String key, String pivot, String value) {
return master().linsertbefore(key, pivot, value);
}
@Override
public Long linsertafter(String key, String pivot, String value) {
return master().linsertafter(key, pivot, value);
}
@Override
public Long llen(String key) {
return master().llen(key);
}
@Override
public String lpop(String key) {
return master().lpop(key);
}
@Override
public Long lpush(String key, String... values) {
return master().lpush(key, values);
}
@Override
public Long lpushx(String key, String value) {
return master().lpushx(key, value);
}
@Override
public List lrange(String key, long start, long stop) {
return master().lrange(key, start, stop);
}
@Override
public Long lrem(String key, long count, String value) {
return master().lrem(key, count, value);
}
@Override
public String lset(String key, long index, String value) {
return master().lset(key, index, value);
}
@Override
public String ltrim(String key, long start, long stop) {
return master().ltrim(key, start, stop);
}
@Override
public String rpop(String key) {
return master().rpop(key);
}
@Override
public String rpoplpush(String source, String destination) {
return master().rpoplpush(source, destination);
}
@Override
public Long rpush(String key, String... values) {
return master().rpush(key, values);
}
@Override
public Long rpushx(String key, String value) {
return master().rpushx(key, value);
}
// ~ ------------------------------------------------------------------------------------------------------- Sets
@Override
public Long sadd(String key, String... members) {
return master().sadd(key, members);
}
@Override
public Long scard(String key) {
return master().scard(key);
}
@Override
public Set sdiff(String... keys) {
return master().sdiff(keys);
}
@Override
public Long sdiffstore(String destination, String... keys) {
return master().sdiffstore(destination, keys);
}
@Override
public Set sinter(String... keys) {
return master().sinter(keys);
}
@Override
public Long sinterstore(String destination, String... keys) {
return master().sinterstore(destination, keys);
}
@Override
public Boolean sismember(String key, String member) {
return master().sismember(key, member);
}
@Override
public Set smembers(String key) {
return master().smembers(key);
}
@Override
public Long smove(String source, String destination, String member) {
return master().smove(source, destination, member);
}
@Override
public String spop(String key) {
return master().spop(key);
}
@Override
public List srandmember(String key, int count) {
return master().srandmember(key, count);
}
@Override
public String srandmember(String key) {
return master().srandmember(key);
}
@Override
public Long srem(String key, String... members) {
return master().srem(key, members);
}
@Override
public Set sunion(String... keys) {
return sunion(keys);
}
@Override
public Long sunionstore(String destination, String... keys) {
return master().sunionstore(destination, keys);
}
// ~ ------------------------------------------------------------------------------------------------- Sorted Sets
@Override
public Long zadd(String key, double score, String member) {
return master().zadd(key, score, member);
}
@Override
public Long zadd(String key, Map scoremembers) {
return master().zadd(key, scoremembers);
}
@Override
public Long zcard(String key) {
return master().zcard(key);
}
@Override
public Long zcount(String key, double min, double max) {
return master().zcount(key, min, max);
}
@Override
public Long zcount(String key, String min, String max) {
return master().zcount(key, min, max);
}
@Override
public Double zincrby(String key, double score, String member) {
return master().zincrby(key, score, member);
}
@Override
public Long zinterstore(String destination, String... keys) {
return master().zinterstore(destination, keys);
}
@Override
public Long zinterstoremax(String destination, String... keys) {
return master().zinterstoremax(destination, keys);
}
@Override
public Long zinterstoremin(String destination, String... keys) {
return master().zinterstoremin(destination, keys);
}
@Override
public Long zinterstore(String destination, Map weightkeys) {
return master().zinterstore(destination, weightkeys);
}
@Override
public Long zinterstoremax(String destination, Map weightkeys) {
return master().zinterstoremax(destination, weightkeys);
}
@Override
public Long zinterstoremin(String destination, Map weightkeys) {
return master().zinterstoremin(destination, weightkeys);
}
@Override
public Set zrange(String key, long start, long stop) {
return master().zrange(key, start, stop);
}
@Override
public Map zrangewithscores(String key, long start, long stop) {
return master().zrangewithscores(key, start, stop);
}
@Override
public Set zrangebyscore(String key, double min, double max) {
return master().zrangebyscore(key, min, max);
}
@Override
public Set zrangebyscore(String key, String min, String max) {
return master().zrangebyscore(key, min, max);
}
@Override
public Set zrangebyscore(String key, double min, double max, int offset, int count) {
return master().zrangebyscore(key, min, max, offset, count);
}
@Override
public Set zrangebyscore(String key, String min, String max, int offset, int count) {
return master().zrangebyscore(key, min, max, offset, count);
}
@Override
public Map zrangebyscorewithscores(String key, double min, double max) {
return master().zrangebyscorewithscores(key, min, max);
}
@Override
public Map zrangebyscorewithscores(String key, String min, String max) {
return master().zrangebyscorewithscores(key, min, max);
}
@Override
public Map zrangebyscorewithscores(String key, double min, double max, int offset, int count) {
return master().zrangebyscorewithscores(key, min, max, offset, count);
}
@Override
public Map zrangebyscorewithscores(String key, String min, String max, int offset, int count) {
return master().zrangebyscorewithscores(key, min, max, offset, count);
}
@Override
public Long zrank(String key, String member) {
return master().zrank(key, member);
}
@Override
public Long zrem(String key, String... members) {
return master().zrem(key, members);
}
@Override
public Long zremrangebyrank(String key, long start, long stop) {
return master().zremrangebyrank(key, start, stop);
}
@Override
public Long zremrangebyscore(String key, double min, double max) {
return master().zremrangebyscore(key, min, max);
}
@Override
public Long zremrangebyscore(String key, String min, String max) {
return master().zremrangebyscore(key, min, max);
}
@Override
public Set zrevrange(String key, long start, long stop) {
return master().zrevrange(key, start, stop);
}
@Override
public Map zrevrangewithscores(String key, long start, long stop) {
return master().zrevrangewithscores(key, start, stop);
}
@Override
public Set zrevrangebyscore(String key, double max, double min) {
return master().zrevrangebyscore(key, max, min);
}
@Override
public Set zrevrangebyscore(String key, String max, String min) {
return master().zrevrangebyscore(key, max, min);
}
@Override
public Set zrevrangebyscore(String key, double max, double min, int offset, int count) {
return master().zrevrangebyscore(key, max, min, offset, count);
}
@Override
public Set zrevrangebyscore(String key, String max, String min, int offset, int count) {
return master().zrevrangebyscore(key, max, min, offset, count);
}
@Override
public Map zrevrangebyscorewithscores(String key, double max, double min) {
return master().zrevrangebyscorewithscores(key, max, min);
}
@Override
public Map zrevrangebyscorewithscores(String key, String max, String min) {
return master().zrevrangebyscorewithscores(key, max, min);
}
@Override
public Map zrevrangebyscorewithscores(String key, double max, double min, int offset, int count) {
return master().zrevrangebyscorewithscores(key, max, min, offset, count);
}
@Override
public Map zrevrangebyscorewithscores(String key, String max, String min, int offset, int count) {
return master().zrevrangebyscorewithscores(key, max, min, offset, count);
}
@Override
public Long zrevrank(String key, String member) {
return master().zrevrank(key, member);
}
@Override
public Double zscore(String key, String member) {
return master().zscore(key, member);
}
@Override
public Long zunionstore(String destination, String... keys) {
return master().zunionstore(destination, keys);
}
@Override
public Long zunionstoremax(String destination, String... keys) {
return master().zunionstoremax(destination, keys);
}
@Override
public Long zunionstoremin(String destination, String... keys) {
return master().zunionstoremin(destination, keys);
}
@Override
public Long zunionstore(String destination, Map weightkeys) {
return master().zunionstore(destination, weightkeys);
}
@Override
public Long zunionstoremax(String destination, Map weightkeys) {
return master().zunionstoremax(destination, weightkeys);
}
@Override
public Long zunionstoremin(String destination, Map weightkeys) {
return master().zunionstoremin(destination, weightkeys);
}
// ~ ----------------------------------------------------------------------------------------------------- Pub/Sub
@Override
public RedisPubSub psubscribe(RedisPsubscribeHandler handler, String... patterns) {
return master().psubscribe(handler, patterns);
}
@Override
public Long publish(String channel, String message) {
return master().publish(channel, message);
}
@Override
public void punsubscribe(RedisPubSub pubsub, String... patterns) {
master().punsubscribe(pubsub, patterns);
}
@Override
public RedisPubSub subscribe(RedisSubscribeHandler handler, String... channels) {
return master().subscribe(handler, channels);
}
@Override
public void unsubscribe(RedisPubSub pubsub, String... channels) {
master().unsubscribe(pubsub, channels);
}
// ~ ------------------------------------------------------------------------------------------------ Transactions
@Override
public String discard(RedisTransaction t) {
return master().discard(t);
}
@Override
public List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy