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性能:
+
+ 
+
+内存性能:
+
+
+
+
+
+
+线程以及classloader性能:
+
+ 
+
+RedisServer性能监控
+
+ 
+ 
+ 
+
+RedisServer 主从自动切换监控
+
+ 
\ 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