diff --git a/README.md b/README.md index 297434610e27d720d9a624a6095c08f024d4ac5a..ce086f728a689989d62563386c6fae65d32e327b 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,39 @@ nredis-proxy 1.0.1 版本优化以下功能点 1:修改netty atrribute 并发问题 2:优化tcp参数 -3:优化连接池,使单机器整体性能损耗在23%左右 \ No newline at end of file +3:优化连接池,升级netty最新版本,使单机器整体性能损耗最高在14%左右 + +nredis-proxy 1.0.2 版本优化以下功能点 +1:修复高并发环境IO泄露,造成 open too many files +2:去掉front channel 与back channel 重量级同步锁以及循环链表算法,使用cpu级别volatile,精简逻辑流程 +3:修复 TCP丢包问题 +4:单机单个redis性能大概在QPS:9千左右 + +nredis-proxy 1.0.2.1 版本修改bug +1: multiBulkReply 超过50 数据量大,出现重复数据问题,已经解决,通过两天暴力测试,没有任何问题 +2: 提供linux 启动命令 + +CPU性能: + + ![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/165825_009f1558_54128.jpeg "在这里输入图片标题") + +内存性能: + + +![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/165858_ab9bde06_54128.jpeg "在这里输入图片标题") + +![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/165908_82918698_54128.jpeg "在这里输入图片标题") + +线程以及classloader性能: + + ![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/165929_35b0aae6_54128.jpeg "在这里输入图片标题") + +RedisServer性能监控 + + ![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/172008_783ff6e8_54128.jpeg "在这里输入图片标题") + ![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/172021_141e70f5_54128.jpeg "在这里输入图片标题") + ![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/172032_5085bc9d_54128.jpeg "在这里输入图片标题") + +RedisServer 主从自动切换监控 + + ![输入图片说明](http://git.oschina.net/uploads/images/2017/0115/172154_e402067a_54128.jpeg "在这里输入图片标题") \ No newline at end of file diff --git a/nredis-proxy-sample/.classpath b/nredis-proxy-bootstrap/.classpath similarity index 84% rename from nredis-proxy-sample/.classpath rename to nredis-proxy-bootstrap/.classpath index 484684893ee7ef4e0f16c74dfd0fcdd7540b8e18..bd32b69cc48b1e9916d57a95a733fbe15dae8842 100644 --- a/nredis-proxy-sample/.classpath +++ b/nredis-proxy-bootstrap/.classpath @@ -6,11 +6,6 @@ - - - - - @@ -18,6 +13,7 @@ + diff --git a/nredis-proxy-failover/.gitignore b/nredis-proxy-bootstrap/.gitignore similarity index 100% rename from nredis-proxy-failover/.gitignore rename to nredis-proxy-bootstrap/.gitignore diff --git a/nredis-proxy-sample/.project b/nredis-proxy-bootstrap/.project similarity index 93% rename from nredis-proxy-sample/.project rename to nredis-proxy-bootstrap/.project index 9c4623ac88f97a81a76159da0e1b58f526c1229c..570b2fd7ae62ec4e183213e46e7928fbd20125ad 100644 --- a/nredis-proxy-sample/.project +++ b/nredis-proxy-bootstrap/.project @@ -1,6 +1,6 @@ - nredis-proxy-sample + nredis-proxy-bootstrap diff --git a/nredis-proxy-sample/.settings/org.eclipse.core.resources.prefs b/nredis-proxy-bootstrap/.settings/org.eclipse.core.resources.prefs similarity index 100% rename from nredis-proxy-sample/.settings/org.eclipse.core.resources.prefs rename to nredis-proxy-bootstrap/.settings/org.eclipse.core.resources.prefs diff --git a/nredis-proxy-sample/.settings/org.eclipse.jdt.core.prefs b/nredis-proxy-bootstrap/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from nredis-proxy-sample/.settings/org.eclipse.jdt.core.prefs rename to nredis-proxy-bootstrap/.settings/org.eclipse.jdt.core.prefs diff --git a/nredis-proxy-failover/.settings/org.eclipse.m2e.core.prefs b/nredis-proxy-bootstrap/.settings/org.eclipse.m2e.core.prefs similarity index 100% rename from nredis-proxy-failover/.settings/org.eclipse.m2e.core.prefs rename to nredis-proxy-bootstrap/.settings/org.eclipse.m2e.core.prefs diff --git a/nredis-proxy-sample/pom.xml b/nredis-proxy-bootstrap/pom.xml similarity index 60% rename from nredis-proxy-sample/pom.xml rename to nredis-proxy-bootstrap/pom.xml index 056f18746fb8b99dfec2034e77672dafe4a0017a..30256f27aa8a3a74490b633261098b8c0b450ce6 100644 --- a/nredis-proxy-sample/pom.xml +++ b/nredis-proxy-bootstrap/pom.xml @@ -8,19 +8,14 @@ 1.0 com.opensource - nredis-proxy-sample + nredis-proxy-bootstrap 1.0 - nredis-proxy-sample + nredis-proxy-bootstrap http://maven.apache.org UTF-8 - - - junit - junit - test - + com.opensource nredis-proxy-spring @@ -28,30 +23,12 @@ org.slf4j slf4j-api - 1.5.8 - - - - org.slf4j - slf4j-log4j12 - 1.5.8 - - - - log4j - log4j - 1.2.14 - - - redis.clients - jedis - 2.7.3 - - - org.springframework - spring-context - 4.1.7.RELEASE - + + + org.springframework + spring-context + 4.1.7.RELEASE + diff --git a/nredis-proxy-sample/src/main/assembly/assembly.xml b/nredis-proxy-bootstrap/src/main/assembly/assembly.xml similarity index 90% rename from nredis-proxy-sample/src/main/assembly/assembly.xml rename to nredis-proxy-bootstrap/src/main/assembly/assembly.xml index 599b447a27a54e3b52c70d84e958186bae888ca9..3ca487b749cdb22dc482c3f617200c7bea27801d 100644 --- a/nredis-proxy-sample/src/main/assembly/assembly.xml +++ b/nredis-proxy-bootstrap/src/main/assembly/assembly.xml @@ -19,7 +19,7 @@ - src/main/deploy/single.sh + src/main/deploy/nredis-proxy-server.sh /bin diff --git a/nredis-proxy-sample/src/main/deploy/single.sh b/nredis-proxy-bootstrap/src/main/deploy/nredis-proxy-server.sh similarity index 74% rename from nredis-proxy-sample/src/main/deploy/single.sh rename to nredis-proxy-bootstrap/src/main/deploy/nredis-proxy-server.sh index 7339eee61283461910741f25048e3304f9938f2b..a893ccbaf9d64cebb71b8d650b22ece68ef9d9b7 100644 --- a/nredis-proxy-sample/src/main/deploy/single.sh +++ b/nredis-proxy-bootstrap/src/main/deploy/nredis-proxy-server.sh @@ -4,7 +4,7 @@ BASE_PATH=`cd "$(dirname "$0")"; pwd` #设置java运行参数 -DEFAULT_JAVA_OPTS=" -server -Xmx1g -Xms1g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Dfile.encoding=utf-8 " +DEFAULT_JAVA_OPTS=" -server -Xmx1g -Xms1g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Dfile.encoding=utf-8 -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false" #引入外部参数配置文件: @@ -20,9 +20,9 @@ if [ ! -d $LOG_PATH ]; then mkdir -p $LOG_PATH fi CLASS_PATH=${CLASS_PATH:-$APP_PATH/config:$APP_PATH/lib/*} -CONSOLE_LOG=${LOG_PATH}/timer_console.log +CONSOLE_LOG=${LOG_PATH}/nredis-proxy-server.log JAVA_OPTS=${JAVA_OPTS:-$DEFAULT_JAVA_OPTS} -MAIN_CLASS=${MAIN_CLASS:-"com.opensource.netty.redis.proxy.sample.RedisProxyServerBootStrap"} +MAIN_CLASS=${MAIN_CLASS:-"com.opensource.netty.redis.proxy.bootstrap.NRedisProxyServerBootStrap"} exist(){ @@ -36,23 +36,23 @@ exist(){ start(){ if exist; then - echo "Timer is already running." + echo "nredis-proxy-server is already running." exit 1 else cd $APP_PATH echo "nohup java $JAVA_OPTS -cp $CLASS_PATH $MAIN_CLASS $APP_PATH 2> /dev/null & " nohup java $JAVA_OPTS -cp $CLASS_PATH $MAIN_CLASS $APP_PATH > ${CONSOLE_LOG} 2>&1 & - echo "Timer is started." + echo "nredis-proxy-server is started." fi } stop(){ runningPID=`pgrep -f "$MAIN_CLASS $APP_PATH"` if [ "$runningPID" ]; then - echo "Timer pid: $runningPID" + echo "nredis-proxy-server pid: $runningPID" count=0 kwait=5 - echo "Timer is stopping, please wait..." + echo "nredis-proxy-server is stopping, please wait..." kill -15 $runningPID until [ `ps --pid $runningPID 2> /dev/null | grep -c $runningPID 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ] do @@ -64,7 +64,7 @@ stop(){ kill -9 $runningPID fi clear - echo "Timer is stopped." + echo "nredis-proxy-server is stopped." else echo "Timer has not been started." fi @@ -72,10 +72,10 @@ stop(){ check(){ if exist; then - echo "Timer is alive." + echo "nredis-proxy-server is alive." exit 0 else - echo "Timer is dead." + echo "nredis-proxy-server is dead." exit -1 fi } diff --git a/nredis-proxy-sample/src/main/deploy/package-dependency.properties b/nredis-proxy-bootstrap/src/main/deploy/package-dependency.properties similarity index 100% rename from nredis-proxy-sample/src/main/deploy/package-dependency.properties rename to nredis-proxy-bootstrap/src/main/deploy/package-dependency.properties diff --git a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/RedisProxyServerBootStrap.java b/nredis-proxy-bootstrap/src/main/java/com/opensource/netty/redis/proxy/bootstrap/NRedisProxyServerBootStrap.java similarity index 34% rename from nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/RedisProxyServerBootStrap.java rename to nredis-proxy-bootstrap/src/main/java/com/opensource/netty/redis/proxy/bootstrap/NRedisProxyServerBootStrap.java index b7097fbbff97c32b231454fcf77ad63a15ed4765..0c196ff71474d18061f2e15a68bd067edcc3b1be 100644 --- a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/RedisProxyServerBootStrap.java +++ b/nredis-proxy-bootstrap/src/main/java/com/opensource/netty/redis/proxy/bootstrap/NRedisProxyServerBootStrap.java @@ -1,7 +1,9 @@ /** * */ -package com.opensource.netty.redis.proxy.sample; +package com.opensource.netty.redis.proxy.bootstrap; + + import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -9,9 +11,9 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * @author liubing * */ -public class RedisProxyServerBootStrap { +public class NRedisProxyServerBootStrap { - public static void main(String[] args) { - new ClassPathXmlApplicationContext(new String[] {"classpath*:redisProxy.xml"}); + public static void main(String[] args) throws Exception { + new ClassPathXmlApplicationContext(new String[] {"classpath*:redisProxy.xml"}); } } diff --git a/nredis-proxy-sample/src/main/resources/log4j.properties b/nredis-proxy-bootstrap/src/main/resources/log4j.properties similarity index 91% rename from nredis-proxy-sample/src/main/resources/log4j.properties rename to nredis-proxy-bootstrap/src/main/resources/log4j.properties index 11b6cc183f2e987eedac9610a1bf292c9b0e9433..df2890480ca3644a366e5f9a816f63e0fef848fd 100644 --- a/nredis-proxy-sample/src/main/resources/log4j.properties +++ b/nredis-proxy-bootstrap/src/main/resources/log4j.properties @@ -5,7 +5,7 @@ log4j.appender.hedis.layout=org.apache.log4j.PatternLayout log4j.appender.hedis.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p %c{1} %x - %m%n log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=./log/hedis.log +log4j.appender.file.File=./log/nredis-proxy.log log4j.appender.file.MaxFileSize=5120KB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout diff --git a/nredis-proxy-sample/src/main/resources/redisProxy.xml b/nredis-proxy-bootstrap/src/main/resources/redisProxy.xml similarity index 49% rename from nredis-proxy-sample/src/main/resources/redisProxy.xml rename to nredis-proxy-bootstrap/src/main/resources/redisProxy.xml index 80af97e9709780b784fe67f35a969300bfafb4be..ef23bd2f3911f16f9c958910f3c2e3639fe69277 100644 --- a/nredis-proxy-sample/src/main/resources/redisProxy.xml +++ b/nredis-proxy-bootstrap/src/main/resources/redisProxy.xml @@ -8,15 +8,29 @@ http://www.nredisproxy.com/nredisProxy/NRedis-Proxy.xsd" > - + - + - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nredis-proxy-commons/src/main/java/com/opensource/netty/redis/proxy/commons/constants/RedisConstants.java b/nredis-proxy-commons/src/main/java/com/opensource/netty/redis/proxy/commons/constants/RedisConstants.java index 6740d241a89e8f5ef51761b3d36a067a3958e22e..507cc963faf5a7d04c41082e5a8d4fe39c4a7911 100644 --- a/nredis-proxy-commons/src/main/java/com/opensource/netty/redis/proxy/commons/constants/RedisConstants.java +++ b/nredis-proxy-commons/src/main/java/com/opensource/netty/redis/proxy/commons/constants/RedisConstants.java @@ -45,16 +45,16 @@ public class RedisConstants { public static final String INFO="info"; - public static final String ZOOKEEPER_REGISTRY_NAMESPACE = "/ffanredisproxy"; + public static final String ZOOKEEPER_REGISTRY_NAMESPACE = "/nredis-proxy"; public static final String ZOOKEEPER_REGISTRY_COMMAND = "/command"; - public static final String TOP_PATH="/ffanredisproxy/command/server"; + public static final String TOP_PATH="/nredis-proxy/command/server"; public static final String REGISTRY_HEARTBEAT_SWITCHER = "feature.configserver.heartbeat"; - public static final String ADDRESS="address"; + public static final String ADDRESS="zkAddress"; public static final Integer TIMEOUT=500; /** @@ -107,7 +107,7 @@ public class RedisConstants { */ public static final String WEIGHT_RULE="weight_rule";//权重规则 - public static final String REDIS_PROXY="redis_proxy_"; + public static final String REDIS_PROXY="nredis_proxy_"; public static final char DOLLAR_BYTE = '$'; public static final char ASTERISK_BYTE = '*'; diff --git a/nredis-proxy-core/pom.xml b/nredis-proxy-core/pom.xml index febf1778835de37ff9b00414328dd4548b614113..08063a69ab6f539be8235041f7a6b13f9a478124 100644 --- a/nredis-proxy-core/pom.xml +++ b/nredis-proxy-core/pom.xml @@ -37,5 +37,13 @@ org.slf4j slf4j-api + + com.alibaba + fastjson + + + com.101tec + zkclient + diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/client/impl/AbstractPoolClient.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/client/impl/AbstractPoolClient.java index 7a972f81a1c9fadb150fee7b744a405c1b154d01..7b869abe82aa9589df07dfbbfd86c47705af101e 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/client/impl/AbstractPoolClient.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/client/impl/AbstractPoolClient.java @@ -4,13 +4,13 @@ package com.opensource.netty.redis.proxy.core.client.impl; +import io.netty.channel.ChannelHandlerContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.opensource.netty.redis.proxy.commons.exception.RedisException; import com.opensource.netty.redis.proxy.core.client.Client; import com.opensource.netty.redis.proxy.core.command.impl.RedisCommand; import com.opensource.netty.redis.proxy.core.connection.IConnection; -import com.opensource.netty.redis.proxy.core.connection.IConnectionCallBack; import com.opensource.netty.redis.proxy.core.pool.utils.LBRedisProxyChannelPoolUtils; import com.opensource.netty.redis.proxy.pool.LBRedisProxyPoolEntry; import com.opensource.netty.redis.proxy.pool.LBRedisProxyPooledObjectFactory; @@ -50,7 +50,7 @@ public abstract class AbstractPoolClient implements Client{ */ protected abstract LBRedisProxyPooledObjectFactory createChannelFactory(); - public abstract void write(RedisCommand request,IConnectionCallBack connectionCallBack); + public abstract void write(RedisCommand request,ChannelHandlerContext frontCtx); protected LBRedisProxyPoolEntry borrowObject() throws Exception { LBRedisProxyPoolEntry nettyChannelEntry=pool.borrowEntry(); diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/command/impl/RedisCommand.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/command/impl/RedisCommand.java index bc9c9e502219611e9937772e3454bd24ebc8ca50..ce7dd8fe4d67fb86d53cf326d377a6b08762e2a3 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/command/impl/RedisCommand.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/command/impl/RedisCommand.java @@ -6,7 +6,6 @@ package com.opensource.netty.redis.proxy.core.command.impl; import java.util.List; import io.netty.buffer.ByteBuf; - import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; import com.opensource.netty.redis.proxy.commons.utils.ProtoUtils; import com.opensource.netty.redis.proxy.core.command.IRedisCommand; @@ -19,8 +18,6 @@ public class RedisCommand implements IRedisCommand { private int argCount; private List args; - - private Long requestKey; /* * (non-Javadoc) * @@ -77,20 +74,4 @@ public class RedisCommand implements IRedisCommand { byteBuf.writeByte(RedisConstants.CR_BYTE); byteBuf.writeByte(RedisConstants.LF_BYTE); } - - /** - * @return the requestKey - */ - public Long getRequestKey() { - return requestKey; - } - - /** - * @param requestKey the requestKey to set - */ - public void setRequestKey(Long requestKey) { - this.requestKey = requestKey; - } - - } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/LBRedisServerMasterCluster.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/LBRedisServerMasterCluster.java index 8ab0b249501fc46fb35f0972cf029601511b075e..90722221d89cb050a9f6081b81c8724f1148c2bb 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/LBRedisServerMasterCluster.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/LBRedisServerMasterCluster.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - import com.opensource.netty.redis.proxy.core.client.impl.AbstractPoolClient; import com.opensource.netty.redis.proxy.core.cluster.LoadBalance; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; @@ -28,12 +27,12 @@ public class LBRedisServerMasterCluster implements Serializable { */ private static final long serialVersionUID = 8481358070088941073L; - private List redisServerClusterBeans; + private List redisServerClusterBeans=new ArrayList(); - private Map ffanRedisServerClusterBeanMap=new HashMap(); + private Map redisServerClusterBeanMap=new HashMap();// key 相当于 zk 路径,而不是值 /** 一主多从模式 */ - private Map> masterClusters = new HashMap>(); + private Map> masterClusters = new HashMap>();// key 相当于 zk 路径,而不是值 /** 主集合 ***/ private List masters = new ArrayList(); @@ -44,7 +43,7 @@ public class LBRedisServerMasterCluster implements Serializable { private LoadBalance loadMasterBalance;//主的一致性算法 - private Map ffanRedisClientBeanMap =new HashMap(); + private Map redisClientBeanMap =new HashMap();//key 代表实际的值 public LBRedisServerMasterCluster( List redisServerClusterBeans) { @@ -59,9 +58,9 @@ public class LBRedisServerMasterCluster implements Serializable { if(redisServerClusterBeans!=null&&redisServerClusterBeans.size()>0){ for(LBRedisServerClusterBean ffanRedisServerClusterBean:redisServerClusterBeans){ - masters.add(ffanRedisServerClusterBean.getFfanMasterRedisServerBean()); - ffanRedisServerClusterBeanMap.put(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey(), ffanRedisServerClusterBean); - masterClusters.put(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey(), ffanRedisServerClusterBean.getFfanRedisServerClusterBeans()); + masters.add(ffanRedisServerClusterBean.getRedisServerMasterBean()); + redisServerClusterBeanMap.put(ffanRedisServerClusterBean.getRedisServerMasterBean().getKey(), ffanRedisServerClusterBean); + masterClusters.put(ffanRedisServerClusterBean.getRedisServerMasterBean().getKey(), ffanRedisServerClusterBean.getRedisServerSlaveBeans()); } } @@ -79,86 +78,22 @@ public class LBRedisServerMasterCluster implements Serializable { return null; } - /** - * 更新指定主的从 - * 更改权重 - * @param key - * @param ffanRedisClusterBeans - */ - public void updateFfanRedisClusterBeans(String key,List ffanRedisClusterBeans){ - - List ffanRedisServerBeans=masterClusters.get(key); - masterClusters.put(key, ffanRedisClusterBeans); - if(ffanRedisServerBeans!=null){ - for(LBRedisServerBean ffanRedisServerBean:ffanRedisServerBeans){//删除挂掉的从 - if(!ffanRedisClusterBeans.contains(ffanRedisServerBean)){ - if(ffanRedisClientBeanMap.containsKey(ffanRedisServerBean.getKey())){ - ffanRedisClientBeanMap.remove(ffanRedisServerBean.getKey()); - } - } - } - - - if(ffanRedisServerClusterBeanMap.containsKey(key)){ - LBRedisServerClusterBean ffanRedisServerClusterBean=ffanRedisServerClusterBeanMap.get(key); - if(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey().equals(key)){ - ffanRedisServerClusterBean.setFfanRedisServerClusterBeans(ffanRedisClusterBeans); - } - } - - for(LBRedisServerClusterBean ffanRedisServerClusterBean:redisServerClusterBeans){ - if(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey().equals(key)){ - ffanRedisServerClusterBean.setFfanRedisServerClusterBeans(ffanRedisClusterBeans); - break; - } - } - } - - - - - - } - /** - * 更改指定的主 - * @param key - * @param ffanRedisMasterBean - */ - public void updateFfanRedisMasterBean(String key,LBRedisServerBean ffanRedisMasterBean){ - - for(LBRedisServerBean ffanRedisServerBean :masters){ - if(ffanRedisServerBean.getKey().equals(key)){ - ffanRedisServerBean=ffanRedisMasterBean; - break; - } - } - - for(LBRedisServerClusterBean ffanRedisServerClusterBean:redisServerClusterBeans){ - if(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey().equals(key)){ - ffanRedisServerClusterBean.setFfanMasterRedisServerBean(ffanRedisMasterBean); - break; - } - } - - if(ffanRedisServerClusterBeanMap.containsKey(key)){ - LBRedisServerClusterBean ffanRedisServerClusterBean=ffanRedisServerClusterBeanMap.get(key); - if(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey().equals(key)){ - ffanRedisServerClusterBean.setFfanMasterRedisServerBean(ffanRedisMasterBean); - } - } - if(ffanRedisClientBeanMap.containsKey(key)){ - AbstractPoolClient abstractPoolClient=ffanRedisClientBeanMap.get(ffanRedisMasterBean.getKey()); - ffanRedisClientBeanMap.put(key, abstractPoolClient); - } - } /** * @return the masters */ public List getMasters() { return masters; } + + + /** + * @param masters the masters to set + */ + public void setMasters(List masters) { + this.masters = masters; + } /** * @return the redisServerClusterBeans @@ -167,67 +102,38 @@ public class LBRedisServerMasterCluster implements Serializable { return redisServerClusterBeans; } - /** - * 主挂了,移除元素 - * @param key - */ - public void remove(String key){ - if(ffanRedisServerClusterBeanMap.containsKey(key)){ - ffanRedisServerClusterBeanMap.remove(key); - } - if(masterClusters.containsKey(key)){ - masterClusters.remove(key); - } - - if(ffanRedisClientBeanMap.containsKey(key)){ - ffanRedisClientBeanMap.remove(key); - } - - for(LBRedisServerBean ffanRedisServerBean :masters){ - if(ffanRedisServerBean.getKey().equals(key)){ - masters.remove(ffanRedisServerBean); - break; - } - } - - for(LBRedisServerClusterBean ffanRedisServerClusterBean:redisServerClusterBeans){ - if(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey().equals(key)){ - redisServerClusterBeans.remove(ffanRedisServerClusterBean); - break; - } - } - } - /** * 有主,无从 * @param key */ public void removeSlavesByMaster(String key){ - if(ffanRedisServerClusterBeanMap.containsKey(key)){ - ffanRedisServerClusterBeanMap.remove(key); - } - List ffanRedisServerBeans=masterClusters.get(key); - - for(LBRedisServerBean ffanRedisServerBean:ffanRedisServerBeans){//删除主对应的从连接客户端 - if(ffanRedisClientBeanMap.containsKey(ffanRedisServerBean.getKey())){ - ffanRedisClientBeanMap.remove(ffanRedisServerBean.getKey()); + if(redisServerClusterBeanMap.containsKey(key)){ + redisServerClusterBeanMap.remove(key); + } + List redisServerSlaveBeans=masterClusters.get(key); + for(LBRedisServerBean serverBean:redisServerSlaveBeans){//删除不存在从的连接 + if(redisClientBeanMap.containsKey(serverBean.getKey())){ + redisClientBeanMap.get(serverBean.getKey()).close(); + redisClientBeanMap.remove(serverBean.getKey()); } } + + if(masterClusters.containsKey(key)){//删除对应的主从 masterClusters.remove(key); } - for(LBRedisServerClusterBean ffanRedisServerClusterBean:redisServerClusterBeans){//主存在,从不存在,删除对应的从 - if(ffanRedisServerClusterBean.getFfanMasterRedisServerBean().getKey().equals(key)){ - ffanRedisServerClusterBean.getFfanRedisServerClusterBeans().clear(); + for(LBRedisServerClusterBean redisServerClusterBean:redisServerClusterBeans){//主存在,从不存在,删除对应的从 + if(redisServerClusterBean.getRedisServerMasterBean().getKey().equals(key)){ + redisServerClusterBean.getRedisServerSlaveBeans().clear(); break; } } } - public LBRedisServerClusterBean getFfanRedisServerClusterBean(String key){ - if(ffanRedisServerClusterBeanMap.containsKey(key)){ - return ffanRedisServerClusterBeanMap.get(key); + public LBRedisServerClusterBean getRedisServerClusterBean(String key){ + if(redisServerClusterBeanMap.containsKey(key)){ + return redisServerClusterBeanMap.get(key); } return null; } @@ -286,30 +192,37 @@ public class LBRedisServerMasterCluster implements Serializable { /** * @return the ffanRedisServerClusterBeanMap */ - public Map getFfanRedisServerClusterBeanMap() { - return ffanRedisServerClusterBeanMap; + public Map getRedisServerClusterBeanMap() { + return redisServerClusterBeanMap; } /** * @param ffanRedisServerClusterBeanMap the ffanRedisServerClusterBeanMap to set */ - public void setFfanRedisServerClusterBeanMap( - Map ffanRedisServerClusterBeanMap) { - this.ffanRedisServerClusterBeanMap = ffanRedisServerClusterBeanMap; + public void setRedisServerClusterBeanMap( + Map redisServerClusterBeanMap) { + this.redisServerClusterBeanMap = redisServerClusterBeanMap; } /** * @return the ffanRedisClientBeanMap */ - public Map getFfanRedisClientBeanMap() { - return ffanRedisClientBeanMap; + public Map getRedisClientBeanMap() { + return redisClientBeanMap; } /** * @param ffanRedisClientBeanMap the ffanRedisClientBeanMap to set */ - public void setFfanRedisClientBeanMap(Map ffanRedisClientBeanMap) { - this.ffanRedisClientBeanMap = ffanRedisClientBeanMap; + public void setRedisClientBeanMap(Map redisClientBeanMap) { + this.redisClientBeanMap = redisClientBeanMap; + } + + /** + * @return the masterClusters + */ + public Map> getMasterClusters() { + return masterClusters; } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/RedisPoolConfig.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/RedisPoolConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..11284589e9bf119a91215e59edb616f47d90d895 --- /dev/null +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/RedisPoolConfig.java @@ -0,0 +1,229 @@ +/** + * + */ +package com.opensource.netty.redis.proxy.core.config; + +import java.io.Serializable; + +/** + * @author liubing + * 连接池配置 + */ +public class RedisPoolConfig implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -7393266010156039L; + + private int timeout;//超时时间 + + private int maxActiveConnection;//最大活动连接数 + + private int maxIdleConnection;//最大 空闲连接数 + + private int minConnection;//最小连接数 + + private int initialConnection = 0;//初始化值 + + private long maxWaitMillisOnBorrow; // 最大等待时间 + + private long timeBetweenEvictionRunsMillis;//回收间隔时间点 + + private long minEvictableIdleTimeMillis;//池中最小生存的时间 + + private int minIdleEntries = 0;//最小等待时间 + + private boolean testOnBorrow=false;//取出验证 + + private int connectionTimeout;//连接超时 + + private boolean testOnReturn=false;//回收时验证 + + private boolean testWhileIdle=false;//回收空闲验证 + + /** + * @return the timeout + */ + public int getTimeout() { + return timeout; + } + + /** + * @param timeout the timeout to set + */ + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + /** + * @return the maxActiveConnection + */ + public int getMaxActiveConnection() { + return maxActiveConnection; + } + + /** + * @param maxActiveConnection the maxActiveConnection to set + */ + public void setMaxActiveConnection(int maxActiveConnection) { + this.maxActiveConnection = maxActiveConnection; + } + + /** + * @return the maxIdleConnection + */ + public int getMaxIdleConnection() { + return maxIdleConnection; + } + + /** + * @param maxIdleConnection the maxIdleConnection to set + */ + public void setMaxIdleConnection(int maxIdleConnection) { + this.maxIdleConnection = maxIdleConnection; + } + + /** + * @return the minConnection + */ + public int getMinConnection() { + return minConnection; + } + + /** + * @param minConnection the minConnection to set + */ + public void setMinConnection(int minConnection) { + this.minConnection = minConnection; + } + + /** + * @return the initialConnection + */ + public int getInitialConnection() { + return initialConnection; + } + + /** + * @param initialConnection the initialConnection to set + */ + public void setInitialConnection(int initialConnection) { + this.initialConnection = initialConnection; + } + + /** + * @return the maxWaitMillisOnBorrow + */ + public long getMaxWaitMillisOnBorrow() { + return maxWaitMillisOnBorrow; + } + + /** + * @param maxWaitMillisOnBorrow the maxWaitMillisOnBorrow to set + */ + public void setMaxWaitMillisOnBorrow(long maxWaitMillisOnBorrow) { + this.maxWaitMillisOnBorrow = maxWaitMillisOnBorrow; + } + + /** + * @return the timeBetweenEvictionRunsMillis + */ + public long getTimeBetweenEvictionRunsMillis() { + return timeBetweenEvictionRunsMillis; + } + + /** + * @param timeBetweenEvictionRunsMillis the timeBetweenEvictionRunsMillis to set + */ + public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { + this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; + } + + /** + * @return the minEvictableIdleTimeMillis + */ + public long getMinEvictableIdleTimeMillis() { + return minEvictableIdleTimeMillis; + } + + /** + * @param minEvictableIdleTimeMillis the minEvictableIdleTimeMillis to set + */ + public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { + this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; + } + + /** + * @return the minIdleEntries + */ + public int getMinIdleEntries() { + return minIdleEntries; + } + + /** + * @param minIdleEntries the minIdleEntries to set + */ + public void setMinIdleEntries(int minIdleEntries) { + this.minIdleEntries = minIdleEntries; + } + + /** + * @return the testOnBorrow + */ + public boolean isTestOnBorrow() { + return testOnBorrow; + } + + /** + * @param testOnBorrow the testOnBorrow to set + */ + public void setTestOnBorrow(boolean testOnBorrow) { + this.testOnBorrow = testOnBorrow; + } + + /** + * @return the connectionTimeout + */ + public int getConnectionTimeout() { + return connectionTimeout; + } + + /** + * @param connectionTimeout the connectionTimeout to set + */ + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + + /** + * @return the testOnReturn + */ + public boolean isTestOnReturn() { + return testOnReturn; + } + + /** + * @param testOnReturn the testOnReturn to set + */ + public void setTestOnReturn(boolean testOnReturn) { + this.testOnReturn = testOnReturn; + } + + /** + * @return the testWhileIdle + */ + public boolean isTestWhileIdle() { + return testWhileIdle; + } + + /** + * @param testWhileIdle the testWhileIdle to set + */ + public void setTestWhileIdle(boolean testWhileIdle) { + this.testWhileIdle = testWhileIdle; + } + + + +} diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerBean.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerBean.java index ad65e284072d5e3d96b459102900a2334a62dde1..1ba785c7475d938c88f7fa04d4b6da4fdfbb1857 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerBean.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerBean.java @@ -5,6 +5,7 @@ package com.opensource.netty.redis.proxy.core.config.support; import com.opensource.netty.redis.proxy.commons.algorithm.impl.support.RedisWeight; import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; +import com.opensource.netty.redis.proxy.core.config.RedisPoolConfig; /** * @author liubing @@ -16,80 +17,12 @@ public class LBRedisServerBean implements RedisWeight{ private int port;//端口号 - private int timeout;//超时时间 - - private int maxActiveConnection;//最大活动连接数 - - private int maxIdleConnection;//最大 空闲连接数 - - private int minConnection;//最小连接数 - - private int initialConnection = 0;//初始化值 - - private long maxWaitMillisOnBorrow; // 最大等待时间 - - private long timeBetweenEvictionRunsMillis;//回收间隔时间点 - - private long minEvictableIdleTimeMillis;//池中最小生存的时间 - - private int minIdleEntries = 0;//最小等待时间 - - private boolean testOnBorrow=false;//取出验证 - - private int connectionTimeout;//连接超时 - - private boolean testOnReturn=false;//回收时验证 - - private boolean testWhileIdle=false;//回收空闲验证 + private RedisPoolConfig redisPoolConfig; private int weight=1;//默认权重比例为1 - /** - * @param host - * @param port - * @param timeout - * @param maxActiveConnection - * @param maxIdleConnection - * @param minConnection - * @param initialConnection - * @param maxWaitMillisOnBorrow - * @param timeBetweenEvictionRunsMillis - * @param minEvictableIdleTimeMillis - * @param minIdleEntries - * @param testOnBorrow - * @param connectionTimeout - * @param testOnReturn - * @param testWhileIdle - * @param weight - */ - public LBRedisServerBean(String host, int port, int timeout, - int maxActiveConnection, int maxIdleConnection, int minConnection, - int initialConnection, long maxWaitMillisOnBorrow, - long timeBetweenEvictionRunsMillis, - long minEvictableIdleTimeMillis, int minIdleEntries, - boolean testOnBorrow, int connectionTimeout, boolean testOnReturn, - boolean testWhileIdle, int weight) { - super(); - this.host = host; - this.port = port; - this.timeout = timeout; - this.maxActiveConnection = maxActiveConnection; - this.maxIdleConnection = maxIdleConnection; - this.minConnection = minConnection; - this.initialConnection = initialConnection; - this.maxWaitMillisOnBorrow = maxWaitMillisOnBorrow; - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; - this.minIdleEntries = minIdleEntries; - this.testOnBorrow = testOnBorrow; - this.connectionTimeout = connectionTimeout; - this.testOnReturn = testOnReturn; - this.testWhileIdle = testWhileIdle; - this.weight = weight; - } - /** * */ @@ -125,62 +58,6 @@ public class LBRedisServerBean implements RedisWeight{ this.port = port; } - /** - * @return the timeout - */ - public int getTimeout() { - return timeout; - } - - /** - * @param timeout the timeout to set - */ - public void setTimeout(int timeout) { - this.timeout = timeout; - } - - /** - * @return the maxActiveConnection - */ - public int getMaxActiveConnection() { - return maxActiveConnection; - } - - /** - * @param maxActiveConnection the maxActiveConnection to set - */ - public void setMaxActiveConnection(int maxActiveConnection) { - this.maxActiveConnection = maxActiveConnection; - } - - /** - * @return the maxIdleConnection - */ - public int getMaxIdleConnection() { - return maxIdleConnection; - } - - /** - * @param maxIdleConnection the maxIdleConnection to set - */ - public void setMaxIdleConnection(int maxIdleConnection) { - this.maxIdleConnection = maxIdleConnection; - } - - /** - * @return the minConnection - */ - public int getMinConnection() { - return minConnection; - } - - /** - * @param minConnection the minConnection to set - */ - public void setMinConnection(int minConnection) { - this.minConnection = minConnection; - } - /** * 关键key * @return @@ -206,180 +83,21 @@ public class LBRedisServerBean implements RedisWeight{ this.weight = weight; } - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((host == null) ? 0 : host.hashCode()); - result = prime * result + maxActiveConnection; - result = prime * result + maxIdleConnection; - result = prime * result + minConnection; - result = prime * result + port; - result = prime * result + timeout; - result = prime * result + weight; - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - LBRedisServerBean other = (LBRedisServerBean) obj; - if (host == null) { - if (other.host != null) - return false; - } else if (!host.equals(other.host)) - return false; - if (maxActiveConnection != other.maxActiveConnection) - return false; - if (maxIdleConnection != other.maxIdleConnection) - return false; - if (minConnection != other.minConnection) - return false; - if (port != other.port) - return false; - if (timeout != other.timeout) - return false; - if (weight != other.weight) - return false; - return true; - } - - /** - * @return the initialConnection - */ - public int getInitialConnection() { - return initialConnection; - } - - /** - * @param initialConnection the initialConnection to set - */ - public void setInitialConnection(int initialConnection) { - this.initialConnection = initialConnection; - } - - /** - * @return the maxWaitMillisOnBorrow - */ - public long getMaxWaitMillisOnBorrow() { - return maxWaitMillisOnBorrow; - } - - /** - * @param maxWaitMillisOnBorrow the maxWaitMillisOnBorrow to set - */ - public void setMaxWaitMillisOnBorrow(long maxWaitMillisOnBorrow) { - this.maxWaitMillisOnBorrow = maxWaitMillisOnBorrow; - } - - /** - * @return the timeBetweenEvictionRunsMillis - */ - public long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; - } - - /** - * @param timeBetweenEvictionRunsMillis the timeBetweenEvictionRunsMillis to set - */ - public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - } - - /** - * @return the minEvictableIdleTimeMillis - */ - public long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; - } - - /** - * @param minEvictableIdleTimeMillis the minEvictableIdleTimeMillis to set - */ - public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; - } - - /** - * @return the minIdleEntries - */ - public int getMinIdleEntries() { - return minIdleEntries; - } - - /** - * @param minIdleEntries the minIdleEntries to set - */ - public void setMinIdleEntries(int minIdleEntries) { - this.minIdleEntries = minIdleEntries; - } - - /** - * @return the testOnBorrow - */ - public boolean isTestOnBorrow() { - return testOnBorrow; - } - - /** - * @param testOnBorrow the testOnBorrow to set - */ - public void setTestOnBorrow(boolean testOnBorrow) { - this.testOnBorrow = testOnBorrow; - } - /** - * @return the connectionTimeout + * @return the redisPoolConfig */ - public int getConnectionTimeout() { - return connectionTimeout; + public RedisPoolConfig getRedisPoolConfig() { + return redisPoolConfig; } /** - * @param connectionTimeout the connectionTimeout to set + * @param redisPoolConfig the redisPoolConfig to set */ - public void setConnectionTimeout(int connectionTimeout) { - this.connectionTimeout = connectionTimeout; + public void setRedisPoolConfig(RedisPoolConfig redisPoolConfig) { + this.redisPoolConfig = redisPoolConfig; } - /** - * @return the testOnReturn - */ - public boolean isTestOnReturn() { - return testOnReturn; - } - - /** - * @param testOnReturn the testOnReturn to set - */ - public void setTestOnReturn(boolean testOnReturn) { - this.testOnReturn = testOnReturn; - } - - /** - * @return the testWhileIdle - */ - public boolean isTestWhileIdle() { - return testWhileIdle; - } - - /** - * @param testWhileIdle the testWhileIdle to set - */ - public void setTestWhileIdle(boolean testWhileIdle) { - this.testWhileIdle = testWhileIdle; - } + } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerClusterBean.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerClusterBean.java index 39f8f2158e50473d3ba80e05501e3ab8e5dfd45f..bc9849adb5dbf167be94dcf040c8c9d6ebf8b2cc 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerClusterBean.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/config/support/LBRedisServerClusterBean.java @@ -14,9 +14,9 @@ import com.opensource.netty.redis.proxy.core.cluster.LoadBalance; */ public class LBRedisServerClusterBean { - private LBRedisServerBean ffanMasterRedisServerBean;//主 + private LBRedisServerBean redisServerMasterBean;//主 - private List ffanRedisServerClusterBeans;//从 + private List redisServerSlaveBeans;//从 private LoadBalance loadClusterBalance;//从权重 /** @@ -27,47 +27,47 @@ public class LBRedisServerClusterBean { } /** - * @return the ffanMasterRedisServerBean + * @return the loadClusterBalance */ - public LBRedisServerBean getFfanMasterRedisServerBean() { - return ffanMasterRedisServerBean; + public LoadBalance getLoadClusterBalance() { + return loadClusterBalance; } /** - * @param ffanMasterRedisServerBean the ffanMasterRedisServerBean to set + * @param loadClusterBalance the loadClusterBalance to set */ - public void setFfanMasterRedisServerBean( - LBRedisServerBean ffanMasterRedisServerBean) { - this.ffanMasterRedisServerBean = ffanMasterRedisServerBean; + public void setLoadClusterBalance(LoadBalance loadClusterBalance) { + this.loadClusterBalance = loadClusterBalance; } /** - * @return the ffanRedisServerClusterBeans + * @return the redisRedisServerMasterBean */ - public List getFfanRedisServerClusterBeans() { - return ffanRedisServerClusterBeans; + public LBRedisServerBean getRedisServerMasterBean() { + return redisServerMasterBean; } /** - * @param ffanRedisServerClusterBeans the ffanRedisServerClusterBeans to set + * @param redisRedisServerMasterBean the redisRedisServerMasterBean to set */ - public void setFfanRedisServerClusterBeans( - List ffanRedisServerClusterBeans) { - this.ffanRedisServerClusterBeans = ffanRedisServerClusterBeans; + public void setRedisServerMasterBean( + LBRedisServerBean redisServerMasterBean) { + this.redisServerMasterBean = redisServerMasterBean; } /** - * @return the loadClusterBalance + * @return the redisServerSlaveBeans */ - public LoadBalance getLoadClusterBalance() { - return loadClusterBalance; + public List getRedisServerSlaveBeans() { + return redisServerSlaveBeans; } /** - * @param loadClusterBalance the loadClusterBalance to set + * @param redisServerSlaveBeans the redisServerSlaveBeans to set */ - public void setLoadClusterBalance(LoadBalance loadClusterBalance) { - this.loadClusterBalance = loadClusterBalance; + public void setRedisServerSlaveBeans( + List redisServerSlaveBeans) { + this.redisServerSlaveBeans = redisServerSlaveBeans; } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/connection/IConnection.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/connection/IConnection.java index 6a040dbfbab9a4744986499182e2e36667233b7e..177a1785ae1ff83815106a14d52f218be2901203 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/connection/IConnection.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/connection/IConnection.java @@ -3,6 +3,7 @@ */ package com.opensource.netty.redis.proxy.core.connection; +import io.netty.channel.ChannelHandlerContext; import com.opensource.netty.redis.proxy.core.command.impl.RedisCommand; import com.opensource.netty.redis.proxy.pool.commons.Pool; @@ -12,7 +13,7 @@ import com.opensource.netty.redis.proxy.pool.commons.Pool; */ public interface IConnection extends Pool{ - public void write(RedisCommand request,IConnectionCallBack connectionCallBack); + public void write(RedisCommand request,ChannelHandlerContext frontCtx); /** * open the channel diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/listen/IRegistryListen.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/listen/IRegistryListen.java index 673cd93e107d410aa5c8831cb0e44d0cef706c4d..bd2bab4a7f8221bbc760c40d97457408150f9e19 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/listen/IRegistryListen.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/listen/IRegistryListen.java @@ -4,6 +4,7 @@ package com.opensource.netty.redis.proxy.core.listen; import java.util.List; +import java.util.Map; /** * @author liubing @@ -13,22 +14,26 @@ public interface IRegistryListen { /** * 监听处理数据变化 - * @param dataPath - * @param data + * @param dataPath 路径 + * @param data 值 */ public void handleDataChange(String dataPath, Object data); /** - * 处理子节点变化 - * 更新从权重的变化 - * @param parentPath - * @param currentChilds + * 处理主节点对应从变化 + * @param parentPath 主节点路径 + * @param data 主节点值 + * @param childPath 从节点路径集合 + * @param ChildDatas 从节点值 */ - public void handleChildChange(String data,String path, List ChildDatas); + public void handleChildChange(String parentPath,String data,List childPath, Map slaveMap); + /** - * 数据节点删除 - * @param dataPath + * 监听从节点处理数据变化 + * @param dataPath 路径 + * @param data 值 */ - public void handleDataDeleted(String dataPath); + public void handleSlaveDataChange(String dataPath, Object data); + } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisReplyDecoder.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisReplyDecoder.java index 064f4dfc5b5a09feeb17b7fb459bd5cae32f6544..4ec3e4996fe056b8a715d0a0a3e85276a08d0b41 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisReplyDecoder.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisReplyDecoder.java @@ -15,7 +15,6 @@ import com.opensource.netty.redis.proxy.core.reply.impl.ErrorRedisReply; import com.opensource.netty.redis.proxy.core.reply.impl.IntegerRedisReply; import com.opensource.netty.redis.proxy.core.reply.impl.MultyBulkRedisReply; import com.opensource.netty.redis.proxy.core.reply.impl.StatusRedisReply; - import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; @@ -66,6 +65,7 @@ public class RedisReplyDecoder extends ReplayingDecoder{ } else if (type == Type.MULTYBULK) { readArrayReply(in, (MultyBulkRedisReply) this.redisReply); } + out.add(this.redisReply); this.redisReply = null; checkpoint(ReplyState.READ_INIT); @@ -75,8 +75,13 @@ public class RedisReplyDecoder extends ReplayingDecoder{ } } + + private void readArrayReply(ByteBuf buffer, MultyBulkRedisReply multyBulkRedisReply) throws UnsupportedEncodingException { int count = readInt(buffer); + if(multyBulkRedisReply!=null&&multyBulkRedisReply.getCount()==count){//已经有值,防止多次,netty在读取85次会多次解析 + multyBulkRedisReply.getList().clear(); + } multyBulkRedisReply.setCount(count); for (int i = 0; i < count; i++) { char type = (char) buffer.readByte(); @@ -90,6 +95,7 @@ public class RedisReplyDecoder extends ReplayingDecoder{ multyBulkRedisReply.addReply(bulkReply); } } + } private void readBulkReply(ByteBuf buffer, BulkRedisReply bulkReply) { @@ -101,8 +107,11 @@ public class RedisReplyDecoder extends ReplayingDecoder{ buffer.skipBytes(2); } else { byte[] value = new byte[length]; + buffer.readBytes(value); + bulkReply.setValue(value); + buffer.skipBytes(2); } } @@ -116,9 +125,9 @@ public class RedisReplyDecoder extends ReplayingDecoder{ char ch = (char) byteBuf.readByte(); while (ch != RedisConstants.CR_BYTE) { sb.append(ch); - ch = (char) byteBuf.readByte(); + ch = (char) byteBuf.readByte();//\r } - byteBuf.skipBytes(1); + byteBuf.readByte();// \n return sb.toString(); } } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisRequestDecoder.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisRequestDecoder.java index 90ce19924ef81ece308d5bfb51ad4cd5d8ccb3e4..42792b5e1de7bfa8ebdc5803df83705d29d929ea 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisRequestDecoder.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/protocol/RedisRequestDecoder.java @@ -73,6 +73,7 @@ public class RedisRequestDecoder extends ReplayingDecoder { byte[] argByte = new byte[length]; buffer.readBytes(argByte); buffer.skipBytes(2);//skip \r\n + //LoggerUtils.info("String:"+new String(argByte)); args.add(argByte); }else{ throw new Exception("READ_ARG Unexpected character,ch:"+String.valueOf(ch)); diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/RegistryService.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/RegistryService.java index 66a8a14d3e2d6fcd47ad5d7bd9ddd269c18a9413..f7074ec69e91d93f6981726b7b61281382b25c3d 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/RegistryService.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/RegistryService.java @@ -20,6 +20,13 @@ public interface RegistryService { */ void register(RedisProxyURL redisProxyURL,IRegistryListen registryListen); + + /** + * 注册监听值的变化 slave节点 + * @param redisProxyURL + */ + void registerSlave(RedisProxyURL redisProxyURL,IRegistryListen registryListen); + /** * 不注册监听值的变化 master 节点 * @@ -27,6 +34,14 @@ public interface RegistryService { */ void unregister(RedisProxyURL redisProxyURL); + + /** + * 不注册监听值的变化 slave 节点 + * + * @param redisProxyURL + */ + void unregisterSlave(RedisProxyURL redisProxyURL); + /** * 创建一个节点 cluster节点 * @param redisProxyURL @@ -57,4 +72,11 @@ public interface RegistryService { * @return */ public String readData(String path,boolean flag); + + /** + * 查看路径是否存在 + * @param nodepath + * @return + */ + public Boolean exist(String nodepath); } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/impl/AbstractRegistry.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/impl/AbstractRegistry.java index 5a4c8e3bec7ab5add625bf76e9a8e6213b0946ed..af05e20985078ee047776cd936882c152e2098e8 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/impl/AbstractRegistry.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/registry/impl/AbstractRegistry.java @@ -18,6 +18,9 @@ import com.opensource.netty.redis.proxy.core.url.RedisProxyURL; public abstract class AbstractRegistry implements Registry { private Set registeredServiceUrls = new ConcurrentHashSet(); + + private Set registeredSlaveServiceUrls = new ConcurrentHashSet(); + protected String registryClassName = this.getClass().getSimpleName(); private RedisProxyURL redisProxyURL; @@ -38,11 +41,21 @@ public abstract class AbstractRegistry implements Registry { LoggerUtils.warn("[{}] register with malformed param, url is null", registryClassName); return; } - LoggerUtils.info("[{}] Url ({}) will register to Registry [{}]", registryClassName, url, redisProxyURL.getServerKey()); doRegister(url,registryListen); registeredServiceUrls.add(url); } - + + @Override + public void registerSlave(RedisProxyURL redisProxyURL, + IRegistryListen registryListen) { + if (redisProxyURL == null) { + LoggerUtils.warn("[{}] register with malformed param, url is null", registryClassName); + return; + } + doRegisterSlave(redisProxyURL,registryListen); + registeredSlaveServiceUrls.add(redisProxyURL); + } + /* * (non-Javadoc) * @@ -56,11 +69,19 @@ public abstract class AbstractRegistry implements Registry { LoggerUtils.warn("[{}] unregister with malformed param, url is null", registryClassName); return; } - LoggerUtils.info("[{}] Url ({}) will unregister to Registry [{}]", registryClassName, url, redisProxyURL.getServerKey()); - doUnregister(url); - registeredServiceUrls.remove(url); + doUnregisterSlave(url); + registeredSlaveServiceUrls.remove(url); + } + + @Override + public void unregisterSlave(RedisProxyURL redisProxyURL) { + if (redisProxyURL == null) { + LoggerUtils.warn("[{}] unregister with malformed param, redisProxyURL is null", registryClassName); + return; + } + doUnregister(redisProxyURL); + registeredServiceUrls.remove(redisProxyURL); } - /* * (non-Javadoc) * @@ -106,8 +127,13 @@ public abstract class AbstractRegistry implements Registry { } protected abstract void doRegister(RedisProxyURL url,IRegistryListen registryListen); + + protected abstract void doRegisterSlave(RedisProxyURL url,IRegistryListen registryListen); protected abstract void doUnregister(RedisProxyURL url); + + protected abstract void doUnregisterSlave(RedisProxyURL url); + protected abstract boolean doDelete(RedisProxyURL redisProxyURL); diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/AbstractRedisReply.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/AbstractRedisReply.java index 500c7a509b7c9d6e2436e9e0b98dc4253c3d7e1f..2c081f799ef924498e496d717757c0e5f392b53f 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/AbstractRedisReply.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/AbstractRedisReply.java @@ -74,7 +74,6 @@ public abstract class AbstractRedisReply implements IRedisReply { */ @Override public void encode(ByteBuf out) { - // TODO Auto-generated method stub writeStart(out); doEncode(out); } diff --git a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/MultyBulkRedisReply.java b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/MultyBulkRedisReply.java index 811a1bcbfcfb5109e63a4e1beb1dc5f290a50d28..9d9757a72c8188828e7973076a387de5c7c70a11 100644 --- a/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/MultyBulkRedisReply.java +++ b/nredis-proxy-core/src/main/java/com/opensource/netty/redis/proxy/core/reply/impl/MultyBulkRedisReply.java @@ -3,9 +3,9 @@ */ package com.opensource.netty.redis.proxy.core.reply.impl; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; - import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; import com.opensource.netty.redis.proxy.commons.utils.ProtoUtils; import com.opensource.netty.redis.proxy.core.enums.Type; @@ -17,15 +17,37 @@ import io.netty.buffer.ByteBuf; * @author liubing * */ -public class MultyBulkRedisReply extends CommonRedisReply { +public class MultyBulkRedisReply extends CommonRedisReply implements Serializable { - protected List list = new ArrayList(); + /** + * + */ + private static final long serialVersionUID = 5695076544459040408L; + protected List list = new ArrayList(); + private int count; public void setCount(int count) { this.count = count; } + + + /** + * @return the list + */ + public List getList() { + return list; + } + + + /** + * @return the count + */ + public int getCount() { + return count; + } + public MultyBulkRedisReply() { super(Type.MULTYBULK); @@ -73,6 +95,8 @@ public class MultyBulkRedisReply extends CommonRedisReply { } } + //LoggerUtils.info("完成"); + //LoggerUtils.info(JSONObject.toJSONString(out)); } public void addReply(IRedisReply reply) { diff --git a/nredis-proxy-failover/.classpath b/nredis-proxy-failover/.classpath deleted file mode 100644 index c0f4868f567875dbdf0bfe783d13efb8a6cc7947..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.classpath +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/nredis-proxy-failover/.project b/nredis-proxy-failover/.project deleted file mode 100644 index 76b45b185923ad8d164dd6510564f15e2c5f023e..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.project +++ /dev/null @@ -1,42 +0,0 @@ - - - nredis-proxy-fallover - - - - - - org.eclipse.wst.jsdt.core.javascriptValidator - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.wst.common.project.facet.core.builder - - - - - org.eclipse.wst.validation.validationbuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.wst.common.modulecore.ModuleCoreNature - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - org.eclipse.wst.common.project.facet.core.nature - org.eclipse.wst.jsdt.core.jsNature - - diff --git a/nredis-proxy-failover/.settings/.jsdtscope b/nredis-proxy-failover/.settings/.jsdtscope deleted file mode 100644 index b72a6a47b2ec21bba2fa061e92969554ed8d5d60..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/.jsdtscope +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/nredis-proxy-failover/.settings/org.eclipse.core.resources.prefs b/nredis-proxy-failover/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index abdea9ac032d4655898933f93050f48bf9581d14..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,4 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding//src/main/resources=UTF-8 -encoding/=UTF-8 diff --git a/nredis-proxy-failover/.settings/org.eclipse.jdt.core.prefs b/nredis-proxy-failover/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 443e08599a2e61e27329fbffd44d862d8675dde4..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,8 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.7 diff --git a/nredis-proxy-failover/.settings/org.eclipse.wst.common.component b/nredis-proxy-failover/.settings/org.eclipse.wst.common.component deleted file mode 100644 index 468cd45a8394df8020b8899af70599906dd0e6c1..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.wst.common.component +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - uses - - - uses - - - uses - - - uses - - - - - diff --git a/nredis-proxy-failover/.settings/org.eclipse.wst.common.project.facet.core.xml b/nredis-proxy-failover/.settings/org.eclipse.wst.common.project.facet.core.xml deleted file mode 100644 index c6144e856bf67c99b8a207b877f98737f03a3852..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.wst.common.project.facet.core.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/nredis-proxy-failover/.settings/org.eclipse.wst.jsdt.ui.superType.container b/nredis-proxy-failover/.settings/org.eclipse.wst.jsdt.ui.superType.container deleted file mode 100644 index 3bd5d0a4803967bc0bf72a7dd66d7e292ed2e586..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.wst.jsdt.ui.superType.container +++ /dev/null @@ -1 +0,0 @@ -org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/nredis-proxy-failover/.settings/org.eclipse.wst.jsdt.ui.superType.name b/nredis-proxy-failover/.settings/org.eclipse.wst.jsdt.ui.superType.name deleted file mode 100644 index 05bd71b6ec2c1982d1e8a5653073281994564ae8..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.wst.jsdt.ui.superType.name +++ /dev/null @@ -1 +0,0 @@ -Window \ No newline at end of file diff --git a/nredis-proxy-failover/.settings/org.eclipse.wst.validation.prefs b/nredis-proxy-failover/.settings/org.eclipse.wst.validation.prefs deleted file mode 100644 index 04cad8cb752a9761c4e5167d0301d3a27674430f..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/.settings/org.eclipse.wst.validation.prefs +++ /dev/null @@ -1,2 +0,0 @@ -disabled=06target -eclipse.preferences.version=1 diff --git a/nredis-proxy-failover/pom.xml b/nredis-proxy-failover/pom.xml deleted file mode 100644 index 6c57f933e8085b5e4280503f0c625854523de9aa..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/pom.xml +++ /dev/null @@ -1,110 +0,0 @@ - - - 4.0.0 - - com.opensource - nredis-proxy - 1.0 - - com.opensource - nredis-proxy-failover - 1.0 - war - nredis-proxy-fallover Maven Webapp - http://maven.apache.org - - UTF-8 - 4.2.7.RELEASE - 2.6.1 - 3.1 - - - - junit - junit - test - - - com.opensource - nredis-proxy-register - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-log12 - - - - - javax.servlet - javax.servlet-api - 3.0.1 - - - redis.clients - jedis - 2.8.0 - - - org.aspectj - aspectjweaver - 1.8.1 - - - org.springframework - spring-tx - ${spring.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - org.springframework - spring-context-support - ${spring.version} - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - commons-logging - commons-logging - 1.2 - - - org.springframework - spring-test - ${spring.version} - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-log4j12 - 1.5.8 - - - log4j - log4j - 1.2.14 - - - org.quartz-scheduler - quartz - 2.2.2 - - - - nredis-proxy-failover - - diff --git a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/bean/RedisMonitorConfiguration.java b/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/bean/RedisMonitorConfiguration.java deleted file mode 100644 index 223ab72b015c5e36911b75ebe0300d0f012fbd14..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/bean/RedisMonitorConfiguration.java +++ /dev/null @@ -1,218 +0,0 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.monitor.spring.bean; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.springframework.beans.factory.InitializingBean; - -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; - -import com.alibaba.fastjson.JSONObject; -import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; -import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; -import com.opensource.netty.redis.proxy.core.registry.Registry; -import com.opensource.netty.redis.proxy.core.url.RedisProxyURL; -import com.opensource.netty.redis.proxy.zk.registry.ZookeeperRegistryFactory; - -/** - * @author liubing - * spring 注入配置文件 - */ -public class RedisMonitorConfiguration implements InitializingBean{ - - private String zkHost;//zk ip:port - - private List masterShardInfos=new ArrayList();//主 线程池 - - //主从 线程池 - private Map> clusterShardInfos=new ConcurrentHashMap>(); - - private List ffanRedisMasterServerBeans=new ArrayList();//主 注册 - - private Map> ffanRedisServerClusterBeans=new ConcurrentHashMap>(); - - private JedisPoolConfig config = new JedisPoolConfig(); - /** - * - */ - public RedisMonitorConfiguration() { - super(); - } - - - - /** - * @return the zkHost - */ - public String getZkHost() { - return zkHost; - } - - /** - * @param zkHost the zkHost to set - */ - public void setZkHost(String zkHost) { - this.zkHost = zkHost; - } - - - - @Override - public void afterPropertiesSet() throws Exception { - ZookeeperRegistryFactory zookeeperRegistryFactory=new ZookeeperRegistryFactory(); - RedisProxyURL redisProxyURL=new RedisProxyURL(); - redisProxyURL.addParameter(RedisConstants.ADDRESS, zkHost); - Registry registry=zookeeperRegistryFactory.getRegistry(redisProxyURL); - List redisMasterNodes=registry.getChildren(RedisConstants.TOP_PATH); - if(redisMasterNodes!=null&&redisMasterNodes.size()>0){ - for(String redisMasterNode:redisMasterNodes){//主 - String masterBean=registry.readData(RedisConstants.TOP_PATH+RedisConstants.PATH_SEPARATOR+redisMasterNode, true); - LBRedisServerBean ffanRedisServerBean=JSONObject.parseObject(masterBean, LBRedisServerBean.class); - ffanRedisMasterServerBeans.add(ffanRedisServerBean); - - String dataPath=redisMasterNode; - String[] dataPaths=dataPath.split(":"); - initMasterServer(dataPaths[0], Integer.parseInt(dataPaths[1]), RedisConstants.TIMEOUT); - List redisClutserNodes=registry.getChildren(RedisConstants.TOP_PATH+RedisConstants.PATH_SEPARATOR+redisMasterNode); - StringBuilder key=new StringBuilder(); - key.append(dataPaths[0]).append(":").append(dataPaths[1]); - for(String redisClusterNode:redisClutserNodes){//从 - String clusterBean=registry.readData(RedisConstants.TOP_PATH+RedisConstants.PATH_SEPARATOR+redisMasterNode+RedisConstants.PATH_SEPARATOR+redisClusterNode, true); - if(ffanRedisServerClusterBeans.containsKey(key.toString())){ - ArrayList ffanRedisClusterServerBeans=ffanRedisServerClusterBeans.get(key.toString()); - LBRedisServerBean ffanRedisClusterServerBean=JSONObject.parseObject(clusterBean, LBRedisServerBean.class); - ffanRedisClusterServerBeans.add(ffanRedisClusterServerBean); - ffanRedisServerClusterBeans.put(key.toString(), ffanRedisClusterServerBeans); - }else{ - ArrayList ffanRedisClusterServerBeans=new ArrayList(); - LBRedisServerBean ffanRedisClusterServerBean=JSONObject.parseObject(clusterBean, LBRedisServerBean.class); - ffanRedisClusterServerBeans.add(ffanRedisClusterServerBean); - ffanRedisServerClusterBeans.put(key.toString(), ffanRedisClusterServerBeans); - } - - - String dataClusterPath=redisClusterNode; - String[] dataClusterPaths=dataClusterPath.split(":"); - initClusterServer(dataClusterPaths[0], Integer.parseInt(dataClusterPaths[1]), RedisConstants.TIMEOUT,key.toString()); - } - } - } - - } - /** - * 初始化主 - * @param host - * @param port - * @param timeout - */ - private void initMasterServer(String host, int port, int timeout) { - - JedisPool jedis=new JedisPool(config,host, port,timeout); - masterShardInfos.add(jedis); - } - - /** - * 初始化从 - * @param host - * @param port - * @param timeout - * @param server - */ - private void initClusterServer(String host, int port, int timeout,String server) { - if(clusterShardInfos.containsKey(server)){//包含 - ArrayList masterJedisPools=clusterShardInfos.get(server); - JedisPool masterJedis=new JedisPool(config,host, port, timeout); - - masterJedisPools.add(masterJedis); - clusterShardInfos.put(server, masterJedisPools); - }else{//不包含 - ArrayList jedisShardInfos=new ArrayList(); - JedisPool clusterJedisPools=new JedisPool(config,host, port, timeout); - jedisShardInfos.add(clusterJedisPools); - clusterShardInfos.put(server, jedisShardInfos); - } - } - - - - /** - * @return the masterShardInfos - */ - public List getMasterShardInfos() { - return masterShardInfos; - } - - - - /** - * @param masterShardInfos the masterShardInfos to set - */ - public void setMasterShardInfos(List masterShardInfos) { - this.masterShardInfos = masterShardInfos; - } - - - - /** - * @return the clusterShardInfos - */ - public Map> getClusterShardInfos() { - return clusterShardInfos; - } - - - - /** - * @param clusterShardInfos the clusterShardInfos to set - */ - public void setClusterShardInfos( - Map> clusterShardInfos) { - this.clusterShardInfos = clusterShardInfos; - } - - - - /** - * @return the ffanRedisMasterServerBeans - */ - public List getFfanRedisMasterServerBeans() { - return ffanRedisMasterServerBeans; - } - - - - /** - * @param ffanRedisMasterServerBeans the ffanRedisMasterServerBeans to set - */ - public void setFfanRedisMasterServerBeans( - List ffanRedisMasterServerBeans) { - this.ffanRedisMasterServerBeans = ffanRedisMasterServerBeans; - } - - - - /** - * @return the ffanRedisServerClusterBeans - */ - public Map> getFfanRedisServerClusterBeans() { - return ffanRedisServerClusterBeans; - } - - - - /** - * @param ffanRedisServerClusterBeans the ffanRedisServerClusterBeans to set - */ - public void setFfanRedisServerClusterBeans( - Map> ffanRedisServerClusterBeans) { - this.ffanRedisServerClusterBeans = ffanRedisServerClusterBeans; - } - - -} diff --git a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/RedisMonitorConfigurationHelper.java b/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/RedisMonitorConfigurationHelper.java deleted file mode 100644 index bf359995b478d252cdb295f97ebdbe3243cbac49..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/RedisMonitorConfigurationHelper.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.opensource.netty.redis.proxy.monitor.spring.helper; - -import com.opensource.netty.redis.proxy.monitor.spring.bean.RedisMonitorConfiguration; - -/** - * RedisMonitorConfigurationHelper获取RedisMonitorConfiguration实例 - * - * @author wangyang - */ -public class RedisMonitorConfigurationHelper { - private static class InstanceHolder { - private static final RedisMonitorConfigurationHelper instance = new RedisMonitorConfigurationHelper(); - } - - public static RedisMonitorConfigurationHelper getInstance() { - return InstanceHolder.instance; - } - - - private static final RedisMonitorConfiguration redisMonitorConfiguration = SpringFactory.getBean("redisMonitorConfiguration"); - - - public RedisMonitorConfiguration getRedisMonitorConfigurationInstance() { - return redisMonitorConfiguration; - } - - -} diff --git a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/SpringFactory.java b/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/SpringFactory.java deleted file mode 100644 index 11d8c83208510c70fcace8cafa4acdfbfd859d11..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/SpringFactory.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.opensource.netty.redis.proxy.monitor.spring.helper; - -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; -/** - * SpringFactory - * - * @author wangyang - */ -public class SpringFactory implements BeanFactoryAware { - - private static Logger logger = LogManager.getLogger(SpringFactory.class); - private static BeanFactory beanFactory; - - public SpringFactory() { - } - - @Override - public void setBeanFactory(BeanFactory factory) throws BeansException { - logger.info("初始化beanFactory=============="); - beanFactory = factory; - } - - @SuppressWarnings("unchecked") - public static T getBean(String beanName) { - if (null == beanFactory) { - logger.error("beanFactory 为空============"); - } - if (null != beanFactory) { - return (T) beanFactory.getBean(beanName); - } - return null; - } -} diff --git a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/SuperClassReflectionUtils.java b/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/SuperClassReflectionUtils.java deleted file mode 100644 index d6bff76e9a183ede5919cd5e9779678b1c1eb628..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/helper/SuperClassReflectionUtils.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.opensource.netty.redis.proxy.monitor.spring.helper; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -/** - * @version v1.0.0 - * @author: wangyang - * Date: 2016-8-29 Time: 16:47 - */ -public class SuperClassReflectionUtils { - - /** - * 循环向上转型, 获取对象的 DeclaredMethod - * @param object : 子类对象 - * @param methodName : 父类中的方法名 - * @param parameterTypes : 父类中的方法参数类型 - * @return 父类中的方法对象 - */ - public static Method getDeclaredMethod(Object object, String methodName, Class ... parameterTypes){ - Method method = null ; - for(Class clazz = object.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { - try { - method = clazz.getDeclaredMethod(methodName, parameterTypes) ; - return method ; - } catch (Exception e) { - //这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。 - //如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了 - } - } - return null; - } - - /** - * 直接调用对象方法, 而忽略修饰符(private, protected, default) - * @param object : 子类对象 - * @param methodName : 父类中的方法名 - * @param parameterTypes : 父类中的方法参数类型 - * @param parameters : 父类中的方法参数 - * @return 父类中方法的执行结果 - */ - public static Object invokeMethod(Object object, String methodName, Class [] parameterTypes, - Object [] parameters) { - //根据 对象、方法名和对应的方法参数 通过反射 调用上面的方法获取 Method 对象 - Method method = getDeclaredMethod(object, methodName, parameterTypes) ; - - //抑制Java对方法进行检查,主要是针对私有方法而言 - method.setAccessible(true) ; - - try { - if(null != method) { - //调用object 的 method 所代表的方法,其方法的参数是 parameters - return method.invoke(object, parameters) ; - } - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - return null; - } - - /** - * 循环向上转型, 获取对象的 DeclaredField - * @param object : 子类对象 - * @param fieldName : 父类中的属性名 - * @return 父类中的属性对象 - */ - public static Field getDeclaredField(Object object, String fieldName){ - Field field = null ; - Class clazz = object.getClass() ; - for(; clazz != Object.class ; clazz = clazz.getSuperclass()) { - try { - field = clazz.getDeclaredField(fieldName) ; - return field ; - } catch (Exception e) { - //这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。 - //如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了 - } - } - return null; - } - - /** - * 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter - * @param object : 子类对象 - * @param fieldName : 父类中的属性名 - * @param value : 将要设置的值 - */ - public static void setFieldValue(Object object, String fieldName, Object value){ - //根据 对象和属性名通过反射 调用上面的方法获取 Field对象 - Field field = getDeclaredField(object, fieldName) ; - //抑制Java对其的检查 - field.setAccessible(true) ; - try { - //将 object 中 field 所代表的值 设置为 value - field.set(object, value) ; - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - - /** - * 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter - * @param object : 子类对象 - * @param fieldName : 父类中的属性名 - * @return : 父类中的属性值 - */ - public static Object getFieldValue(Object object, String fieldName){ - - //根据 对象和属性名通过反射 调用上面的方法获取 Field对象 - Field field = getDeclaredField(object, fieldName) ; - //抑制Java对其的检查 - field.setAccessible(true) ; - try { - //获取 object 中 field 所代表的属性值 - return field.get(object) ; - } catch(Exception e) { - e.printStackTrace() ; - } - return null; - } - - - - - -} diff --git a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/job/RedisMasterSlaveMonitorJob.java b/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/job/RedisMasterSlaveMonitorJob.java deleted file mode 100644 index 88962c015832202beb4664fbdce6245f7ff46164..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/java/com/opensource/netty/redis/proxy/monitor/spring/job/RedisMasterSlaveMonitorJob.java +++ /dev/null @@ -1,283 +0,0 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.monitor.spring.job; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; - -import org.I0Itec.zkclient.ZkClient; -import org.apache.commons.pool2.PooledObjectFactory; -import org.apache.commons.pool2.impl.GenericObjectPool; -import org.apache.log4j.Logger; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.util.CollectionUtils; - -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; -import com.opensource.netty.redis.proxy.core.enums.ZkNodeType; -import com.opensource.netty.redis.proxy.core.registry.impl.support.ZkUtils; -import com.opensource.netty.redis.proxy.core.url.RedisProxyURL; -import com.opensource.netty.redis.proxy.monitor.spring.bean.RedisMonitorConfiguration; -import com.opensource.netty.redis.proxy.monitor.spring.helper.RedisMonitorConfigurationHelper; -import com.opensource.netty.redis.proxy.monitor.spring.helper.SuperClassReflectionUtils; - -/** - * @author liubing - * 监控从 - */ -public class RedisMasterSlaveMonitorJob implements InitializingBean { - - private Logger logger = Logger.getLogger("RedisMasterSlaveMonitorJob"); - - private String zkHost; - - public static final int TRY_TIMES = 1; - - private int connectTimeout; - - private ZkClient zkClient; - - - /** - * 监控主从 - * @throws NoSuchFieldException - * @throws IllegalAccessException - */ - public void monitorMasterSlaves()throws NoSuchFieldException, IllegalAccessException { - updateSlaveRedisConfiguration(); - updateMasterRedisConfiguration(); - } - /** - * 监测从redis连接池 - * - * @throws NoSuchFieldException - * @throws IllegalAccessException - */ - public void updateSlaveRedisConfiguration() throws NoSuchFieldException, IllegalAccessException { - logger.info("monitor slave start..."); - RedisMonitorConfiguration redisMonitorConfiguration = RedisMonitorConfigurationHelper.getInstance().getRedisMonitorConfigurationInstance(); - List masterShardInfos = redisMonitorConfiguration.getMasterShardInfos(); - - logger.info("masterShardInfos size is:" + masterShardInfos.size()); - if (CollectionUtils.isEmpty(masterShardInfos)) { - return; - } - Map> clusterShardInfos = redisMonitorConfiguration.getClusterShardInfos(); - for (JedisPool masterJedisPool : masterShardInfos) { - - AtomicReference hostAndPort= getHostAndPort(masterJedisPool); - if(hostAndPort==null){ - continue; - } - String masterHostAndPort=String.valueOf(hostAndPort.get()); - ArrayList clusterJedisPools = clusterShardInfos.get(masterHostAndPort); - if(clusterJedisPools==null||clusterJedisPools.size()==0){ - continue; - } - Iterator clusterJedisPoolIterator = clusterJedisPools.iterator(); - if (!CollectionUtils.isEmpty(clusterJedisPools)) { - while (clusterJedisPoolIterator.hasNext()) { - JedisPool clusterJedisPool = clusterJedisPoolIterator.next(); - boolean isClusterCrash = getRedisCrashFlag(clusterJedisPool, TRY_TIMES); - AtomicReference slaveHostAndPort= getHostAndPort(clusterJedisPool); - if(slaveHostAndPort==null){ - continue; - } - RedisProxyURL parentRedisProxyURL = new RedisProxyURL(hostAndPort.get().getHost(), Integer.valueOf(hostAndPort.get().getPort()), connectTimeout); - String parentPath = ZkUtils.toNodePath(parentRedisProxyURL, null, ZkNodeType.AVAILABLE_SERVER); - String childPath = new StringBuilder(parentPath).append("/").append(slaveHostAndPort.get().getHost()).append(":").append(slaveHostAndPort.get().getPort()).toString(); - if (isClusterCrash) { - logger.info("cluster crash, delete child path..."); - //删除map中的从(RedisMonitorConfiguration) - zkClient.delete(childPath); - logger.info("cluster crash, remove child shard info from clusterShardInfos..."); - clusterJedisPoolIterator.remove(); - continue; - } - } - } - } - } - - /** - * 监测主redis连接池 - * - * @throws NoSuchFieldException - * @throws IllegalAccessException - */ - public void updateMasterRedisConfiguration() throws NoSuchFieldException, IllegalAccessException { - logger.info("monitor start..."); - RedisMonitorConfiguration redisMonitorConfiguration = RedisMonitorConfigurationHelper.getInstance().getRedisMonitorConfigurationInstance(); - List masterShardInfos = redisMonitorConfiguration.getMasterShardInfos(); - logger.info("masterShardInfos size is:" + masterShardInfos.size()); - if (CollectionUtils.isEmpty(masterShardInfos)) { - return; - } - Map> clusterShardInfos = redisMonitorConfiguration.getClusterShardInfos(); - for (JedisPool masterJedisPool : masterShardInfos) { - boolean isMasterCrash = getRedisCrashFlag(masterJedisPool, TRY_TIMES); - logger.info("is master crash?,flag:" + isMasterCrash); - if (isMasterCrash) {//主挂了 - AtomicReference hostAndPort= getHostAndPort(masterJedisPool); - if(hostAndPort==null){ - continue; - } - String masterHostAndPort = String.valueOf(hostAndPort.get()); - ArrayList clusterJedisPools = clusterShardInfos.get(masterHostAndPort); - Iterator clusterJedisPoolIterator = clusterJedisPools.iterator(); - if (!CollectionUtils.isEmpty(clusterJedisPools)) { - Jedis clusterSelectedClient =null; - while (clusterJedisPoolIterator.hasNext()) { - JedisPool clusterJedisPool = clusterJedisPoolIterator.next(); - boolean isClusterCrash = getRedisCrashFlag(clusterJedisPool, TRY_TIMES);//检查是否完好 - if(isClusterCrash){//从挂了,下次循环 - continue; - } - AtomicReference slaveHostAndPort= getHostAndPort(clusterJedisPool); - if(slaveHostAndPort==null){ - continue; - } - RedisProxyURL parentRedisProxyURL = new RedisProxyURL(hostAndPort.get().getHost(), Integer.valueOf(hostAndPort.get().getPort()), connectTimeout); - String parentPath = ZkUtils.toNodePath(parentRedisProxyURL, null, ZkNodeType.AVAILABLE_SERVER); - String childPath = new StringBuilder(parentPath).append("/").append(slaveHostAndPort.get().getHost()).append(":").append(slaveHostAndPort.get().getPort()).toString(); - if (zkClient.exists(parentPath)) { - String childData = zkClient.readData(childPath, true); - LBRedisServerBean childFfanRedisServerBean = JSONObject.parseObject(childData, LBRedisServerBean.class); - //将从的data设置的主的parentPath - logger.info("write child data to parent path..."); - zkClient.writeData(parentPath, JSON.toJSONString(childFfanRedisServerBean)); - //删除从的childPath - logger.info("delete child path..."); - zkClient.delete(childPath); - //将从设置为主(RedisMonitorConfiguration) - logger.info("update masterShardInfos..."); - logger.info("remove master jedis pool from masterShardInfos"); - if (masterShardInfos.contains(masterJedisPool)) { - masterShardInfos.remove(masterJedisPool); - } - masterShardInfos.add(clusterJedisPool); - //删除map中的从(RedisMonitorConfiguration) - logger.info("remove child shard info from clusterShardInfos..."); - clusterJedisPoolIterator.remove(); - clusterSelectedClient=clusterJedisPool.getResource(); - break; - } - } - changeMasterSlave(clusterSelectedClient, clusterJedisPools); - } - } - } - } - - /** - * 改变主从server - * @param clusterSelectedClient - * @param clusterJedisPools - */ - private void changeMasterSlave(Jedis clusterSelectedClient,ArrayList clusterJedisPools){ - clusterSelectedClient.slaveofNoOne();//成为主 - if(clusterJedisPools!=null&&clusterJedisPools.size()>0){ - for(JedisPool jedisPool:clusterJedisPools){//设置主从关系 - jedisPool.getResource().slaveof(clusterSelectedClient.getClient().getHost(), clusterSelectedClient.getClient().getPort()); - } - } - - - } - /** - * 获取IP与端口号 - * @param masterJedisPool - * @return - */ - private AtomicReference getHostAndPort(JedisPool jedisPool){ - Object field = getInternalPool(jedisPool); - if (field != null) { - Object hostAndPortField = getHostAndPortField((GenericObjectPool) field); - AtomicReference hostAndPort = (AtomicReference) hostAndPortField; - return hostAndPort; - } - return null; - } - /** - * 反射获取JedisFactory属性hostAndPort的值 - * - * @param field - * @return - */ - private Object getHostAndPortField(GenericObjectPool field) { - GenericObjectPool internalPool = field; - PooledObjectFactory poolFactory = internalPool.getFactory(); - return SuperClassReflectionUtils.getFieldValue(poolFactory, "hostAndPort"); - } - - /** - * 反射获取JedisPool属性internalPool值 - * - * @param masterJedisPool - * @return - */ - private Object getInternalPool(JedisPool masterJedisPool) { - return SuperClassReflectionUtils.getFieldValue(masterJedisPool, "internalPool"); - } - - /** - * redis是否连通 - * - * @param jedisPool - * @param tryTimes - * @return - */ - private boolean getRedisCrashFlag(JedisPool jedisPool, int tryTimes) { - boolean isRedisCrash = false; - //循环10次获取Redis主是否挂掉 - try { - jedisPool.getResource().getClient(); - } catch (Exception e) { - if (tryTimes <=5) { - tryTimes = tryTimes + 1; - return getRedisCrashFlag(jedisPool, tryTimes); - } else { - return true; - } - } - return isRedisCrash; - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - @Override - public void afterPropertiesSet() throws Exception { - zkClient = new ZkClient(zkHost); - } - - public String getZkHost() { - return zkHost; - } - - public void setZkHost(String zkHost) { - this.zkHost = zkHost; - } - /** - * @return the connectTimeout - */ - public int getConnectTimeout() { - return connectTimeout; - } - /** - * @param connectTimeout the connectTimeout to set - */ - public void setConnectTimeout(int connectTimeout) { - this.connectTimeout = connectTimeout; - } - -} diff --git a/nredis-proxy-failover/src/main/resources/applicationContext.xml b/nredis-proxy-failover/src/main/resources/applicationContext.xml deleted file mode 100644 index 6ac7a1decf416941497b6d114801db9d841240f8..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/resources/applicationContext.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/nredis-proxy-failover/src/main/resources/log4j.properties b/nredis-proxy-failover/src/main/resources/log4j.properties deleted file mode 100644 index 8810f96a4eb900c7496c3600e36d2832ca1c2c6e..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/resources/log4j.properties +++ /dev/null @@ -1,10 +0,0 @@ -log4j.rootCategory=INFO, stdout - -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n - -log4j.category.org.springframework.data.redis.listener=TRACE - -# for debugging datasource initialization -# log4j.category.test.jdbc=DEBUG diff --git a/nredis-proxy-failover/src/main/resources/timer.xml b/nredis-proxy-failover/src/main/resources/timer.xml deleted file mode 100644 index 498c72c2898560f77842dd89bc1b113f7fb2151d..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/resources/timer.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - - - - - - - - - - monitorMasterSlaves - - - false - - - - - - - - - - 0 0/3 * * * ? - - - - - - - - - - - \ No newline at end of file diff --git a/nredis-proxy-failover/src/main/webapp/WEB-INF/web.xml b/nredis-proxy-failover/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index a42bd07022c5e6b6d8e1cf86d9ede080e2ebc195..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,63 +0,0 @@ - - - - nredis-proxy-fallover - - - contextConfigLocation - classpath:applicationContext.xml - - - - - - - org.springframework.web.context.ContextLoaderListener - - - org.springframework.web.context.request.RequestContextListener - - - - spring-web - org.springframework.web.servlet.DispatcherServlet - - transformWsdlLocations - true - - - contextConfigLocation - - - - debug - false - - 0 - - - spring-web - /* - - - - - - - encodingFilter - org.springframework.web.filter.CharacterEncodingFilter - - encoding - UTF-8 - - - - encodingFilter - /* - - - - index.html - - \ No newline at end of file diff --git a/nredis-proxy-failover/src/main/webapp/index.jsp b/nredis-proxy-failover/src/main/webapp/index.jsp deleted file mode 100644 index c38169bb958579c635a5c09ee2f379cc5956c0c2..0000000000000000000000000000000000000000 --- a/nredis-proxy-failover/src/main/webapp/index.jsp +++ /dev/null @@ -1,5 +0,0 @@ - - -

Hello World!

- - diff --git a/nredis-proxy-net/.gitignore b/nredis-proxy-net/.gitignore index b83d22266ac8aa2f8df2edef68082c789727841d..1dd33310812560b414ca155359fca42cd9b8af9f 100644 --- a/nredis-proxy-net/.gitignore +++ b/nredis-proxy-net/.gitignore @@ -1 +1,2 @@ /target/ +/target/ diff --git a/nredis-proxy-net/pom.xml b/nredis-proxy-net/pom.xml index f7c0dae0b0f0bbc55b9ec13fbc4c6b108e6e1c95..4c99f5202b52266a165812ef20991b6033cccbc2 100644 --- a/nredis-proxy-net/pom.xml +++ b/nredis-proxy-net/pom.xml @@ -24,6 +24,12 @@ com.opensource nredis-proxy-core + + + netty + io.netty + + diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisClient.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisClient.java index 57d4fbecaf575c0109b9300e2b9fc590e3c24885..79abd9e856c4fa356a4c7790db2836d8d5c99a88 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisClient.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisClient.java @@ -3,12 +3,14 @@ */ package com.opensource.netty.redis.proxy.net.client; +import io.netty.channel.ChannelHandlerContext; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import com.opensource.netty.redis.proxy.core.client.impl.AbstractPoolClient; import com.opensource.netty.redis.proxy.core.command.impl.RedisCommand; import com.opensource.netty.redis.proxy.core.connection.IConnection; -import com.opensource.netty.redis.proxy.core.connection.IConnectionCallBack; import com.opensource.netty.redis.proxy.pool.LBRedisProxyPoolEntry; import com.opensource.netty.redis.proxy.pool.LBRedisProxyPooledObjectFactory; import com.opensource.netty.redis.proxy.pool.commons.LBRedisProxyPoolConfig; @@ -22,7 +24,7 @@ public class LBRedisClient extends AbstractPoolClient{ private Logger logger = LoggerFactory.getLogger(LBRedisClient.class); private LBRedisProxyPoolConfig lbRedisProxyPoolConfig; - + /** * @param conf */ @@ -37,8 +39,8 @@ public class LBRedisClient extends AbstractPoolClient{ */ @Override public synchronized void close() { - try { - pool.shutDown();//连接池关闭,所有都关闭 + try { + super.pool.shutDown();//连接池关闭,所有都关闭 } catch (Exception e) { logger.error("NettyClient close Error,HOST:"+lbRedisProxyPoolConfig.getHost()+",PORT:"+lbRedisProxyPoolConfig.getPort(), e); } @@ -53,7 +55,7 @@ public class LBRedisClient extends AbstractPoolClient{ } @Override - public void write(RedisCommand request,IConnectionCallBack connectionCallBack) { + public void write(RedisCommand request,ChannelHandlerContext frontCtx) { IConnection connection=null; LBRedisProxyPoolEntry entry=null; try{ @@ -63,10 +65,7 @@ public class LBRedisClient extends AbstractPoolClient{ return ; } connection=entry.getObject(); - if(!connection.isAvailable()){ - connection.open(); - } - connection.write(request,connectionCallBack); + connection.write(request,frontCtx); }catch(Exception e){ logger.error("NettyClient write request Error :" , e); diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnection.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnection.java index 6566fba424d2698b33697d533f100e011e7ad820..e13b594db8a6f987cb56837732c2069bc6560958 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnection.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnection.java @@ -5,11 +5,14 @@ import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; +import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; @@ -17,11 +20,10 @@ import com.opensource.netty.redis.proxy.commons.constants.LBRedisProxyErrorMsgCo import com.opensource.netty.redis.proxy.commons.exception.LBRedisProxyFrameworkException; import com.opensource.netty.redis.proxy.core.command.impl.RedisCommand; import com.opensource.netty.redis.proxy.core.connection.IConnection; -import com.opensource.netty.redis.proxy.core.connection.IConnectionCallBack; import com.opensource.netty.redis.proxy.core.enums.ChannelState; import com.opensource.netty.redis.proxy.core.protocol.RedisReplyDecoder; import com.opensource.netty.redis.proxy.core.protocol.RedisRequestEncoder; -import com.opensource.netty.redis.proxy.net.client.support.LBRedisClientInHandler; +import com.opensource.netty.redis.proxy.core.reply.IRedisReply; import com.opensource.netty.redis.proxy.net.client.support.LBRedisClientOutHandler; import com.opensource.netty.redis.proxy.pool.commons.LBRedisProxyPoolConfig; @@ -36,12 +38,13 @@ public class LBRedisConnection implements IConnection{ private volatile ChannelState state = ChannelState.UNINIT; - private Channel channel = null; + private Channel backChannel = null; private LBRedisProxyPoolConfig lbRedisProxyPoolConfig; private Bootstrap bootstrap; + private ChannelHandlerContext frontCtx; /** * @param ffanRedisClient */ @@ -80,22 +83,29 @@ public class LBRedisConnection implements IConnection{ LBRedisProxyErrorMsgConstant.FRAMEWORK_INIT_ERROR); } bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout); + open(); } /** * 发送消息 */ - public void write(RedisCommand request,IConnectionCallBack connectionCallBack) { - open();//判断连接是否可用 - this.channel.attr(LBRedisClientOutHandler.CALLBACK_KEY).setIfAbsent(connectionCallBack); - this.channel.writeAndFlush(request); + public void write(final RedisCommand request,ChannelHandlerContext frontCtx) { + if(!isAvailable()){ + open(); + } + this.frontCtx=frontCtx; + backChannel.eventLoop().execute(new Runnable() { + + @Override + public void run() { + backChannel.writeAndFlush(request, backChannel.voidPromise()); + } + }); } @Override public boolean open() { - if (isAvailable()) { - return true; - } + try { ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(lbRedisProxyPoolConfig.getHost(),lbRedisProxyPoolConfig.getPort())); @@ -111,7 +121,7 @@ public class LBRedisConnection implements IConnection{ boolean success = channelFuture.isSuccess(); if (result && success) { - channel = channelFuture.channel(); + backChannel = channelFuture.channel(); state = ChannelState.ALIVE; return true; } @@ -142,10 +152,16 @@ public class LBRedisConnection implements IConnection{ @Override public void close(int timeout) { try { - if (channel != null) { - channel.close(); + if(state.isAliveState()){ + if (backChannel != null&&backChannel.isOpen()) { + backChannel.close(); + } + if (frontCtx != null&&frontCtx.channel().isOpen()) { + frontCtx.close(); + } + state = ChannelState.CLOSE; } - state = ChannelState.CLOSE; + } catch (Exception e) { logger.error("NettyChannel close Error,HOST:"+lbRedisProxyPoolConfig.getHost()+",port:"+lbRedisProxyPoolConfig.getPort(), e); } @@ -158,11 +174,47 @@ public class LBRedisConnection implements IConnection{ @Override public boolean isAvailable() { - if(channel!=null){//判断通道状态,防止通道假活 - return state.isAliveState()&&channel.isActive(); + if(backChannel!=null){//判断通道状态,防止通道假活 + return state.isAliveState()&&backChannel.isActive(); } return state.isAliveState(); } + /** + * 目标服务器写入客户端通道 + * @author liubing + * + */ + private class LBRedisClientInHandler extends SimpleChannelInboundHandler { + + /** + * @param frontChannel + */ + public LBRedisClientInHandler() { + super(); + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, final IRedisReply msg) + throws Exception { + // Always write from the event loop, minimize the wakeup events + frontCtx.channel().eventLoop().execute(new Runnable() { + + @Override + public void run() { + // Not interested in the channel promise + + frontCtx.writeAndFlush(msg, frontCtx.channel().voidPromise()); + } + }); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) + throws Exception { + + super.exceptionCaught(ctx, cause); + } + } } diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnectionFactory.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnectionFactory.java index 306ae1847c7d2fb72fbe464d4a022e8e2bb41c3a..7b01682912f765c1c594ff4fae2f3500a17581dc 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnectionFactory.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/LBRedisConnectionFactory.java @@ -15,9 +15,9 @@ public class LBRedisConnectionFactory implements LBRedisProxyPooledObjectFactory private LBRedisProxyPoolConfig ffanRedisProxyPoolConfig; - /** - * @param ffanRedisClient + * + * @param ffanRedisProxyPoolConfig */ public LBRedisConnectionFactory(LBRedisProxyPoolConfig ffanRedisProxyPoolConfig) { super(); @@ -45,5 +45,7 @@ public class LBRedisConnectionFactory implements LBRedisProxyPooledObjectFactory public void destroyEntry(IConnection connection) throws LBRedisProxyPoolException { connection.close(); } - + + + } diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientInHandler.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientInHandler.java index f7383f8e3dcf6926d40d359a2cf07b183516ec8e..8fc17ecfeae50b67c76819d916ca4b0cbe8aafa7 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientInHandler.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientInHandler.java @@ -1,38 +1,46 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.net.client.support; - -import com.opensource.netty.redis.proxy.core.connection.IConnectionCallBack; -import com.opensource.netty.redis.proxy.core.log.impl.LoggerUtils; -import com.opensource.netty.redis.proxy.core.reply.IRedisReply; - -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.SimpleChannelInboundHandler; - -/** - * 目标服务器与客户端通道写入 - * @author liubing - * - */ -public class LBRedisClientInHandler extends SimpleChannelInboundHandler { - - @Override - protected void channelRead0(ChannelHandlerContext ctx, IRedisReply msg) - throws Exception { - IConnectionCallBack callBack = ctx.channel().attr(LBRedisClientOutHandler.CALLBACK_KEY).get(); - if(callBack!=null){ - callBack.handleReply(msg); - }else{ - LoggerUtils.error(ctx.channel().remoteAddress().toString()+" has no callBack"); - } - - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) - throws Exception { - - super.exceptionCaught(ctx, cause); - } -} +///** +// * +// */ +//package com.opensource.netty.redis.proxy.net.client.support; +// +//import com.opensource.netty.redis.proxy.core.reply.IRedisReply; +//import io.netty.channel.Channel; +//import io.netty.channel.ChannelHandlerContext; +//import io.netty.channel.SimpleChannelInboundHandler; +///** +// * 目标服务器与客户端通道写入 +// * @author liubing +// * +// */ +//public class LBRedisClientInHandler extends SimpleChannelInboundHandler { +// +// private Channel frontChannel; +// +// /** +// * @param frontChannel +// */ +// public LBRedisClientInHandler(Channel frontChannel) { +// super(); +// this.frontChannel = frontChannel; +// } +// @Override +// public void channelActive(ChannelHandlerContext ctx) throws Exception { +// super.channelActive(ctx); +// +// } +// @Override +// protected void channelRead0(ChannelHandlerContext ctx, IRedisReply msg) +// throws Exception { +// frontChannel.writeAndFlush(msg); +// } +// +// @Override +// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) +// throws Exception { +// +// super.exceptionCaught(ctx, cause); +// } +// +// +// +//} diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientOutHandler.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientOutHandler.java index ba646540c5431ff64fe16b354d61c6a3d7c57a26..0bd97b3357f3f7a1cb49a51659ba61a2fc40930b 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientOutHandler.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/client/support/LBRedisClientOutHandler.java @@ -4,14 +4,10 @@ package com.opensource.netty.redis.proxy.net.client.support; import com.opensource.netty.redis.proxy.core.command.impl.RedisCommand; -import com.opensource.netty.redis.proxy.core.connection.IConnectionCallBack; import com.opensource.netty.redis.proxy.core.log.impl.LoggerUtils; - import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; -import io.netty.util.AttributeKey; - /** * 解码处理 * @@ -20,10 +16,10 @@ import io.netty.util.AttributeKey; */ public class LBRedisClientOutHandler extends ChannelOutboundHandlerAdapter { - public static final AttributeKey CALLBACK_KEY = AttributeKey.valueOf("CALLBACK_KEY"); + @Override public void write(ChannelHandlerContext ctx, Object msg, - ChannelPromise promise) throws Exception { + ChannelPromise promise) throws Exception { if(msg instanceof RedisCommand){ ctx.write(msg, promise); }else{ diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/LBRedisServer.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/LBRedisServer.java index 60331e1c8d88aa387e0a8e1a6d02830a453d394c..2d997086911ce759c58bbc0d584c06a1d9a25ba9 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/LBRedisServer.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/LBRedisServer.java @@ -3,12 +3,13 @@ */ package com.opensource.netty.redis.proxy.net.server; - import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.AdaptiveRecvByteBufAllocator; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; @@ -17,6 +18,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import com.opensource.netty.redis.proxy.core.config.LBRedisServerMasterCluster; +import com.opensource.netty.redis.proxy.core.config.RedisPoolConfig; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerClusterBean; import com.opensource.netty.redis.proxy.core.protocol.RedisReplyEncoder; @@ -34,7 +36,9 @@ public class LBRedisServer { private Logger logger = LoggerFactory.getLogger(LBRedisServer.class); private LBRedisServerMasterCluster ffanRedisServerMasterCluster; - + + protected static final PooledByteBufAllocator BUF_ALLOCATOR = PooledByteBufAllocator.DEFAULT; + // 线程组 private static EventLoopGroup bossGroup = new NioEventLoopGroup(Runtime .getRuntime().availableProcessors()); @@ -44,32 +48,40 @@ public class LBRedisServer { /** * @param conf */ - public LBRedisServer( - LBRedisServerMasterCluster ffanRedisServerMasterCluster) { + public LBRedisServer(LBRedisServerMasterCluster ffanRedisServerMasterCluster) { super(); this.ffanRedisServerMasterCluster = ffanRedisServerMasterCluster; init(); } - /** + + /** + * 销废 + */ + public void destroy(){ + for(String key:ffanRedisServerMasterCluster.getRedisClientBeanMap().keySet()){ + ffanRedisServerMasterCluster.getRedisClientBeanMap().get(key).close(); + } + } + /** * 初始化客户端 */ private void init(){ if(ffanRedisServerMasterCluster!=null&&ffanRedisServerMasterCluster.getRedisServerClusterBeans()!=null&&ffanRedisServerMasterCluster.getRedisServerClusterBeans().size()>0){ for(LBRedisServerClusterBean ffanRedisServerClusterBean:ffanRedisServerMasterCluster.getRedisServerClusterBeans()){ - LBRedisServerBean lbRedisServerBean=ffanRedisServerClusterBean.getFfanMasterRedisServerBean(); + LBRedisServerBean lbRedisServerBean=ffanRedisServerClusterBean.getRedisServerMasterBean(); if(lbRedisServerBean!=null){//主 LBRedisProxyPoolConfig lbRedisProxyPoolConfig=convertLBRedisProxyPoolConfig(lbRedisServerBean); LBRedisClient ffanRedisClient=new LBRedisClient(lbRedisProxyPoolConfig); - ffanRedisServerMasterCluster.getFfanRedisClientBeanMap().put(lbRedisServerBean.getKey(), ffanRedisClient); + ffanRedisServerMasterCluster.getRedisClientBeanMap().put(lbRedisServerBean.getKey(), ffanRedisClient); } - List ffanRedisServerClusterBeans=ffanRedisServerClusterBean.getFfanRedisServerClusterBeans(); + List ffanRedisServerClusterBeans=ffanRedisServerClusterBean.getRedisServerSlaveBeans(); if(ffanRedisServerClusterBeans!=null&&ffanRedisServerClusterBeans.size()>0){ for(LBRedisServerBean ffanRedisServerSlave:ffanRedisServerClusterBeans){ LBRedisProxyPoolConfig lbRedisProxyPoolConfig=convertLBRedisProxyPoolConfig(lbRedisServerBean); LBRedisClient ffanRedisClient=new LBRedisClient(lbRedisProxyPoolConfig); - ffanRedisServerMasterCluster.getFfanRedisClientBeanMap().put(ffanRedisServerSlave.getKey(), ffanRedisClient); + ffanRedisServerMasterCluster.getRedisClientBeanMap().put(ffanRedisServerSlave.getKey(), ffanRedisClient); } } @@ -77,6 +89,7 @@ public class LBRedisServer { } } + /** * 转换 * @param lbRedisServerBean @@ -84,19 +97,20 @@ public class LBRedisServer { */ private LBRedisProxyPoolConfig convertLBRedisProxyPoolConfig(LBRedisServerBean lbRedisServerBean){ LBRedisProxyPoolConfig lbRedisProxyPoolConfig=new LBRedisProxyPoolConfig(); - lbRedisProxyPoolConfig.setConnectionTimeout(lbRedisServerBean.getConnectionTimeout()); + RedisPoolConfig redisPoolConfig= lbRedisServerBean.getRedisPoolConfig(); + lbRedisProxyPoolConfig.setConnectionTimeout(redisPoolConfig.getConnectionTimeout()); lbRedisProxyPoolConfig.setHost(lbRedisServerBean.getHost()); - lbRedisProxyPoolConfig.setInitialEntries(lbRedisServerBean.getInitialConnection()); - lbRedisProxyPoolConfig.setMaxActiveEntries(lbRedisServerBean.getMaxActiveConnection()); - lbRedisProxyPoolConfig.setMaxWaitMillisOnBorrow(lbRedisServerBean.getMaxWaitMillisOnBorrow()); - lbRedisProxyPoolConfig.setMinActiveEntries(lbRedisServerBean.getMinConnection()); - lbRedisProxyPoolConfig.setMinEvictableIdleTimeMillis(lbRedisServerBean.getMinEvictableIdleTimeMillis()); - lbRedisProxyPoolConfig.setMinIdleEntries(lbRedisServerBean.getMinIdleEntries()); + lbRedisProxyPoolConfig.setInitialEntries(redisPoolConfig.getInitialConnection()); + lbRedisProxyPoolConfig.setMaxActiveEntries(redisPoolConfig.getMaxActiveConnection()); + lbRedisProxyPoolConfig.setMaxWaitMillisOnBorrow(redisPoolConfig.getMaxWaitMillisOnBorrow()); + lbRedisProxyPoolConfig.setMinActiveEntries(redisPoolConfig.getMinConnection()); + lbRedisProxyPoolConfig.setMinEvictableIdleTimeMillis(redisPoolConfig.getMinEvictableIdleTimeMillis()); + lbRedisProxyPoolConfig.setMinIdleEntries(redisPoolConfig.getMinIdleEntries()); lbRedisProxyPoolConfig.setPort(lbRedisServerBean.getPort()); - lbRedisProxyPoolConfig.setTestOnBorrow(lbRedisServerBean.isTestOnBorrow()); - lbRedisProxyPoolConfig.setTestOnReturn(lbRedisServerBean.isTestOnReturn()); - lbRedisProxyPoolConfig.setTestWhileIdle(lbRedisServerBean.isTestWhileIdle()); - lbRedisProxyPoolConfig.setTimeBetweenEvictionRunsMillis(lbRedisServerBean.getTimeBetweenEvictionRunsMillis()); + lbRedisProxyPoolConfig.setTestOnBorrow(redisPoolConfig.isTestOnBorrow()); + lbRedisProxyPoolConfig.setTestOnReturn(redisPoolConfig.isTestOnReturn()); + lbRedisProxyPoolConfig.setTestWhileIdle(redisPoolConfig.isTestWhileIdle()); + lbRedisProxyPoolConfig.setTimeBetweenEvictionRunsMillis(redisPoolConfig.getTimeBetweenEvictionRunsMillis()); return lbRedisProxyPoolConfig; } @@ -104,11 +118,14 @@ public class LBRedisServer { * 启动系统,开启接收连接,处理业务 */ public void start() { - ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE) + .channel(NioServerSocketChannel.class) + .childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 1024, 65536)).childOption(ChannelOption.ALLOCATOR, BUF_ALLOCATOR) + .childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE) .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE) + .childOption(ChannelOption.SO_SNDBUF ,1024).childOption(ChannelOption.SO_RCVBUF ,1024) + .childOption(ChannelOption.SO_REUSEADDR, true) .childHandler(new ChannelInitializer() { @Override @@ -121,7 +138,7 @@ public class LBRedisServer { ch.pipeline().addLast( "FfanRedisServerHandler", new LBRedisServerHandler( - ffanRedisServerMasterCluster.getFfanRedisClientBeanMap(),ffanRedisServerMasterCluster)); + ffanRedisServerMasterCluster.getRedisClientBeanMap(),ffanRedisServerMasterCluster)); } }); ChannelFuture channelFuture = bootstrap.bind( diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/support/LBRedisServerHandler.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/support/LBRedisServerHandler.java index b06bcaac6d676b466f70f243d37956845d9afd05..8a6d73da162f0e0e8680718684ff362f9e7bdc82 100644 --- a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/support/LBRedisServerHandler.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/net/server/support/LBRedisServerHandler.java @@ -20,14 +20,10 @@ import com.opensource.netty.redis.proxy.core.command.impl.RedisCommand; import com.opensource.netty.redis.proxy.core.config.LBRedisServerMasterCluster; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerClusterBean; -import com.opensource.netty.redis.proxy.core.connection.impl.RedisConnectionCallBack; import com.opensource.netty.redis.proxy.core.enums.RedisCommandEnums; import com.opensource.netty.redis.proxy.core.reply.impl.ErrorRedisReply; - -import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; -import io.netty.util.ReferenceCountUtil; /** * redis服务端回答 @@ -50,47 +46,52 @@ public class LBRedisServerHandler extends SimpleChannelInboundHandler1&&!command.equals(RedisConstants.KEYS)){//第一个是命令,第二个是key RedisCommandEnums commandEnums=getRedisCommandEnums(command); + if(commandEnums!=null&&commandEnums.isIswrite()){//主 AbstractPoolClient ffanRedisClient=getShardFfanRedisClient(request,command);//默认一致性hash算法 - - ffanRedisClient.write(request,callback); + ffanRedisClient.write(request,ctx); }else if(commandEnums!=null&&!commandEnums.isIswrite()){//从 - AbstractPoolClient ffanRedisClient=getShardClusterFfanRedisClient(request,command,commandEnums.isIswrite());//权重算法 - - ffanRedisClient.write(request,callback); + AbstractPoolClient ffanRedisClient=getShardClusterFfanRedisClient(request,command,commandEnums.isIswrite());//权重算法 + ffanRedisClient.write(request,ctx); } }else if(request!=null&&request.getArgs().size()>1&&command.equals(RedisConstants.KEYS)){//keys 级别 找主 - for(LBRedisServerBean ffanRedisServerBean:ffanRedisServerMasterCluster.getMasters()){ - AbstractPoolClient ffanRedisClient=ffanRedisServerBeanMap.get(ffanRedisServerBean.getKey()); - ffanRedisClient.write(request,callback); + if(ffanRedisServerMasterCluster.getMasters().size()==1){ + AbstractPoolClient ffanRedisClient=ffanRedisServerBeanMap.get(ffanRedisServerMasterCluster.getMasters().get(0).getKey()); + ffanRedisClient.write(request,ctx); + }else{ + ctx.channel().writeAndFlush(new ErrorRedisReply(ProtoUtils.buildErrorReplyBytes("not support command:"+command))); } - }else if(request!=null&&request.getArgs().size()==1){//info 级别 - for(String key:ffanRedisServerBeanMap.keySet()){ - AbstractPoolClient ffanRedisClient=ffanRedisServerBeanMap.get(key); - ffanRedisClient.write(request,callback); + + }else if(request!=null&&request.getArgs().size()==1&&command.equals(RedisConstants.INFO)){//info 级别 + if(ffanRedisServerBeanMap.size()==1){ + for(String key:ffanRedisServerBeanMap.keySet()){ + AbstractPoolClient ffanRedisClient=ffanRedisServerBeanMap.get(key); + ffanRedisClient.write(request,ctx); + } + }else{ + ctx.channel().writeAndFlush(new ErrorRedisReply(ProtoUtils.buildErrorReplyBytes("not support command:"+command))); } + }else{ + ctx.channel().writeAndFlush(new ErrorRedisReply(ProtoUtils.buildErrorReplyBytes("unknown command:"+command))); } - }finally{ - ReferenceCountUtil.release(request); - } - + + } - /** * 根据参数获取枚举类 * @param command @@ -116,9 +117,9 @@ public class LBRedisServerHandler extends SimpleChannelInboundHandler ffanRedisServerBeans=ffanRedisServerMasterCluster.getMasterFfanRedisServerBean(ffanRedisServerBean.getKey()); if(ffanRedisServerBeans!=null&&ffanRedisServerBeans.size()>0){ - LBRedisServerClusterBean ffanRedisServerClusterBean= ffanRedisServerMasterCluster.getFfanRedisServerClusterBean(ffanRedisServerBean.getKey()); - if(ffanRedisServerClusterBean!=null){ - LoadBalance loadClusterBalance=ffanRedisServerClusterBean.getLoadClusterBalance(); + LBRedisServerClusterBean redisServerClusterBean= ffanRedisServerMasterCluster.getRedisServerClusterBean(ffanRedisServerBean.getKey()); + if(redisServerClusterBean!=null){ + LoadBalance loadClusterBalance=redisServerClusterBean.getLoadClusterBalance(); loadClusterBalance.setFfanRedisServerMasterCluster(ffanRedisServerMasterCluster); redisQuestBean.setWrite(flag); LBRedisServerBean ffanClusterRedisServerBean=loadClusterBalance.select(redisQuestBean, ffanRedisServerBean); @@ -140,17 +141,24 @@ public class LBRedisServerHandler extends SimpleChannelInboundHandler currentChilds) throws Exception { - String value=zkClient.readData(parentPath, true); + String parentValue=zkClient.readData(parentPath, true); currentChilds=zkClient.getChildren(parentPath); - List childDatas=new ArrayList(); + Map slaveMap=new HashMap(); if(currentChilds!=null){ for(String currentChild:currentChilds){ StringBuilder sbStringBuilder=new StringBuilder(); sbStringBuilder.append(parentPath).append("/").append(currentChild); String result=zkClient.readData(sbStringBuilder.toString(), true); - childDatas.add(result); + slaveMap.put(currentChild, result); } } - registryListen.handleChildChange(value,parentPath, childDatas); + registryListen.handleChildChange(parentPath,parentValue,currentChilds, slaveMap); } + }); } }catch(Exception e){ @@ -128,8 +130,7 @@ public class ZookeeperRegistry extends AbstractRegistry { List childRen=new ArrayList(); if (zkClient.exists(nodePath)) { try{ - childRen=zkClient.getChildren(nodePath); - + childRen=zkClient.getChildren(nodePath); }catch(Exception e){ } @@ -162,4 +163,52 @@ public class ZookeeperRegistry extends AbstractRegistry { String result=zkClient.readData(path, flag); return result; } + + @Override + public Boolean exist(String nodepath) { + return zkClient.exists(nodepath); + } + + /** + * 监控主节点的变化 + */ + @Override + protected void doRegisterSlave(RedisProxyURL url, + final IRegistryListen registryListen) { + try{ + serverLock.lock(); + String nodeTypePath = ZkUtils.toNodePath(url,url.getParentServerPath(), ZkNodeType.AVAILABLE_SERVER); + if(url.getParentServerPath()!=null){ + zkClient.subscribeDataChanges(nodeTypePath, new IZkDataListener() { + @Override + public void handleDataDeleted(String dataPath) throws Exception { + //主节点不可能删除 + } + @Override + public void handleDataChange(String dataPath, Object data) throws Exception { + registryListen.handleSlaveDataChange(dataPath, data); + } + }); + } + }catch(Exception e){ + throw new LBRedisProxyFrameworkException(String.format("Failed to register %s to zookeeper(%s), cause: %s", url, getRedisProxyURL(), e.getMessage()), e); + }finally{ + serverLock.unlock(); + } + } + + /** + * 监听从节点变化 + */ + @Override + protected void doUnregisterSlave(RedisProxyURL url) { + try { + clientLock.lock(); + removeNode(url, ZkNodeType.AVAILABLE_SERVER); + } catch (Throwable e) { + throw new LBRedisProxyFrameworkException(String.format("Failed to unregister %s to zookeeper(%s), cause: %s", url, getRedisProxyURL(), e.getMessage()), e); + } finally { + clientLock.unlock(); + } + } } diff --git a/nredis-proxy-register/src/main/java/com/opensource/netty/redis/proxy/zk/registry/ZookeeperRegistryFactory.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/zk/registry/ZookeeperRegistryFactory.java similarity index 91% rename from nredis-proxy-register/src/main/java/com/opensource/netty/redis/proxy/zk/registry/ZookeeperRegistryFactory.java rename to nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/zk/registry/ZookeeperRegistryFactory.java index 48015fb4c769e47619493a2b5fe7a97b71f2ade7..4c011c3a1c4f313a6191dd85d8e220b9680db6b1 100644 --- a/nredis-proxy-register/src/main/java/com/opensource/netty/redis/proxy/zk/registry/ZookeeperRegistryFactory.java +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/zk/registry/ZookeeperRegistryFactory.java @@ -5,6 +5,7 @@ package com.opensource.netty.redis.proxy.zk.registry; import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zkclient.exception.ZkException; +import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; import com.opensource.netty.redis.proxy.core.enums.RedisProxyParamType; import com.opensource.netty.redis.proxy.core.log.impl.LoggerUtils; import com.opensource.netty.redis.proxy.core.registry.Registry; @@ -28,7 +29,7 @@ public class ZookeeperRegistryFactory extends AbstractRegistryFactory { int sessionTimeout = redisProxyURL.getIntParameter(RedisProxyParamType.registrySessionTimeout.getName(), RedisProxyParamType.registrySessionTimeout.getIntValue()); - ZkClient zkClient = new ZkClient(redisProxyURL.getParameter("address"), sessionTimeout, timeout); + ZkClient zkClient = new ZkClient(redisProxyURL.getParameter(RedisConstants.ADDRESS), sessionTimeout, timeout); return new ZookeeperRegistry(redisProxyURL, zkClient); } catch (ZkException e) { LoggerUtils.error("[ZookeeperRegistry] fail to connect zookeeper, cause: " + e.getMessage()); diff --git a/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/zk/registry/listen/ZookeeperRegistryListen.java b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/zk/registry/listen/ZookeeperRegistryListen.java new file mode 100644 index 0000000000000000000000000000000000000000..587246146a3582a2578de670644edb657efeda2c --- /dev/null +++ b/nredis-proxy-net/src/main/java/com/opensource/netty/redis/proxy/zk/registry/listen/ZookeeperRegistryListen.java @@ -0,0 +1,357 @@ +/** + * + */ +package com.opensource.netty.redis.proxy.zk.registry.listen; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.alibaba.fastjson.JSONObject; +import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; +import com.opensource.netty.redis.proxy.commons.utils.StringUtils; +import com.opensource.netty.redis.proxy.core.config.LBRedisServerMasterCluster; +import com.opensource.netty.redis.proxy.core.config.RedisPoolConfig; +import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; +import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerClusterBean; +import com.opensource.netty.redis.proxy.core.listen.impl.AbstractRegistryListenImpl; +import com.opensource.netty.redis.proxy.core.log.impl.LoggerUtils; +import com.opensource.netty.redis.proxy.net.client.LBRedisClient; +import com.opensource.netty.redis.proxy.pool.commons.LBRedisProxyPoolConfig; + +/** + * @author liubing + * + */ +public class ZookeeperRegistryListen extends AbstractRegistryListenImpl { + + + public ZookeeperRegistryListen(LBRedisServerMasterCluster ffanRedisServerMasterCluster) { + super(ffanRedisServerMasterCluster); + } + + /* (non-Javadoc) + * 监控主数据的变化 + * @see com.wanda.ffan.redis.proxy.core.listen.IRegistryListen#handleDataChange(java.lang.String, java.lang.Object) + */ + @Override + public void handleDataChange(String dataPath, Object data) { + int index=dataPath.lastIndexOf(RedisConstants.PATH_SEPARATOR); + dataPath=dataPath.substring(index+1, dataPath.length()); + dataPath=dataPath.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); + StringBuffer sbBuffer=new StringBuffer(); + sbBuffer.append(RedisConstants.REDIS_PROXY).append(dataPath); + JSONObject jsonObject=JSONObject.parseObject(String.valueOf(data)); + updateRedisMasterBean(sbBuffer.toString(), jsonObject); + } + + + /** + * 更改指定的主 + * @param key + * @param ffanRedisMasterBean + */ + private void updateRedisMasterBean(String key,JSONObject jsonObject){ + LBRedisServerBean newRedisMasterBean =new LBRedisServerBean(); + List redisServerMasterBeans=new ArrayList(); + for(LBRedisServerBean redisServerBean :super.getFfanRedisServerMasterCluster().getMasters()){ + if(redisServerBean.getKey().equals(key)){ + RedisPoolConfig redisPoolConfig=redisServerBean.getRedisPoolConfig(); + newRedisMasterBean.setHost(jsonObject.getString("host")); + newRedisMasterBean.setPort(jsonObject.getIntValue("port")); + newRedisMasterBean.setRedisPoolConfig(redisPoolConfig); + redisServerMasterBeans.add(newRedisMasterBean); + //break; + }else{ + redisServerMasterBeans.add(redisServerBean); + } + } + super.getFfanRedisServerMasterCluster().setMasters(redisServerMasterBeans); + //-------------------------------------------------------------------------- + for(LBRedisServerClusterBean ffanRedisServerClusterBean:super.getFfanRedisServerMasterCluster().getRedisServerClusterBeans()){ + if(ffanRedisServerClusterBean.getRedisServerMasterBean().getKey().equals(key)){ + ffanRedisServerClusterBean.setRedisServerMasterBean(newRedisMasterBean); + break; + } + } + //-------------------------------------------------------------------------- + if(super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().containsKey(key)){ + LBRedisServerClusterBean ffanRedisServerClusterBean=super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().get(key); + if(ffanRedisServerClusterBean.getRedisServerMasterBean().getKey().equals(key)){ + ffanRedisServerClusterBean.setRedisServerMasterBean(newRedisMasterBean); + } + } + //-------------------------------------------------------------------------- + if(!super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().containsKey(newRedisMasterBean.getKey())){ + //初始化新的客户端 + LBRedisProxyPoolConfig lbRedisProxyPoolConfig=convertLBRedisProxyPoolConfig(newRedisMasterBean); + LBRedisClient ffanRedisClient=new LBRedisClient(lbRedisProxyPoolConfig); + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().put(newRedisMasterBean.getKey(), ffanRedisClient); + } + if(super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().containsKey(key)){ + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().get(key).close(); + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().remove(key); + } + } + + /** + * 监控主对应从的变化 + */ + @Override + public void handleChildChange(String parentPath,String data,List childPaths, Map slaveMap) { + if(StringUtils.isBlank(data)){ + LoggerUtils.warn("this master had been deleted,path:"+parentPath); + }else{ + if(childPaths==null||childPaths.size()==0){//有主节点,无从节点 + int index=parentPath.lastIndexOf(RedisConstants.PATH_SEPARATOR); + parentPath=parentPath.substring(index+1, parentPath.length()); + parentPath=parentPath.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); + StringBuffer sbBuffer=new StringBuffer(); + sbBuffer.append(RedisConstants.REDIS_PROXY).append(parentPath); + super.getFfanRedisServerMasterCluster().removeSlavesByMaster(sbBuffer.toString()); + }else{//有主有从节点 + Map jsonSlaveDatas=new HashMap(); + for(String childData:childPaths){//childData 格式 IP:port + childData=childData.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); + StringBuffer sbBuffer=new StringBuffer(); + sbBuffer.append(RedisConstants.REDIS_PROXY).append(parentPath); + JSONObject jsonObject=JSONObject.parseObject(slaveMap.get(childData)); + jsonSlaveDatas.put(sbBuffer.toString(), jsonObject); + } + updateFfanRedisClusterBeans(parentPath, jsonSlaveDatas); + } + } + } + + + /** + * 更新指定主的从 + * @param parentKey 父节点路径 + * @param jsonSlaveDatas 子节点路径对应值 + */ + private void updateFfanRedisClusterBeans(String parentKey,Map jsonSlaveDatas){ + + operateSlaveMasterClusters(parentKey, jsonSlaveDatas); + //--------------------------------------------------------------------------------------------- + operateRedisServerClusterBeanMap(parentKey, jsonSlaveDatas); + //------------------------------------------------------------------------------------------------ + operateRedisServerClusterBeans(parentKey, jsonSlaveDatas); + //------------------------------------------------------------------------------------------------ + operateRedisClientBeanMap(parentKey); + } + + private void operateRedisClientBeanMap(String parentKey){ + List redisServerSlaveBeans= super.getFfanRedisServerMasterCluster().getMasterClusters().get(parentKey); + + if(redisServerSlaveBeans!=null&&redisServerSlaveBeans.size()>0){ + for(LBRedisServerBean lbRedisServerBean:redisServerSlaveBeans){ + if(!super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().containsKey(lbRedisServerBean.getKey())){ + LBRedisProxyPoolConfig lbRedisProxyPoolConfig=convertLBRedisProxyPoolConfig(lbRedisServerBean); + LBRedisClient ffanRedisClient=new LBRedisClient(lbRedisProxyPoolConfig); + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().put(lbRedisServerBean.getKey(), ffanRedisClient); + } + } + } + + } + + private void operateSlaveMasterClusters(String parentKey,Map jsonSlaveDatas){ + List redisServerSlaveBeans=super.getFfanRedisServerMasterCluster().getMasterClusters().get(parentKey); + if(redisServerSlaveBeans!=null){ + List newRedisServerSlaveBeans=new ArrayList(); + for(LBRedisServerBean ffanRedisServerBean:redisServerSlaveBeans){ + if(jsonSlaveDatas.containsKey(ffanRedisServerBean.getKey())){//不包含 + JSONObject jsonObject=jsonSlaveDatas.get(ffanRedisServerBean.getKey()); + LBRedisServerBean newRedisMasterBean =new LBRedisServerBean(); + RedisPoolConfig redisPoolConfig=ffanRedisServerBean.getRedisPoolConfig(); + newRedisMasterBean.setHost(jsonObject.getString("host")); + newRedisMasterBean.setPort(jsonObject.getIntValue("port")); + newRedisMasterBean.setWeight(jsonObject.getIntValue("weight")); + newRedisMasterBean.setRedisPoolConfig(redisPoolConfig); + newRedisServerSlaveBeans.add(newRedisMasterBean); + } + } + if(newRedisServerSlaveBeans.size()>0){//具有值 + super.getFfanRedisServerMasterCluster().getMasterClusters().put(parentKey, newRedisServerSlaveBeans); + }else{//无任何值 + super.getFfanRedisServerMasterCluster().getMasterClusters().remove(parentKey); + } + } + } + + private void operateRedisServerClusterBeanMap(String parentKey,Map jsonSlaveDatas){ + if(super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().containsKey(parentKey)){ + LBRedisServerClusterBean ffanRedisServerClusterBean=super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().get(parentKey); + if(ffanRedisServerClusterBean.getRedisServerMasterBean().getKey().equals(parentKey)&&ffanRedisServerClusterBean.getRedisServerSlaveBeans()!=null&&ffanRedisServerClusterBean.getRedisServerSlaveBeans().size()>0){//父节点相同 + List redisServerSlaveBeans1=ffanRedisServerClusterBean.getRedisServerSlaveBeans(); + List newRedisServerSlaveBeans1=new ArrayList(); + for(LBRedisServerBean ffanRedisServerBean:redisServerSlaveBeans1){ + if(jsonSlaveDatas.containsKey(ffanRedisServerBean.getKey())){//包含 + JSONObject jsonObject=jsonSlaveDatas.get(ffanRedisServerBean.getKey()); + LBRedisServerBean newRedisMasterBean =new LBRedisServerBean(); + RedisPoolConfig redisPoolConfig=ffanRedisServerBean.getRedisPoolConfig(); + newRedisMasterBean.setHost(jsonObject.getString("host")); + newRedisMasterBean.setPort(jsonObject.getIntValue("port")); + newRedisMasterBean.setWeight(jsonObject.getIntValue("weight")); + newRedisMasterBean.setRedisPoolConfig(redisPoolConfig); + newRedisServerSlaveBeans1.add(newRedisMasterBean); + } + } + if(newRedisServerSlaveBeans1.size()>0){//具有值 + super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().get(parentKey).setRedisServerSlaveBeans(newRedisServerSlaveBeans1); + }else{//无任何值 + super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().get(parentKey).getRedisServerSlaveBeans().clear(); + } + } + } + } + + private void operateRedisServerClusterBeans(String parentKey,Map jsonSlaveDatas){ + if(super.getFfanRedisServerMasterCluster().getRedisServerClusterBeans()!=null&&super.getFfanRedisServerMasterCluster().getRedisServerClusterBeans().size()>0){ + for(LBRedisServerClusterBean ffanRedisServerClusterBean:super.getFfanRedisServerMasterCluster().getRedisServerClusterBeans()){ + if(ffanRedisServerClusterBean.getRedisServerMasterBean().getKey().equals(parentKey)&&ffanRedisServerClusterBean.getRedisServerSlaveBeans()!=null&&ffanRedisServerClusterBean.getRedisServerSlaveBeans().size()>0){//父节点相同 + List redisServerSlaveBeans2=ffanRedisServerClusterBean.getRedisServerSlaveBeans(); + List newRedisServerSlaveBeans2=new ArrayList(); + for(LBRedisServerBean ffanRedisServerBean:redisServerSlaveBeans2){//删除挂掉的从 + if(jsonSlaveDatas.containsKey(ffanRedisServerBean.getKey())){//不包含 + JSONObject jsonObject=jsonSlaveDatas.get(ffanRedisServerBean.getKey()); + LBRedisServerBean newRedisMasterBean =new LBRedisServerBean(); + RedisPoolConfig redisPoolConfig=ffanRedisServerBean.getRedisPoolConfig(); + newRedisMasterBean.setHost(jsonObject.getString("host")); + newRedisMasterBean.setPort(jsonObject.getIntValue("port")); + newRedisMasterBean.setWeight(jsonObject.getIntValue("weight")); + newRedisMasterBean.setRedisPoolConfig(redisPoolConfig); + newRedisServerSlaveBeans2.add(newRedisMasterBean); + } + } + if(newRedisServerSlaveBeans2.size()>0){//具有值 + ffanRedisServerClusterBean.setRedisServerSlaveBeans(newRedisServerSlaveBeans2); + }else{//无任何值 + ffanRedisServerClusterBean.getRedisServerSlaveBeans().clear(); + } + } + } + } + } + + /** + * 转换 + * @param lbRedisServerBean + * @return + */ + private LBRedisProxyPoolConfig convertLBRedisProxyPoolConfig(LBRedisServerBean lbRedisServerBean){ + LBRedisProxyPoolConfig lbRedisProxyPoolConfig=new LBRedisProxyPoolConfig(); + RedisPoolConfig redisPoolConfig= lbRedisServerBean.getRedisPoolConfig(); + lbRedisProxyPoolConfig.setConnectionTimeout(redisPoolConfig.getConnectionTimeout()); + lbRedisProxyPoolConfig.setHost(lbRedisServerBean.getHost()); + lbRedisProxyPoolConfig.setInitialEntries(redisPoolConfig.getInitialConnection()); + lbRedisProxyPoolConfig.setMaxActiveEntries(redisPoolConfig.getMaxActiveConnection()); + lbRedisProxyPoolConfig.setMaxWaitMillisOnBorrow(redisPoolConfig.getMaxWaitMillisOnBorrow()); + lbRedisProxyPoolConfig.setMinActiveEntries(redisPoolConfig.getMinConnection()); + lbRedisProxyPoolConfig.setMinEvictableIdleTimeMillis(redisPoolConfig.getMinEvictableIdleTimeMillis()); + lbRedisProxyPoolConfig.setMinIdleEntries(redisPoolConfig.getMinIdleEntries()); + lbRedisProxyPoolConfig.setPort(lbRedisServerBean.getPort()); + lbRedisProxyPoolConfig.setTestOnBorrow(redisPoolConfig.isTestOnBorrow()); + lbRedisProxyPoolConfig.setTestOnReturn(redisPoolConfig.isTestOnReturn()); + lbRedisProxyPoolConfig.setTestWhileIdle(redisPoolConfig.isTestWhileIdle()); + lbRedisProxyPoolConfig.setTimeBetweenEvictionRunsMillis(redisPoolConfig.getTimeBetweenEvictionRunsMillis()); + return lbRedisProxyPoolConfig; + } + + @Override + public void handleSlaveDataChange(String dataPath, Object data) { + int index=dataPath.lastIndexOf(RedisConstants.PATH_SEPARATOR); + dataPath=dataPath.substring(index+1, dataPath.length()); + dataPath=dataPath.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); + StringBuffer sbBuffer=new StringBuffer(); + sbBuffer.append(RedisConstants.REDIS_PROXY).append(dataPath); + JSONObject jsonObject=JSONObject.parseObject(String.valueOf(data)); + updateRedisSlaveBean(sbBuffer.toString(), jsonObject); + } + + + /** + * 更改指定的从 + * @param key + * @param ffanRedisMasterBean + */ + private void updateRedisSlaveBean(String key,JSONObject jsonObject){ + LBRedisServerBean newRedisSlaveBean =new LBRedisServerBean(); + + for(LBRedisServerClusterBean ffanRedisServerClusterBean:super.getFfanRedisServerMasterCluster().getRedisServerClusterBeans()){ + List redisServerSlaveBeans=new ArrayList(); + for(LBRedisServerBean redisServerBean:ffanRedisServerClusterBean.getRedisServerSlaveBeans()){ + if(redisServerBean.getKey().equals(key)){ + RedisPoolConfig redisPoolConfig=redisServerBean.getRedisPoolConfig(); + newRedisSlaveBean.setHost(jsonObject.getString("host")); + newRedisSlaveBean.setPort(jsonObject.getIntValue("port")); + newRedisSlaveBean.setWeight(jsonObject.getIntValue("weight")); + newRedisSlaveBean.setRedisPoolConfig(redisPoolConfig); + redisServerSlaveBeans.add(newRedisSlaveBean); + }else{ + redisServerSlaveBeans.add(redisServerBean); + } + } + ffanRedisServerClusterBean.setRedisServerSlaveBeans(redisServerSlaveBeans); + } + + //-------------------------------------------------------------------------- + for(String key1:super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().keySet()){ + LBRedisServerClusterBean ffanRedisServerClusterBean=super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().get(key1); + if(ffanRedisServerClusterBean!=null){ + List redisServerSlaveBeans=new ArrayList(); + for(LBRedisServerBean redisServerBean:ffanRedisServerClusterBean.getRedisServerSlaveBeans()){ + if(redisServerBean.getKey().equals(key)){ + RedisPoolConfig redisPoolConfig=redisServerBean.getRedisPoolConfig(); + newRedisSlaveBean.setHost(jsonObject.getString("host")); + newRedisSlaveBean.setPort(jsonObject.getIntValue("port")); + newRedisSlaveBean.setWeight(jsonObject.getIntValue("weight")); + newRedisSlaveBean.setRedisPoolConfig(redisPoolConfig); + redisServerSlaveBeans.add(newRedisSlaveBean); + }else{ + redisServerSlaveBeans.add(redisServerBean); + } + } + ffanRedisServerClusterBean.setRedisServerSlaveBeans(redisServerSlaveBeans); + super.getFfanRedisServerMasterCluster().getRedisServerClusterBeanMap().put(key1, ffanRedisServerClusterBean); + } + + } + + //-------------------------------------------------------------------------- + if(!super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().containsKey(newRedisSlaveBean.getKey())){ + //初始化新的客户端 + LBRedisProxyPoolConfig lbRedisProxyPoolConfig=convertLBRedisProxyPoolConfig(newRedisSlaveBean); + LBRedisClient ffanRedisClient=new LBRedisClient(lbRedisProxyPoolConfig); + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().put(newRedisSlaveBean.getKey(), ffanRedisClient); + } + if(super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().containsKey(key)){ + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().get(key).close(); + super.getFfanRedisServerMasterCluster().getRedisClientBeanMap().remove(key); + } + //----------------------------------------------------------------------------- + for(String key2:super.getFfanRedisServerMasterCluster().getMasterClusters().keySet()){ + List slaveServerBeans=super.getFfanRedisServerMasterCluster().getMasterClusters().get(key2); + if(slaveServerBeans!=null){ + List redisServerSlaveBeans=new ArrayList(); + for(LBRedisServerBean redisServerBean:slaveServerBeans){ + if(redisServerBean.getKey().equals(key)){ + RedisPoolConfig redisPoolConfig=redisServerBean.getRedisPoolConfig(); + newRedisSlaveBean.setHost(jsonObject.getString("host")); + newRedisSlaveBean.setPort(jsonObject.getIntValue("port")); + newRedisSlaveBean.setWeight(jsonObject.getIntValue("weight")); + newRedisSlaveBean.setRedisPoolConfig(redisPoolConfig); + redisServerSlaveBeans.add(newRedisSlaveBean); + }else{ + redisServerSlaveBeans.add(redisServerBean); + } + } + super.getFfanRedisServerMasterCluster().getMasterClusters().put(key2, redisServerSlaveBeans); + } + + + } + + } +} diff --git a/nredis-proxy-pool/src/main/java/com/opensource/netty/redis/proxy/pool/impl/LBRedisProxyBasicPool.java b/nredis-proxy-pool/src/main/java/com/opensource/netty/redis/proxy/pool/impl/LBRedisProxyBasicPool.java index 06084087788d30b22eb640ef44674a9cfe57ee1f..ae0804ddc7a9b09150b7e330608ef99adb8e4074 100644 --- a/nredis-proxy-pool/src/main/java/com/opensource/netty/redis/proxy/pool/impl/LBRedisProxyBasicPool.java +++ b/nredis-proxy-pool/src/main/java/com/opensource/netty/redis/proxy/pool/impl/LBRedisProxyBasicPool.java @@ -121,6 +121,7 @@ public class LBRedisProxyBasicPool implements LBRedisProxyPool implements LBRedisProxyPool 0) { + + while (this.idleEntriesQueue.getIdleEntriesCount() > 0) { LBRedisProxyPoolEntry entry = idleEntriesQueue.poll(); if (entry != null) { decreaseObject(entry); } } + //idleEntriesQueue.clear(); shuttingDown=true; } diff --git a/nredis-proxy-register/.classpath b/nredis-proxy-register/.classpath deleted file mode 100644 index a67314913efb2ec1ed7b610ce7c35f6ef3018d1f..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.classpath +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/nredis-proxy-register/.gitignore b/nredis-proxy-register/.gitignore deleted file mode 100644 index b83d22266ac8aa2f8df2edef68082c789727841d..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/nredis-proxy-register/.project b/nredis-proxy-register/.project deleted file mode 100644 index b1492238f4e54d22576358663cc06600c14e0ec3..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.project +++ /dev/null @@ -1,36 +0,0 @@ - - - nredis-proxy-register - - - - - - org.eclipse.wst.common.project.facet.core.builder - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.wst.validation.validationbuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.wst.common.modulecore.ModuleCoreNature - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - org.eclipse.wst.common.project.facet.core.nature - - diff --git a/nredis-proxy-register/.settings/org.eclipse.core.resources.prefs b/nredis-proxy-register/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index e9441bb123ec3e1ab029c7eac896bc45681d9a71..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,3 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding/=UTF-8 diff --git a/nredis-proxy-register/.settings/org.eclipse.jdt.core.prefs b/nredis-proxy-register/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 443e08599a2e61e27329fbffd44d862d8675dde4..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,8 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.7 diff --git a/nredis-proxy-register/.settings/org.eclipse.m2e.core.prefs b/nredis-proxy-register/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb2389f85fe6381425d29f0a9866fb65..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/nredis-proxy-register/.settings/org.eclipse.wst.common.component b/nredis-proxy-register/.settings/org.eclipse.wst.common.component deleted file mode 100644 index 7cf792c9e77cd10d88a5fc2ca0e03860548d8622..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.settings/org.eclipse.wst.common.component +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/nredis-proxy-register/.settings/org.eclipse.wst.common.project.facet.core.xml b/nredis-proxy-register/.settings/org.eclipse.wst.common.project.facet.core.xml deleted file mode 100644 index 1b22d705a50f9dc88531be25f5affcb3946da29d..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.settings/org.eclipse.wst.common.project.facet.core.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/nredis-proxy-register/.settings/org.eclipse.wst.validation.prefs b/nredis-proxy-register/.settings/org.eclipse.wst.validation.prefs deleted file mode 100644 index 04cad8cb752a9761c4e5167d0301d3a27674430f..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/.settings/org.eclipse.wst.validation.prefs +++ /dev/null @@ -1,2 +0,0 @@ -disabled=06target -eclipse.preferences.version=1 diff --git a/nredis-proxy-register/pom.xml b/nredis-proxy-register/pom.xml deleted file mode 100644 index 5305f2725d5f432dc09d06bc2e86df94a17d703b..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/pom.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - 4.0.0 - - com.opensource - nredis-proxy - 1.0 - - com.opensource - nredis-proxy-register - 1.0 - nredis-proxy-register - http://maven.apache.org - - UTF-8 - - - - junit - junit - test - - - org.apache.zookeeper - zookeeper - - - com.opensource - nredis-proxy-core - - - com.alibaba - fastjson - - - diff --git a/nredis-proxy-register/src/main/java/com/opensource/netty/redis/proxy/zk/registry/listen/ZookeeperRegistryListen.java b/nredis-proxy-register/src/main/java/com/opensource/netty/redis/proxy/zk/registry/listen/ZookeeperRegistryListen.java deleted file mode 100644 index 731c40ca1352d6c29914f659d5bdd5556fdf4478..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/com/opensource/netty/redis/proxy/zk/registry/listen/ZookeeperRegistryListen.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.zk.registry.listen; - -import java.util.ArrayList; -import java.util.List; - -import com.alibaba.fastjson.JSONObject; -import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; -import com.opensource.netty.redis.proxy.commons.utils.StringUtils; -import com.opensource.netty.redis.proxy.core.config.LBRedisServerMasterCluster; -import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; -import com.opensource.netty.redis.proxy.core.listen.impl.AbstractRegistryListenImpl; -import com.opensource.netty.redis.proxy.core.log.impl.LoggerUtils; - -/** - * @author liubing - * - */ -public class ZookeeperRegistryListen extends AbstractRegistryListenImpl { - - - public ZookeeperRegistryListen(LBRedisServerMasterCluster ffanRedisServerMasterCluster) { - super(ffanRedisServerMasterCluster); - } - - /* (non-Javadoc) - * @see com.wanda.ffan.redis.proxy.core.listen.IRegistryListen#handleDataChange(java.lang.String, java.lang.Object) - */ - @Override - public void handleDataChange(String dataPath, Object data) { - //LoggerUtils.info("dataPath:"+dataPath+",data:"+data); - int index=dataPath.lastIndexOf(RedisConstants.PATH_SEPARATOR); - dataPath=dataPath.substring(index+1, dataPath.length()); - dataPath=dataPath.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); - StringBuffer sbBuffer=new StringBuffer(); - sbBuffer.append(RedisConstants.REDIS_PROXY).append(dataPath); - LBRedisServerBean ffanRedisServerBean=JSONObject.parseObject(String.valueOf(data), LBRedisServerBean.class); - super.getFfanRedisServerMasterCluster().updateFfanRedisMasterBean(sbBuffer.toString(), ffanRedisServerBean); - - } - - /* (non-Javadoc) - * @see com.wanda.ffan.redis.proxy.core.listen.IRegistryListen#handleChildChange(java.lang.String, java.util.List) - */ - @Override - public void handleChildChange(String data,String dataPath, List ChildDatas) { - if(StringUtils.isBlank(data)){ - LoggerUtils.warn("this master had been deleted,path:"+dataPath); - }else{ - int index=dataPath.lastIndexOf(RedisConstants.PATH_SEPARATOR); - dataPath=dataPath.substring(index+1, dataPath.length()); - dataPath=dataPath.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); - StringBuffer sbBuffer=new StringBuffer(); - sbBuffer.append(RedisConstants.REDIS_PROXY).append(dataPath); - List ffanRedisClusterBeans=new ArrayList(); - if(ChildDatas!=null&&ChildDatas.size()>0){//有从,从的值发生改变 - for(String childData:ChildDatas){ - LBRedisServerBean ffanRedisServerBean=JSONObject.parseObject(childData, LBRedisServerBean.class); - ffanRedisClusterBeans.add(ffanRedisServerBean); - } - super.getFfanRedisServerMasterCluster().updateFfanRedisClusterBeans(sbBuffer.toString(), ffanRedisClusterBeans); - }else{//有主无从 - super.getFfanRedisServerMasterCluster().removeSlavesByMaster(sbBuffer.toString()); - } - } - } - - /* (non-Javadoc) - * @see com.wanda.ffan.redis.proxy.core.listen.IRegistryListen#handleDataDeleted(java.lang.String) - * 主节点删除的变化 - */ - @Override - public void handleDataDeleted(String dataPath) { - int index=dataPath.lastIndexOf(RedisConstants.PATH_SEPARATOR); - dataPath=dataPath.substring(index+1, dataPath.length()); - dataPath=dataPath.replace(RedisConstants.PROTOCOL_SEPARATOR, RedisConstants.SEPERATOR_ACCESS_LOG); - StringBuffer sbBuffer=new StringBuffer(); - sbBuffer.append(RedisConstants.REDIS_PROXY).append(dataPath); - super.getFfanRedisServerMasterCluster().remove(sbBuffer.toString()); - } - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ContentWatcher.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ContentWatcher.java deleted file mode 100644 index 3c4a295ec79dc5b00ea504935ce16b01009b9614..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ContentWatcher.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import org.I0Itec.zkclient.exception.ZkNoNodeException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @param - * The data type that is being watched. - */ -public final class ContentWatcher implements IZkDataListener { - - private static final Logger LOG = LoggerFactory.getLogger(ContentWatcher.class); - - private Lock _contentLock = new ReentrantLock(true); - private Condition _contentAvailable = _contentLock.newCondition(); - - private Holder _content; - private String _fileName; - private ZkClient _zkClient; - - public ContentWatcher(ZkClient zkClient, String fileName) { - _fileName = fileName; - _zkClient = zkClient; - } - - public void start() { - _zkClient.subscribeDataChanges(_fileName, this); - readData(); - LOG.debug("Started ContentWatcher"); - } - - @SuppressWarnings("unchecked") - private void readData() { - try { - setContent((T) _zkClient.readData(_fileName)); - } catch (ZkNoNodeException e) { - // ignore if the node has not yet been created - } - } - - public void stop() { - _zkClient.unsubscribeDataChanges(_fileName, this); - } - - public void setContent(T data) { - LOG.debug("Received new data: " + data); - _contentLock.lock(); - try { - _content = new Holder(data); - _contentAvailable.signalAll(); - } finally { - _contentLock.unlock(); - } - } - - @Override - @SuppressWarnings("unchecked") - public void handleDataChange(String dataPath, Object data) { - setContent((T) data); - } - - @Override - public void handleDataDeleted(String dataPath) { - // ignore - } - - public T getContent() throws InterruptedException { - _contentLock.lock(); - try { - while (_content == null) { - _contentAvailable.await(); - } - return _content.get(); - } finally { - _contentLock.unlock(); - } - } - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/DataUpdater.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/DataUpdater.java deleted file mode 100644 index 2395f3c7235f0f2fb083f44510c6550fb7541afb..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/DataUpdater.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -/** - * Updates the data of a znode. This is used together with {@link ZkClient#updateDataSerialized(String, DataUpdater)}. - * - * @param - */ -public interface DataUpdater { - - /** - * Updates the current data of a znode. - * - * @param currentData - * The current contents. - * @return the new data that should be written back to ZooKeeper. - */ - public T update(T currentData); - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/DistributedQueue.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/DistributedQueue.java deleted file mode 100644 index ac2588c3957f3888b8ea2ece430702c070960504..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/DistributedQueue.java +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.io.Serializable; -import java.util.List; - -import org.I0Itec.zkclient.exception.ZkNoNodeException; - -public class DistributedQueue { - - private static class Element { - private String _name; - private T _data; - - public Element(String name, T data) { - _name = name; - _data = data; - } - - public String getName() { - return _name; - } - - public T getData() { - return _data; - } - } - - private ZkClient _zkClient; - private String _root; - - private static final String ELEMENT_NAME = "element"; - - public DistributedQueue(ZkClient zkClient, String root) { - _zkClient = zkClient; - _root = root; - } - - public boolean offer(T element) { - try { - _zkClient.createPersistentSequential(_root + "/" + ELEMENT_NAME + "-", element); - } catch (Exception e) { - throw ExceptionUtil.convertToRuntimeException(e); - } - return true; - } - - public T poll() { - while (true) { - Element element = getFirstElement(); - if (element == null) { - return null; - } - - try { - _zkClient.delete(element.getName()); - return element.getData(); - } catch (ZkNoNodeException e) { - // somebody else picked up the element first, so we have to - // retry with the new first element - } catch (Exception e) { - throw ExceptionUtil.convertToRuntimeException(e); - } - } - } - - private String getSmallestElement(List list) { - String smallestElement = list.get(0); - for (String element : list) { - if (element.compareTo(smallestElement) < 0) { - smallestElement = element; - } - } - - return smallestElement; - } - - public boolean isEmpty() { - return _zkClient.getChildren(_root).size() == 0; - } - - @SuppressWarnings("unchecked") - private Element getFirstElement() { - try { - while (true) { - List list = _zkClient.getChildren(_root); - if (list.size() == 0) { - return null; - } - String elementName = getSmallestElement(list); - - try { - return new Element(_root + "/" + elementName, (T) _zkClient.readData(_root + "/" + elementName)); - } catch (ZkNoNodeException e) { - // somebody else picked up the element first, so we have to - // retry with the new first element - } - } - } catch (Exception e) { - throw ExceptionUtil.convertToRuntimeException(e); - } - } - - public T peek() { - Element element = getFirstElement(); - if (element == null) { - return null; - } - return element.getData(); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ExceptionUtil.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ExceptionUtil.java deleted file mode 100644 index f72e3ea10aadd0c7c007e51d2536dd670271dab3..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ExceptionUtil.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import org.I0Itec.zkclient.exception.ZkInterruptedException; - -public class ExceptionUtil { - - public static RuntimeException convertToRuntimeException(Throwable e) { - if (e instanceof RuntimeException) { - return (RuntimeException) e; - } - retainInterruptFlag(e); - return new RuntimeException(e); - } - - /** - * This sets the interrupt flag if the catched exception was an {@link InterruptedException}. Catching such an - * exception always clears the interrupt flag. - * - * @param catchedException - * The catched exception. - */ - public static void retainInterruptFlag(Throwable catchedException) { - if (catchedException instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - } - - public static void rethrowInterruptedException(Throwable e) throws InterruptedException { - if (e instanceof InterruptedException) { - throw (InterruptedException) e; - } - if (e instanceof ZkInterruptedException) { - throw (ZkInterruptedException) e; - } - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/Gateway.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/Gateway.java deleted file mode 100644 index a6f59109dc32c99338fa4639b785176b16277303..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/Gateway.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -public class Gateway { - - private GatewayThread _thread; - private final int _port; - private final int _destinationPort; - - public Gateway(int port, int destinationPort) { - _port = port; - _destinationPort = destinationPort; - } - - public synchronized void start() { - if (_thread != null) { - throw new IllegalStateException("Gateway already running"); - } - _thread = new GatewayThread(_port, _destinationPort); - _thread.start(); - _thread.awaitUp(); - } - - public synchronized void stop() { - if (_thread != null) { - try { - _thread.interruptAndJoin(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - _thread = null; - } - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/GatewayThread.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/GatewayThread.java deleted file mode 100644 index 9ca8e2d8e44d6200803b86fa9aaba1a7a473eea0..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/GatewayThread.java +++ /dev/null @@ -1,189 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; -import java.util.ArrayList; -import java.util.List; -import java.util.Vector; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class GatewayThread extends Thread { - - protected final static Logger LOG = LoggerFactory.getLogger(GatewayThread.class); - - private final int _port; - private final int _destinationPort; - private ServerSocket _serverSocket; - private Lock _lock = new ReentrantLock(); - private Condition _runningCondition = _lock.newCondition(); - private boolean _running = false; - - public GatewayThread(int port, int destinationPort) { - _port = port; - _destinationPort = destinationPort; - setDaemon(true); - } - - @Override - public void run() { - final List runningThreads = new Vector(); - try { - LOG.info("Starting gateway on port " + _port + " pointing to port " + _destinationPort); - _serverSocket = new ServerSocket(_port); - _lock.lock(); - try { - _running = true; - _runningCondition.signalAll(); - } finally { - _lock.unlock(); - } - while (true) { - final Socket socket = _serverSocket.accept(); - LOG.info("new client is connected " + socket.getInetAddress()); - final InputStream incomingInputStream = socket.getInputStream(); - final OutputStream incomingOutputStream = socket.getOutputStream(); - - final Socket outgoingSocket; - try { - outgoingSocket = new Socket("localhost", _destinationPort); - } catch (Exception e) { - LOG.warn("could not connect to " + _destinationPort); - continue; - } - final InputStream outgoingInputStream = outgoingSocket.getInputStream(); - final OutputStream outgoingOutputStream = outgoingSocket.getOutputStream(); - - Thread writeThread = new Thread() { - @Override - public void run() { - runningThreads.add(this); - try { - int read = -1; - while ((read = incomingInputStream.read()) != -1) { - outgoingOutputStream.write(read); - } - } catch (IOException e) { - // ignore - } finally { - closeQuietly(outgoingOutputStream); - runningThreads.remove(this); - } - } - - @Override - public void interrupt() { - try { - socket.close(); - outgoingSocket.close(); - } catch (IOException e) { - LOG.error("error on stopping closing sockets", e); - } - - super.interrupt(); - } - }; - - Thread readThread = new Thread() { - @Override - public void run() { - runningThreads.add(this); - try { - int read = -1; - while ((read = outgoingInputStream.read()) != -1) { - incomingOutputStream.write(read); - } - } catch (IOException e) { - // ignore - } finally { - closeQuietly(incomingOutputStream); - runningThreads.remove(this); - } - } - }; - - writeThread.setDaemon(true); - readThread.setDaemon(true); - - writeThread.start(); - readThread.start(); - } - } catch (SocketException e) { - if (!_running) { - throw ExceptionUtil.convertToRuntimeException(e); - } - LOG.info("Stopping gateway"); - } catch (Exception e) { - LOG.error("error on gateway execution", e); - } - - for (Thread thread : new ArrayList(runningThreads)) { - thread.interrupt(); - try { - thread.join(); - } catch (InterruptedException e) { - // ignore - } - } - } - - protected void closeQuietly(Closeable closable) { - try { - closable.close(); - } catch (IOException e) { - // ignore - } - } - - @Override - public void interrupt() { - try { - _serverSocket.close(); - } catch (Exception cE) { - LOG.error("error on stopping gateway", cE); - } - super.interrupt(); - } - - public void interruptAndJoin() throws InterruptedException { - interrupt(); - join(); - } - - public void awaitUp() { - _lock.lock(); - try { - while (!_running) { - _runningCondition.await(); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } finally { - _lock.unlock(); - } - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/Holder.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/Holder.java deleted file mode 100644 index afebabba3ffd4d5b1da20df77b296b6dff405bd9..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/Holder.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -public class Holder { - - private T _value; - - public Holder() { - // do nothing - } - - public Holder(T value) { - _value = value; - } - - public T get() { - return _value; - } - - public void set(T value) { - _value = value; - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IDefaultNameSpace.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IDefaultNameSpace.java deleted file mode 100644 index 9ef652169fecec5111db67f339f3f8c0dfc261a6..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IDefaultNameSpace.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -public interface IDefaultNameSpace { - - /** - * Creates a set of default folder structure within a zookeeper . - * - * @param zkClient - * The zkclient. - */ - public void createDefaultNameSpace(ZkClient zkClient); -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkChildListener.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkChildListener.java deleted file mode 100644 index 1e953678986bf2df89ac67549250ad48dfdee0f0..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkChildListener.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.util.List; - -/** - * An {@link IZkChildListener} can be registered at a {@link ZkClient} for listening on zk child changes for a given - * path. - * - * Node: Also this listener re-subscribes it watch for the path on each zk event (zk watches are one-timers) is is not - * guaranteed that events on the path are missing (see http://zookeeper.wiki.sourceforge.net/ZooKeeperWatches). An - * implementation of this class should take that into account. - * - */ -public interface IZkChildListener { - - /** - * Called when the children of the given path changed. - * - * @param parentPath - * The parent path - * @param currentChilds - * The children or null if the root node (parent path) was deleted. - * @throws Exception - */ - public void handleChildChange(String parentPath, List currentChilds) throws Exception; -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkConnection.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkConnection.java deleted file mode 100644 index 28dd89f3f8b35707592989a5bc755b5a68a21d0d..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkConnection.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.util.List; -import java.util.Map; - -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Op; -import org.apache.zookeeper.OpResult; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooKeeper.States; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; - -public interface IZkConnection { - - public void connect(Watcher watcher); - - void close() throws InterruptedException; - - public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException; - - public String create(String path, byte[] data, List acl, CreateMode mode) throws KeeperException, InterruptedException; - - public void delete(String path) throws InterruptedException, KeeperException; - - boolean exists(final String path, final boolean watch) throws KeeperException, InterruptedException; - - List getChildren(final String path, final boolean watch) throws KeeperException, InterruptedException; - - public byte[] readData(String path, Stat stat, boolean watch) throws KeeperException, InterruptedException; - - public void writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException; - - public Stat writeDataReturnStat(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException; - - public States getZookeeperState(); - - public long getCreateTime(String path) throws KeeperException, InterruptedException; - - public String getServers(); - - public List multi(Iterable ops) throws KeeperException, InterruptedException; - - public void addAuthInfo(String scheme, byte[] auth); - - public void setAcl(final String path, List acl, int version) throws KeeperException, InterruptedException; - - public Map.Entry, Stat> getAcl(final String path) throws KeeperException, InterruptedException; -} \ No newline at end of file diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkDataListener.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkDataListener.java deleted file mode 100644 index 83e069584b0cc52da3e2067ac63802ff6ee38829..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkDataListener.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -/** - * An {@link IZkDataListener} can be registered at a {@link ZkClient} for listening on zk data changes for a given path. - * - * Node: Also this listener re-subscribes it watch for the path on each zk event (zk watches are one-timers) is is not - * guaranteed that events on the path are missing (see http://zookeeper.wiki.sourceforge.net/ZooKeeperWatches). An - * implementation of this class should take that into account. - */ -public interface IZkDataListener { - - public void handleDataChange(String dataPath, Object data) throws Exception; - - public void handleDataDeleted(String dataPath) throws Exception; - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkStateListener.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkStateListener.java deleted file mode 100644 index 3657dba21cc4a65166f3bb2400d5267d2a50a3c2..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/IZkStateListener.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import org.apache.zookeeper.Watcher.Event.KeeperState; - -public interface IZkStateListener { - - /** - * Called when the zookeeper connection state has changed. - * - * @param state - * The new state. - * @throws Exception - * On any error. - */ - public void handleStateChanged(KeeperState state) throws Exception; - - /** - * Called after the zookeeper session has expired and a new session has been created. You would have to re-create - * any ephemeral nodes here. - * - * @throws Exception - * On any error. - */ - public void handleNewSession() throws Exception; - - /** - * Called when a session cannot be re-established. This should be used to implement connection - * failure handling e.g. retry to connect or pass the error up - * - * @param error - * The error that prevents a session from being established - * @throws Exception - * On any error. - */ - public void handleSessionEstablishmentError(final Throwable error) throws Exception; - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/InMemoryConnection.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/InMemoryConnection.java deleted file mode 100644 index 724cabe3921527025600d8581e3f0b10c119097a..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/InMemoryConnection.java +++ /dev/null @@ -1,462 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import org.I0Itec.zkclient.exception.ZkException; -import org.I0Itec.zkclient.exception.ZkInterruptedException; -import org.I0Itec.zkclient.exception.ZkNoNodeException; -import org.I0Itec.zkclient.util.ZkPathUtil; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.KeeperException.Code; -import org.apache.zookeeper.Op; -import org.apache.zookeeper.OpResult; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.Watcher.Event.EventType; -import org.apache.zookeeper.Watcher.Event.KeeperState; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper.States; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Id; -import org.apache.zookeeper.data.Stat; -import org.apache.zookeeper.proto.CheckVersionRequest; -import org.apache.zookeeper.proto.CreateRequest; -import org.apache.zookeeper.proto.DeleteRequest; -import org.apache.zookeeper.proto.SetDataRequest; - -/** - * Emulating a ZooKeeper server with few hash tables. Basically a mock class used for testing. Please avoid using this - * as your ZK in production :) - * - * Note that the addAuth is even more mocked than usual Since we have no authentication provider (i.e. Kerberos) around - * we simply take the auth byte[] and convert it to string to get the Id scheme remains the same - */ -public class InMemoryConnection implements IZkConnection { - - public static class DataAndVersion { - private byte[] _data; - private int _version; - private List _acl; - - public DataAndVersion(byte[] data, int version, List acl) { - _data = data; - _version = version; - _acl = acl; - } - - public DataAndVersion(byte[] data, int version) { - this(data, version, null); - } - - public byte[] getData() { - return _data; - } - - public int getVersion() { - return _version; - } - - public List getAcl() { - return _acl; - } - } - - private Lock _lock = new ReentrantLock(true); - private Map _data = new HashMap(); - private Map _creationTime = new HashMap(); - private List _ids = new ArrayList(); - private final AtomicInteger sequence = new AtomicInteger(0); - - private Set _dataWatches = new HashSet(); - private Set _nodeWatches = new HashSet(); - private EventThread _eventThread; - - private class EventThread extends Thread { - - private Watcher _watcher; - private BlockingQueue _blockingQueue = new LinkedBlockingDeque(); - - public EventThread(Watcher watcher) { - _watcher = watcher; - } - - @Override - public void run() { - try { - while (true) { - _watcher.process(_blockingQueue.take()); - } - } catch (InterruptedException e) { - // stop event thread - } - } - - public void send(WatchedEvent event) { - _blockingQueue.add(event); - } - } - - public InMemoryConnection() { - try { - create("/", null, CreateMode.PERSISTENT); - } catch (KeeperException e) { - throw ZkException.create(e); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new ZkInterruptedException(e); - } - } - - @Override - public void close() throws InterruptedException { - _lock.lockInterruptibly(); - try { - if (_eventThread != null) { - _eventThread.interrupt(); - _eventThread.join(); - _eventThread = null; - } - } finally { - _lock.unlock(); - } - } - - @Override - public void connect(Watcher watcher) { - _lock.lock(); - try { - if (_eventThread != null) { - throw new IllegalStateException("Already connected."); - } - _eventThread = new EventThread(watcher); - _eventThread.start(); - _eventThread.send(new WatchedEvent(null, KeeperState.SyncConnected, null)); - } finally { - _lock.unlock(); - } - } - - @Override - public String create(String path, byte[] data, List acl, CreateMode mode) throws KeeperException, - InterruptedException { - _lock.lock(); - try { - - if (mode.isSequential()) { - final int newSequence = sequence.getAndIncrement(); - path = path + ZkPathUtil.leadingZeros(newSequence, 10); - } - - if (exists(path, false)) { - throw new KeeperException.NodeExistsException(); - } - String parentPath = getParentPath(path); - checkACL(parentPath, ZooDefs.Perms.CREATE); - - _data.put(path, new DataAndVersion(data, 0, acl)); - _creationTime.put(path, System.currentTimeMillis()); - checkWatch(_nodeWatches, path, EventType.NodeCreated); - // we also need to send a child change event for the parent - if (parentPath != null) { - checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged); - } - return path; - } finally { - _lock.unlock(); - } - } - - @Override - public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException { - return create(path, data, null, mode); - } - - private String getParentPath(String path) { - int lastIndexOf = path.lastIndexOf("/"); - if (lastIndexOf == -1 || lastIndexOf == 0) { - return null; - } - return path.substring(0, lastIndexOf); - } - - @Override - public void delete(String path) throws InterruptedException, KeeperException { - _lock.lock(); - try { - if (!exists(path, false)) { - throw new KeeperException.NoNodeException(); - } - String parentPath = getParentPath(path); - checkACL(parentPath, ZooDefs.Perms.DELETE); - _data.remove(path); - _creationTime.remove(path); - checkWatch(_nodeWatches, path, EventType.NodeDeleted); - if (parentPath != null) { - checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged); - } - } finally { - _lock.unlock(); - } - } - - @Override - public boolean exists(String path, boolean watch) throws KeeperException, InterruptedException { - _lock.lock(); - try { - if (watch) { - installWatch(_nodeWatches, path); - } - return _data.containsKey(path); - } finally { - _lock.unlock(); - } - } - - private void installWatch(Set watches, String path) { - watches.add(path); - } - - @Override - public List getChildren(String path, boolean watch) throws KeeperException, InterruptedException { - if (!exists(path, false)) { - throw KeeperException.create(Code.NONODE, path); - } - if (exists(path, false) && watch) { - installWatch(_nodeWatches, path); - } - - checkACL(path, ZooDefs.Perms.READ); - ArrayList children = new ArrayList(); - String[] directoryStack = path.split("/"); - Set keySet = _data.keySet(); - - for (String string : keySet) { - if (string.startsWith(path)) { - String[] stack = string.split("/"); - // is one folder level below the one we loockig for and starts - // with path... - if (stack.length == directoryStack.length + 1) { - children.add(stack[stack.length - 1]); - } - } - - } - return children; - } - - @Override - public States getZookeeperState() { - _lock.lock(); - try { - if (_eventThread == null) { - return States.CLOSED; - } - return States.CONNECTED; - } finally { - _lock.unlock(); - } - } - - @Override - public byte[] readData(String path, Stat stat, boolean watch) throws KeeperException, InterruptedException { - if (watch) { - installWatch(_dataWatches, path); - } - _lock.lock(); - try { - DataAndVersion dataAndVersion = _data.get(path); - if (dataAndVersion == null) { - throw new ZkNoNodeException(new KeeperException.NoNodeException()); - } - checkACL(path, ZooDefs.Perms.READ); - byte[] bs = dataAndVersion.getData(); - if (stat != null) - stat.setVersion(dataAndVersion.getVersion()); - return bs; - } finally { - _lock.unlock(); - } - } - - @Override - public void writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException { - writeDataReturnStat(path, data, expectedVersion); - } - - @Override - public Stat writeDataReturnStat(String path, byte[] data, int expectedVersion) throws KeeperException, - InterruptedException { - int newVersion = -1; - _lock.lock(); - try { - checkWatch(_dataWatches, path, EventType.NodeDataChanged); - if (!exists(path, false)) { - throw new KeeperException.NoNodeException(); - } - checkACL(path, ZooDefs.Perms.WRITE); - newVersion = _data.get(path).getVersion() + 1; - _data.put(path, new DataAndVersion(data, newVersion)); - String parentPath = getParentPath(path); - if (parentPath != null) { - checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged); - } - } finally { - _lock.unlock(); - } - Stat stat = new Stat(); - stat.setVersion(newVersion); - return stat; - } - - private void checkWatch(Set watches, String path, EventType eventType) { - if (watches.contains(path)) { - watches.remove(path); - _eventThread.send(new WatchedEvent(eventType, KeeperState.SyncConnected, path)); - } - } - - @Override - public long getCreateTime(String path) { - Long time = _creationTime.get(path); - if (time == null) { - return -1; - } - return time; - } - - @Override - public String getServers() { - return "mem"; - } - - @Override - public List multi(Iterable ops) throws KeeperException, InterruptedException { - List opResults = new ArrayList(); - for (Op op : ops) { - if (Op.Check.class.isAssignableFrom(op.getClass())) { - CheckVersionRequest check = (CheckVersionRequest) op.toRequestRecord(); - exists(check.getPath(), false); - opResults.add(new OpResult.CheckResult()); - } else if (Op.Create.class.isAssignableFrom(op.getClass())) { - CreateRequest create = (CreateRequest) op.toRequestRecord(); - String path = create(create.getPath(), create.getData(), CreateMode.fromFlag(create.getFlags())); - opResults.add(new OpResult.CreateResult(path)); - } else if (Op.Delete.class.isAssignableFrom(op.getClass())) { - DeleteRequest delete = (DeleteRequest) op.toRequestRecord(); - delete(delete.getPath()); - opResults.add(new OpResult.DeleteResult()); - } else if (Op.SetData.class.isAssignableFrom(op.getClass())) { - SetDataRequest setData = (SetDataRequest) op.toRequestRecord(); - writeData(setData.getPath(), setData.getData(), setData.getVersion()); - opResults.add(new OpResult.SetDataResult(null)); - } - } - return opResults; - } - - @Override - public void addAuthInfo(String scheme, byte[] auth) { - _ids.add(new Id(scheme, new String(auth))); - } - - @Override - public void setAcl(String path, List acl, int version) throws KeeperException, InterruptedException { - if (!exists(path, false)) { - throw new KeeperException.NoNodeException(); - } - - DataAndVersion dataAndVersion = _data.get(path); - if (version != dataAndVersion._version) { - throw new KeeperException.BadVersionException(); - } - - checkACL(path, ZooDefs.Perms.ADMIN); - - _lock.lock(); - try { - _data.put(path, new DataAndVersion(dataAndVersion.getData(), dataAndVersion.getVersion() + 1, acl)); - } finally { - _lock.unlock(); - } - } - - @Override - public Map.Entry, Stat> getAcl(String path) throws KeeperException, InterruptedException { - if (!exists(path, false)) { - throw new KeeperException.NoNodeException(); - } - - DataAndVersion dataAndVersion = _data.get(path); - - Stat stat = new Stat(); - stat.setVersion(dataAndVersion.getVersion()); - stat.setCtime(_creationTime.get(path)); - - return new AbstractMap.SimpleEntry, Stat>(dataAndVersion.getAcl(), stat); - } - - /*** - * - * @param path - * - path of znode we are accessing - * @param perm - * - Privileges required for the action - * @throws KeeperException.NoAuthException - */ - private void checkACL(String path, int perm) throws KeeperException.NoAuthException { - DataAndVersion node = _data.get(path); - if (node == null) { - return; - } - List acl = node.getAcl(); - if (acl == null || acl.size() == 0) { - return; - } - for (Id authId : _ids) { - if (authId.getScheme().equals("super")) { - return; - } - } - for (ACL a : acl) { - Id id = a.getId(); - if ((a.getPerms() & perm) != 0) { - if (id.getScheme().equals("world") && id.getId().equals("anyone")) { - return; - } - for (Id authId : _ids) { - if (authId.getScheme().equals(id.getScheme()) && authId.getId().equals(id.getId())) { - return; - } - } - } - } - throw new KeeperException.NoAuthException(); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/NetworkUtil.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/NetworkUtil.java deleted file mode 100644 index a129c15db11867f5afe4190e44d21879fa97ff79..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/NetworkUtil.java +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.io.IOException; -import java.net.ConnectException; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.Socket; -import java.net.SocketException; -import java.net.UnknownHostException; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; - -public class NetworkUtil { - - public final static String OVERWRITE_HOSTNAME_SYSTEM_PROPERTY = "zkclient.hostname.overwritten"; - - public static String[] getLocalHostNames() { - final Set hostNames = new HashSet(); - // we add localhost to this set manually, because if the ip 127.0.0.1 is - // configured with more than one name in the /etc/hosts, only the first - // name - // is returned - hostNames.add("localhost"); - try { - final Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); - for (final Enumeration ifaces = networkInterfaces; ifaces.hasMoreElements();) { - final NetworkInterface iface = ifaces.nextElement(); - InetAddress ia = null; - for (final Enumeration ips = iface.getInetAddresses(); ips.hasMoreElements();) { - ia = ips.nextElement(); - hostNames.add(ia.getCanonicalHostName()); - hostNames.add(ipToString(ia.getAddress())); - } - } - } catch (final SocketException e) { - throw new RuntimeException("unable to retrieve host names of localhost"); - } - return hostNames.toArray(new String[hostNames.size()]); - } - - private static String ipToString(final byte[] bytes) { - final StringBuffer addrStr = new StringBuffer(); - for (int cnt = 0; cnt < bytes.length; cnt++) { - final int uByte = bytes[cnt] < 0 ? bytes[cnt] + 256 : bytes[cnt]; - addrStr.append(uByte); - if (cnt < 3) - addrStr.append('.'); - } - return addrStr.toString(); - } - - public static int hostNamesInList(final String serverList, final String[] hostNames) { - final String[] serverNames = serverList.split(","); - for (int i = 0; i < hostNames.length; i++) { - final String hostname = hostNames[i]; - for (int j = 0; j < serverNames.length; j++) { - final String serverNameAndPort = serverNames[j]; - final String serverName = serverNameAndPort.split(":")[0]; - if (serverName.equalsIgnoreCase(hostname)) { - return j; - } - } - } - return -1; - } - - public static boolean hostNameInArray(final String[] hostNames, final String hostName) { - for (final String name : hostNames) { - if (name.equalsIgnoreCase(hostName)) { - return true; - } - } - return false; - } - - public static boolean isPortFree(int port) { - try { - Socket socket = new Socket("localhost", port); - socket.close(); - return false; - } catch (ConnectException e) { - return true; - } catch (SocketException e) { - if (e.getMessage().equals("Connection reset by peer")) { - return true; - } - throw new RuntimeException(e); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static String getLocalhostName() { - String property = System.getProperty(OVERWRITE_HOSTNAME_SYSTEM_PROPERTY); - if (property != null && property.trim().length() > 0) { - return property; - } - try { - return InetAddress.getLocalHost().getHostName(); - } catch (final UnknownHostException e) { - throw new RuntimeException("unable to retrieve localhost name"); - } - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkClient.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkClient.java deleted file mode 100644 index e0ed1f5269d4aaa70366fd2a88f534acc3e9aef6..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkClient.java +++ /dev/null @@ -1,1347 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.TimeUnit; - -import javax.security.auth.login.Configuration; - -import org.I0Itec.zkclient.ZkEventThread.ZkEvent; -import org.I0Itec.zkclient.exception.ZkBadVersionException; -import org.I0Itec.zkclient.exception.ZkException; -import org.I0Itec.zkclient.exception.ZkInterruptedException; -import org.I0Itec.zkclient.exception.ZkNoNodeException; -import org.I0Itec.zkclient.exception.ZkNodeExistsException; -import org.I0Itec.zkclient.exception.ZkTimeoutException; -import org.I0Itec.zkclient.serialize.SerializableSerializer; -import org.I0Itec.zkclient.serialize.ZkSerializer; -import org.I0Itec.zkclient.util.ZkPathUtil; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.KeeperException.ConnectionLossException; -import org.apache.zookeeper.KeeperException.SessionExpiredException; -import org.apache.zookeeper.Op; -import org.apache.zookeeper.OpResult; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.Watcher.Event.EventType; -import org.apache.zookeeper.Watcher.Event.KeeperState; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Abstracts the interaction with zookeeper and allows permanent (not just one time) watches on nodes in ZooKeeper - */ -public class ZkClient implements Watcher { - - private final static Logger LOG = LoggerFactory.getLogger(ZkClient.class); - - protected static final String JAVA_LOGIN_CONFIG_PARAM = "java.security.auth.login.config"; - protected static final String ZK_SASL_CLIENT = "zookeeper.sasl.client"; - protected static final String ZK_LOGIN_CONTEXT_NAME_KEY = "zookeeper.sasl.clientconfig"; - - protected final IZkConnection _connection; - protected final long operationRetryTimeoutInMillis; - private final Map> _childListener = new ConcurrentHashMap>(); - private final ConcurrentHashMap> _dataListener = new ConcurrentHashMap>(); - private final Set _stateListener = new CopyOnWriteArraySet(); - private KeeperState _currentState; - private final ZkLock _zkEventLock = new ZkLock(); - private boolean _shutdownTriggered; - private ZkEventThread _eventThread; - // TODO PVo remove this later - private Thread _zookeeperEventThread; - private ZkSerializer _zkSerializer; - private volatile boolean _closed; - private boolean _isZkSaslEnabled; - - public ZkClient(String serverstring) { - this(serverstring, Integer.MAX_VALUE); - } - - public ZkClient(String zkServers, int connectionTimeout) { - this(new ZkConnection(zkServers), connectionTimeout); - } - - public ZkClient(String zkServers, int sessionTimeout, int connectionTimeout) { - this(new ZkConnection(zkServers, sessionTimeout), connectionTimeout); - } - - public ZkClient(String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer) { - this(new ZkConnection(zkServers, sessionTimeout), connectionTimeout, zkSerializer); - } - - /** - * - * @param zkServers - * The Zookeeper servers - * @param sessionTimeout - * The session timeout in milli seconds - * @param connectionTimeout - * The connection timeout in milli seconds - * @param zkSerializer - * The Zookeeper data serializer - * @param operationRetryTimeout - * Most operations done through this {@link org.I0Itec.zkclient.ZkClient} are retried in cases like - * connection loss with the Zookeeper servers. During such failures, this - * operationRetryTimeout decides the maximum amount of time, in milli seconds, each - * operation is retried. A value lesser than 0 is considered as - * "retry forever until a connection has been reestablished". - */ - public ZkClient(final String zkServers, final int sessionTimeout, final int connectionTimeout, - final ZkSerializer zkSerializer, final long operationRetryTimeout) { - this(new ZkConnection(zkServers, sessionTimeout), connectionTimeout, zkSerializer, operationRetryTimeout); - } - - public ZkClient(IZkConnection connection) { - this(connection, Integer.MAX_VALUE); - } - - public ZkClient(IZkConnection connection, int connectionTimeout) { - this(connection, connectionTimeout, new SerializableSerializer()); - } - - public ZkClient(IZkConnection zkConnection, int connectionTimeout, ZkSerializer zkSerializer) { - this(zkConnection, connectionTimeout, zkSerializer, -1); - } - - /** - * - * @param zkConnection - * The Zookeeper servers - * @param connectionTimeout - * The connection timeout in milli seconds - * @param zkSerializer - * The Zookeeper data serializer - * @param operationRetryTimeout - * Most operations done through this {@link org.I0Itec.zkclient.ZkClient} are retried in cases like - * connection loss with the Zookeeper servers. During such failures, this - * operationRetryTimeout decides the maximum amount of time, in milli seconds, each - * operation is retried. A value lesser than 0 is considered as - * "retry forever until a connection has been reestablished". - */ - public ZkClient(final IZkConnection zkConnection, final int connectionTimeout, final ZkSerializer zkSerializer, - final long operationRetryTimeout) { - if (zkConnection == null) { - throw new NullPointerException("Zookeeper connection is null!"); - } - _connection = zkConnection; - _zkSerializer = zkSerializer; - this.operationRetryTimeoutInMillis = operationRetryTimeout; - _isZkSaslEnabled = isZkSaslEnabled(); - connect(connectionTimeout, this); - } - - public void setZkSerializer(ZkSerializer zkSerializer) { - _zkSerializer = zkSerializer; - } - - public List subscribeChildChanges(String path, IZkChildListener listener) { - synchronized (_childListener) { - Set listeners = _childListener.get(path); - if (listeners == null) { - listeners = new CopyOnWriteArraySet(); - _childListener.put(path, listeners); - } - listeners.add(listener); - } - return watchForChilds(path); - } - - public void unsubscribeChildChanges(String path, IZkChildListener childListener) { - synchronized (_childListener) { - final Set listeners = _childListener.get(path); - if (listeners != null) { - listeners.remove(childListener); - } - } - } - - public void subscribeDataChanges(String path, IZkDataListener listener) { - Set listeners; - synchronized (_dataListener) { - listeners = _dataListener.get(path); - if (listeners == null) { - listeners = new CopyOnWriteArraySet(); - _dataListener.put(path, listeners); - } - listeners.add(listener); - } - watchForData(path); - LOG.debug("Subscribed data changes for " + path); - } - - public void unsubscribeDataChanges(String path, IZkDataListener dataListener) { - synchronized (_dataListener) { - final Set listeners = _dataListener.get(path); - if (listeners != null) { - listeners.remove(dataListener); - } - if (listeners == null || listeners.isEmpty()) { - _dataListener.remove(path); - } - } - } - - public void subscribeStateChanges(final IZkStateListener listener) { - synchronized (_stateListener) { - _stateListener.add(listener); - } - } - - public void unsubscribeStateChanges(IZkStateListener stateListener) { - synchronized (_stateListener) { - _stateListener.remove(stateListener); - } - } - - public void unsubscribeAll() { - synchronized (_childListener) { - _childListener.clear(); - } - synchronized (_dataListener) { - _dataListener.clear(); - } - synchronized (_stateListener) { - _stateListener.clear(); - } - } - - // - - /** - * Create a persistent node. - * - * @param path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createPersistent(String path) throws ZkInterruptedException, IllegalArgumentException, ZkException, - RuntimeException { - createPersistent(path, false); - } - - /** - * Create a persistent node and set its ACLs. - * - * @param path - * @param createParents - * if true all parent dirs are created as well and no {@link ZkNodeExistsException} is thrown in case the - * path already exists - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createPersistent(String path, boolean createParents) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - createPersistent(path, createParents, ZooDefs.Ids.OPEN_ACL_UNSAFE); - } - - /** - * Create a persistent node and set its ACLs. - * - * @param path - * @param acl - * List of ACL permissions to assign to the node - * @param createParents - * if true all parent dirs are created as well and no {@link ZkNodeExistsException} is thrown in case the - * path already exists - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createPersistent(String path, boolean createParents, List acl) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - try { - create(path, null, acl, CreateMode.PERSISTENT); - } catch (ZkNodeExistsException e) { - if (!createParents) { - throw e; - } - } catch (ZkNoNodeException e) { - if (!createParents) { - throw e; - } - String parentDir = path.substring(0, path.lastIndexOf('/')); - createPersistent(parentDir, createParents, acl); - createPersistent(path, createParents, acl); - } - } - - /** - * Sets the acl on path - * - * @param path - * @param acl - * List of ACL permissions to assign to the path. - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void setAcl(final String path, final List acl) throws ZkException { - if (path == null) { - throw new NullPointerException("Missing value for path"); - } - - if (acl == null || acl.size() == 0) { - throw new NullPointerException("Missing value for ACL"); - } - - if (!exists(path)) { - throw new RuntimeException("trying to set acls on non existing node " + path); - } - - retryUntilConnected(new Callable() { - @Override - public Void call() throws Exception { - Stat stat = new Stat(); - _connection.readData(path, stat, false); - _connection.setAcl(path, acl, stat.getAversion()); - return null; - } - }); - } - - /** - * Gets the acl on path - * - * @param path - * @return an entry instance with key = list of acls on node and value = stats. - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public Map.Entry, Stat> getAcl(final String path) throws ZkException { - if (path == null) { - throw new NullPointerException("Missing value for path"); - } - - if (!exists(path)) { - throw new RuntimeException("trying to get acls on non existing node " + path); - } - - return retryUntilConnected(new Callable, Stat>>() { - @Override - public Map.Entry, Stat> call() throws Exception { - return _connection.getAcl(path); - } - }); - } - - /** - * Create a persistent node. - * - * @param path - * @param data - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createPersistent(String path, Object data) throws ZkInterruptedException, IllegalArgumentException, - ZkException, RuntimeException { - create(path, data, CreateMode.PERSISTENT); - } - - /** - * Create a persistent node. - * - * @param path - * @param data - * @param acl - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createPersistent(String path, Object data, List acl) { - create(path, data, acl, CreateMode.PERSISTENT); - } - - /** - * Create a persistent, sequental node. - * - * @param path - * @param data - * @return create node's path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public String createPersistentSequential(String path, Object data) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - return create(path, data, CreateMode.PERSISTENT_SEQUENTIAL); - } - - /** - * Create a persistent, sequential node and set its ACL. - * - * @param path - * @param acl - * @param data - * @return create node's path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public String createPersistentSequential(String path, Object data, List acl) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - return create(path, data, acl, CreateMode.PERSISTENT_SEQUENTIAL); - } - - /** - * Create an ephemeral node. - * - * @param path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createEphemeral(final String path) throws ZkInterruptedException, IllegalArgumentException, - ZkException, RuntimeException { - create(path, null, CreateMode.EPHEMERAL); - } - - /** - * Create an ephemeral node and set its ACL. - * - * @param path - * @param acl - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createEphemeral(final String path, final List acl) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - create(path, null, acl, CreateMode.EPHEMERAL); - } - - /** - * Create a node. - * - * @param path - * @param data - * @param mode - * @return create node's path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public String create(final String path, Object data, final CreateMode mode) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - return create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode); - } - - /** - * Create a node with ACL. - * - * @param path - * @param data - * @param acl - * @param mode - * @return create node's path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public String create(final String path, Object data, final List acl, final CreateMode mode) { - if (path == null) { - throw new NullPointerException("Missing value for path"); - } - if (acl == null || acl.size() == 0) { - throw new NullPointerException("Missing value for ACL"); - } - final byte[] bytes = data == null ? null : serialize(data); - - return retryUntilConnected(new Callable() { - @Override - public String call() throws Exception { - return _connection.create(path, bytes, acl, mode); - } - }); - - } - - /** - * Create an ephemeral node. - * - * @param path - * @param data - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createEphemeral(final String path, final Object data) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - create(path, data, CreateMode.EPHEMERAL); - } - - /** - * Create an ephemeral node. - * - * @param path - * @param data - * @param acl - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public void createEphemeral(final String path, final Object data, final List acl) - throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { - create(path, data, acl, CreateMode.EPHEMERAL); - } - - /** - * Create an ephemeral, sequential node. - * - * @param path - * @param data - * @return created path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public String createEphemeralSequential(final String path, final Object data) throws ZkInterruptedException, - IllegalArgumentException, ZkException, RuntimeException { - return create(path, data, CreateMode.EPHEMERAL_SEQUENTIAL); - } - - /** - * Create an ephemeral, sequential node with ACL. - * - * @param path - * @param data - * @param acl - * @return created path - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs - */ - public String createEphemeralSequential(final String path, final Object data, final List acl) - throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { - return create(path, data, acl, CreateMode.EPHEMERAL_SEQUENTIAL); - } - - @Override - public void process(WatchedEvent event) { - LOG.debug("Received event: " + event); - _zookeeperEventThread = Thread.currentThread(); - - boolean stateChanged = event.getPath() == null; - boolean znodeChanged = event.getPath() != null; - boolean dataChanged = event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted - || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeChildrenChanged; - - getEventLock().lock(); - try { - - // We might have to install child change event listener if a new node was created - if (getShutdownTrigger()) { - LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath() - + "}' since shutdown triggered"); - return; - } - if (stateChanged) { - processStateChanged(event); - } - if (dataChanged) { - processDataOrChildChange(event); - } - } finally { - if (stateChanged) { - getEventLock().getStateChangedCondition().signalAll(); - - // If the session expired we have to signal all conditions, because watches might have been removed and - // there is no guarantee that those - // conditions will be signaled at all after an Expired event - // TODO PVo write a test for this - if (event.getState() == KeeperState.Expired) { - getEventLock().getZNodeEventCondition().signalAll(); - getEventLock().getDataChangedCondition().signalAll(); - // We also have to notify all listeners that something might have changed - fireAllEvents(); - } - } - if (znodeChanged) { - getEventLock().getZNodeEventCondition().signalAll(); - } - if (dataChanged) { - getEventLock().getDataChangedCondition().signalAll(); - } - getEventLock().unlock(); - LOG.debug("Leaving process event"); - } - } - - private void fireAllEvents() { - for (Entry> entry : _childListener.entrySet()) { - fireChildChangedEvents(entry.getKey(), entry.getValue()); - } - for (Entry> entry : _dataListener.entrySet()) { - fireDataChangedEvents(entry.getKey(), entry.getValue()); - } - } - - public List getChildren(String path) { - return getChildren(path, hasListeners(path)); - } - - protected List getChildren(final String path, final boolean watch) { - return retryUntilConnected(new Callable>() { - @Override - public List call() throws Exception { - return _connection.getChildren(path, watch); - } - }); - } - - /** - * Counts number of children for the given path. - * - * @param path - * @return number of children or 0 if path does not exist. - */ - public int countChildren(String path) { - try { - return getChildren(path).size(); - } catch (ZkNoNodeException e) { - return 0; - } - } - - protected boolean exists(final String path, final boolean watch) { - return retryUntilConnected(new Callable() { - @Override - public Boolean call() throws Exception { - return _connection.exists(path, watch); - } - }); - } - - public boolean exists(final String path) { - return exists(path, hasListeners(path)); - } - - private void processStateChanged(WatchedEvent event) { - LOG.info("zookeeper state changed (" + event.getState() + ")"); - setCurrentState(event.getState()); - if (getShutdownTrigger()) { - return; - } - fireStateChangedEvent(event.getState()); - if (event.getState() == KeeperState.Expired) { - try { - reconnect(); - fireNewSessionEvents(); - } catch (final Exception e) { - LOG.info("Unable to re-establish connection. Notifying consumer of the following exception: ", e); - fireSessionEstablishmentError(e); - } - } - } - - private void fireNewSessionEvents() { - for (final IZkStateListener stateListener : _stateListener) { - _eventThread.send(new ZkEvent("New session event sent to " + stateListener) { - - @Override - public void run() throws Exception { - stateListener.handleNewSession(); - } - }); - } - } - - private void fireStateChangedEvent(final KeeperState state) { - for (final IZkStateListener stateListener : _stateListener) { - _eventThread.send(new ZkEvent("State changed to " + state + " sent to " + stateListener) { - - @Override - public void run() throws Exception { - stateListener.handleStateChanged(state); - } - }); - } - } - - private void fireSessionEstablishmentError(final Throwable error) { - for (final IZkStateListener stateListener : _stateListener) { - _eventThread.send(new ZkEvent("Session establishment error(" + error + ") sent to " + stateListener) { - - @Override - public void run() throws Exception { - stateListener.handleSessionEstablishmentError(error); - } - }); - } - } - - private boolean hasListeners(String path) { - Set dataListeners = _dataListener.get(path); - if (dataListeners != null && dataListeners.size() > 0) { - return true; - } - Set childListeners = _childListener.get(path); - if (childListeners != null && childListeners.size() > 0) { - return true; - } - return false; - } - - public boolean deleteRecursive(String path) { - List children; - try { - children = getChildren(path, false); - } catch (ZkNoNodeException e) { - return true; - } - - for (String subPath : children) { - if (!deleteRecursive(path + "/" + subPath)) { - return false; - } - } - - return delete(path); - } - - private void processDataOrChildChange(WatchedEvent event) { - final String path = event.getPath(); - - if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated - || event.getType() == EventType.NodeDeleted) { - Set childListeners = _childListener.get(path); - if (childListeners != null && !childListeners.isEmpty()) { - fireChildChangedEvents(path, childListeners); - } - } - - if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted - || event.getType() == EventType.NodeCreated) { - Set listeners = _dataListener.get(path); - if (listeners != null && !listeners.isEmpty()) { - fireDataChangedEvents(event.getPath(), listeners); - } - } - } - - private void fireDataChangedEvents(final String path, Set listeners) { - for (final IZkDataListener listener : listeners) { - _eventThread.send(new ZkEvent("Data of " + path + " changed sent to " + listener) { - - @Override - public void run() throws Exception { - // reinstall watch - exists(path, true); - try { - Object data = readData(path, null, true); - listener.handleDataChange(path, data); - } catch (ZkNoNodeException e) { - listener.handleDataDeleted(path); - } - } - }); - } - } - - private void fireChildChangedEvents(final String path, Set childListeners) { - try { - // reinstall the watch - for (final IZkChildListener listener : childListeners) { - _eventThread.send(new ZkEvent("Children of " + path + " changed sent to " + listener) { - - @Override - public void run() throws Exception { - try { - // if the node doesn't exist we should listen for the root node to reappear - exists(path); - List children = getChildren(path); - listener.handleChildChange(path, children); - } catch (ZkNoNodeException e) { - listener.handleChildChange(path, null); - } - } - }); - } - } catch (Exception e) { - LOG.error("Failed to fire child changed event. Unable to getChildren. ", e); - } - } - - public boolean waitUntilExists(String path, TimeUnit timeUnit, long time) throws ZkInterruptedException { - Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time)); - LOG.debug("Waiting until znode '" + path + "' becomes available."); - if (exists(path)) { - return true; - } - acquireEventLock(); - try { - while (!exists(path, true)) { - boolean gotSignal = getEventLock().getZNodeEventCondition().awaitUntil(timeout); - if (!gotSignal) { - return false; - } - } - return true; - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } finally { - getEventLock().unlock(); - } - } - - protected Set getDataListener(String path) { - return _dataListener.get(path); - } - - public void showFolders(OutputStream output) { - try { - output.write(ZkPathUtil.toString(this).getBytes()); - } catch (final IOException e) { - e.printStackTrace(); - } - } - - private boolean isZkSaslEnabled() { - boolean isSecurityEnabled = false; - boolean zkSaslEnabled = Boolean.parseBoolean(System.getProperty(ZK_SASL_CLIENT, "true")); - String zkLoginContextName = System.getProperty(ZK_LOGIN_CONTEXT_NAME_KEY, "Client"); - - if (!zkSaslEnabled) { - LOG.warn("Client SASL has been explicitly disabled with " + ZK_SASL_CLIENT); - return false; - } - - String loginConfigFile = System.getProperty(JAVA_LOGIN_CONFIG_PARAM); - if (loginConfigFile != null && loginConfigFile.length() > 0) { - LOG.info("JAAS File name: " + loginConfigFile); - File configFile = new File(loginConfigFile); - if (!configFile.canRead()) { - throw new IllegalArgumentException("File " + loginConfigFile + "cannot be read."); - } - - try { - Configuration loginConf = Configuration.getConfiguration(); - isSecurityEnabled = loginConf.getAppConfigurationEntry(zkLoginContextName) != null; - } catch (Exception e) { - throw new ZkException(e); - } - } - return isSecurityEnabled; - } - - public void waitUntilConnected() throws ZkInterruptedException { - waitUntilConnected(Integer.MAX_VALUE, TimeUnit.MILLISECONDS); - } - - public boolean waitUntilConnected(long time, TimeUnit timeUnit) throws ZkInterruptedException { - if (_isZkSaslEnabled) { - return waitForKeeperState(KeeperState.SaslAuthenticated, time, timeUnit); - } else { - return waitForKeeperState(KeeperState.SyncConnected, time, timeUnit); - } - } - - public boolean waitForKeeperState(KeeperState keeperState, long time, TimeUnit timeUnit) - throws ZkInterruptedException { - if (_zookeeperEventThread != null && Thread.currentThread() == _zookeeperEventThread) { - throw new IllegalArgumentException("Must not be done in the zookeeper event thread."); - } - Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time)); - - LOG.info("Waiting for keeper state " + keeperState); - acquireEventLock(); - try { - boolean stillWaiting = true; - while (_currentState != keeperState) { - if (!stillWaiting) { - return false; - } - stillWaiting = getEventLock().getStateChangedCondition().awaitUntil(timeout); - } - LOG.debug("State is " + _currentState); - return true; - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } finally { - getEventLock().unlock(); - } - } - - private void acquireEventLock() { - try { - getEventLock().lockInterruptibly(); - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } - } - - /** - * - * @param - * @param callable - * @return result of Callable - * @throws ZkInterruptedException - * if operation was interrupted, or a required reconnection got interrupted - * @throws IllegalArgumentException - * if called from anything except the ZooKeeper event thread - * @throws ZkException - * if any ZooKeeper exception occurred - * @throws RuntimeException - * if any other exception occurs from invoking the Callable - */ - public T retryUntilConnected(Callable callable) throws ZkInterruptedException, IllegalArgumentException, - ZkException, RuntimeException { - if (_zookeeperEventThread != null && Thread.currentThread() == _zookeeperEventThread) { - throw new IllegalArgumentException("Must not be done in the zookeeper event thread."); - } - final long operationStartTime = System.currentTimeMillis(); - while (true) { - if (_closed) { - throw new IllegalStateException("ZkClient already closed!"); - } - try { - return callable.call(); - } catch (ConnectionLossException e) { - // we give the event thread some time to update the status to 'Disconnected' - Thread.yield(); - waitForRetry(); - } catch (SessionExpiredException e) { - // we give the event thread some time to update the status to 'Expired' - Thread.yield(); - waitForRetry(); - } catch (KeeperException e) { - throw ZkException.create(e); - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } catch (Exception e) { - throw ExceptionUtil.convertToRuntimeException(e); - } - // before attempting a retry, check whether retry timeout has elapsed - if (this.operationRetryTimeoutInMillis > -1 - && (System.currentTimeMillis() - operationStartTime) >= this.operationRetryTimeoutInMillis) { - throw new ZkTimeoutException("Operation cannot be retried because of retry timeout (" - + this.operationRetryTimeoutInMillis + " milli seconds)"); - } - } - } - - private void waitForRetry() { - if (this.operationRetryTimeoutInMillis < 0) { - this.waitUntilConnected(); - return; - } - this.waitUntilConnected(this.operationRetryTimeoutInMillis, TimeUnit.MILLISECONDS); - } - - public void setCurrentState(KeeperState currentState) { - getEventLock().lock(); - try { - _currentState = currentState; - } finally { - getEventLock().unlock(); - } - } - - /** - * Returns a mutex all zookeeper events are synchronized aginst. So in case you need to do something without getting - * any zookeeper event interruption synchronize against this mutex. Also all threads waiting on this mutex object - * will be notified on an event. - * - * @return the mutex. - */ - public ZkLock getEventLock() { - return _zkEventLock; - } - - public boolean delete(final String path) { - try { - retryUntilConnected(new Callable() { - - @Override - public Object call() throws Exception { - _connection.delete(path); - return null; - } - }); - - return true; - } catch (ZkNoNodeException e) { - return false; - } - } - - private byte[] serialize(Object data) { - return _zkSerializer.serialize(data); - } - - @SuppressWarnings("unchecked") - private T derializable(byte[] data) { - if (data == null) { - return null; - } - return (T) _zkSerializer.deserialize(data); - } - - @SuppressWarnings("unchecked") - public T readData(String path) { - return (T) readData(path, false); - } - - @SuppressWarnings("unchecked") - public T readData(String path, boolean returnNullIfPathNotExists) { - T data = null; - try { - data = (T) readData(path, null); - } catch (ZkNoNodeException e) { - if (!returnNullIfPathNotExists) { - throw e; - } - } - return data; - } - - @SuppressWarnings("unchecked") - public T readData(String path, Stat stat) { - return (T) readData(path, stat, hasListeners(path)); - } - - @SuppressWarnings("unchecked") - protected T readData(final String path, final Stat stat, final boolean watch) { - byte[] data = retryUntilConnected(new Callable() { - - @Override - public byte[] call() throws Exception { - return _connection.readData(path, stat, watch); - } - }); - return (T) derializable(data); - } - - public void writeData(String path, Object object) { - writeData(path, object, -1); - } - - /** - * Updates data of an existing znode. The current content of the znode is passed to the {@link DataUpdater} that is - * passed into this method, which returns the new content. The new content is only written back to ZooKeeper if - * nobody has modified the given znode in between. If a concurrent change has been detected the new data of the - * znode is passed to the updater once again until the new contents can be successfully written back to ZooKeeper. - * - * @param - * @param path - * The path of the znode. - * @param updater - * Updater that creates the new contents. - */ - @SuppressWarnings("unchecked") - public void updateDataSerialized(String path, DataUpdater updater) { - Stat stat = new Stat(); - boolean retry; - do { - retry = false; - try { - T oldData = (T) readData(path, stat); - T newData = updater.update(oldData); - writeData(path, newData, stat.getVersion()); - } catch (ZkBadVersionException e) { - retry = true; - } - } while (retry); - } - - public void writeData(final String path, Object datat, final int expectedVersion) { - writeDataReturnStat(path, datat, expectedVersion); - } - - public Stat writeDataReturnStat(final String path, Object datat, final int expectedVersion) { - final byte[] data = serialize(datat); - return (Stat) retryUntilConnected(new Callable() { - - @Override - public Object call() throws Exception { - Stat stat = _connection.writeDataReturnStat(path, data, expectedVersion); - return stat; - } - }); - } - - public void watchForData(final String path) { - retryUntilConnected(new Callable() { - @Override - public Object call() throws Exception { - _connection.exists(path, true); - return null; - } - }); - } - - /** - * Installs a child watch for the given path. - * - * @param path - * @return the current children of the path or null if the zk node with the given path doesn't exist. - */ - public List watchForChilds(final String path) { - if (_zookeeperEventThread != null && Thread.currentThread() == _zookeeperEventThread) { - throw new IllegalArgumentException("Must not be done in the zookeeper event thread."); - } - return retryUntilConnected(new Callable>() { - @Override - public List call() throws Exception { - exists(path, true); - try { - return getChildren(path, true); - } catch (ZkNoNodeException e) { - // ignore, the "exists" watch will listen for the parent node to appear - } - return null; - } - }); - } - - /** - * Add authentication information to the connection. This will be used to identify the user and check access to - * nodes protected by ACLs - * - * @param scheme - * @param auth - */ - public void addAuthInfo(final String scheme, final byte[] auth) { - retryUntilConnected(new Callable() { - @Override - public Object call() throws Exception { - _connection.addAuthInfo(scheme, auth); - return null; - } - }); - } - - /** - * Connect to ZooKeeper. - * - * @param maxMsToWaitUntilConnected - * @param watcher - * @throws ZkInterruptedException - * if the connection timed out due to thread interruption - * @throws ZkTimeoutException - * if the connection timed out - * @throws IllegalStateException - * if the connection timed out due to thread interruption - */ - public void connect(final long maxMsToWaitUntilConnected, Watcher watcher) throws ZkInterruptedException, - ZkTimeoutException, IllegalStateException { - boolean started = false; - acquireEventLock(); - try { - setShutdownTrigger(false); - _eventThread = new ZkEventThread(_connection.getServers()); - _eventThread.start(); - _connection.connect(watcher); - - LOG.debug("Awaiting connection to Zookeeper server"); - boolean waitSuccessful = waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS); - if (!waitSuccessful) { - throw new ZkTimeoutException("Unable to connect to zookeeper server within timeout: " - + maxMsToWaitUntilConnected); - } - started = true; - } finally { - getEventLock().unlock(); - - // we should close the zookeeper instance, otherwise it would keep - // on trying to connect - if (!started) { - close(); - } - } - } - - public long getCreationTime(String path) { - acquireEventLock(); - try { - return _connection.getCreateTime(path); - } catch (KeeperException e) { - throw ZkException.create(e); - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } finally { - getEventLock().unlock(); - } - } - - /** - * Close the client. - * - * @throws ZkInterruptedException - */ - public void close() throws ZkInterruptedException { - if (_closed) { - return; - } - LOG.debug("Closing ZkClient..."); - getEventLock().lock(); - try { - setShutdownTrigger(true); - _eventThread.interrupt(); - _eventThread.join(2000); - _connection.close(); - _closed = true; - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } finally { - getEventLock().unlock(); - } - LOG.debug("Closing ZkClient...done"); - } - - private void reconnect() { - getEventLock().lock(); - try { - _connection.close(); - _connection.connect(this); - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } finally { - getEventLock().unlock(); - } - } - - public void setShutdownTrigger(boolean triggerState) { - _shutdownTriggered = triggerState; - } - - public boolean getShutdownTrigger() { - return _shutdownTriggered; - } - - public int numberOfListeners() { - int listeners = 0; - for (Set childListeners : _childListener.values()) { - listeners += childListeners.size(); - } - for (Set dataListeners : _dataListener.values()) { - listeners += dataListeners.size(); - } - listeners += _stateListener.size(); - - return listeners; - } - - public List multi(final Iterable ops) throws ZkException { - if (ops == null) { - throw new NullPointerException("ops must not be null."); - } - - return retryUntilConnected(new Callable>() { - - @Override - public List call() throws Exception { - return _connection.multi(ops); - } - }); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkConnection.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkConnection.java deleted file mode 100644 index cf1285a80feb1dae0f4d11af28a0a2b115a61724..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkConnection.java +++ /dev/null @@ -1,184 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.io.IOException; -import java.util.AbstractMap.SimpleEntry; -import java.util.List; -import java.util.Map; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import org.I0Itec.zkclient.exception.ZkException; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Op; -import org.apache.zookeeper.OpResult; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooDefs.Ids; -import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.ZooKeeper.States; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ZkConnection implements IZkConnection { - - private static final Logger LOG = LoggerFactory.getLogger(ZkConnection.class); - - /** It is recommended to use quite large sessions timeouts for ZooKeeper. */ - private static final int DEFAULT_SESSION_TIMEOUT = 30000; - - private ZooKeeper _zk = null; - private Lock _zookeeperLock = new ReentrantLock(); - - private final String _servers; - private final int _sessionTimeOut; - - public ZkConnection(String zkServers) { - this(zkServers, DEFAULT_SESSION_TIMEOUT); - } - - public ZkConnection(String zkServers, int sessionTimeOut) { - _servers = zkServers; - _sessionTimeOut = sessionTimeOut; - } - - @Override - public void connect(Watcher watcher) { - _zookeeperLock.lock(); - try { - if (_zk != null) { - throw new IllegalStateException("zk client has already been started"); - } - try { - LOG.debug("Creating new ZookKeeper instance to connect to " + _servers + "."); - _zk = new ZooKeeper(_servers, _sessionTimeOut, watcher); - } catch (IOException e) { - throw new ZkException("Unable to connect to " + _servers, e); - } - } finally { - _zookeeperLock.unlock(); - } - } - - @Override - public void close() throws InterruptedException { - _zookeeperLock.lock(); - try { - if (_zk != null) { - LOG.debug("Closing ZooKeeper connected to " + _servers); - _zk.close(); - _zk = null; - } - } finally { - _zookeeperLock.unlock(); - } - } - - @Override - public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException { - return _zk.create(path, data, Ids.OPEN_ACL_UNSAFE, mode); - } - - @Override - public String create(String path, byte[] data, List acl, CreateMode mode) throws KeeperException, - InterruptedException { - return _zk.create(path, data, acl, mode); - } - - @Override - public void delete(String path) throws InterruptedException, KeeperException { - _zk.delete(path, -1); - } - - @Override - public boolean exists(String path, boolean watch) throws KeeperException, InterruptedException { - return _zk.exists(path, watch) != null; - } - - @Override - public List getChildren(final String path, final boolean watch) throws KeeperException, - InterruptedException { - return _zk.getChildren(path, watch); - } - - @Override - public byte[] readData(String path, Stat stat, boolean watch) throws KeeperException, InterruptedException { - return _zk.getData(path, watch, stat); - } - - public void writeData(String path, byte[] data) throws KeeperException, InterruptedException { - writeData(path, data, -1); - } - - @Override - public void writeData(String path, byte[] data, int version) throws KeeperException, InterruptedException { - _zk.setData(path, data, version); - } - - @Override - public Stat writeDataReturnStat(String path, byte[] data, int version) throws KeeperException, InterruptedException { - return _zk.setData(path, data, version); - } - - @Override - public States getZookeeperState() { - return _zk != null ? _zk.getState() : null; - } - - public ZooKeeper getZookeeper() { - return _zk; - } - - @Override - public long getCreateTime(String path) throws KeeperException, InterruptedException { - Stat stat = _zk.exists(path, false); - if (stat != null) { - return stat.getCtime(); - } - return -1; - } - - @Override - public String getServers() { - return _servers; - } - - @Override - public List multi(Iterable ops) throws KeeperException, InterruptedException { - return _zk.multi(ops); - } - - @Override - public void addAuthInfo(String scheme, byte[] auth) { - _zk.addAuthInfo(scheme, auth); - } - - @Override - public void setAcl(String path, List acl, int version) throws KeeperException, InterruptedException { - _zk.setACL(path, acl, version); - } - - @Override - public Map.Entry, Stat> getAcl(String path) throws KeeperException, InterruptedException { - Stat stat = new Stat(); - List acl = _zk.getACL(path, stat); - return new SimpleEntry, Stat>(acl, stat); - } - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkEventThread.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkEventThread.java deleted file mode 100644 index eec869457722ee161d3ac198ce48b6f7402044f3..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkEventThread.java +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.atomic.AtomicInteger; - -import org.I0Itec.zkclient.exception.ZkInterruptedException; -import org.apache.zookeeper.ZooKeeper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * All listeners registered at the {@link ZkClient} will be notified from this event thread. This is to prevent - * dead-lock situations. The {@link ZkClient} pulls some information out of the {@link ZooKeeper} events to signal - * {@link ZkLock} conditions. Re-using the {@link ZooKeeper} event thread to also notify {@link ZkClient} listeners, - * would stop the ZkClient from receiving events from {@link ZooKeeper} as soon as one of the listeners blocks (because - * it is waiting for something). {@link ZkClient} would then for instance not be able to maintain it's connection state - * anymore. - */ -class ZkEventThread extends Thread { - - private static final Logger LOG = LoggerFactory.getLogger(ZkEventThread.class); - - private BlockingQueue _events = new LinkedBlockingQueue(); - - private static AtomicInteger _eventId = new AtomicInteger(0); - - static abstract class ZkEvent { - - private String _description; - - public ZkEvent(String description) { - _description = description; - } - - public abstract void run() throws Exception; - - @Override - public String toString() { - return "ZkEvent[" + _description + "]"; - } - } - - ZkEventThread(String name) { - setDaemon(true); - setName("ZkClient-EventThread-" + getId() + "-" + name); - } - - @Override - public void run() { - LOG.info("Starting ZkClient event thread."); - try { - while (!isInterrupted()) { - ZkEvent zkEvent = _events.take(); - int eventId = _eventId.incrementAndGet(); - LOG.debug("Delivering event #" + eventId + " " + zkEvent); - try { - zkEvent.run(); - } catch (InterruptedException e) { - interrupt(); - } catch (ZkInterruptedException e) { - interrupt(); - } catch (Throwable e) { - LOG.error("Error handling event " + zkEvent, e); - } - LOG.debug("Delivering event #" + eventId + " done"); - } - } catch (InterruptedException e) { - LOG.info("Terminate ZkClient event thread."); - } - } - - public void send(ZkEvent event) { - if (!isInterrupted()) { - LOG.debug("New event: " + event); - _events.add(event); - } - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkLock.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkLock.java deleted file mode 100644 index ad20b39752e70b48293ef8c804310254c5c953e1..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkLock.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.ReentrantLock; - -public class ZkLock extends ReentrantLock { - - private static final long serialVersionUID = 1L; - - private Condition _dataChangedCondition = newCondition(); - private Condition _stateChangedCondition = newCondition(); - private Condition _zNodeEventCondition = newCondition(); - - /** - * This condition will be signaled if a zookeeper event was processed and the event contains a data/child change. - * - * @return the condition. - */ - public Condition getDataChangedCondition() { - return _dataChangedCondition; - } - - /** - * This condition will be signaled if a zookeeper event was processed and the event contains a state change - * (connected, disconnected, session expired, etc ...). - * - * @return the condition. - */ - public Condition getStateChangedCondition() { - return _stateChangedCondition; - } - - /** - * This condition will be signaled if any znode related zookeeper event was received. - * - * @return the condition. - */ - public Condition getZNodeEventCondition() { - return _zNodeEventCondition; - } -} \ No newline at end of file diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkServer.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkServer.java deleted file mode 100644 index 8932d09a43e4c18e1073ff4f0b118ecfe6bc2da6..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/ZkServer.java +++ /dev/null @@ -1,184 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient; - -import java.io.File; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.Arrays; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; - -import org.I0Itec.zkclient.exception.ZkException; -import org.I0Itec.zkclient.exception.ZkInterruptedException; -import org.apache.zookeeper.server.NIOServerCnxnFactory; -import org.apache.zookeeper.server.ZooKeeperServer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ZkServer { - - private final static Logger LOG = LoggerFactory.getLogger(ZkServer.class); - - public static final int DEFAULT_PORT = 2181; - public static final int DEFAULT_TICK_TIME = 5000; - public static final int DEFAULT_MIN_SESSION_TIMEOUT = 2 * DEFAULT_TICK_TIME; - - private String _dataDir; - private String _logDir; - - private IDefaultNameSpace _defaultNameSpace; - - private ZooKeeperServer _zk; - private NIOServerCnxnFactory _nioFactory; - private ZkClient _zkClient; - private int _port; - private int _tickTime; - private int _minSessionTimeout; - - public ZkServer(String dataDir, String logDir, IDefaultNameSpace defaultNameSpace) { - this(dataDir, logDir, defaultNameSpace, DEFAULT_PORT); - } - - public ZkServer(String dataDir, String logDir, IDefaultNameSpace defaultNameSpace, int port) { - this(dataDir, logDir, defaultNameSpace, port, DEFAULT_TICK_TIME); - } - - public ZkServer(String dataDir, String logDir, IDefaultNameSpace defaultNameSpace, int port, int tickTime) { - this(dataDir, logDir, defaultNameSpace, port, tickTime, DEFAULT_MIN_SESSION_TIMEOUT); - } - - public ZkServer(String dataDir, String logDir, IDefaultNameSpace defaultNameSpace, int port, int tickTime, - int minSessionTimeout) { - _dataDir = dataDir; - _logDir = logDir; - _defaultNameSpace = defaultNameSpace; - _port = port; - _tickTime = tickTime; - _minSessionTimeout = minSessionTimeout; - } - - public int getPort() { - return _port; - } - - @PostConstruct - public void start() { - final String[] localHostNames = NetworkUtil.getLocalHostNames(); - String names = ""; - for (int i = 0; i < localHostNames.length; i++) { - final String name = localHostNames[i]; - names += " " + name; - if (i + 1 != localHostNames.length) { - names += ","; - } - } - LOG.info("Starting ZkServer on: [" + names + "] port " + _port + "..."); - try { - startZooKeeperServer(); - _zkClient = new ZkClient("localhost:" + _port, 10000); - _defaultNameSpace.createDefaultNameSpace(_zkClient); - } catch (RuntimeException e) { - shutdown(); - throw e; - } - } - - private void startZooKeeperServer() { - final String[] localhostHostNames = NetworkUtil.getLocalHostNames(); - final String servers = "localhost:" + _port; - // check if this server needs to start a _client server. - int pos = -1; - LOG.debug("check if hostNames " + servers + " is in list: " + Arrays.asList(localhostHostNames)); - if ((pos = NetworkUtil.hostNamesInList(servers, localhostHostNames)) != -1) { - // yes this server needs to start a zookeeper server - final String[] hosts = servers.split(","); - final String[] hostSplitted = hosts[pos].split(":"); - int port = _port; - if (hostSplitted.length > 1) { - port = Integer.parseInt(hostSplitted[1]); - } - // check if this machine is already something running.. - if (NetworkUtil.isPortFree(port)) { - final File dataDir = new File(_dataDir); - final File dataLogDir = new File(_logDir); - dataDir.mkdirs(); - dataLogDir.mkdirs(); - - if (hosts.length > 1) { - // multiple zk servers - LOG.info("Start distributed zookeeper server..."); - throw new IllegalArgumentException("Unable to start distributed zookeeper server"); - } - // single zk server - LOG.info("Start single zookeeper server..."); - LOG.info("data dir: " + dataDir.getAbsolutePath()); - LOG.info("data log dir: " + dataLogDir.getAbsolutePath()); - LOG.info("JAAS login file: " + System.getProperty("java.security.auth.login.config", "none")); - startSingleZkServer(_tickTime, dataDir, dataLogDir, port); - } else { - throw new IllegalStateException("Zookeeper port " + port - + " was already in use. Running in single machine mode?"); - } - } - } - - private void startSingleZkServer(final int tickTime, final File dataDir, final File dataLogDir, final int port) { - try { - _zk = new ZooKeeperServer(dataDir, dataLogDir, tickTime); - _zk.setMinSessionTimeout(_minSessionTimeout); - _nioFactory = new NIOServerCnxnFactory(); - int maxClientConnections = 0; // 0 means unlimited - _nioFactory.configure(new InetSocketAddress(port), maxClientConnections); - _nioFactory.startup(_zk); - } catch (IOException e) { - throw new ZkException("Unable to start single ZooKeeper server.", e); - } catch (InterruptedException e) { - throw new ZkInterruptedException(e); - } - } - - @PreDestroy - public void shutdown() { - LOG.info("Shutting down ZkServer..."); - try { - if (_zkClient != null) { - _zkClient.close(); - } - } catch (ZkException e) { - LOG.warn("Error on closing zkclient: " + e.getClass().getName()); - } - if (_nioFactory != null) { - _nioFactory.shutdown(); - try { - _nioFactory.join(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - _nioFactory = null; - } - if (_zk != null) { - _zk.shutdown(); - _zk = null; - } - LOG.info("Shutting down ZkServer...done"); - } - - public ZkClient getZkClient() { - return _zkClient; - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkBadVersionException.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkBadVersionException.java deleted file mode 100644 index fb69e8348a433941f7b427b55d5ce44de510275e..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkBadVersionException.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -import org.apache.zookeeper.KeeperException; - -public class ZkBadVersionException extends ZkException { - - private static final long serialVersionUID = 1L; - - public ZkBadVersionException() { - super(); - } - - public ZkBadVersionException(KeeperException cause) { - super(cause); - } - - public ZkBadVersionException(String message, KeeperException cause) { - super(message, cause); - } - - public ZkBadVersionException(String message) { - super(message); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkException.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkException.java deleted file mode 100644 index e8cf5b4e4ddafe3080703416f85e094fac7e6a32..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkException.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -import org.apache.zookeeper.KeeperException; - -public class ZkException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - public ZkException() { - super(); - } - - public ZkException(String message, Throwable cause) { - super(message, cause); - } - - public ZkException(String message) { - super(message); - } - - public ZkException(Throwable cause) { - super(cause); - } - - public static ZkException create(KeeperException e) { - switch (e.code()) { - // case DATAINCONSISTENCY: - // return new DataInconsistencyException(); - // case CONNECTIONLOSS: - // return new ConnectionLossException(); - case NONODE: - return new ZkNoNodeException(e); - // case NOAUTH: - // return new ZkNoAuthException(); - case BADVERSION: - return new ZkBadVersionException(e); - // case NOCHILDRENFOREPHEMERALS: - // return new NoChildrenForEphemeralsException(); - case NODEEXISTS: - return new ZkNodeExistsException(e); - // case INVALIDACL: - // return new ZkInvalidACLException(); - // case AUTHFAILED: - // return new AuthFailedException(); - // case NOTEMPTY: - // return new NotEmptyException(); - // case SESSIONEXPIRED: - // return new SessionExpiredException(); - // case INVALIDCALLBACK: - // return new InvalidCallbackException(); - - default: - return new ZkException(e); - } - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkInterruptedException.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkInterruptedException.java deleted file mode 100644 index 19d6f30f9577242a1b40318c83491bb43e4da0ea..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkInterruptedException.java +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -public class ZkInterruptedException extends ZkException { - - private static final long serialVersionUID = 1L; - - public ZkInterruptedException(InterruptedException e) { - super(e); - Thread.currentThread().interrupt(); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkMarshallingError.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkMarshallingError.java deleted file mode 100644 index cff949e04898da4a6f6f875c175d21e5cde4d3c7..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkMarshallingError.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -public class ZkMarshallingError extends ZkException { - - private static final long serialVersionUID = 1L; - - public ZkMarshallingError() { - super(); - } - - public ZkMarshallingError(Throwable cause) { - super(cause); - } - - public ZkMarshallingError(String message, Throwable cause) { - super(message, cause); - } - - public ZkMarshallingError(String message) { - super(message); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkNoNodeException.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkNoNodeException.java deleted file mode 100644 index 1dc2abb32e58a2620f45be2f38c2338534ade3aa..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkNoNodeException.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -import org.apache.zookeeper.KeeperException; - -public class ZkNoNodeException extends ZkException { - - private static final long serialVersionUID = 1L; - - public ZkNoNodeException() { - super(); - } - - public ZkNoNodeException(KeeperException cause) { - super(cause); - } - - public ZkNoNodeException(String message, KeeperException cause) { - super(message, cause); - } - - public ZkNoNodeException(String message) { - super(message); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkNodeExistsException.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkNodeExistsException.java deleted file mode 100644 index cc900d0704379bd1d85a30a98f83e24b59a33422..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkNodeExistsException.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -import org.apache.zookeeper.KeeperException; - -public class ZkNodeExistsException extends ZkException { - - private static final long serialVersionUID = 1L; - - public ZkNodeExistsException() { - super(); - } - - public ZkNodeExistsException(KeeperException cause) { - super(cause); - } - - public ZkNodeExistsException(String message, KeeperException cause) { - super(message, cause); - } - - public ZkNodeExistsException(String message) { - super(message); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkTimeoutException.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkTimeoutException.java deleted file mode 100644 index ecb1ad922b46cd67a029f62e84418108ea5f8f3d..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/exception/ZkTimeoutException.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.exception; - -public class ZkTimeoutException extends ZkException { - - private static final long serialVersionUID = 1L; - - public ZkTimeoutException() { - super(); - } - - public ZkTimeoutException(String message, Throwable cause) { - super(message, cause); - } - - public ZkTimeoutException(String message) { - super(message); - } - - public ZkTimeoutException(Throwable cause) { - super(cause); - } -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/BytesPushThroughSerializer.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/BytesPushThroughSerializer.java deleted file mode 100644 index dcf5c74bf98dc7b23663c5b40a9c63f999f12e8d..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/BytesPushThroughSerializer.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.serialize; - -import org.I0Itec.zkclient.exception.ZkMarshallingError; - -/** - * A {@link ZkSerializer} which simply passes byte arrays to zk and back. - */ -public class BytesPushThroughSerializer implements ZkSerializer { - - @Override - public Object deserialize(byte[] bytes) throws ZkMarshallingError { - return bytes; - } - - @Override - public byte[] serialize(Object bytes) throws ZkMarshallingError { - return (byte[]) bytes; - } - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/SerializableSerializer.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/SerializableSerializer.java deleted file mode 100644 index 3d84079cfea211e721d5f91a32142a8818ecb4ba..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/SerializableSerializer.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.serialize; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -import org.I0Itec.zkclient.exception.ZkMarshallingError; - -public class SerializableSerializer implements ZkSerializer { - - @Override - public Object deserialize(byte[] bytes) throws ZkMarshallingError { - try (ObjectInputStream inputStream = new TcclAwareObjectIputStream(new ByteArrayInputStream(bytes));) { - Object object = inputStream.readObject(); - return object; - } catch (ClassNotFoundException e) { - throw new ZkMarshallingError("Unable to find object class.", e); - } catch (IOException e) { - throw new ZkMarshallingError(e); - } - } - - @Override - public byte[] serialize(Object serializable) throws ZkMarshallingError { - try (ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); - ObjectOutputStream stream = new ObjectOutputStream(byteArrayOS);) { - stream.writeObject(serializable); - return byteArrayOS.toByteArray(); - } catch (IOException e) { - throw new ZkMarshallingError(e); - } - } - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/TcclAwareObjectIputStream.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/TcclAwareObjectIputStream.java deleted file mode 100644 index cd90e654edc66eea5ec7bfeb0f9ba86505527277..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/TcclAwareObjectIputStream.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.serialize; - -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.io.ObjectStreamClass; -import java.lang.reflect.Proxy; - -/** - * An ObjectInputStream that is aware of the TCCL. - */ -public class TcclAwareObjectIputStream extends ObjectInputStream { - - public TcclAwareObjectIputStream(InputStream in) throws IOException { - super(in); - } - - /** - * Load the local class equivalent of the specified stream class - * description. - * Uses the current class {@link ClassLoader} and falls back to the {@link Thread} context {@link ClassLoader}. - */ - @Override - protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException { - try { - return getClass().getClassLoader().loadClass(classDesc.getName()); - } catch (ClassNotFoundException ex) { - ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - if (tccl != null) { - return tccl.loadClass(classDesc.getName()); - } else { - throw ex; - } - } - } - - /** - * Returns a proxy class that implements the interfaces named in a proxy - * class descriptor; subclasses may implement this method to read custom - * data from the stream along with the descriptors for dynamic proxy - * classes, allowing them to use an alternate loading mechanism for the - * interfaces and the proxy class. - * - * For each interface uses the current class {@link ClassLoader} and falls back to the {@link Thread} context {@link ClassLoader}. - */ - @Override - protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException { - ClassLoader cl = getClass().getClassLoader(); - Class[] cinterfaces = new Class[interfaces.length]; - - for (int i = 0; i < interfaces.length; i++) { - try { - cinterfaces[i] = cl.loadClass(interfaces[i]); - } catch (ClassNotFoundException ex) { - ClassLoader tccl = Thread.currentThread().getContextClassLoader(); - if (tccl != null) { - return tccl.loadClass(interfaces[i]); - } else { - throw ex; - } - } - } - try { - return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces); - } catch (IllegalArgumentException e) { - throw new ClassNotFoundException(null, e); - } - } - -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/ZkSerializer.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/ZkSerializer.java deleted file mode 100644 index b11c5e3737f9651fbb28b6165ead137562d1e70c..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/serialize/ZkSerializer.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.serialize; - -import org.I0Itec.zkclient.exception.ZkMarshallingError; - -/** - * Zookeeper is able to store data in form of byte arrays. This interfacte is a bridge between those byte-array format - * and higher level objects. - * - * @see BytesPushThroughSerializer - * @see SerializableSerializer - */ -public interface ZkSerializer { - - public byte[] serialize(Object data) throws ZkMarshallingError; - - public Object deserialize(byte[] bytes) throws ZkMarshallingError; -} diff --git a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/util/ZkPathUtil.java b/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/util/ZkPathUtil.java deleted file mode 100644 index 98a3a0e9dd7f0dd23d8f54e9ea7354824cef0483..0000000000000000000000000000000000000000 --- a/nredis-proxy-register/src/main/java/org/I0Itec/zkclient/util/ZkPathUtil.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.I0Itec.zkclient.util; - -import java.util.List; - -import org.I0Itec.zkclient.ZkClient; - -public class ZkPathUtil { - - public static String leadingZeros(long number, int numberOfLeadingZeros) { - return String.format("%0" + numberOfLeadingZeros + "d", number); - } - - public static String toString(ZkClient zkClient) { - return toString(zkClient, "/", PathFilter.ALL); - } - - public static String toString(ZkClient zkClient, String startPath, PathFilter pathFilter) { - final int level = 1; - final StringBuilder builder = new StringBuilder("+ (" + startPath + ")"); - builder.append("\n"); - addChildrenToStringBuilder(zkClient, pathFilter, level, builder, startPath); - return builder.toString(); - } - - private static void addChildrenToStringBuilder(ZkClient zkClient, PathFilter pathFilter, final int level, final StringBuilder builder, final String startPath) { - final List children = zkClient.getChildren(startPath); - for (final String node : children) { - String nestedPath; - if (startPath.endsWith("/")) { - nestedPath = startPath + node; - } else { - nestedPath = startPath + "/" + node; - } - if (pathFilter.showChilds(nestedPath)) { - builder.append(getSpaces(level - 1) + "'-" + "+" + node + "\n"); - addChildrenToStringBuilder(zkClient, pathFilter, level + 1, builder, nestedPath); - } else { - builder.append(getSpaces(level - 1) + "'-" + "-" + node + " (contents hidden)\n"); - } - } - } - - private static String getSpaces(final int level) { - String s = ""; - for (int i = 0; i < level; i++) { - s += " "; - } - return s; - } - - public static interface PathFilter { - - public static PathFilter ALL = new PathFilter() { - - @Override - public boolean showChilds(String path) { - return true; - } - }; - - boolean showChilds(String path); - } - -} diff --git a/nredis-proxy-sample/.gitignore b/nredis-proxy-sample/.gitignore deleted file mode 100644 index b83d22266ac8aa2f8df2edef68082c789727841d..0000000000000000000000000000000000000000 --- a/nredis-proxy-sample/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/nredis-proxy-sample/.settings/org.eclipse.m2e.core.prefs b/nredis-proxy-sample/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb2389f85fe6381425d29f0a9866fb65..0000000000000000000000000000000000000000 --- a/nredis-proxy-sample/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/ClassRoom.java b/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/ClassRoom.java deleted file mode 100644 index aae224ebc58e084dff71540401071ba6db835546..0000000000000000000000000000000000000000 --- a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/ClassRoom.java +++ /dev/null @@ -1,80 +0,0 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.sample; - -import java.io.Serializable; - -/** - * @author liubing - * - */ -public class ClassRoom implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 3045043390218727347L; - - private String name; - - private String height; - - private Integer weight; - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the height - */ - public String getHeight() { - return height; - } - - /** - * @param height the height to set - */ - public void setHeight(String height) { - this.height = height; - } - - /** - * @return the weight - */ - public Integer getWeight() { - return weight; - } - - /** - * @param weight the weight to set - */ - public void setWeight(Integer weight) { - this.weight = weight; - } - - /** - * @param name - * @param height - * @param weight - */ - public ClassRoom(String name, String height, Integer weight) { - super(); - this.name = name; - this.height = height; - this.weight = weight; - } - - -} diff --git a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/RedisClientDemo.java b/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/RedisClientDemo.java deleted file mode 100644 index 66759e0414a99a7e3bfe6d18dbd1efc9109c0328..0000000000000000000000000000000000000000 --- a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/RedisClientDemo.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.sample; - -import com.alibaba.fastjson.JSONObject; - -import redis.clients.jedis.Jedis; - -/** - * @author liubing - * - */ -public class RedisClientDemo { - - /** - * @param args - */ - public static void main(String[] args) { - - Jedis redis = new Jedis ("127.0.0.1",6379); - System.out.println(redis.get("classRoom")); -// for(int i=0;i<100000;i++){ -// System.out.println(redis.get("classRoom")); -// } - -// ClassRoom classRoom=new ClassRoom("liubing", "168", 120); -// redis.set("classRoom", JSONObject.toJSONString(classRoom)); - //redis.expire("classRoom", 100); - //redis.close(); -// redis.slaveofNoOne(); - //System.out.println(redis.get("name")); - - - //redis.set("name", "liubing1"); - //redis.expire("name", 10); - -// for(int z=0;z<5;z++){ -// long startTime=System.currentTimeMillis(); -// for(int j=0;j<1000;j++){ -// for(int i=0;i<100;i++){ -// //System.out.println(i); -// redis.get("name"); -// //redis.set("name", "liubing1"); -// } -// } -// long endTime=System.currentTimeMillis(); -// System.out.println("完成时间:"+(endTime-startTime)); -// } - - } - -} diff --git a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/ShortUrl.java b/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/ShortUrl.java deleted file mode 100644 index 5a55bd14f3b40092b5a20f3144eb3ae86314a7e0..0000000000000000000000000000000000000000 --- a/nredis-proxy-sample/src/main/java/com/opensource/netty/redis/proxy/sample/ShortUrl.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * - */ -package com.opensource.netty.redis.proxy.sample; - -/** - * @author liubing - * - */ -public class ShortUrl { - - public static final String ALPHABET = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"; - public static final int BASE = ALPHABET.length(); - - public static String encode(int num) { - StringBuilder str = new StringBuilder(); - while (num > 0) { - str.insert(0, ALPHABET.charAt(num % BASE)); - num = num / BASE; - } - return str.toString(); - } - - public static int decode(String str) { - int num = 0; - for (int i = 0; i < str.length(); i++) { - num = num * BASE + ALPHABET.indexOf(str.charAt(i)); - } - return num; - } - - public static void main(String[] args) { - System.out.println(ShortUrl.encode(1148665819)); - System.out.println(ShortUrl.decode("baidu")); - } -} diff --git a/nredis-proxy-sample/src/test/java/org/nredis/proxy/sample/AppTest.java b/nredis-proxy-sample/src/test/java/org/nredis/proxy/sample/AppTest.java deleted file mode 100644 index dabdf6789e36ae6d6bbdda657a0f75da109f0386..0000000000000000000000000000000000000000 --- a/nredis-proxy-sample/src/test/java/org/nredis/proxy/sample/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.nredis.proxy.sample; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/nredis-proxy-spring/pom.xml b/nredis-proxy-spring/pom.xml index 7a4a9fcc939f554c14867b40ecd398ca53f6c59c..3e33cd1da1dec6041bd7cd4d15542b9000816cf3 100644 --- a/nredis-proxy-spring/pom.xml +++ b/nredis-proxy-spring/pom.xml @@ -25,10 +25,6 @@ com.opensource nredis-proxy-net - - com.opensource - nredis-proxy-register - org.springframework spring-context diff --git a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/handler/RedisProxyHandlerSupport.java b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/handler/RedisProxyHandlerSupport.java index 8de335f4fc236c7f108ca22a54630b8333c340a7..e10328a4d0460cb60faf02d4ee289c615917d4db 100644 --- a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/handler/RedisProxyHandlerSupport.java +++ b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/handler/RedisProxyHandlerSupport.java @@ -20,8 +20,6 @@ public class RedisProxyHandlerSupport extends NamespaceHandlerSupport { */ @Override public void init() { -// registerBeanDefinitionParser(RedisProxyConstant.REDISPROXYCLUSTER, new RedisProxyClusterParser()); -// registerBeanDefinitionParser(RedisProxyConstant.REDISPROXYMASTER, new RedisProxyMasterParser()); registerBeanDefinitionParser(RedisProxyConstant.REDISPROXYNODE, new RedisProxyNodeParser()); } diff --git a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/RedisProxyNodeParser.java b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/RedisProxyNodeParser.java index 8d9be4c1ca7b3de1cbafe5c7c3b74fb87275e871..60d53b7f59d4fac3da69c349cc780cc49fbe0fe1 100644 --- a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/RedisProxyNodeParser.java +++ b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/RedisProxyNodeParser.java @@ -98,19 +98,6 @@ public class RedisProxyNodeParser extends AbstractSimpleBeanDefinitionParser { private void parseMasterElement(Element element,ParserContext parserContext,BeanDefinitionBuilder builder){ String host = element.getAttribute(RedisProxyConstant.HOST); String port=element.getAttribute(RedisProxyConstant.PORT); - String connectionTimeout=element.getAttribute(RedisProxyConstant.CONNECTIONTIMEOUT); - int maxActiveConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.MAXACTIVECONNECTION)); - int maxIdleConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.MAXIDLECONNECTION)); - int minConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.MINCONNECTION)); - - int initialConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.INITIALCONNECTION)); - int maxWaitMillisOnBorrow=Integer.parseInt(element.getAttribute(RedisProxyConstant.MAXWAITMILLISONBORROW)); - int timeBetweenEvictionRunsMillis=Integer.parseInt(element.getAttribute(RedisProxyConstant.TIMEBETWEENEVICTIONRUNSMILLIS)); - int minEvictableIdleTimeMillis=Integer.parseInt(element.getAttribute(RedisProxyConstant.MINEVICTABLEIDLETIMEMILLIS)); - int minIdleEntries=Integer.parseInt(element.getAttribute(RedisProxyConstant.MINIDLEENTRIES)); - Boolean testOnBorrow =Boolean.parseBoolean(element.getAttribute(RedisProxyConstant.TESTONBORROW)); - Boolean testOnReturn =Boolean.parseBoolean(element.getAttribute(RedisProxyConstant.TESTONRETURN)); - Boolean testWhileIdle=Boolean.parseBoolean(element.getAttribute(RedisProxyConstant.TESTWHILEIDLE)); String algorithmRefName=element.getAttribute(RedisProxyConstant.ALGORITHMREF); @@ -118,26 +105,22 @@ public class RedisProxyNodeParser extends AbstractSimpleBeanDefinitionParser { RuntimeBeanReference ruleAlgorithm = new RuntimeBeanReference(algorithmRefName, false); ruleAlgorithm.setSource(parserContext.getDelegate().getReaderContext().extractSource(element)); builder.addPropertyValue(RedisProxyConstant.LOADCLUSTERBALANCE, ruleAlgorithm);//从 的算法 - } + } + + String configRefName=element.getAttribute(RedisProxyConstant.CONFIGREF); + if(StringUtils.isNotBlank(configRefName)){ + RuntimeBeanReference redisPoolConfig = new RuntimeBeanReference(configRefName, false); + redisPoolConfig.setSource(parserContext.getDelegate().getReaderContext().extractSource(element)); + builder.addPropertyValue(RedisProxyConstant.REDISPOOLCONFIG, redisPoolConfig);//获取配置 + } + builder.setLazyInit(false); builder.addPropertyValue(RedisProxyConstant.HOST, host); builder.addPropertyValue(RedisProxyConstant.PORT, Integer.parseInt(port)); - builder.addPropertyValue(RedisProxyConstant.CONNECTIONTIMEOUT, Integer.parseInt(connectionTimeout)); - builder.addPropertyValue(RedisProxyConstant.MAXACTIVECONNECTION, maxActiveConnection); - builder.addPropertyValue(RedisProxyConstant.MAXIDLECONNECTION, maxIdleConnection); - builder.addPropertyValue(RedisProxyConstant.MINCONNECTION, minConnection); - builder.addPropertyValue(RedisProxyConstant.INITIALCONNECTION, initialConnection); - builder.addPropertyValue(RedisProxyConstant.MAXWAITMILLISONBORROW, maxWaitMillisOnBorrow); - builder.addPropertyValue(RedisProxyConstant.TIMEBETWEENEVICTIONRUNSMILLIS, timeBetweenEvictionRunsMillis); - builder.addPropertyValue(RedisProxyConstant.MINEVICTABLEIDLETIMEMILLIS, minEvictableIdleTimeMillis); - builder.addPropertyValue(RedisProxyConstant.MINIDLEENTRIES, minIdleEntries); - builder.addPropertyValue(RedisProxyConstant.TESTONBORROW, testOnBorrow); - builder.addPropertyValue(RedisProxyConstant.TESTONRETURN, testOnReturn); - builder.addPropertyValue(RedisProxyConstant.TESTWHILEIDLE, testWhileIdle); - List proxySlaves=getRedisProxySlaves(element); + List proxySlaves=getRedisProxySlaves(element,parserContext); builder.addPropertyValue(RedisProxyConstant.REDISPROXYCLUSTERS, proxySlaves); } @@ -146,35 +129,30 @@ public class RedisProxyNodeParser extends AbstractSimpleBeanDefinitionParser { * @param masterElement * @return */ - private List getRedisProxySlaves(Element masterElement){ + private List getRedisProxySlaves(Element masterElement, ParserContext parserContext){ List entryClusterEles = DomUtils.getChildElementsByTagName(masterElement, RedisProxyConstant.REDISPROXYSLAVE); List result = new ManagedList<>(entryClusterEles.size()); if(entryClusterEles!=null&&entryClusterEles.size()>0){//从 for (Iterator it = entryClusterEles.iterator(); it.hasNext();) { BeanDefinitionBuilder redisProxySlaveFactory = BeanDefinitionBuilder.rootBeanDefinition(RedisProxySlave.class); Element clusterElement = (Element) it.next(); - parseSlaveElement(clusterElement, redisProxySlaveFactory); + parseSlaveElement(clusterElement,parserContext, redisProxySlaveFactory); result.add(redisProxySlaveFactory.getBeanDefinition()); } } return result; } - private void parseSlaveElement(Element element,BeanDefinitionBuilder builder){ + private void parseSlaveElement(Element element,ParserContext parserContext,BeanDefinitionBuilder builder){ String host = element.getAttribute(RedisProxyConstant.HOST); String port=element.getAttribute(RedisProxyConstant.PORT); - String connectionTimeout=element.getAttribute(RedisProxyConstant.CONNECTIONTIMEOUT); - int maxActiveConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.MAXACTIVECONNECTION)); - int maxIdleConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.MAXIDLECONNECTION)); - int minConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.MINCONNECTION)); - int initialConnection=Integer.parseInt(element.getAttribute(RedisProxyConstant.INITIALCONNECTION)); - int maxWaitMillisOnBorrow=Integer.parseInt(element.getAttribute(RedisProxyConstant.MAXWAITMILLISONBORROW)); - int timeBetweenEvictionRunsMillis=Integer.parseInt(element.getAttribute(RedisProxyConstant.TIMEBETWEENEVICTIONRUNSMILLIS)); - int minEvictableIdleTimeMillis=Integer.parseInt(element.getAttribute(RedisProxyConstant.MINEVICTABLEIDLETIMEMILLIS)); - int minIdleEntries=Integer.parseInt(element.getAttribute(RedisProxyConstant.MINIDLEENTRIES)); - Boolean testOnBorrow =Boolean.parseBoolean(element.getAttribute(RedisProxyConstant.TESTONBORROW)); - Boolean testOnReturn =Boolean.parseBoolean(element.getAttribute(RedisProxyConstant.TESTONRETURN)); - Boolean testWhileIdle=Boolean.parseBoolean(element.getAttribute(RedisProxyConstant.TESTWHILEIDLE)); + + String configRefName=element.getAttribute(RedisProxyConstant.CONFIGREF); + if(StringUtils.isNotBlank(configRefName)){ + RuntimeBeanReference redisPoolConfig = new RuntimeBeanReference(configRefName, false); + redisPoolConfig.setSource(parserContext.getDelegate().getReaderContext().extractSource(element)); + builder.addPropertyValue(RedisProxyConstant.REDISPOOLCONFIG, redisPoolConfig);//获取配置 + } int weight=1; if(element.getAttribute(RedisProxyConstant.WEIGHT)!=null){ @@ -184,17 +162,6 @@ public class RedisProxyNodeParser extends AbstractSimpleBeanDefinitionParser { builder.setLazyInit(false); builder.addPropertyValue(RedisProxyConstant.HOST, host); builder.addPropertyValue(RedisProxyConstant.PORT, Integer.parseInt(port)); - builder.addPropertyValue(RedisProxyConstant.CONNECTIONTIMEOUT, Integer.parseInt(connectionTimeout)); - builder.addPropertyValue(RedisProxyConstant.MAXACTIVECONNECTION, maxActiveConnection); - builder.addPropertyValue(RedisProxyConstant.MAXIDLECONNECTION, maxIdleConnection); - builder.addPropertyValue(RedisProxyConstant.MINCONNECTION, minConnection); - builder.addPropertyValue(RedisProxyConstant.INITIALCONNECTION, initialConnection); - builder.addPropertyValue(RedisProxyConstant.MAXWAITMILLISONBORROW, maxWaitMillisOnBorrow); - builder.addPropertyValue(RedisProxyConstant.TIMEBETWEENEVICTIONRUNSMILLIS, timeBetweenEvictionRunsMillis); - builder.addPropertyValue(RedisProxyConstant.MINEVICTABLEIDLETIMEMILLIS, minEvictableIdleTimeMillis); - builder.addPropertyValue(RedisProxyConstant.MINIDLEENTRIES, minIdleEntries); - builder.addPropertyValue(RedisProxyConstant.TESTONBORROW, testOnBorrow); - builder.addPropertyValue(RedisProxyConstant.TESTONRETURN, testOnReturn); - builder.addPropertyValue(RedisProxyConstant.TESTWHILEIDLE, testWhileIdle); + } } diff --git a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyConstant.java b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyConstant.java index fc32f538e16e121223ab5f76c6c76daf1ad3bb54..61570d421158ced4d69e5d82ef96d2dd3232f0c9 100644 --- a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyConstant.java +++ b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyConstant.java @@ -50,7 +50,11 @@ public class RedisProxyConstant { public static final String ALGORITHMREF="algorithm-ref"; + public static final String CONFIGREF="config-ref"; + public static final String LOADCLUSTERBALANCE="loadClusterBalance"; public static final String LOADMASTERRBALANCE="loadMasterBalance"; + + public static final String REDISPOOLCONFIG="redisPoolConfig"; } diff --git a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyMaster.java b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyMaster.java index b99852adc4536a50be20cf0813a15ed7625f0d10..867ab60522e5e5041d0d9ae36ba2dc1928b22b04 100644 --- a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyMaster.java +++ b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyMaster.java @@ -7,6 +7,7 @@ import java.io.Serializable; import java.util.List; import com.opensource.netty.redis.proxy.core.cluster.LoadBalance; +import com.opensource.netty.redis.proxy.core.config.RedisPoolConfig; /** * 主节点 * @author liubing @@ -23,29 +24,7 @@ public class RedisProxyMaster implements Serializable{ private int port;//端口号 - private int maxActiveConnection;//最大活动连接数 - - private int maxIdleConnection;//最大 空闲连接数 - - private int minConnection;//最小连接数 - - private int initialConnection = 0;//初始化值 - - private long maxWaitMillisOnBorrow; // 最大等待时间 - - private long timeBetweenEvictionRunsMillis;//回收间隔时间点 - - private long minEvictableIdleTimeMillis;//池中最小生存的时间 - - private int minIdleEntries = 0;//最小等待时间 - - private Boolean testOnBorrow=false;//取出验证 - - private int connectionTimeout;//连接超时 - - private Boolean testOnReturn=false;//回收时验证 - - private Boolean testWhileIdle=false;//回收空闲验证 + private RedisPoolConfig redisPoolConfig;//连接池配置 private List redisProxyClusters;//多个从 @@ -86,49 +65,6 @@ public class RedisProxyMaster implements Serializable{ this.port = port; } - - /** - * @return the maxActiveConnection - */ - public int getMaxActiveConnection() { - return maxActiveConnection; - } - - /** - * @param maxActiveConnection the maxActiveConnection to set - */ - public void setMaxActiveConnection(int maxActiveConnection) { - this.maxActiveConnection = maxActiveConnection; - } - - /** - * @return the maxIdleConnection - */ - public int getMaxIdleConnection() { - return maxIdleConnection; - } - - /** - * @param maxIdleConnection the maxIdleConnection to set - */ - public void setMaxIdleConnection(int maxIdleConnection) { - this.maxIdleConnection = maxIdleConnection; - } - - /** - * @return the minConnection - */ - public int getMinConnection() { - return minConnection; - } - - /** - * @param minConnection the minConnection to set - */ - public void setMinConnection(int minConnection) { - this.minConnection = minConnection; - } - /** * @return the redisProxyClusters */ @@ -167,130 +103,18 @@ public class RedisProxyMaster implements Serializable{ } /** - * @return the initialConnection - */ - public int getInitialConnection() { - return initialConnection; - } - - /** - * @param initialConnection the initialConnection to set - */ - public void setInitialConnection(int initialConnection) { - this.initialConnection = initialConnection; - } - - /** - * @return the maxWaitMillisOnBorrow - */ - public long getMaxWaitMillisOnBorrow() { - return maxWaitMillisOnBorrow; - } - - /** - * @param maxWaitMillisOnBorrow the maxWaitMillisOnBorrow to set - */ - public void setMaxWaitMillisOnBorrow(long maxWaitMillisOnBorrow) { - this.maxWaitMillisOnBorrow = maxWaitMillisOnBorrow; - } - - /** - * @return the timeBetweenEvictionRunsMillis - */ - public long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; - } - - /** - * @param timeBetweenEvictionRunsMillis the timeBetweenEvictionRunsMillis to set - */ - public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - } - - /** - * @return the minEvictableIdleTimeMillis - */ - public long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; - } - - /** - * @param minEvictableIdleTimeMillis the minEvictableIdleTimeMillis to set - */ - public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; - } - - /** - * @return the minIdleEntries - */ - public int getMinIdleEntries() { - return minIdleEntries; - } - - /** - * @param minIdleEntries the minIdleEntries to set - */ - public void setMinIdleEntries(int minIdleEntries) { - this.minIdleEntries = minIdleEntries; - } - - /** - * @return the testOnBorrow - */ - public Boolean getTestOnBorrow() { - return testOnBorrow; - } - - /** - * @param testOnBorrow the testOnBorrow to set - */ - public void setTestOnBorrow(Boolean testOnBorrow) { - this.testOnBorrow = testOnBorrow; - } - - /** - * @return the connectionTimeout - */ - public int getConnectionTimeout() { - return connectionTimeout; - } - - /** - * @param connectionTimeout the connectionTimeout to set + * @return the redisPoolConfig */ - public void setConnectionTimeout(int connectionTimeout) { - this.connectionTimeout = connectionTimeout; + public RedisPoolConfig getRedisPoolConfig() { + return redisPoolConfig; } /** - * @return the testOnReturn + * @param redisPoolConfig the redisPoolConfig to set */ - public Boolean getTestOnReturn() { - return testOnReturn; + public void setRedisPoolConfig(RedisPoolConfig redisPoolConfig) { + this.redisPoolConfig = redisPoolConfig; } - /** - * @param testOnReturn the testOnReturn to set - */ - public void setTestOnReturn(Boolean testOnReturn) { - this.testOnReturn = testOnReturn; - } - - /** - * @return the testWhileIdle - */ - public Boolean getTestWhileIdle() { - return testWhileIdle; - } - - /** - * @param testWhileIdle the testWhileIdle to set - */ - public void setTestWhileIdle(Boolean testWhileIdle) { - this.testWhileIdle = testWhileIdle; - } - } diff --git a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyNode.java b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyNode.java index 49896ecea33820c1591a2efe0e778eb10f0d9edb..267b990e195019982a86ec1809f3b7ff6f0f63fd 100644 --- a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyNode.java +++ b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxyNode.java @@ -10,12 +10,16 @@ import org.springframework.beans.factory.InitializingBean; import com.alibaba.fastjson.JSONObject; import com.opensource.netty.redis.proxy.commons.constants.RedisConstants; +import com.opensource.netty.redis.proxy.commons.utils.StringUtils; import com.opensource.netty.redis.proxy.core.cluster.LoadBalance; import com.opensource.netty.redis.proxy.core.config.LBRedisServerMasterCluster; +import com.opensource.netty.redis.proxy.core.config.RedisPoolConfig; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerBean; import com.opensource.netty.redis.proxy.core.config.support.LBRedisServerClusterBean; import com.opensource.netty.redis.proxy.core.enums.RedisProxyParamType; +import com.opensource.netty.redis.proxy.core.enums.ZkNodeType; import com.opensource.netty.redis.proxy.core.registry.Registry; +import com.opensource.netty.redis.proxy.core.registry.impl.support.ZkUtils; import com.opensource.netty.redis.proxy.core.url.RedisProxyURL; import com.opensource.netty.redis.proxy.net.server.LBRedisServer; import com.opensource.netty.redis.proxy.zk.registry.ZookeeperRegistryFactory; @@ -39,7 +43,7 @@ public class RedisProxyNode implements InitializingBean { private LoadBalance loadMasterBalance;//主的一致性算法 - private String address;//ZK地址 + private String zkAddress;//ZK地址 /** * */ @@ -74,14 +78,14 @@ public class RedisProxyNode implements InitializingBean { LBRedisServerClusterBean ffanRedisServerClusterBean=new LBRedisServerClusterBean(); LBRedisServerBean lbRedisServerBean=convertLBRedisServerBean(redisProxyMaster); ffanRedisServerClusterBean.setLoadClusterBalance(redisProxyMaster.getLoadClusterBalance()); - ffanRedisServerClusterBean.setFfanMasterRedisServerBean(lbRedisServerBean); + ffanRedisServerClusterBean.setRedisServerMasterBean(lbRedisServerBean); List ffanRedisServerBeans=new ArrayList(); for(RedisProxySlave proxySlave:redisProxyMaster.getRedisProxyClusters()){ LBRedisServerBean redisServerBean=convertSlaveLBRedisServerBean(proxySlave); ffanRedisServerBeans.add(redisServerBean); - ffanRedisServerClusterBean.setFfanRedisServerClusterBeans(ffanRedisServerBeans); + ffanRedisServerClusterBean.setRedisServerSlaveBeans(ffanRedisServerBeans); } redisServerClusterBeans.add(ffanRedisServerClusterBean); } @@ -101,19 +105,11 @@ public class RedisProxyNode implements InitializingBean { */ private LBRedisServerBean convertSlaveLBRedisServerBean(RedisProxySlave proxySlave){ LBRedisServerBean lbRedisServerBean=new LBRedisServerBean(); - lbRedisServerBean.setConnectionTimeout(proxySlave.getConnectionTimeout()); + RedisPoolConfig redisPoolConfig=proxySlave.getRedisPoolConfig(); + lbRedisServerBean.setHost(proxySlave.getHost()); - lbRedisServerBean.setInitialConnection(proxySlave.getInitialConnection()); - lbRedisServerBean.setMaxActiveConnection(proxySlave.getMaxActiveConnection()); - lbRedisServerBean.setMaxWaitMillisOnBorrow(proxySlave.getMaxWaitMillisOnBorrow()); - lbRedisServerBean.setMinConnection(proxySlave.getMinConnection()); - lbRedisServerBean.setMinEvictableIdleTimeMillis(proxySlave.getMinEvictableIdleTimeMillis()); - lbRedisServerBean.setMinIdleEntries(proxySlave.getMinIdleEntries()); lbRedisServerBean.setPort(proxySlave.getPort()); - lbRedisServerBean.setTestOnBorrow(proxySlave.getTestOnBorrow()); - lbRedisServerBean.setTestOnReturn(proxySlave.getTestOnReturn()); - lbRedisServerBean.setTestWhileIdle(proxySlave.getTestWhileIdle()); - lbRedisServerBean.setTimeBetweenEvictionRunsMillis(proxySlave.getTimeBetweenEvictionRunsMillis()); + lbRedisServerBean.setRedisPoolConfig(redisPoolConfig); return lbRedisServerBean; } @@ -123,20 +119,12 @@ public class RedisProxyNode implements InitializingBean { * @return */ private LBRedisServerBean convertLBRedisServerBean(RedisProxyMaster redisProxyMaster){ - LBRedisServerBean lbRedisServerBean=new LBRedisServerBean(); - lbRedisServerBean.setConnectionTimeout(redisProxyMaster.getConnectionTimeout()); + LBRedisServerBean lbRedisServerBean=new LBRedisServerBean(); + RedisPoolConfig redisPoolConfig=redisProxyMaster.getRedisPoolConfig(); + lbRedisServerBean.setHost(redisProxyMaster.getHost()); - lbRedisServerBean.setInitialConnection(redisProxyMaster.getInitialConnection()); - lbRedisServerBean.setMaxActiveConnection(redisProxyMaster.getMaxActiveConnection()); - lbRedisServerBean.setMaxWaitMillisOnBorrow(redisProxyMaster.getMaxWaitMillisOnBorrow()); - lbRedisServerBean.setMinConnection(redisProxyMaster.getMinConnection()); - lbRedisServerBean.setMinEvictableIdleTimeMillis(redisProxyMaster.getMinEvictableIdleTimeMillis()); - lbRedisServerBean.setMinIdleEntries(redisProxyMaster.getMinIdleEntries()); lbRedisServerBean.setPort(redisProxyMaster.getPort()); - lbRedisServerBean.setTestOnBorrow(redisProxyMaster.getTestOnBorrow()); - lbRedisServerBean.setTestOnReturn(redisProxyMaster.getTestOnReturn()); - lbRedisServerBean.setTestWhileIdle(redisProxyMaster.getTestWhileIdle()); - lbRedisServerBean.setTimeBetweenEvictionRunsMillis(redisProxyMaster.getTimeBetweenEvictionRunsMillis()); + lbRedisServerBean.setRedisPoolConfig(redisPoolConfig); return lbRedisServerBean; } @@ -159,28 +147,65 @@ public class RedisProxyNode implements InitializingBean { ZookeeperRegistryFactory zookeeperRegistryFactory=new ZookeeperRegistryFactory(); RedisProxyURL redisProxyURL=new RedisProxyURL(); - redisProxyURL.addParameter(RedisConstants.ADDRESS, this.getAddress()); + redisProxyURL.addParameter(RedisConstants.ADDRESS, this.getZkAddress()); Registry registry=zookeeperRegistryFactory.getRegistry(redisProxyURL); ZookeeperRegistryListen registryListen=new ZookeeperRegistryListen(ffanRedisServerMasterCluster); if(ffanRedisServerMasterCluster.getMasters()!=null&&ffanRedisServerMasterCluster.getMasters().size()>0){ - for(LBRedisServerBean ffanRedisServerBean:ffanRedisServerMasterCluster.getMasters()){ - RedisProxyURL newRedisProxyURL=new RedisProxyURL(ffanRedisServerBean.getHost(),ffanRedisServerBean.getPort(),ffanRedisServerBean.getTimeout()); - newRedisProxyURL.addParameter(RedisProxyParamType.REDISSERVER.getName(), JSONObject.toJSONString(ffanRedisServerBean)); - registry.register(newRedisProxyURL, registryListen);//监听主的变化 - List ffanRedisServerBeans=ffanRedisServerMasterCluster.getMasterFfanRedisServerBean(ffanRedisServerBean.getKey()); + for(LBRedisServerBean redisMasterServerBean:ffanRedisServerMasterCluster.getMasters()){ + RedisProxyURL newRedisProxyURL=new RedisProxyURL(redisMasterServerBean.getHost(),redisMasterServerBean.getPort(),redisMasterServerBean.getRedisPoolConfig().getTimeout()); + newRedisProxyURL.addParameter(RedisProxyParamType.REDISSERVER.getName(), JSONObject.toJSONString(redisMasterServerBean)); + Boolean masterFlag=checkRedisValueFromZookeeper(newRedisProxyURL, redisMasterServerBean, registry);//检查主 + + List ffanRedisServerBeans=ffanRedisServerMasterCluster.getMasterFfanRedisServerBean(redisMasterServerBean.getKey()); if(ffanRedisServerBeans!=null){ for(LBRedisServerBean ffanClusterRedisServerBean:ffanRedisServerBeans){//从 - RedisProxyURL newClusterRedisProxyURL=new RedisProxyURL(ffanClusterRedisServerBean.getHost(),ffanClusterRedisServerBean.getPort(),ffanClusterRedisServerBean.getTimeout()); - newClusterRedisProxyURL.setParentServerPath(ffanRedisServerBean.getServerKey()); - registry.createPersistent(newClusterRedisProxyURL, JSONObject.toJSONString(ffanClusterRedisServerBean));//写入从 + RedisProxyURL newClusterRedisProxyURL=new RedisProxyURL(ffanClusterRedisServerBean.getHost(),ffanClusterRedisServerBean.getPort(),ffanClusterRedisServerBean.getRedisPoolConfig().getTimeout()); + newClusterRedisProxyURL.setParentServerPath(redisMasterServerBean.getServerKey()); + Boolean flag=checkRedisValueFromZookeeper(newClusterRedisProxyURL, ffanClusterRedisServerBean, registry); + if(!flag&&!masterFlag){//没有值 + JSONObject slaveJSONObject=new JSONObject(); + slaveJSONObject.put("host", ffanClusterRedisServerBean.getHost()); + slaveJSONObject.put("port", ffanClusterRedisServerBean.getPort()); + slaveJSONObject.put("weight", ffanClusterRedisServerBean.getWeight());//权重 + registry.createPersistent(newClusterRedisProxyURL, slaveJSONObject.toJSONString());//写入从 + } + registry.registerSlave(newClusterRedisProxyURL, registryListen); } } + + if(!masterFlag){//没有值 + JSONObject masterJSONObject=new JSONObject(); + masterJSONObject.put("host", redisMasterServerBean.getHost()); + masterJSONObject.put("port", redisMasterServerBean.getPort()); + registry.createPersistent(newRedisProxyURL, masterJSONObject.toJSONString());//写入主 + } + registry.register(newRedisProxyURL, registryListen);//系统初始化不监控主的变化,启动成功以后监控 } } } + + /** + * 检查从zk是否有值 + * @return + */ + private Boolean checkRedisValueFromZookeeper(RedisProxyURL url,LBRedisServerBean ffanSlaveRedisServerBean,Registry registry){ + String nodeTypePath = ZkUtils.toNodePath(url,url.getParentServerPath(), ZkNodeType.AVAILABLE_SERVER); + if(registry.exist(nodeTypePath)){ + String data=registry.readData(nodeTypePath, true); + if(StringUtils.isEmpty(data)){ + return false; + } + JSONObject jsonObject=JSONObject.parseObject(data); + ffanSlaveRedisServerBean.setHost(jsonObject.getString("host")); + ffanSlaveRedisServerBean.setPort(jsonObject.getIntValue("port")); + return true; + } + return false; + } + /** * 启动server 容器 */ @@ -246,18 +271,20 @@ public class RedisProxyNode implements InitializingBean { } /** - * @return the address + * @return the zkAddress */ - public String getAddress() { - return address; + public String getZkAddress() { + return zkAddress; } /** - * @param address the address to set + * @param zkAddress the zkAddress to set */ - public void setAddress(String address) { - this.address = address; + public void setZkAddress(String zkAddress) { + this.zkAddress = zkAddress; } + + } diff --git a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxySlave.java b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxySlave.java index 21190cd8493e48a443463bbb631ec9e823945ad7..c49f518309c4277fc6db8134347889f28f666409 100644 --- a/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxySlave.java +++ b/nredis-proxy-spring/src/main/java/com/opensource/netty/redis/proxy/spring/schema/support/RedisProxySlave.java @@ -5,6 +5,8 @@ package com.opensource.netty.redis.proxy.spring.schema.support; import java.io.Serializable; +import com.opensource.netty.redis.proxy.core.config.RedisPoolConfig; + /** * @author liubing * @@ -20,35 +22,9 @@ public class RedisProxySlave implements Serializable { private int port;//端口号 - private int maxActiveConnection;//最大活动连接数 - - private int maxIdleConnection;//最大 空闲连接数 - - private int minConnection;//最小连接数 - - private int connectionTimeout;//连接超时 - - private int initialConnection = 0;//初始化值 - - private long maxWaitMillisOnBorrow; // 最大等待时间 - - private long timeBetweenEvictionRunsMillis;//回收间隔时间点 - - private long minEvictableIdleTimeMillis;//池中最小生存的时间 - - private int minIdleEntries = 0;//最小等待个数 - - private Boolean testOnBorrow=false;//取出验证 - - - - private Boolean testOnReturn=false;//回收时验证 - - private Boolean testWhileIdle=false;//回收空闲验证 - private int weight=1;//默认权重比例为1 - + private RedisPoolConfig redisPoolConfig; /** * @return the host */ @@ -77,48 +53,6 @@ public class RedisProxySlave implements Serializable { this.port = port; } - /** - * @return the maxActiveConnection - */ - public int getMaxActiveConnection() { - return maxActiveConnection; - } - - /** - * @param maxActiveConnection the maxActiveConnection to set - */ - public void setMaxActiveConnection(int maxActiveConnection) { - this.maxActiveConnection = maxActiveConnection; - } - - /** - * @return the maxIdleConnection - */ - public int getMaxIdleConnection() { - return maxIdleConnection; - } - - /** - * @param maxIdleConnection the maxIdleConnection to set - */ - public void setMaxIdleConnection(int maxIdleConnection) { - this.maxIdleConnection = maxIdleConnection; - } - - /** - * @return the minConnection - */ - public int getMinConnection() { - return minConnection; - } - - /** - * @param minConnection the minConnection to set - */ - public void setMinConnection(int minConnection) { - this.minConnection = minConnection; - } - /** * @return the weight */ @@ -133,141 +67,28 @@ public class RedisProxySlave implements Serializable { this.weight = weight; } - - - /** - * @return the initialConnection - */ - public int getInitialConnection() { - return initialConnection; - } - - /** - * @param initialConnection the initialConnection to set - */ - public void setInitialConnection(int initialConnection) { - this.initialConnection = initialConnection; - } - - /** - * @return the maxWaitMillisOnBorrow - */ - public long getMaxWaitMillisOnBorrow() { - return maxWaitMillisOnBorrow; - } /** - * @param maxWaitMillisOnBorrow the maxWaitMillisOnBorrow to set - */ - public void setMaxWaitMillisOnBorrow(long maxWaitMillisOnBorrow) { - this.maxWaitMillisOnBorrow = maxWaitMillisOnBorrow; - } - - /** - * @return the timeBetweenEvictionRunsMillis - */ - public long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; - } - - /** - * @param timeBetweenEvictionRunsMillis the timeBetweenEvictionRunsMillis to set - */ - public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - } - - /** - * @return the minEvictableIdleTimeMillis - */ - public long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; - } - - /** - * @param minEvictableIdleTimeMillis the minEvictableIdleTimeMillis to set - */ - public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; - } - - /** - * @return the minIdleEntries - */ - public int getMinIdleEntries() { - return minIdleEntries; - } - - /** - * @param minIdleEntries the minIdleEntries to set - */ - public void setMinIdleEntries(int minIdleEntries) { - this.minIdleEntries = minIdleEntries; - } - - /** - * @return the testOnBorrow - */ - public Boolean getTestOnBorrow() { - return testOnBorrow; - } - - /** - * @param testOnBorrow the testOnBorrow to set - */ - public void setTestOnBorrow(Boolean testOnBorrow) { - this.testOnBorrow = testOnBorrow; - } - - /** - * @return the connectionTimeout - */ - public int getConnectionTimeout() { - return connectionTimeout; - } - - /** - * @param connectionTimeout the connectionTimeout to set - */ - public void setConnectionTimeout(int connectionTimeout) { - this.connectionTimeout = connectionTimeout; - } - - /** - * @return the testOnReturn - */ - public Boolean getTestOnReturn() { - return testOnReturn; - } - - /** - * @param testOnReturn the testOnReturn to set - */ - public void setTestOnReturn(Boolean testOnReturn) { - this.testOnReturn = testOnReturn; - } - - /** - * @return the testWhileIdle + * @return the key */ - public Boolean getTestWhileIdle() { - return testWhileIdle; + public String getKey() { + StringBuilder stringBuilder=new StringBuilder(); + stringBuilder.append(host).append("-").append(port); + return stringBuilder.toString(); } /** - * @param testWhileIdle the testWhileIdle to set + * @return the redisPoolConfig */ - public void setTestWhileIdle(Boolean testWhileIdle) { - this.testWhileIdle = testWhileIdle; + public RedisPoolConfig getRedisPoolConfig() { + return redisPoolConfig; } /** - * @return the key + * @param redisPoolConfig the redisPoolConfig to set */ - public String getKey() { - StringBuilder stringBuilder=new StringBuilder(); - stringBuilder.append(host).append("-").append(port); - return stringBuilder.toString(); + public void setRedisPoolConfig(RedisPoolConfig redisPoolConfig) { + this.redisPoolConfig = redisPoolConfig; } } diff --git a/nredis-proxy-spring/src/main/resources/META-INF/NRedis-Proxy.xsd b/nredis-proxy-spring/src/main/resources/META-INF/NRedis-Proxy.xsd index 1e59242083bd80c29a8595458942d58e74571f79..b1fe73c1948f648a7b0f9715e4397969f0b4b28d 100644 --- a/nredis-proxy-spring/src/main/resources/META-INF/NRedis-Proxy.xsd +++ b/nredis-proxy-spring/src/main/resources/META-INF/NRedis-Proxy.xsd @@ -14,32 +14,8 @@ use="required" /> - - - - - - - - - - - - - - + @@ -58,31 +34,8 @@ use="required" /> - - - - - - - - - - - - - @@ -105,7 +58,7 @@ use="required" /> - diff --git a/pom.xml b/pom.xml index 3fa6640dec703a3379dd8375d1505d417a94d27d..77fe74ecb351118bc83497a4a6a41b8e4464dcc7 100644 --- a/pom.xml +++ b/pom.xml @@ -62,11 +62,7 @@ nredis-proxy-net 1.0 - - com.opensource - nredis-proxy-register - 1.0 - + com.opensource nredis-proxy-spring @@ -77,11 +73,6 @@ zookeeper 3.4.8 - - com.101tec - zkclient - 0.3 - org.slf4j slf4j-api @@ -133,9 +124,7 @@ nredis-proxy-core nredis-proxy-net nredis-proxy-pool - nredis-proxy-register nredis-proxy-spring - nredis-proxy-failover - nredis-proxy-sample + nredis-proxy-bootstrap \ No newline at end of file