From ec5181d5e528da5b4b4662dfb280b9d7a55e0781 Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Wed, 20 Sep 2017 08:34:04 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/mycat/mycat2/ProxyStarter.java | 14 +- .../io/mycat/mycat2/beans/BalancerBean.java | 59 +++++ .../io/mycat/mycat2/beans/ClusterBean.java | 67 ++++++ .../io/mycat/mycat2/beans/HeartbeatBean.java | 73 +++++++ .../java/io/mycat/mycat2/beans/ProxyBean.java | 34 +++ .../beans/heartbeat/MySQLHeartbeat.java | 12 +- .../main/java/io/mycat/proxy/ProxyConfig.java | 204 ++++-------------- .../java/io/mycat/proxy/ProxyRuntime.java | 7 +- .../java/io/mycat/proxy/man/MyCluster.java | 2 +- .../man/cmds/ConfigUpdatePacketCommand.java | 2 +- .../proxy/man/packet/NodeRegInfoPacket.java | 2 +- source/src/main/resources/mycat1.yml | 38 ++-- 12 files changed, 314 insertions(+), 200 deletions(-) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java create mode 100644 source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java create mode 100644 source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java create mode 100644 source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java diff --git a/source/src/main/java/io/mycat/mycat2/ProxyStarter.java b/source/src/main/java/io/mycat/mycat2/ProxyStarter.java index 13ba29e..6d720de 100644 --- a/source/src/main/java/io/mycat/mycat2/ProxyStarter.java +++ b/source/src/main/java/io/mycat/mycat2/ProxyStarter.java @@ -2,6 +2,7 @@ import java.io.IOException; +import io.mycat.mycat2.beans.ClusterBean; import io.mycat.mycat2.beans.ReplicaConfBean; import io.mycat.mycat2.beans.ReplicaIndexBean; import io.mycat.mycat2.beans.SchemaConfBean; @@ -36,11 +37,12 @@ public void start() throws IOException { acceptor.start(); runtime.setAcceptor(acceptor); - if (conf.isClusterEnable()) { + ClusterBean clusterBean = conf.getCluster(); + if (clusterBean.isEnable()) { // 集群开启状态,需要等集群启动,主节点确认完配置才能提供服务 - acceptor.startServerChannel(conf.getClusterIP(), conf.getClusterPort(), ServerType.CLUSTER); + acceptor.startServerChannel(clusterBean.getIp(), clusterBean.getPort(), ServerType.CLUSTER); runtime.setAdminCmdResolver(new AdminCommandResovler()); - MyCluster cluster = new MyCluster(acceptor.getSelector(), conf.getMyNodeId(), ClusterNode.parseNodesInf(conf.getAllNodeInfs())); + MyCluster cluster = new MyCluster(acceptor.getSelector(), clusterBean.getMyNodeId(), ClusterNode.parseNodesInf(clusterBean.getAllNodes())); runtime.setMyCLuster(cluster); cluster.initCluster(); } else { @@ -56,15 +58,15 @@ public void startProxy(boolean isLeader) throws IOException { // 加载配置文件信息 ConfigLoader.INSTANCE.loadAll(); NIOAcceptor acceptor = runtime.getAcceptor(); - acceptor.startServerChannel(conf.getBindIP(), conf.getBindPort(), ServerType.MYCAT); + acceptor.startServerChannel(conf.getProxy().getIp(), conf.getProxy().getPort(), ServerType.MYCAT); startReactor(); // 初始化 init(conf); - if(conf.isLoadBalanceEnable()){ + if(conf.getBalancer().isEnable()){ //开启负载均衡服务 runtime.setLocalLoadChecker(new LocalLoadChecker()); runtime.setLoadBalanceStrategy(new RandomStrategy()); - acceptor.startServerChannel(conf.getLoadBalanceIp(), conf.getLoadBalancePort(), ServerType.LOAD_BALANCER); + acceptor.startServerChannel(conf.getBalancer().getIp(), conf.getBalancer().getPort(), ServerType.LOAD_BALANCER); } // 主节点才启动心跳,非集群下也启动心跳 diff --git a/source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java b/source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java new file mode 100644 index 0000000..86c6a9b --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java @@ -0,0 +1,59 @@ +package io.mycat.mycat2.beans; + +/** + * Desc: 对应mycat.yml文件中的balancer + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class BalancerBean { + private boolean enable = false; + /** + * 负载均衡绑定的ip + */ + private String ip = "0.0.0.0"; + /** + * 负载均衡缓定的端口 + */ + private int port = 9066; + /** + * 负载均衡策略 + */ + private BalancerStrategy strategy; + + public enum BalancerStrategy { + RANDOM, ROUND_ROBIN, WEIGHT_RANDOM, WEIGHT_ROUND_ROBIN, RESPONSE_TIME, LEAST_CONNECTION, CAPACITY; + } + + public boolean isEnable() { + return enable; + } + + public void setEnable(boolean enable) { + this.enable = enable; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public BalancerStrategy getStrategy() { + return strategy; + } + + public void setStrategy(BalancerStrategy strategy) { + this.strategy = strategy; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java b/source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java new file mode 100644 index 0000000..3197971 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java @@ -0,0 +1,67 @@ +package io.mycat.mycat2.beans; + +/** + * Desc: 对应mycat.yml文件中的cluster + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class ClusterBean { + private boolean enable = false; + private String ip = "0.0.0.0"; + private int port = 9066; + private String myNodeId; + private String allNodes; + /** + * 用于集群中发送prepare报文等待确认的时间 + */ + private int prepareDelaySeconds = 30; + + public boolean isEnable() { + return enable; + } + + public void setEnable(boolean enable) { + this.enable = enable; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getMyNodeId() { + return myNodeId; + } + + public void setMyNodeId(String myNodeId) { + this.myNodeId = myNodeId; + } + + public String getAllNodes() { + return allNodes; + } + + public void setAllNodes(String allNodes) { + this.allNodes = allNodes; + } + + public int getPrepareDelaySeconds() { + return prepareDelaySeconds; + } + + public void setPrepareDelaySeconds(int prepareDelaySeconds) { + this.prepareDelaySeconds = prepareDelaySeconds; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java b/source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java new file mode 100644 index 0000000..e73dc1c --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java @@ -0,0 +1,73 @@ +package io.mycat.mycat2.beans; + +/** + * Desc: 对应mycat.yml文件中的heartbeat + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class HeartbeatBean { + private int timerExecutor = 2; + /** + * 默认复制组心跳周期 + */ + private long replicaHeartbeatPeriod = 10 * 1000L; + /** + * 默认复制组 空闲检查周期 + */ + private long replicaIdleCheckPeriod = 5 * 60 * 1000L; + /** + * 默认空闲超时时间 + */ + private long idleTimeout = 30 * 60 * 1000L; + private long processorCheckPeriod = 1 * 1000L;; + private long minSwitchtimeInterval = 30 * 60 * 1000L;; + + public int getTimerExecutor() { + return timerExecutor; + } + + public void setTimerExecutor(int timerExecutor) { + this.timerExecutor = timerExecutor; + } + + public long getReplicaHeartbeatPeriod() { + return replicaHeartbeatPeriod; + } + + public void setReplicaHeartbeatPeriod(long replicaHeartbeatPeriod) { + this.replicaHeartbeatPeriod = replicaHeartbeatPeriod; + } + + public long getReplicaIdleCheckPeriod() { + return replicaIdleCheckPeriod; + } + + public void setReplicaIdleCheckPeriod(long replicaIdleCheckPeriod) { + this.replicaIdleCheckPeriod = replicaIdleCheckPeriod; + } + + public long getIdleTimeout() { + return idleTimeout; + } + + public void setIdleTimeout(long idleTimeout) { + this.idleTimeout = idleTimeout; + } + + public long getProcessorCheckPeriod() { + return processorCheckPeriod; + } + + public void setProcessorCheckPeriod(long processorCheckPeriod) { + this.processorCheckPeriod = processorCheckPeriod; + } + + public long getMinSwitchtimeInterval() { + return minSwitchtimeInterval; + } + + public void setMinSwitchtimeInterval(long minSwitchtimeInterval) { + this.minSwitchtimeInterval = minSwitchtimeInterval; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java b/source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java new file mode 100644 index 0000000..85b595c --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java @@ -0,0 +1,34 @@ +package io.mycat.mycat2.beans; + +/** + * Desc: 对应mycat.yml文件中的proxy + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class ProxyBean { + /** + * 绑定的数据传输IP地址 + */ + private String ip = "0.0.0.0"; + /** + * 绑定的数据传输端口 + */ + private int port = 8066; + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java index 3c24e50..0a9c4cb 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java +++ b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java @@ -192,7 +192,7 @@ private void setOk(MySQLDetector detector) { case DBHeartbeat.INIT_STATUS: logger.info("current repl status [INIT_STATUS ---> OK_STATUS ]. update lastSwitchTime .{}:{}", source.getIp(), source.getPort()); MycatConfig conf = (MycatConfig)ProxyRuntime.INSTANCE.getProxyConfig(); - source.getRepBean().setLastSwitchTime(System.currentTimeMillis() - conf.getMinSwitchtimeInterval()); + source.getRepBean().setLastSwitchTime(System.currentTimeMillis() - conf.getHeartbeat().getMinSwitchtimeInterval()); case DBHeartbeat.OK_STATUS: default: this.status = OK_STATUS; @@ -216,11 +216,11 @@ private void setError(MySQLDetector detector, String msg) { MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); long curTime = System.currentTimeMillis(); - if (((curTime - source.getRepBean().getLastSwitchTime()) < conf.getMinSwitchtimeInterval()) - || (curTime - source.getRepBean().getLastInitTime()) < conf.getMinSwitchtimeInterval()) { + long minSwitchTimeInterval = conf.getHeartbeat().getMinSwitchtimeInterval(); + if (((curTime - source.getRepBean().getLastSwitchTime()) < minSwitchTimeInterval) + || (curTime - source.getRepBean().getLastInitTime()) < minSwitchTimeInterval) { if (logger.isDebugEnabled()) { - logger.warn("the Minimum time interval for switchSource is {} seconds.", - conf.getMinSwitchtimeInterval() / 1000L); + logger.warn("the Minimum time interval for switchSource is {} seconds.", minSwitchTimeInterval / 1000L); } return; } @@ -230,7 +230,7 @@ private void setError(MySQLDetector detector, String msg) { logger.error("all metaBean in replica is invalid !!!"); } else { String repName = source.getRepBean().getName(); - if (ProxyRuntime.INSTANCE.getProxyConfig().isClusterEnable()) { + if (ProxyRuntime.INSTANCE.getProxyConfig().getCluster().isEnable()) { ReplicaIndexBean bean = new ReplicaIndexBean(); Map map = new HashMap(conf.getRepIndexMap()); map.put(repName, next); diff --git a/source/src/main/java/io/mycat/proxy/ProxyConfig.java b/source/src/main/java/io/mycat/proxy/ProxyConfig.java index b138fa2..924a9b9 100644 --- a/source/src/main/java/io/mycat/proxy/ProxyConfig.java +++ b/source/src/main/java/io/mycat/proxy/ProxyConfig.java @@ -1,7 +1,9 @@ package io.mycat.proxy; -import java.util.HashMap; -import java.util.Map; +import io.mycat.mycat2.beans.BalancerBean; +import io.mycat.mycat2.beans.ClusterBean; +import io.mycat.mycat2.beans.HeartbeatBean; +import io.mycat.mycat2.beans.ProxyBean; /** * 代理的配置信息 @@ -10,186 +12,52 @@ * */ public class ProxyConfig implements Configurable { - // 绑定的数据传输IP地址 - private String bindIP = "0.0.0.0"; - // 绑定的数据传输端口 - private int bindPort = 8066; - private boolean clusterEnable = false; - // 是否开启负载均衡 - private boolean loadBalanceEnable = false; - // 负载均衡绑定的ip - private String loadBalanceIp = "0.0.0.0"; - // 负载均衡缓定的端口 - private int loadBalancePort = 9088; - // 集群通信绑定的IP地址 - private String clusterIP = "0.0.0.0"; - // 集群通信绑定的端口 - private int clusterPort = 9066; - private String myNodeId; - // 逗号分隔的所有集群节点的ID:IP:Port信息,如 - // leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9068,leader-3:127.0.0.1:9069 - private String allNodeInfs; + /** + * 代理相关配置 + */ + private ProxyBean proxy; + /** + * 集群相关配置 + */ + private ClusterBean cluster; + /** + * 负载均衡相关配置 + */ + private BalancerBean balancer; + /** + * 心跳相关配置 + */ + private HeartbeatBean heartbeat; - // 默认空闲超时时间 - private long idleTimeout = 30 * 60 * 1000L; - // 默认复制组 空闲检查周期 - private long replicaIdleCheckPeriod = 5 * 60 * 1000L; - // 默认复制组心跳周期 - private long replicaHeartbeatPeriod = 10 * 1000L; - - private int timerExecutor = 2; - - // sql execute timeout (second) - private long sqlExecuteTimeout = 300; - private long processorCheckPeriod = 1 * 1000L; - - private long minSwitchtimeInterval = 30 * 60 * 1000L; //默认三十分钟 - // 用于集群中发送prepare报文等待确认的时间,超时则认为失败,默认30s - private int prepareDelaySeconds = 30; - - public ProxyConfig() {} - - public String getBindIP() { - return bindIP; - } - - public void setBindIP(String bindIP) { - this.bindIP = bindIP; - } - - public int getBindPort() { - return bindPort; - } - - public void setBindPort(int bindPort) { - this.bindPort = bindPort; - } - - public boolean isClusterEnable() { - return clusterEnable; - } - - public void setClusterEnable(boolean clusterEnable) { - this.clusterEnable = clusterEnable; - } - - public String getMyNodeId() { - return myNodeId; - } - - public void setMyNodeId(String myNodeId) { - this.myNodeId = myNodeId; - } - - public String getAllNodeInfs() { - return allNodeInfs; - } - - public void setAllNodeInfs(String allNodeInfs) { - this.allNodeInfs = allNodeInfs; - } - - public String getClusterIP() { - return (clusterIP != null) ? clusterIP : this.bindIP; - } - - public void setClusterIP(String clusterIP) { - this.clusterIP = clusterIP; - } - - public int getClusterPort() { - return clusterPort; - } - - public void setClusterPort(int clusterPort) { - this.clusterPort = clusterPort; - } - - public long getIdleTimeout() { - return idleTimeout; - } - - public void setIdleTimeout(long idleTimeout) { - this.idleTimeout = idleTimeout; - } - - public int getTimerExecutor() { - return timerExecutor; - } - - public void setTimerExecutor(int timerExecutor) { - this.timerExecutor = timerExecutor; - } - - public long getSqlExecuteTimeout() { - return sqlExecuteTimeout; - } - - public void setSqlExecuteTimeout(long sqlExecuteTimeout) { - this.sqlExecuteTimeout = sqlExecuteTimeout; - } - - public long getProcessorCheckPeriod() { - return processorCheckPeriod; - } - - public void setProcessorCheckPeriod(long processorCheckPeriod) { - this.processorCheckPeriod = processorCheckPeriod; - } - - public long getReplicaIdleCheckPeriod() { - return replicaIdleCheckPeriod; - } - - public void setReplicaIdleCheckPeriod(long replicaIdleCheckPeriod) { - this.replicaIdleCheckPeriod = replicaIdleCheckPeriod; - } - - public long getReplicaHeartbeatPeriod() { - return replicaHeartbeatPeriod; - } - - public void setReplicaHeartbeatPeriod(long replicaHeartbeatPeriod) { - this.replicaHeartbeatPeriod = replicaHeartbeatPeriod; - } - - public long getMinSwitchtimeInterval() { - return minSwitchtimeInterval; - } - - public void setMinSwitchtimeInterval(long minSwitchtimeInterval) { - this.minSwitchtimeInterval = minSwitchtimeInterval; - } - - public boolean isLoadBalanceEnable() { - return loadBalanceEnable; + public ProxyBean getProxy() { + return proxy; } - public void setLoadBalanceEnable(boolean loadBalanceEnable) { - this.loadBalanceEnable = loadBalanceEnable; + public void setProxy(ProxyBean proxy) { + this.proxy = proxy; } - public String getLoadBalanceIp() { - return loadBalanceIp; + public ClusterBean getCluster() { + return cluster; } - public void setLoadBalanceIp(String loadBalanceIp) { - this.loadBalanceIp = loadBalanceIp; + public void setCluster(ClusterBean cluster) { + this.cluster = cluster; } - public int getLoadBalancePort() { - return loadBalancePort; + public BalancerBean getBalancer() { + return balancer; } - public void setLoadBalancePort(int loadBalancePort) { - this.loadBalancePort = loadBalancePort; + public void setBalancer(BalancerBean balancer) { + this.balancer = balancer; } - public int getPrepareDelaySeconds() { - return prepareDelaySeconds; + public HeartbeatBean getHeartbeat() { + return heartbeat; } - public void setPrepareDelaySeconds(int prepareDelaySeconds) { - this.prepareDelaySeconds = prepareDelaySeconds; + public void setHeartbeat(HeartbeatBean heartbeat) { + this.heartbeat = heartbeat; } } diff --git a/source/src/main/java/io/mycat/proxy/ProxyRuntime.java b/source/src/main/java/io/mycat/proxy/ProxyRuntime.java index 288320d..6de84a5 100644 --- a/source/src/main/java/io/mycat/proxy/ProxyRuntime.java +++ b/source/src/main/java/io/mycat/proxy/ProxyRuntime.java @@ -98,7 +98,7 @@ public class ProxyRuntime { public void init() { //心跳调度独立出来,避免被其他任务影响 heartbeatScheduler = Executors.newSingleThreadScheduledExecutor(); - timerExecutor = ExecutorUtil.create("Timer", getProxyConfig().getTimerExecutor()); + timerExecutor = ExecutorUtil.create("Timer", getProxyConfig().getHeartbeat().getTimerExecutor()); businessExecutor = ExecutorUtil.create("BusinessExecutor",Runtime.getRuntime().availableProcessors()); listeningExecutorService = MoreExecutors.listeningDecorator(businessExecutor); } @@ -118,7 +118,7 @@ public ProxyReactorThread getProxyReactorThread(ReactorEnv reactorEnv){ */ public void startHeartBeatScheduler(){ if(heartBeatTasks.get(REPLICA_HEARTBEAT)==null){ - long replicaHeartbeat = ((MycatConfig)getProxyConfig()).getReplicaHeartbeatPeriod(); + long replicaHeartbeat = getProxyConfig().getHeartbeat().getReplicaHeartbeatPeriod(); heartBeatTasks.put(REPLICA_HEARTBEAT, heartbeatScheduler.scheduleAtFixedRate(replicaHeartbeat(), 0, @@ -284,7 +284,8 @@ public void addDelayedNIOJob(Runnable job, int delayedSeconds, ProxyReactorThrea * 在NIO主线程中调度的延迟任务,重复执行 * * @param job - * @param delayedSeconds + * @param initialDelay + * @param period * @param nioThread */ public void addCronNIOJob(Runnable job, int initialDelay, int period, ProxyReactorThread nioThread) { diff --git a/source/src/main/java/io/mycat/proxy/man/MyCluster.java b/source/src/main/java/io/mycat/proxy/man/MyCluster.java index 9763392..0c65d71 100644 --- a/source/src/main/java/io/mycat/proxy/man/MyCluster.java +++ b/source/src/main/java/io/mycat/proxy/man/MyCluster.java @@ -68,7 +68,7 @@ public MyCluster(Selector nioSelector, String myNodeId, ArrayList a } myNode.setState(NodeState.Online); this.myNode = myNode; - this.myNode.proxyPort = ProxyRuntime.INSTANCE.getProxyConfig().getBindPort(); + this.myNode.proxyPort = ProxyRuntime.INSTANCE.getProxyConfig().getProxy().getPort(); } /** diff --git a/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java b/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java index f87ecc5..5de0052 100644 --- a/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java +++ b/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java @@ -83,7 +83,7 @@ public boolean sendPreparePacket(ConfigEnum configEnum, Object bean, String atta //todo config update 命令处理需要给前端返回 } cluster.configConfirmMap.remove(type); - }, runtime.getProxyConfig().getPrepareDelaySeconds()); + }, runtime.getProxyConfig().getCluster().getPrepareDelaySeconds()); return true; } diff --git a/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java b/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java index 5f6ff2a..72e7ded 100644 --- a/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java +++ b/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java @@ -29,7 +29,7 @@ public NodeRegInfoPacket(String nodeId, MyCluster.ClusterState myClusterState,lo this.lastClusterStateTime=lastClusterStateTime; setMyLeader(myLeader); this.startupTime = startupTime; - this.proxyPort = ProxyRuntime.INSTANCE.getProxyConfig().getBindPort(); + this.proxyPort = ProxyRuntime.INSTANCE.getProxyConfig().getProxy().getPort(); } public NodeRegInfoPacket() { diff --git a/source/src/main/resources/mycat1.yml b/source/src/main/resources/mycat1.yml index 83bebe9..5585552 100644 --- a/source/src/main/resources/mycat1.yml +++ b/source/src/main/resources/mycat1.yml @@ -1,15 +1,25 @@ -bindIP: 0.0.0.0 -bindPort: 8066 -clusterEnable: false -clusterIP: 0.0.0.0 -clusterPort: 9066 -myNodeId: leader-1 -allNodeInfs: leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9067,leader-3:127.0.0.1:9068 -timerExecutor: 2 -replicaHeartbeatPeriod: 10000 -replicaIdleCheckPeriod: 2000 -idleTimeout: 2000 -processorCheckPeriod: 2000 -prepareDelaySeconds: 30 -minSwitchtimeInterval: 120000 +proxy: + ip: 0.0.0.0 + port: 8066 +cluster: + enable: false + ip: 0.0.0.0 + port: 9066 + myNodeId: leader-1 + allNodes: leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9067,leader-3:127.0.0.1:9068 + prepareDelaySeconds: 30 + +balancer: + enable: false + ip: 0.0.0.0 + port: 7066 + strategy: RANDOM + +heartbeat: + timerExecutor: 2 + replicaHeartbeatPeriod: 10000 + replicaIdleCheckPeriod: 2000 + idleTimeout: 2000 + processorCheckPeriod: 2000 + minSwitchtimeInterval: 120000 \ No newline at end of file From 0cd7f26f5f17613ef523eeede716da54a4ae3f31 Mon Sep 17 00:00:00 2001 From: ishotoli Date: Sat, 16 Sep 2017 11:45:10 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmds/strategy/AbstractCmdStrategy.java | 22 +++++++++---------- .../SQLParserWithByteArrayInterfaceTest.java | 8 +++++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/source/src/main/java/io/mycat/mycat2/cmds/strategy/AbstractCmdStrategy.java b/source/src/main/java/io/mycat/mycat2/cmds/strategy/AbstractCmdStrategy.java index 7719376..1496ce8 100644 --- a/source/src/main/java/io/mycat/mycat2/cmds/strategy/AbstractCmdStrategy.java +++ b/source/src/main/java/io/mycat/mycat2/cmds/strategy/AbstractCmdStrategy.java @@ -7,6 +7,7 @@ import io.mycat.mycat2.MycatSession; import io.mycat.mycat2.cmds.CmdStrategy; import io.mycat.mycat2.cmds.DirectPassthrouhCmd; +import io.mycat.mycat2.sqlparser.BufferSQLParser; import io.mycat.mysql.packet.MySQLPacket; public abstract class AbstractCmdStrategy implements CmdStrategy { @@ -20,7 +21,12 @@ public abstract class AbstractCmdStrategy implements CmdStrategy { * 进行SQL命令的处理的容器 */ protected Map MYSQLCOMMANDMAP = new HashMap<>(); - + + /** + * sqlparser + */ + protected BufferSQLParser parser = new BufferSQLParser(); + public AbstractCmdStrategy(){ initMyCmdHandler(); initMySqlCmdHandler(); @@ -56,15 +62,9 @@ protected MySQLCommand doGetMyCommand(MycatSession session){ * @return */ protected MySQLCommand doGetMySQLCommand(MycatSession session){ - - /** - * sqlparser - */ -// BufferSQLParser parser = new BufferSQLParser(); -// int rowDataIndex = session.curMSQLPackgInf.startPos + MySQLPacket.packetHeaderSize +1 ; -// int length = session.curMSQLPackgInf.pkgLength - MySQLPacket.packetHeaderSize - 1 ; -// parser.parse(session.proxyBuffer.getBytes(rowDataIndex, length),session.sqlContext); - - return MYSQLCOMMANDMAP.get(session.sqlContext.getSQLType()); + parser.parse(session.proxyBuffer.getBuffer(), session.curMSQLPackgInf.startPos+MySQLPacket.packetHeaderSize+1, + session.curMSQLPackgInf.pkgLength - MySQLPacket.packetHeaderSize - 1, session.sqlContext); + System.out.println("getSQLType(0) : "+session.sqlContext.getSQLType(0)+" getSQLType() : "+session.sqlContext.getSQLType()); + return MYSQLCOMMANDMAP.get(session.sqlContext.getSQLType(0)); } } diff --git a/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java b/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java index 985f6db..fccf2c0 100644 --- a/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java +++ b/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java @@ -505,6 +505,14 @@ public void testSelectForUpdateSQL() throws Exception { assertEquals("tbl_A", context.getTableName(0)); } + @Test + public void testCustomSQL() throws Exception { + String sql = "SELECT DATABASE()"; + parser.parse(sql.getBytes(), context); + assertEquals(BufferSQLContext.SELECT_SQL, context.getSQLType()); + // assertEquals("sbtest1", context.getTableName(0)); + } + private static final String sql1 = "select t3.*,ztd3.TypeDetailName as UseStateName\n" + "from\n" + "( \n" + From 87e59b888cf4726fc8d3d87c3beafc16cd628929 Mon Sep 17 00:00:00 2001 From: ishotoli Date: Sat, 23 Sep 2017 15:54:26 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=95=B0?= =?UTF-8?q?=E5=80=BC=E8=A7=A3=E6=9E=90=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/mycat/mycat2/AbstractMySQLSession.java | 2 +- .../mycat2/sqlparser/BufferSQLParser.java | 3 ++- .../byteArrayInterface/Tokenizer2.java | 19 +++++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/source/src/main/java/io/mycat/mycat2/AbstractMySQLSession.java b/source/src/main/java/io/mycat/mycat2/AbstractMySQLSession.java index 1d14202..d2ccf71 100644 --- a/source/src/main/java/io/mycat/mycat2/AbstractMySQLSession.java +++ b/source/src/main/java/io/mycat/mycat2/AbstractMySQLSession.java @@ -25,7 +25,7 @@ * @author wuzhihui * */ -public abstract class AbstractMySQLSession extends AbstractSession { +public abstract class AbstractMySQLSession extends AbstractSession { // 当前接收到的包类型 public enum CurrPacketType { diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java index 4cc442c..c5965d1 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java @@ -647,7 +647,8 @@ public static void main(String[] args) { // byte[] defaultByteArray = "/*!MyCAT:DB_Type=Master*/select * from tbl_A where id=1;".getBytes(StandardCharsets.UTF_8); // byte[] defaultByteArray = "insert tbl_A(id, val) values(1, 2);\ninsert tbl_B(id, val) values(2, 2);\nSELECT id, val FROM tbl_S where id=19;\n".getBytes(StandardCharsets.UTF_8); - ByteArrayInterface src = new DefaultByteArray("/* mycat:balance*/select * into tbl_B from tbl_A;".getBytes()); +// ByteArrayInterface src = new DefaultByteArray("/* mycat:balance*/select * into tbl_B from tbl_A;".getBytes()); + ByteArrayInterface src = new DefaultByteArray("select 121345678;".getBytes()); // ByteArrayInterface src = new DefaultByteArray("select * into tbl_B from tbl_A;".getBytes()); // long min = 0; // for (int i = 0; i < 50; i++) { diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java index 88258dc..35a7383 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java @@ -121,10 +121,11 @@ int parseToken(ByteArrayInterface sql, int pos, final int sqlLength, byte c) { return pos; } - int parseString(ByteArrayInterface sql, int pos, final int sqlLength, int startSign) { + int parseString(ByteArrayInterface sql, int pos, final int sqlLength, byte startSign) { int size = 1; int start = pos; - int c; + long hash = 1315423911; + byte c = 0; while (++pos < sqlLength ) { c = sql.get(pos); if (c == '\\') { @@ -136,17 +137,19 @@ int parseString(ByteArrayInterface sql, int pos, final int sqlLength, int startS size++; } } - hashArray.set(STRINGS, start, size, 0L); + hashArray.set(STRINGS, start, size, hash); return ++pos; } - int parseDigits(ByteArrayInterface sql, int pos, final int sqlLength) { + int parseDigits(ByteArrayInterface sql, int pos, final int sqlLength, byte c) { // TODO: 需要增加小数类型处理吗? int start = pos; int size = 1; - while (++pos Date: Sat, 23 Sep 2017 15:55:54 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E8=A7=A3=E6=9E=90=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java | 1 + 1 file changed, 1 insertion(+) diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java index 35a7383..df301f0 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java @@ -134,6 +134,7 @@ int parseString(ByteArrayInterface sql, int pos, final int sqlLength, byte start size++; break; } else { + hash ^= (hash<<5) + c + (hash>>2);//使用JSHash对字符串进行哈希 size++; } } From fd8ef46942d8001a938cf4665821defdc5f40ab1 Mon Sep 17 00:00:00 2001 From: ishotoli Date: Sat, 23 Sep 2017 22:05:50 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E8=B0=83=E6=95=B4HashArray=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mycat2/sqlparser/BufferSQLContext.java | 5 ++--- .../mycat2/sqlparser/BufferSQLParser.java | 22 ++++++++++++------- .../byteArrayInterface/Tokenizer2.java | 9 ++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java index 55ec1e9..0dc760f 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java @@ -93,7 +93,7 @@ public class BufferSQLContext { private boolean hasLimit = false; private int limitStart = 0; private int limitCount = 0; - private HashArray hashArray; + private HashArray hashArray = new HashArray(); private int curSQLIdx; private int curSQLTblCount = 0; private int preHashArrayPos = 0; @@ -109,9 +109,8 @@ public BufferSQLContext() { myCmdValue = new HashArray(10); } - public void setCurBuffer(ByteArrayInterface curBuffer, HashArray hashArray) { + public void setCurBuffer(ByteArrayInterface curBuffer) { buffer = curBuffer; - this.hashArray = hashArray; totalTblCount = 0; schemaCount = 0; tblResultPos = 0; diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java index c5965d1..c7fdd3c 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java @@ -44,8 +44,8 @@ public class BufferSQLParser { ByteArrayInterface sql; - HashArray hashArray = new HashArray(); - Tokenizer2 tokenizer = new Tokenizer2(hashArray); + HashArray hashArray;// = new HashArray(); + Tokenizer2 tokenizer = new Tokenizer2(); DefaultByteArray defaultByteArray = new DefaultByteArray(); ByteBufferArray byteBufferArray = new ByteBufferArray(); @@ -600,24 +600,30 @@ public void parse(ByteBuffer src, int offset, int length, BufferSQLContext conte this.byteBufferArray.setLength(length); System.out.println("kaiz : "+this.byteBufferArray.getString(offset, length)); sql = this.byteBufferArray; - context.setCurBuffer(sql, hashArray); - tokenizer.tokenize(sql); + hashArray = context.getHashArray(); + hashArray.init(); + context.setCurBuffer(sql); + tokenizer.tokenize(sql, hashArray); firstParse(context); } public void parse(ByteArrayInterface src, BufferSQLContext context) { sql = src; - context.setCurBuffer(src, hashArray); - tokenizer.tokenize(src); + hashArray = context.getHashArray(); + hashArray.init(); + context.setCurBuffer(src); + tokenizer.tokenize(src, hashArray); firstParse(context); } public void parse(byte[] src, BufferSQLContext context) { this.defaultByteArray.setSrc(src); sql = this.defaultByteArray; - context.setCurBuffer(sql, hashArray); - tokenizer.tokenize(sql); + hashArray = context.getHashArray(); + hashArray.init(); + context.setCurBuffer(sql); + tokenizer.tokenize(sql, hashArray); firstParse(context); } diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java index df301f0..d0ac8f5 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/byteArrayInterface/Tokenizer2.java @@ -59,8 +59,7 @@ public class Tokenizer2 { - public Tokenizer2(HashArray hashArray) { - this.hashArray = hashArray; + public Tokenizer2() { //// TODO: 2017/2/21 可能需要调整顺序进行优化 IntStream.rangeClosed('0', '9').forEach(c -> charType[c<<1] = DIGITS); IntStream.rangeClosed('A', 'Z').forEach(c -> charType[c<<1] = CHARS); @@ -142,7 +141,7 @@ int parseString(ByteArrayInterface sql, int pos, final int sqlLength, byte start return ++pos; } - int parseDigits(ByteArrayInterface sql, int pos, final int sqlLength, byte c) { // TODO: 需要增加小数类型处理吗? + int parseDigits(ByteArrayInterface sql, int pos, final int sqlLength, byte c) { // TODO: 需要增加小数和hex类型处理吗? int start = pos; int size = 1; long longValue = (long)(c-'0'); @@ -209,11 +208,11 @@ int skipMultiLineComment(ByteArrayInterface sql, int pos, final int sqlLength, b return pos; } - public void tokenize(ByteArrayInterface sql) { + public void tokenize(ByteArrayInterface sql, HashArray hashArray) { int pos = sql.getOffset(); final int sqlLength = sql.length()+pos; this.sql = sql; - hashArray.init(); + this.hashArray = hashArray; byte c; byte cType; byte next; From 43b9e5e4caecb9287644e504d910695c3887acff Mon Sep 17 00:00:00 2001 From: ishotoli Date: Sat, 23 Sep 2017 22:44:48 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=20getTokenTyp?= =?UTF-8?q?e=20=E5=92=8C=20getTokenHash=20=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E6=B3=95=E5=8F=82=E8=A7=81=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mycat2/sqlparser/BufferSQLContext.java | 8 +++++++ .../sqlparser/SQLParseUtils/HashArray.java | 2 +- .../SQLParserWithByteArrayInterfaceTest.java | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java index 0dc760f..987fedd 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLContext.java @@ -161,6 +161,14 @@ public String getSchemaName(int idx) { } } + public long getTokenType(int sqlIdx, int sqlPos) { + return hashArray.getType(sqlInfoArray[sqlIdx << 2] + sqlPos); + } + + public long getTokenHash(int sqlIdx, int sqlPos) { + return hashArray.getHash(sqlInfoArray[sqlIdx << 2] + sqlPos); + } + public long getSchemaHash(int idx) { int hashArrayIdx = tblResult[idx << 1]; if (hashArrayIdx == 0) diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/SQLParseUtils/HashArray.java b/source/src/main/java/io/mycat/mycat2/sqlparser/SQLParseUtils/HashArray.java index a50af03..f95b273 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/SQLParseUtils/HashArray.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/SQLParseUtils/HashArray.java @@ -26,7 +26,7 @@ public void set(int type, int start, int size) { public void set(int type, int start, int size, long hash) { hashArray[pos++] = (long)type << 32 | size << 16 | start; hashArray[pos++] = hash; } public int getPos(int idx) { return (((int)hashArray[idx<<1]) & 0xFFFF); } public int getSize(int idx) { return (((int)hashArray[idx<<1]&0xFFFF0000) >>> 16); } - public int getType(int idx) { return (int)((hashArray[idx<<1]&0xFFFFFFFF00000000L)>>>32); } + public int getType(int idx) { return (int)((hashArray[idx<<1]&0xFFFFFFFF00000000L) >>> 32); } public void setType(int idx, int type) { hashArray[idx<<1] = (hashArray[idx<<1] & 0xFFFFFFFFL) | ((long)type << 32); } public long getHash(int idx) { return hashArray[(idx<<1)+1]; } public int getIntHash(int idx) { diff --git a/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java b/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java index fccf2c0..b5bcb08 100644 --- a/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java +++ b/source/src/test/java/io/mycat/mycat2/sqlparser/SQLParserWithByteArrayInterfaceTest.java @@ -1,5 +1,6 @@ package io.mycat.mycat2.sqlparser; +import io.mycat.mycat2.sqlparser.SQLParseUtils.Tokenizer; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; @@ -513,6 +514,27 @@ public void testCustomSQL() throws Exception { // assertEquals("sbtest1", context.getTableName(0)); } + @Test + public void testTokenHash1() throws Exception { + String sql = "select * from tbl_A;"; + parser.parse(sql.getBytes(), context); + assertEquals(TokenHash.FROM, context.getTokenHash(0, 2)); + } + + @Test + public void testTokenHash2() throws Exception { + String sql = "select * from tbl_A; select * from tbl_B where id > 100;"; + parser.parse(sql.getBytes(), context); + assertEquals(TokenHash.WHERE, context.getTokenHash(1, 4)); + } + + @Test + public void testTokenType() throws Exception { + String sql = "SELECT * FROM tbl_A where id = 1234;"; + parser.parse(sql.getBytes(), context); + assertEquals(Tokenizer.DIGITS, context.getTokenType(0, 7)); + } + private static final String sql1 = "select t3.*,ztd3.TypeDetailName as UseStateName\n" + "from\n" + "( \n" + From e52d5bcd2bd0fede1d3204222614edb274568a4c Mon Sep 17 00:00:00 2001 From: ishotoli Date: Sat, 23 Sep 2017 22:45:35 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E8=A1=A8?= =?UTF-8?q?=E5=90=8D=E5=90=8E=E7=9B=B4=E6=8E=A5=E8=B7=9F;=E4=BC=9A?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E6=AD=A3=E7=A1=AE=E5=88=A4=E6=96=ADsql?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java index c7fdd3c..ff078c3 100644 --- a/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java +++ b/source/src/main/java/io/mycat/mycat2/sqlparser/BufferSQLParser.java @@ -65,7 +65,9 @@ int pickTableNames(int pos, final int arrayCount, BufferSQLContext context) { //context.setTblNameStart(hashArray.getPos(pos));// TODO: 2017/3/10 可以优化成一个接口 //context.setTblNameSize(hashArray.getSize(pos)); ++pos; - } else if (type == Tokenizer.SEMICOLON || type == Tokenizer.RIGHT_PARENTHESES || type == Tokenizer.LEFT_PARENTHESES) { + } else if (type == Tokenizer.SEMICOLON) { + return pos; + } else if (type == Tokenizer.RIGHT_PARENTHESES || type == Tokenizer.LEFT_PARENTHESES) { return ++pos; } else if (type == Tokenizer.COMMA) { return pickTableNames(++pos, arrayCount, context); From f24ed008955c92d3438626c56ace07764bc846ce Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Sun, 24 Sep 2017 14:18:51 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/mycat/mycat2/ConfigLoader.java | 94 ++-------- .../java/io/mycat/mycat2/MycatConfig.java | 73 +++----- .../main/java/io/mycat/mycat2/MycatCore.java | 17 +- .../java/io/mycat/mycat2/ProxyStarter.java | 7 +- .../io/mycat/mycat2/beans/GlobalBean.java | 29 +++ .../io/mycat/mycat2/beans/MySQLMetaBean.java | 93 ++-------- .../io/mycat/mycat2/beans/MySQLRepBean.java | 166 +++--------------- .../mycat/mycat2/beans/ReplicaConfBean.java | 23 --- .../io/mycat/mycat2/beans/SchemaBean.java | 89 ---------- .../mycat/mycat2/beans/ShardingRuleBean.java | 75 -------- .../io/mycat/mycat2/beans/TableDefBean.java | 87 --------- .../mycat2/beans/{ => conf}/BalancerBean.java | 34 ++-- .../mycat2/beans/conf/BalancerConfig.java | 21 +++ .../mycat2/beans/{ => conf}/ClusterBean.java | 12 +- .../mycat2/beans/conf/ClusterConfig.java | 21 +++ .../mycat/mycat2/beans/{ => conf}/DNBean.java | 151 +++++++--------- .../mycat2/beans/conf/DatasourceConfig.java | 24 +++ .../mycat2/beans/conf/DatasourceMetaBean.java | 79 +++++++++ .../beans/{ => conf}/HeartbeatBean.java | 13 +- .../mycat2/beans/conf/HeartbeatConfig.java | 21 +++ .../mycat2/beans/{ => conf}/ProxyBean.java | 11 +- .../mycat/mycat2/beans/conf/ProxyConfig.java | 21 +++ .../mycat/mycat2/beans/conf/ReplicaBean.java | 100 +++++++++++ .../ReplicaIndexConfig.java} | 4 +- .../mycat/mycat2/beans/conf/SchemaBean.java | 64 +++++++ .../SchemaConfig.java} | 6 +- .../mycat2/beans/conf/ShardingRuleConfig.java | 52 ++++++ .../mycat/mycat2/beans/conf/TableDefBean.java | 65 +++++++ .../main/java/io/mycat/proxy/ConfigEnum.java | 19 +- .../main/java/io/mycat/proxy/ProxyConfig.java | 63 ------- .../java/io/mycat/proxy/ProxyRuntime.java | 20 +-- source/src/main/resources/balancer.yml | 5 + source/src/main/resources/cluster.yml | 7 + source/src/main/resources/datasource.yml | 6 +- source/src/main/resources/heartbeat.yml | 7 + source/src/main/resources/mycat.yml | 3 + source/src/main/resources/schema.yml | 12 +- 37 files changed, 748 insertions(+), 846 deletions(-) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java delete mode 100644 source/src/main/java/io/mycat/mycat2/beans/ReplicaConfBean.java delete mode 100644 source/src/main/java/io/mycat/mycat2/beans/SchemaBean.java delete mode 100644 source/src/main/java/io/mycat/mycat2/beans/ShardingRuleBean.java delete mode 100644 source/src/main/java/io/mycat/mycat2/beans/TableDefBean.java rename source/src/main/java/io/mycat/mycat2/beans/{ => conf}/BalancerBean.java (60%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/BalancerConfig.java rename source/src/main/java/io/mycat/mycat2/beans/{ => conf}/ClusterBean.java (77%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/ClusterConfig.java rename source/src/main/java/io/mycat/mycat2/beans/{ => conf}/DNBean.java (54%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java rename source/src/main/java/io/mycat/mycat2/beans/{ => conf}/HeartbeatBean.java (78%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatConfig.java rename source/src/main/java/io/mycat/mycat2/beans/{ => conf}/ProxyBean.java (68%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/ProxyConfig.java create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaBean.java rename source/src/main/java/io/mycat/mycat2/beans/{ReplicaIndexBean.java => conf/ReplicaIndexConfig.java} (82%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/SchemaBean.java rename source/src/main/java/io/mycat/mycat2/beans/{SchemaConfBean.java => conf/SchemaConfig.java} (72%) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/ShardingRuleConfig.java create mode 100644 source/src/main/java/io/mycat/mycat2/beans/conf/TableDefBean.java delete mode 100644 source/src/main/java/io/mycat/proxy/ProxyConfig.java create mode 100644 source/src/main/resources/balancer.yml create mode 100644 source/src/main/resources/cluster.yml create mode 100644 source/src/main/resources/heartbeat.yml create mode 100644 source/src/main/resources/mycat.yml diff --git a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java index c5f703d..58ed648 100644 --- a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java +++ b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java @@ -1,9 +1,10 @@ package io.mycat.mycat2; -import io.mycat.mycat2.beans.ReplicaConfBean; -import io.mycat.mycat2.beans.ReplicaIndexBean; -import io.mycat.mycat2.beans.SchemaConfBean; +import io.mycat.mycat2.beans.conf.DatasourceConfig; +import io.mycat.mycat2.beans.conf.ReplicaIndexConfig; +import io.mycat.mycat2.beans.conf.SchemaConfig; import io.mycat.proxy.ConfigEnum; +import io.mycat.proxy.Configurable; import io.mycat.proxy.ProxyRuntime; import io.mycat.util.YamlUtil; import org.slf4j.Logger; @@ -29,50 +30,36 @@ public class ConfigLoader { private ConfigLoader() {} public void loadAll() throws IOException { - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); // 保证文件夹存在 YamlUtil.createDirectoryIfNotExists(DIR_PREPARE); YamlUtil.createDirectoryIfNotExists(DIR_ARCHIVE); - loadReplicaIndex(false, conf, ConfigEnum.REPLICA_INDEX, null); - loadDatasource(false, conf, ConfigEnum.DATASOURCE, null); - loadSchema(false, conf, ConfigEnum.SCHEMA, null); + loadConfig(false, ConfigEnum.REPLICA_INDEX, null); + loadConfig(false, ConfigEnum.DATASOURCE, null); + loadConfig(false, ConfigEnum.SCHEMA, null); // 清空prepare文件夹 YamlUtil.clearDirectory(DIR_PREPARE, null); } public void load(ConfigEnum configEnum, Integer targetVersion) throws IOException { - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); - switch (configEnum) { - case REPLICA_INDEX: - loadReplicaIndex(true, conf, ConfigEnum.REPLICA_INDEX, targetVersion); - YamlUtil.clearDirectory(DIR_PREPARE, ConfigEnum.REPLICA_INDEX.getFileName()); - break; - case DATASOURCE: - loadDatasource(true, conf, ConfigEnum.DATASOURCE, targetVersion); - YamlUtil.clearDirectory(DIR_PREPARE, ConfigEnum.DATASOURCE.getFileName()); - break; - case SCHEMA: - loadSchema(true, conf, ConfigEnum.SCHEMA, targetVersion); - YamlUtil.clearDirectory(DIR_PREPARE, ConfigEnum.SCHEMA.getFileName()); - break; - case SHARDING_RULE: - break; - default: - return; - } + loadConfig(true, configEnum, targetVersion); + YamlUtil.clearDirectory(DIR_PREPARE, configEnum.getFileName()); } /** - * replica-index.yml + * 加载指定配置文件 + * @param needAchive 是否需要归档 + * @param configEnum 配置文件的枚举 + * @param targetVersion 指定的版本号 + * @throws IOException */ - private void loadReplicaIndex(boolean needAchive, MycatConfig conf, ConfigEnum configEnum, Integer targetVersion) throws IOException { + private void loadConfig(boolean needAchive, ConfigEnum configEnum, Integer targetVersion) throws IOException { // 加载replica-index String fileName = configEnum.getFileName(); - byte configType = configEnum.getType(); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); - Integer curVersion = conf.getConfigVersion(configType); + Integer curVersion = conf.getConfigVersion(configEnum); if (needAchive) { Integer repVersion = YamlUtil.archive(fileName, curVersion, targetVersion); if (repVersion == null) { @@ -80,53 +67,8 @@ private void loadReplicaIndex(boolean needAchive, MycatConfig conf, ConfigEnum c } curVersion = repVersion; } - LOGGER.debug("load config for {}", configEnum); - ReplicaIndexBean replicaIndexBean = YamlUtil.load(fileName, ReplicaIndexBean.class); - conf.addRepIndex(replicaIndexBean); - conf.putConfig(configType, replicaIndexBean, curVersion); - } - - /** - * datasource.yml - */ - private void loadDatasource(boolean needAchive, MycatConfig conf, ConfigEnum configEnum, Integer targetVersion) throws IOException { - // 加载datasource - String fileName = configEnum.getFileName(); - byte configType = configEnum.getType(); - Integer curVersion = conf.getConfigVersion(configType); - if (needAchive) { - Integer dsVersion = YamlUtil.archive(fileName, curVersion, targetVersion); - if (dsVersion == null) { - return; - } - curVersion = dsVersion; - } - LOGGER.debug("load config for {}", configEnum); - ReplicaConfBean replicaConfBean = YamlUtil.load(fileName, ReplicaConfBean.class); - replicaConfBean.getMysqlReplicas().forEach(replicaBean -> conf.addMySQLRepBean(replicaBean)); - conf.putConfig(configType, replicaConfBean, curVersion); - } - - /** - * schema.yml - */ - private void loadSchema(boolean needAchive, MycatConfig conf, ConfigEnum configEnum, Integer targetVersion) throws IOException { - // 加载schema - String fileName = configEnum.getFileName(); - byte configType = configEnum.getType(); - - Integer curVersion = conf.getConfigVersion(configType); - if (needAchive) { - Integer schemaVersion = YamlUtil.archive(fileName, curVersion, targetVersion); - if (schemaVersion == null) { - return; - } - curVersion = schemaVersion; - } LOGGER.debug("load config for {}", configEnum); - SchemaConfBean schemaConfBean = YamlUtil.load(fileName, SchemaConfBean.class); - schemaConfBean.getSchemas().forEach(schemaBean -> conf.addSchemaBean(schemaBean)); - conf.putConfig(configType, schemaConfBean, curVersion); + conf.putConfig(configEnum, (Configurable) YamlUtil.load(fileName, configEnum.getClazz()), curVersion); } } diff --git a/source/src/main/java/io/mycat/mycat2/MycatConfig.java b/source/src/main/java/io/mycat/mycat2/MycatConfig.java index 7c8800c..5541a97 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatConfig.java +++ b/source/src/main/java/io/mycat/mycat2/MycatConfig.java @@ -4,50 +4,41 @@ import java.util.HashMap; import java.util.Map; +import io.mycat.mycat2.beans.GlobalBean; import io.mycat.mycat2.beans.MySQLRepBean; -import io.mycat.mycat2.beans.ReplicaIndexBean; -import io.mycat.mycat2.beans.SchemaBean; +import io.mycat.mycat2.beans.conf.ProxyConfig; +import io.mycat.mycat2.beans.conf.SchemaBean; import io.mycat.proxy.ConfigEnum; import io.mycat.proxy.Configurable; -import io.mycat.proxy.ProxyConfig; -public class MycatConfig extends ProxyConfig { +public class MycatConfig { + private ProxyConfig proxyConfig; + // 当前节点所用的配置文件的版本 - private Map configVersionMap = new HashMap<>(); - private Map configMap = new HashMap<>(); + private Map configVersionMap = new HashMap<>(); + private Map configMap = new HashMap<>(); /** * 系统中所有MySQLRepBean的Map */ private Map mysqlRepMap = new HashMap(); - /** * 系统中所有SchemaBean的Map */ private Map mycatSchemaMap = new HashMap(); - /** * 默认Schema,取配置文件种第一个Schema */ private SchemaBean defaultSchemaBean; - private Map repIndexMap; + public MycatConfig(ProxyConfig proxyConfig) { + this.proxyConfig = proxyConfig; + } public Map getMysqlRepMap() { return this.mysqlRepMap; } - protected void addMySQLRepBean(final MySQLRepBean mySQLRepBean) { - this.mysqlRepMap.put(mySQLRepBean.getName(), mySQLRepBean); - } - - protected void addSchemaBean(SchemaBean schemaBean) { - if (defaultSchemaBean == null) { - defaultSchemaBean = schemaBean; - } - this.mycatSchemaMap.put(schemaBean.getName(), schemaBean); - } - public SchemaBean getMycatSchema(String schema) { return this.mycatSchemaMap.get(schema); } @@ -64,44 +55,30 @@ public Collection getMySQLReplicaSet(){ return this.mysqlRepMap.values(); } - public Integer getRepIndex(String repName) { - return repIndexMap.get(repName); - } - - public Map getRepIndexMap() { - return this.repIndexMap; - } - - public void addRepIndex(ReplicaIndexBean replicaIndexBean) { - if (replicaIndexBean != null && replicaIndexBean.getReplicaIndexes() != null) { - repIndexMap = replicaIndexBean.getReplicaIndexes(); - } - } - - public void setConfigVersion(byte configKey, int version) { - configVersionMap.put(configKey, version); + public void setConfigVersion(ConfigEnum configEnum, int version) { + configVersionMap.put(configEnum, version); } - public int getConfigVersion(byte configKey) { - Integer oldVersion = configVersionMap.get(configKey); - return oldVersion == null ? ConfigEnum.INIT_VERSION : oldVersion; + public int getConfigVersion(ConfigEnum configEnum) { + Integer oldVersion = configVersionMap.get(configEnum); + return oldVersion == null ? GlobalBean.INIT_VERSION : oldVersion; } - public int getNextConfigVersion(byte configKey) { - return getConfigVersion(configKey) + 1; + public int getNextConfigVersion(ConfigEnum configEnum) { + return getConfigVersion(configEnum) + 1; } - public Configurable getConfig(byte configKey) { - return configMap.get(configKey); + public Configurable getConfig(ConfigEnum configEnum) { + return configMap.get(configEnum); } - public void putConfig(byte configKey, Configurable config, Integer version) { - configMap.put(configKey, config); - version = version == null ? ConfigEnum.INIT_VERSION : version; - configVersionMap.put(configKey, version); + public void putConfig(ConfigEnum configEnum, Configurable config, Integer version) { + configMap.put(configEnum, config); + version = version == null ? GlobalBean.INIT_VERSION : version; + configVersionMap.put(configEnum, version); } - public Map getConfigVersionMap() { + public Map getConfigVersionMap() { return configVersionMap; } } diff --git a/source/src/main/java/io/mycat/mycat2/MycatCore.java b/source/src/main/java/io/mycat/mycat2/MycatCore.java index b4168d0..f24fd8d 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatCore.java +++ b/source/src/main/java/io/mycat/mycat2/MycatCore.java @@ -25,6 +25,8 @@ import java.io.IOException; +import io.mycat.mycat2.beans.conf.ProxyConfig; +import io.mycat.proxy.ConfigEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,22 +39,15 @@ */ public class MycatCore { private static final Logger logger = LoggerFactory.getLogger(MycatCore.class); - public static final String MOCK_HOSTNAME = "host1"; - - public static final String MOCK_SCHEMA = "mysql"; public static void main(String[] args) throws IOException { - String mySeq = "1"; - if (args.length > 0) { - mySeq = args[0]; - } - String mycatConf = "mycat" + mySeq + ".yml"; - logger.debug("load config for {}", mycatConf); // mycat.conf的加载不需要在集群内 - MycatConfig conf = YamlUtil.load(mycatConf, MycatConfig.class); + ProxyConfig proxy = YamlUtil.load(ConfigEnum.PROXY.getFileName(), ProxyConfig.class); + logger.debug("load config for {}", ConfigEnum.PROXY.getFileName()); + MycatConfig conf = new MycatConfig(proxy); ProxyRuntime runtime = ProxyRuntime.INSTANCE; - runtime.setProxyConfig(conf); + runtime.setConfig(conf); int cpus = Runtime.getRuntime().availableProcessors(); // int cpus = 1; diff --git a/source/src/main/java/io/mycat/mycat2/ProxyStarter.java b/source/src/main/java/io/mycat/mycat2/ProxyStarter.java index 6d720de..ce02831 100644 --- a/source/src/main/java/io/mycat/mycat2/ProxyStarter.java +++ b/source/src/main/java/io/mycat/mycat2/ProxyStarter.java @@ -2,10 +2,7 @@ import java.io.IOException; -import io.mycat.mycat2.beans.ClusterBean; -import io.mycat.mycat2.beans.ReplicaConfBean; -import io.mycat.mycat2.beans.ReplicaIndexBean; -import io.mycat.mycat2.beans.SchemaConfBean; +import io.mycat.mycat2.beans.conf.ClusterBean; import io.mycat.mycat2.loadbalance.LocalLoadChecker; import io.mycat.mycat2.loadbalance.RandomStrategy; import io.mycat.proxy.*; @@ -30,7 +27,7 @@ private ProxyStarter(){} public void start() throws IOException { ProxyRuntime runtime = ProxyRuntime.INSTANCE; - MycatConfig conf = (MycatConfig) runtime.getProxyConfig(); + MycatConfig conf = runtime.getConfig(); // 启动NIO Acceptor NIOAcceptor acceptor = new NIOAcceptor(new BufferPool(1024 * 10)); diff --git a/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java b/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java new file mode 100644 index 0000000..7509776 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java @@ -0,0 +1,29 @@ +package io.mycat.mycat2.beans; + +/** + * Desc: + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class GlobalBean { + public static final String SINGLE_NODE_HEARTBEAT_SQL = "select 1"; + public static final String MASTER_SLAVE_HEARTBEAT_SQL = "show slave status"; + public static final String GARELA_CLUSTER_HEARTBEAT_SQL = "show status like 'wsrep%'"; + public static final String GROUP_REPLICATION_HEARTBEAT_SQL = "show slave status"; + + public static final String[] MYSQL_SLAVE_STAUTS_COLMS = { + "Seconds_Behind_Master", + "Slave_IO_Running", + "Slave_SQL_Running", + "Slave_IO_State", + "Master_Host", + "Master_User", + "Master_Port", + "Connect_Retry", + "Last_IO_Error"}; + + public static final String[] MYSQL_CLUSTER_STAUTS_COLMS = {"Variable_name", "Value"}; + + public static final int INIT_VERSION = 1; +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java b/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java index 8135370..158af9c 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java @@ -29,6 +29,8 @@ import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; +import io.mycat.mycat2.beans.conf.DatasourceConfig; +import io.mycat.mycat2.beans.conf.DatasourceMetaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,19 +47,12 @@ * 后端mysql连接元数据类,对应datasource.xml配置中的mysql元数据信息 * @author wuzhihui */ -public class MySQLMetaBean { - +public class MySQLMetaBean extends DatasourceMetaBean { //默认的重试次数 private static final int MAX_RETRY_COUNT = 5; private static final Logger logger = LoggerFactory.getLogger(MySQLMetaBean.class); - private String hostName; - private String ip; - private int port; - private String user; - private String password; - private int maxCon = 1000; - private int minCon = 1; + private volatile boolean slaveNode = true; // 默认为slave节点 private volatile long heartbeatRecoveryTime; // 心跳暂停时间 private DBHeartbeat heartbeat; @@ -76,7 +71,7 @@ public class MySQLMetaBean { public boolean init(MySQLRepBean repBean,long maxwaitTime) throws IOException { - logger.info("init backend myqsl source ,create connections total " + minCon + " for " + hostName + " index :" + repBean.getWriteIndex()); + logger.info("init backend myqsl source ,create connections total " + getMinCon() + " for " + getHostName() + " index :" + repBean.getWriteIndex()); this.repBean = repBean; heartbeat = new MySQLHeartbeat(this,DBHeartbeat.INIT_STATUS); @@ -84,8 +79,8 @@ public boolean init(MySQLRepBean repBean,long maxwaitTime) throws IOException { MycatReactorThread[] reactorThreads = (MycatReactorThread[]) runtime.getReactorThreads(); int reactorSize = runtime.getNioReactorThreads(); CopyOnWriteArrayList list = new CopyOnWriteArrayList(); - BackendGetConnectionTask getConTask = new BackendGetConnectionTask(list, minCon); - for (int i = 0; i < minCon; i++) { + BackendGetConnectionTask getConTask = new BackendGetConnectionTask(list, getMinCon()); + for (int i = 0; i < getMinCon(); i++) { MycatReactorThread reactorThread = reactorThreads[i % reactorSize]; reactorThread.addNIOJob(() -> { try { @@ -135,7 +130,7 @@ public void doHeartbeat() { try { heartbeat.heartbeat(); } catch (Exception e) { - logger.error(hostName + " heartbeat error.", e); + logger.error(getHostName() + " heartbeat error.", e); } } @@ -170,62 +165,6 @@ public boolean canSelectAsReadNode() { return isSync && isNotDelay; } - public String getHostName() { - return hostName; - } - - public void setHostName(String hostName) { - this.hostName = hostName; - } - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public String getUser() { - return user; - } - - public void setUser(String user) { - this.user = user; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public int getMaxCon() { - return maxCon; - } - - public void setMaxCon(int maxCon) { - this.maxCon = maxCon; - } - - public int getMinCon() { - return minCon; - } - - public void setMinCon(int minCon) { - this.minCon = minCon; - } - public boolean isSlaveNode() { return slaveNode; } @@ -234,12 +173,6 @@ public void setSlaveNode(boolean slaveNode) { this.slaveNode = slaveNode; } - @Override - public String toString() { - return "MySQLMetaBean [hostName=" + hostName + ", ip=" + ip + ", port=" + port + ", user=" + user + ", password=" - + password + ", maxCon=" + maxCon + ", minCon=" + minCon + ", slaveNode=" + slaveNode + "]"; - } - public MySQLRepBean getRepBean() { return repBean; } @@ -247,7 +180,7 @@ public MySQLRepBean getRepBean() { public void setRepBean(MySQLRepBean repBean) { this.repBean = repBean; } - + public int getSlaveThreshold() { return slaveThreshold; } @@ -267,7 +200,7 @@ public void setMaxRetryCount(int maxRetryCount) { public DBHeartbeat getHeartbeat() { return heartbeat; } - + /** * 当前节点是否存活 * @return @@ -275,4 +208,10 @@ public DBHeartbeat getHeartbeat() { public boolean isAlive() { return heartbeat.getStatus() == DBHeartbeat.OK_STATUS; } + + @Override + public String toString() { + return "MySQLMetaBean [hostName=" + getHostName() + ", ip=" + getIp() + ", port=" + getPort() + ", user=" + getUser() + ", password=" + + getPassword() + ", maxCon=" + getMaxCon() + ", minCon=" + getMinCon() + ", slaveNode=" + slaveNode + "]"; + } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java b/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java index ce3631c..55f684b 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java @@ -31,6 +31,8 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; +import io.mycat.mycat2.beans.conf.DatasourceConfig; +import io.mycat.mycat2.beans.conf.ReplicaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,80 +45,13 @@ * * @author wuzhihui */ -public class MySQLRepBean { - +public class MySQLRepBean extends ReplicaBean { private static Logger logger = LoggerFactory.getLogger(MySQLRepBean.class); - - private final static String SINGLENODE_hearbeatSQL = "select 1"; - private final static String MASTER_SLAVE_hearbeatSQL = "show slave status"; - private final static String GARELA_CLUSTER_hearbeatSQL = "show status like 'wsrep%'"; - private final static String GROUP_REPLICATION_hearbeatSQL = "show slave status"; - - private static final String[] MYSQL_SLAVE_STAUTS_COLMS = new String[] { - "Seconds_Behind_Master", - "Slave_IO_Running", - "Slave_SQL_Running", - "Slave_IO_State", - "Master_Host", - "Master_User", - "Master_Port", - "Connect_Retry", - "Last_IO_Error"}; - - private static final String[] MYSQL_CLUSTER_STAUTS_COLMS = new String[] { - "Variable_name", - "Value"}; - - public enum RepTypeEnum { - - SINGLENODE(SINGLENODE_hearbeatSQL,MYSQL_SLAVE_STAUTS_COLMS), //单一节点 - MASTER_SLAVE(MASTER_SLAVE_hearbeatSQL,MYSQL_SLAVE_STAUTS_COLMS), //普通主从 - GARELA_CLUSTER(GARELA_CLUSTER_hearbeatSQL,MYSQL_CLUSTER_STAUTS_COLMS), //普通基于garela cluster 集群 - GROUP_REPLICATION(GROUP_REPLICATION_hearbeatSQL,MYSQL_SLAVE_STAUTS_COLMS); //基于 MGR 集群 - - private String hearbeatSQL; - - String[] fetchColms; - - RepTypeEnum(String hearbeatSQL,String[] fetchColms) { - this.hearbeatSQL = hearbeatSQL; - this.fetchColms = fetchColms; - } - - public String getHearbeatSQL() { - return hearbeatSQL; - } - - public String[] getFetchColms() { - return fetchColms; - } - } - public enum RepSwitchTypeEnum { - NOT_SWITCH, - DEFAULT_SWITCH, - SYN_STATUS_SWITCH, - CLUSTER_STATUS_SWITCH; - RepSwitchTypeEnum() {} - } - - public enum BalanceTypeEnum{ - BALANCE_ALL, - BALANCE_ALL_READ, - BALANCE_NONE; - BalanceTypeEnum() {} - } - - private String name; - private RepTypeEnum type; - private RepSwitchTypeEnum switchType = RepSwitchTypeEnum.DEFAULT_SWITCH; - private BalanceTypeEnum balance = BalanceTypeEnum.BALANCE_NONE; - private List mysqls; private String slaveIDs; // 在线数据迁移 虚拟从节点 private boolean tempReadHostAvailable = false; //如果写服务挂掉, 临时读服务是否继续可用 - - private MySQLMetaBean writeMetaBean; - private List readMetaBeans = new ArrayList<>(); + + private List metaBeans = new ArrayList<>(); protected final ReentrantLock switchLock = new ReentrantLock(); public AtomicBoolean switchResult = new AtomicBoolean(); @@ -126,71 +61,37 @@ public enum BalanceTypeEnum{ public void initMaster() { // 根据配置replica-index的配置文件修改主节点 - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); - Integer repIndex = conf.getRepIndex(name); - if (repIndex != null&&checkIndex(repIndex)) { + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + Integer repIndex = conf.getRepIndex(getName()); + if (repIndex != null && checkIndex(repIndex)) { writeIndex = repIndex; - }else{ + } else { writeIndex = 0; } - writeMetaBean = mysqls.get(writeIndex); + metaBeans.add(new MySQLMetaBean()); + writeMetaBean = getMysqls().get(writeIndex); writeMetaBean.setSlaveNode(false); readMetaBeans.addAll(mysqls); readMetaBeans.remove(writeIndex); } public void doHeartbeat() { - if (writeMetaBean == null) { return; } for (MySQLMetaBean source : this.mysqls) { - if (source != null) { source.doHeartbeat(); } else { StringBuilder s = new StringBuilder(); - s.append(Alarms.DEFAULT).append(name).append(" current dataSource is null!"); + s.append(Alarms.DEFAULT).append(getName()).append(" current dataSource is null!"); logger.error(s.toString()); } } } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public RepTypeEnum getType() { - return type; - } - - public void setType(RepTypeEnum type) { - this.type = type; - } - - public RepSwitchTypeEnum getSwitchType() { - return switchType; - } - - public void setSwitchType(RepSwitchTypeEnum switchType) { - this.switchType = switchType; - } - - public List getMysqls() { - return mysqls; - } - - public void setMysqls(List mysqls) { - this.mysqls = mysqls; - } - private boolean checkIndex(int newIndex){ return newIndex >= 0 && newIndex < mysqls.size(); } @@ -209,20 +110,16 @@ public int getNextIndex(){ } public void switchSource(int newIndex,long maxwaittime) { - - if (getSwitchType() == RepSwitchTypeEnum.NOT_SWITCH) { - if (logger.isDebugEnabled()) { - logger.debug("not switch datasource ,for switchType is {}",RepSwitchTypeEnum.NOT_SWITCH.name()); - } - + if (getSwitchType() == DatasourceConfig.RepSwitchTypeEnum.NOT_SWITCH) { + logger.debug("not switch datasource ,for switchType is {}", + DatasourceConfig.RepSwitchTypeEnum.NOT_SWITCH); + switchResult.set(false); return; } if(!checkIndex(newIndex)){ - if (logger.isDebugEnabled()) { - logger.debug("not switch datasource ,writeIndex > mysqls.size () "); - } + logger.debug("not switch datasource ,writeIndex > mysqls.size () "); switchResult.set(false); return; } @@ -270,7 +167,7 @@ public void switchSource(int newIndex,long maxwaittime) { private String switchMessage(int current, int newIndex, String reason) { StringBuilder s = new StringBuilder(); - s.append("[Host=").append(name).append(",result=[").append(current).append("->"); + s.append("[Host=").append(getName()).append(",result=[").append(current).append("->"); s.append(newIndex).append("],reason=").append(reason).append(']'); return s.toString(); } @@ -281,17 +178,15 @@ private String switchMessage(int current, int newIndex, String reason) { private MySQLMetaBean getCurWriteMetaBean() { return writeMetaBean.isAlive()?writeMetaBean:null; } - - + public MySQLMetaBean getBalanceMetaBean(boolean runOnSlave){ - - if(RepTypeEnum.SINGLENODE == type||!runOnSlave){ + if(DatasourceConfig.RepTypeEnum.SINGLE_NODE == getRepType()||!runOnSlave){ return getCurWriteMetaBean(); } MySQLMetaBean datas = null; - switch(balance){ + switch(getBalanceType()){ case BALANCE_ALL: datas = getLBReadWriteMetaBean(); break; @@ -299,7 +194,7 @@ public MySQLMetaBean getBalanceMetaBean(boolean runOnSlave){ datas = getLBReadMetaBean(); //如果从节点不可用,从主节点获取连接 if(datas==null){ - logger.debug("all slaveNode is Unavailable. use master node for read . balance type is {}",balance); + logger.debug("all slaveNode is Unavailable. use master node for read . balance type is {}",getBalanceType()); datas = getCurWriteMetaBean(); } break; @@ -307,7 +202,7 @@ public MySQLMetaBean getBalanceMetaBean(boolean runOnSlave){ datas = getCurWriteMetaBean(); break; default: - logger.debug("current balancetype is not supported!! [{}], use writenode connection .",balance); + logger.debug("current balancetype is not supported!! [{}], use writenode connection .",getBalanceType()); datas = getCurWriteMetaBean(); break; } @@ -334,11 +229,6 @@ private MySQLMetaBean getLBReadMetaBean(){ .collect(Collectors.toList()); return result.isEmpty()?null:result.get(ThreadLocalRandom.current().nextInt(result.size())); } - - @Override - public String toString() { - return "MySQLRepBean [name=" + name + ", type=" + type + ", switchType=" + switchType + ", mysqls=" + mysqls + "]"; - } public String getSlaveIDs() { return slaveIDs; @@ -360,16 +250,6 @@ public void setWriteMetaBean(MySQLMetaBean writeMetaBean) { this.writeMetaBean = writeMetaBean; } - - public BalanceTypeEnum getBalance() { - return balance; - } - - - public void setBalance(BalanceTypeEnum balance) { - this.balance = balance; - } - public int getWriteIndex() { return writeIndex; } diff --git a/source/src/main/java/io/mycat/mycat2/beans/ReplicaConfBean.java b/source/src/main/java/io/mycat/mycat2/beans/ReplicaConfBean.java deleted file mode 100644 index f65e1b5..0000000 --- a/source/src/main/java/io/mycat/mycat2/beans/ReplicaConfBean.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.mycat.mycat2.beans; - -import io.mycat.proxy.Configurable; - -import java.util.List; - -/** - * Desc: 用于加载datasource.yml的类 - * - * @date: 10/09/2017 - * @author: gaozhiwen - */ -public class ReplicaConfBean implements Configurable { - private List mysqlReplicas; - - public List getMysqlReplicas() { - return mysqlReplicas; - } - - public void setMysqlReplicas(List mysqlReplicas) { - this.mysqlReplicas = mysqlReplicas; - } -} diff --git a/source/src/main/java/io/mycat/mycat2/beans/SchemaBean.java b/source/src/main/java/io/mycat/mycat2/beans/SchemaBean.java deleted file mode 100644 index 3299d44..0000000 --- a/source/src/main/java/io/mycat/mycat2/beans/SchemaBean.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2016, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software;Designed and Developed mainly by many Chinese - * opensource volunteers. you can redistribute it and/or modify it under the - * terms of the GNU General Public License version 2 only, as published by the - * Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Any questions about this component can be directed to it's project Web address - * https://code.google.com/p/opencloudb/. - * - */ -package io.mycat.mycat2.beans; - -import java.util.List; - -/** - * Mycat Logic Schema - * - * @author wuzhihui - */ - -public class SchemaBean { - public enum SchemaType { - // 所有表在一个MySQL Server上(但不分片), - DB_IN_ONE_SERVER, - // 所有表在不同的MySQL Server上(但不分片), - DB_IN_MULTI_SERVER, - // 只使用基于SQL注解的路由模式(高性能但手工指定) - ANNOTATION_ROUTE, - // 使用SQL解析的方式去判断路由 - SQL_PARSE_ROUTE - } - - public String name; - public SchemaType type; - private DNBean defaultDN; - private List tables; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public SchemaType getType() { - return type; - } - - public void setType(SchemaType type) { - this.type = type; - } - - public DNBean getDefaultDN() { - return defaultDN; - } - - public void setDefaultDN(DNBean defaultDN) { - this.defaultDN = defaultDN; - } - - public List getTables() { - return tables; - } - - public void setTables(List tables) { - this.tables = tables; - } - - @Override - public String toString() { - return "SchemaBean [name=" + name + ", type=" + type + ", defaultDN=" + defaultDN - + ", tables=" + tables + "]"; - } - -} diff --git a/source/src/main/java/io/mycat/mycat2/beans/ShardingRuleBean.java b/source/src/main/java/io/mycat/mycat2/beans/ShardingRuleBean.java deleted file mode 100644 index 0291337..0000000 --- a/source/src/main/java/io/mycat/mycat2/beans/ShardingRuleBean.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2016, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software;Designed and Developed mainly by many Chinese - * opensource volunteers. you can redistribute it and/or modify it under the - * terms of the GNU General Public License version 2 only, as published by the - * Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Any questions about this component can be directed to it's project Web address - * https://code.google.com/p/opencloudb/. - * - */ -package io.mycat.mycat2.beans; - -import java.util.Map; - -/** - * sharding rule bean - * - * @author wuzhihui - */ -public class ShardingRuleBean { - private String name; - private String algorithm; - private Map params; - - public ShardingRuleBean(String name, String algorithm, Map params) { - super(); - this.name = name; - this.algorithm = algorithm; - this.params = params; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlgorithm() { - return algorithm; - } - - public void setAlgorithm(String algorithm) { - this.algorithm = algorithm; - } - - public Map getParams() { - return params; - } - - public void setParams(Map params) { - this.params = params; - } - - @Override - public String toString() { - return "ShardingRuleBean [name=" + name + ", algorithm=" + algorithm + ", params=" + params + "]"; - } - - -} diff --git a/source/src/main/java/io/mycat/mycat2/beans/TableDefBean.java b/source/src/main/java/io/mycat/mycat2/beans/TableDefBean.java deleted file mode 100644 index 864fc80..0000000 --- a/source/src/main/java/io/mycat/mycat2/beans/TableDefBean.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2016, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software;Designed and Developed mainly by many Chinese - * opensource volunteers. you can redistribute it and/or modify it under the - * terms of the GNU General Public License version 2 only, as published by the - * Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Any questions about this component can be directed to it's project Web address - * https://code.google.com/p/opencloudb/. - * - */ -package io.mycat.mycat2.beans; - -/** - * Mycat Table def Bean - * - * @author wuzhihui - */ -public class TableDefBean { - public enum TableTypeEnum { - MASTER, SLAVE; - } - - private String name; - private TableTypeEnum type; - private String shardingKey; - private String shardingRule; - private String store; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public TableTypeEnum getType() { - return type; - } - - public void setType(TableTypeEnum type) { - this.type = type; - } - - public String getShardingKey() { - return shardingKey; - } - - public void setShardingKey(String shardingKey) { - this.shardingKey = shardingKey; - } - - public String getShardingRule() { - return shardingRule; - } - - public void setShardingRule(String shardingRule) { - this.shardingRule = shardingRule; - } - - public String getStore() { - return store; - } - - public void setStore(String store) { - this.store = store; - } - - @Override - public String toString() { - return "TableDefBean [name=" + name + ", type=" + type + ", store=" + store - + ", shardingKey=" + shardingKey + ", shardingRule=" + shardingRule + "]"; - } -} diff --git a/source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java similarity index 60% rename from source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java index 86c6a9b..a7f6a54 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/BalancerBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java @@ -1,29 +1,20 @@ -package io.mycat.mycat2.beans; +package io.mycat.mycat2.beans.conf; /** - * Desc: 对应mycat.yml文件中的balancer + * Desc: 负载均衡配置类 * - * @date: 19/09/2017 + * @date: 24/09/2017 * @author: gaozhiwen */ public class BalancerBean { + public enum BalancerStrategyEnum { + RANDOM, ROUND_ROBIN, WEIGHT_RANDOM, WEIGHT_ROUND_ROBIN, RESPONSE_TIME, LEAST_CONNECTION, CAPACITY; + } + private boolean enable = false; - /** - * 负载均衡绑定的ip - */ private String ip = "0.0.0.0"; - /** - * 负载均衡缓定的端口 - */ private int port = 9066; - /** - * 负载均衡策略 - */ - private BalancerStrategy strategy; - - public enum BalancerStrategy { - RANDOM, ROUND_ROBIN, WEIGHT_RANDOM, WEIGHT_ROUND_ROBIN, RESPONSE_TIME, LEAST_CONNECTION, CAPACITY; - } + private BalancerStrategyEnum strategy = BalancerStrategyEnum.RANDOM; public boolean isEnable() { return enable; @@ -49,11 +40,16 @@ public void setPort(int port) { this.port = port; } - public BalancerStrategy getStrategy() { + public BalancerStrategyEnum getStrategy() { return strategy; } - public void setStrategy(BalancerStrategy strategy) { + public void setStrategy(BalancerStrategyEnum strategy) { this.strategy = strategy; } + + @Override + public String toString() { + return "BalancerBean{" + "enable=" + enable + ", ip='" + ip + '\'' + ", port=" + port + ", strategy=" + strategy + '}'; + } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerConfig.java new file mode 100644 index 0000000..bd54252 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerConfig.java @@ -0,0 +1,21 @@ +package io.mycat.mycat2.beans.conf; + +import io.mycat.proxy.Configurable; + +/** + * Desc: 对应mycat.yml文件,用于负载均衡,只有在集群模式启动下才生效 + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class BalancerConfig implements Configurable { + private BalancerBean balancer; + + public BalancerBean getBalancer() { + return balancer; + } + + public void setBalancer(BalancerBean balancer) { + this.balancer = balancer; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ClusterBean.java similarity index 77% rename from source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/ClusterBean.java index 3197971..5d0bfc4 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/ClusterBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ClusterBean.java @@ -1,9 +1,9 @@ -package io.mycat.mycat2.beans; +package io.mycat.mycat2.beans.conf; /** - * Desc: 对应mycat.yml文件中的cluster + * Desc: 集群配置类 * - * @date: 19/09/2017 + * @date: 24/09/2017 * @author: gaozhiwen */ public class ClusterBean { @@ -64,4 +64,10 @@ public int getPrepareDelaySeconds() { public void setPrepareDelaySeconds(int prepareDelaySeconds) { this.prepareDelaySeconds = prepareDelaySeconds; } + + @Override + public String toString() { + return "ClusterBean{" + "enable=" + enable + ", ip='" + ip + '\'' + ", port=" + port + ", myNodeId='" + myNodeId + '\'' + ", allNodes='" + + allNodes + '\'' + ", prepareDelaySeconds=" + prepareDelaySeconds + '}'; + } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/ClusterConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ClusterConfig.java new file mode 100644 index 0000000..8e4fec0 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ClusterConfig.java @@ -0,0 +1,21 @@ +package io.mycat.mycat2.beans.conf; + +import io.mycat.proxy.Configurable; + +/** + * Desc: 对应mycat.yml文件,集群配置 + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class ClusterConfig implements Configurable { + private ClusterBean cluster; + + public ClusterBean getCluster() { + return cluster; + } + + public void setCluster(ClusterBean cluster) { + this.cluster = cluster; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/DNBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/DNBean.java similarity index 54% rename from source/src/main/java/io/mycat/mycat2/beans/DNBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/DNBean.java index c118351..eee41ef 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/DNBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/DNBean.java @@ -1,87 +1,64 @@ -/* - * Copyright (c) 2016, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software;Designed and Developed mainly by many Chinese - * opensource volunteers. you can redistribute it and/or modify it under the - * terms of the GNU General Public License version 2 only, as published by the - * Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Any questions about this component can be directed to it's project Web address - * https://code.google.com/p/opencloudb/. - * - */ -package io.mycat.mycat2.beans; - -/** - * refer a database on a mysql replica - * - * @author wuzhihui - */ - -public class DNBean { - private String database; - private String mysqlReplica; - - public String getDatabase() { - return database; - } - - public void setDatabase(String database) { - this.database = database; - } - - public String getMysqlReplica() { - return mysqlReplica; - } - - public void setMysqlReplica(String mysqlReplica) { - this.mysqlReplica = mysqlReplica; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((database == null) ? 0 : database.hashCode()); - result = prime * result + ((mysqlReplica == null) ? 0 : mysqlReplica.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - DNBean other = (DNBean) obj; - if (database == null) { - if (other.database != null) - return false; - } else if (!database.equals(other.database)) - return false; - if (mysqlReplica == null) { - if (other.mysqlReplica != null) - return false; - } else if (!mysqlReplica.equals(other.mysqlReplica)) - return false; - return true; - } - - @Override - public String toString() { - return "DNBean [database=" + database + ", mysqlReplica=" + mysqlReplica + "]"; - } -} +package io.mycat.mycat2.beans.conf; + +/** + * Desc: + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class DNBean { + private String database; + private String mysqlReplica; + + public String getDatabase() { + return database; + } + + public void setDatabase(String database) { + this.database = database; + } + + public String getMysqlReplica() { + return mysqlReplica; + } + + public void setMysqlReplica(String mysqlReplica) { + this.mysqlReplica = mysqlReplica; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((database == null) ? 0 : database.hashCode()); + result = prime * result + ((mysqlReplica == null) ? 0 : mysqlReplica.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DNBean other = (DNBean) obj; + if (database == null) { + if (other.database != null) + return false; + } else if (!database.equals(other.database)) + return false; + if (mysqlReplica == null) { + if (other.mysqlReplica != null) + return false; + } else if (!mysqlReplica.equals(other.mysqlReplica)) + return false; + return true; + } + + @Override + public String toString() { + return "DNBean [database=" + database + ", mysqlReplica=" + mysqlReplica + "]"; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java new file mode 100644 index 0000000..c702f49 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java @@ -0,0 +1,24 @@ +package io.mycat.mycat2.beans.conf; + +import io.mycat.mycat2.beans.GlobalBean; +import io.mycat.proxy.Configurable; + +import java.util.List; + +/** + * Desc: 用于加载datasource.yml的类 + * + * @date: 10/09/2017 + * @author: gaozhiwen + */ +public class DatasourceConfig implements Configurable { + private List replicas; + + public List getReplicas() { + return replicas; + } + + public void setReplicas(List replicas) { + this.replicas = replicas; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java new file mode 100644 index 0000000..60fd150 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java @@ -0,0 +1,79 @@ +package io.mycat.mycat2.beans.conf; + +/** + * Desc: + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class DatasourceMetaBean { + private String hostName; + private String ip; + private int port; + private String user; + private String password; + private int maxCon = 1000; + private int minCon = 1; + + public String getHostName() { + return hostName; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public int getMaxCon() { + return maxCon; + } + + public void setMaxCon(int maxCon) { + this.maxCon = maxCon; + } + + public int getMinCon() { + return minCon; + } + + public void setMinCon(int minCon) { + this.minCon = minCon; + } + + @Override + public String toString() { + return "DatasourceMetaBean{" + "hostName='" + hostName + '\'' + ", ip='" + ip + '\'' + ", port=" + port + ", user='" + user + '\'' + + ", password='" + password + '\'' + ", maxCon=" + maxCon + ", minCon=" + minCon + '}'; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatBean.java similarity index 78% rename from source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatBean.java index e73dc1c..df075b5 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/HeartbeatBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatBean.java @@ -1,9 +1,9 @@ -package io.mycat.mycat2.beans; +package io.mycat.mycat2.beans.conf; /** - * Desc: 对应mycat.yml文件中的heartbeat + * Desc: 心跳配置类 * - * @date: 19/09/2017 + * @date: 24/09/2017 * @author: gaozhiwen */ public class HeartbeatBean { @@ -70,4 +70,11 @@ public long getMinSwitchtimeInterval() { public void setMinSwitchtimeInterval(long minSwitchtimeInterval) { this.minSwitchtimeInterval = minSwitchtimeInterval; } + + @Override + public String toString() { + return "HeartbeatBean{" + "timerExecutor=" + timerExecutor + ", replicaHeartbeatPeriod=" + replicaHeartbeatPeriod + + ", replicaIdleCheckPeriod=" + replicaIdleCheckPeriod + ", idleTimeout=" + idleTimeout + ", processorCheckPeriod=" + + processorCheckPeriod + ", minSwitchtimeInterval=" + minSwitchtimeInterval + '}'; + } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatConfig.java new file mode 100644 index 0000000..c19871c --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/HeartbeatConfig.java @@ -0,0 +1,21 @@ +package io.mycat.mycat2.beans.conf; + +import io.mycat.proxy.Configurable; + +/** + * Desc: 对应mycat.yml文件 + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class HeartbeatConfig implements Configurable { + private HeartbeatBean heartbeat; + + public HeartbeatBean getHeartbeat() { + return heartbeat; + } + + public void setHeartbeat(HeartbeatBean heartbeat) { + this.heartbeat = heartbeat; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ProxyBean.java similarity index 68% rename from source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/ProxyBean.java index 85b595c..c8532ef 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/ProxyBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ProxyBean.java @@ -1,9 +1,9 @@ -package io.mycat.mycat2.beans; +package io.mycat.mycat2.beans.conf; /** - * Desc: 对应mycat.yml文件中的proxy + * Desc: mycat代理配置类 * - * @date: 19/09/2017 + * @date: 24/09/2017 * @author: gaozhiwen */ public class ProxyBean { @@ -31,4 +31,9 @@ public int getPort() { public void setPort(int port) { this.port = port; } + + @Override + public String toString() { + return "ProxyBean{" + "ip='" + ip + '\'' + ", port=" + port + '}'; + } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/ProxyConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ProxyConfig.java new file mode 100644 index 0000000..b1d1579 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ProxyConfig.java @@ -0,0 +1,21 @@ +package io.mycat.mycat2.beans.conf; + +import io.mycat.proxy.Configurable; + +/** + * Desc: 对应mycat.yml文件 + * + * @date: 19/09/2017 + * @author: gaozhiwen + */ +public class ProxyConfig implements Configurable { + private ProxyBean proxy; + + public ProxyBean getProxy() { + return proxy; + } + + public void setProxy(ProxyBean proxy) { + this.proxy = proxy; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaBean.java new file mode 100644 index 0000000..3e1c3f5 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaBean.java @@ -0,0 +1,100 @@ +package io.mycat.mycat2.beans.conf; + +import io.mycat.mycat2.beans.GlobalBean; + +import java.util.List; + +/** + * Desc: 数据源replica配置类 + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class ReplicaBean { + public enum BalanceTypeEnum { + BALANCE_ALL, BALANCE_ALL_READ, BALANCE_NONE; + } + + public enum RepSwitchTypeEnum { + NOT_SWITCH, DEFAULT_SWITCH, SYN_STATUS_SWITCH, CLUSTER_STATUS_SWITCH; + } + + public enum RepTypeEnum { + // 单一节点 + SINGLE_NODE(GlobalBean.SINGLE_NODE_HEARTBEAT_SQL, GlobalBean.MYSQL_SLAVE_STAUTS_COLMS), + // 普通主从 + MASTER_SLAVE(GlobalBean.MASTER_SLAVE_HEARTBEAT_SQL, GlobalBean.MYSQL_SLAVE_STAUTS_COLMS), + // 普通基于garela cluster集群 + GARELA_CLUSTER(GlobalBean.GARELA_CLUSTER_HEARTBEAT_SQL, GlobalBean.MYSQL_CLUSTER_STAUTS_COLMS), + // 基于MGR集群 + GROUP_REPLICATION(GlobalBean.GROUP_REPLICATION_HEARTBEAT_SQL, GlobalBean.MYSQL_SLAVE_STAUTS_COLMS); + + private String hearbeatSQL; + private String[] fetchColms; + + RepTypeEnum(String hearbeatSQL, String[] fetchColms) { + this.hearbeatSQL = hearbeatSQL; + this.fetchColms = fetchColms; + } + + public String getHearbeatSQL() { + return hearbeatSQL; + } + + public String[] getFetchColms() { + return fetchColms; + } + } + + private String name; + private RepTypeEnum repType; + private RepSwitchTypeEnum switchType; + private BalanceTypeEnum balanceType; + private List mysqls; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public RepTypeEnum getRepType() { + return repType; + } + + public void setRepType(RepTypeEnum repType) { + this.repType = repType; + } + + public RepSwitchTypeEnum getSwitchType() { + return switchType; + } + + public void setSwitchType(RepSwitchTypeEnum switchType) { + this.switchType = switchType; + } + + public BalanceTypeEnum getBalanceType() { + return balanceType; + } + + public void setBalanceType(BalanceTypeEnum balanceType) { + this.balanceType = balanceType; + } + + public List getMysqls() { + return mysqls; + } + + public void setMysqls(List mysqls) { + this.mysqls = mysqls; + } + + @Override + public String toString() { + return "ReplicaBean{" + "name='" + name + '\'' + ", repType=" + repType + ", switchType=" + switchType + ", balanceType=" + balanceType + + ", mysqls=" + mysqls + '}'; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/ReplicaIndexBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaIndexConfig.java similarity index 82% rename from source/src/main/java/io/mycat/mycat2/beans/ReplicaIndexBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaIndexConfig.java index cb7197c..62d57b3 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/ReplicaIndexBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ReplicaIndexConfig.java @@ -1,4 +1,4 @@ -package io.mycat.mycat2.beans; +package io.mycat.mycat2.beans.conf; import io.mycat.proxy.Configurable; @@ -10,7 +10,7 @@ * @date: 10/09/2017 * @author: gaozhiwen */ -public class ReplicaIndexBean implements Configurable { +public class ReplicaIndexConfig implements Configurable { private Map replicaIndexes; public Map getReplicaIndexes() { diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/SchemaBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/SchemaBean.java new file mode 100644 index 0000000..0900773 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/SchemaBean.java @@ -0,0 +1,64 @@ +package io.mycat.mycat2.beans.conf; + +import java.util.List; + +/** + * Desc: + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class SchemaBean { + public enum SchemaTypeEnum { + // 所有表在一个MySQL Server上(但不分片), + DB_IN_ONE_SERVER, + // 所有表在不同的MySQL Server上(但不分片), + DB_IN_MULTI_SERVER, + // 只使用基于SQL注解的路由模式(高性能但手工指定) + ANNOTATION_ROUTE, + // 使用SQL解析的方式去判断路由 + SQL_PARSE_ROUTE; + } + + public String name; + public SchemaTypeEnum schemaType; + private DNBean defaultDN; + private List tables; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public SchemaTypeEnum getSchemaType() { + return schemaType; + } + + public void setSchemaType(SchemaTypeEnum schemaType) { + this.schemaType = schemaType; + } + + public DNBean getDefaultDN() { + return defaultDN; + } + + public void setDefaultDN(DNBean defaultDN) { + this.defaultDN = defaultDN; + } + + public List getTables() { + return tables; + } + + public void setTables(List tables) { + this.tables = tables; + } + + @Override + public String toString() { + return "SchemaBean{" + "name='" + name + '\'' + ", schemaType=" + schemaType + ", defaultDN=" + defaultDN + ", tables=" + tables + '}'; + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/SchemaConfBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/SchemaConfig.java similarity index 72% rename from source/src/main/java/io/mycat/mycat2/beans/SchemaConfBean.java rename to source/src/main/java/io/mycat/mycat2/beans/conf/SchemaConfig.java index 149c85a..60a5300 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/SchemaConfBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/SchemaConfig.java @@ -1,16 +1,16 @@ -package io.mycat.mycat2.beans; +package io.mycat.mycat2.beans.conf; import io.mycat.proxy.Configurable; import java.util.List; /** - * Desc: 用于加载schema.yml的类 + * Desc: 对应schema.yml文件 * * @date: 10/09/2017 * @author: gaozhiwen */ -public class SchemaConfBean implements Configurable { +public class SchemaConfig implements Configurable { private List schemas; public List getSchemas() { diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/ShardingRuleConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/ShardingRuleConfig.java new file mode 100644 index 0000000..a532591 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/ShardingRuleConfig.java @@ -0,0 +1,52 @@ +package io.mycat.mycat2.beans.conf; + +import java.util.List; +import java.util.Map; + +/** + * Desc: 对应sharding-rule.yml文件 + * + * @date: 23/09/2017 + * @author: gaozhiwen + */ +public class ShardingRuleConfig { + private List shardingRules; + + public List getShardingRules() { + return shardingRules; + } + + public void setShardingRules(List shardingRules) { + this.shardingRules = shardingRules; + } + + public static class ShardingRuleBean { + private String name; + private String algorithm; + private Map params; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAlgorithm() { + return algorithm; + } + + public void setAlgorithm(String algorithm) { + this.algorithm = algorithm; + } + + public Map getParams() { + return params; + } + + public void setParams(Map params) { + this.params = params; + } + } +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/TableDefBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/TableDefBean.java new file mode 100644 index 0000000..057f26b --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/TableDefBean.java @@ -0,0 +1,65 @@ +package io.mycat.mycat2.beans.conf; + +/** + * Desc: + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class TableDefBean { + public enum TableTypeEnum { + MASTER, SLAVE; + } + + private String name; + private TableTypeEnum tableType; + private String shardingKey; + private String shardingRule; + private String store; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public TableTypeEnum getTableType() { + return tableType; + } + + public void setTableType(TableTypeEnum tableType) { + this.tableType = tableType; + } + + public String getShardingKey() { + return shardingKey; + } + + public void setShardingKey(String shardingKey) { + this.shardingKey = shardingKey; + } + + public String getShardingRule() { + return shardingRule; + } + + public void setShardingRule(String shardingRule) { + this.shardingRule = shardingRule; + } + + public String getStore() { + return store; + } + + public void setStore(String store) { + this.store = store; + } + + @Override + public String toString() { + return "TableDefBean [name=" + name + ", tableType=" + tableType + ", store=" + store + ", shardingKey=" + shardingKey + ", shardingRule=" + + shardingRule + "]"; + } +} diff --git a/source/src/main/java/io/mycat/proxy/ConfigEnum.java b/source/src/main/java/io/mycat/proxy/ConfigEnum.java index cd4ee6a..2b63622 100644 --- a/source/src/main/java/io/mycat/proxy/ConfigEnum.java +++ b/source/src/main/java/io/mycat/proxy/ConfigEnum.java @@ -1,9 +1,6 @@ package io.mycat.proxy; -import io.mycat.mycat2.beans.ReplicaConfBean; -import io.mycat.mycat2.beans.ReplicaIndexBean; -import io.mycat.mycat2.beans.SchemaConfBean; -import io.mycat.mycat2.beans.ShardingRuleBean; +import io.mycat.mycat2.beans.conf.*; import java.util.stream.Stream; @@ -14,10 +11,14 @@ * @author: gaozhiwen */ public enum ConfigEnum { - DATASOURCE((byte) 1, "datasource.yml", ReplicaConfBean.class), - REPLICA_INDEX((byte) 2, "replica-index.yml", ReplicaIndexBean.class), - SCHEMA((byte) 3, "schema.yml", SchemaConfBean.class), - SHARDING_RULE((byte) 4, "sharding-rule.yml", ShardingRuleBean.class); + PROXY((byte) 1, "mycat.yml", ProxyConfig.class), + CLUSTER((byte) 2, "cluster.yml", ClusterConfig.class), + BALANCER((byte) 3, "balancer.yml", BalancerConfig.class), + HEARTBEAT((byte) 4, "heartbeat.yml", HeartbeatConfig.class), + DATASOURCE((byte) 5, "datasource.yml", DatasourceConfig.class), + REPLICA_INDEX((byte) 6, "replica-index.yml", ReplicaIndexConfig.class), + SCHEMA((byte) 7, "schema.yml", SchemaConfig.class), + SHARDING_RULE((byte) 8, "sharding-rule.yml", ShardingRuleConfig.class); private byte type; private String fileName; @@ -45,6 +46,4 @@ public static ConfigEnum getConfigEnum(byte type) { return Stream.of(ConfigEnum.values()).filter(configEnum -> configEnum.type == type) .findFirst().orElse(null); } - - public static final int INIT_VERSION = 1; } diff --git a/source/src/main/java/io/mycat/proxy/ProxyConfig.java b/source/src/main/java/io/mycat/proxy/ProxyConfig.java deleted file mode 100644 index 924a9b9..0000000 --- a/source/src/main/java/io/mycat/proxy/ProxyConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package io.mycat.proxy; - -import io.mycat.mycat2.beans.BalancerBean; -import io.mycat.mycat2.beans.ClusterBean; -import io.mycat.mycat2.beans.HeartbeatBean; -import io.mycat.mycat2.beans.ProxyBean; - -/** - * 代理的配置信息 - * - * @author wuzhihui - * - */ -public class ProxyConfig implements Configurable { - /** - * 代理相关配置 - */ - private ProxyBean proxy; - /** - * 集群相关配置 - */ - private ClusterBean cluster; - /** - * 负载均衡相关配置 - */ - private BalancerBean balancer; - /** - * 心跳相关配置 - */ - private HeartbeatBean heartbeat; - - public ProxyBean getProxy() { - return proxy; - } - - public void setProxy(ProxyBean proxy) { - this.proxy = proxy; - } - - public ClusterBean getCluster() { - return cluster; - } - - public void setCluster(ClusterBean cluster) { - this.cluster = cluster; - } - - public BalancerBean getBalancer() { - return balancer; - } - - public void setBalancer(BalancerBean balancer) { - this.balancer = balancer; - } - - public HeartbeatBean getHeartbeat() { - return heartbeat; - } - - public void setHeartbeat(HeartbeatBean heartbeat) { - this.heartbeat = heartbeat; - } -} diff --git a/source/src/main/java/io/mycat/proxy/ProxyRuntime.java b/source/src/main/java/io/mycat/proxy/ProxyRuntime.java index 6de84a5..e93ff20 100644 --- a/source/src/main/java/io/mycat/proxy/ProxyRuntime.java +++ b/source/src/main/java/io/mycat/proxy/ProxyRuntime.java @@ -16,6 +16,7 @@ import io.mycat.mycat2.ConfigLoader; import io.mycat.mycat2.beans.ReplicaIndexBean; +import io.mycat.mycat2.beans.conf.ProxyConfig; import io.mycat.proxy.man.cmds.ConfigUpdatePacketCommand; import io.mycat.util.YamlUtil; import org.slf4j.Logger; @@ -38,9 +39,9 @@ import io.mycat.util.TimeUtil; public class ProxyRuntime { - + public static final ProxyRuntime INSTANCE = new ProxyRuntime(); private static final Logger logger = LoggerFactory.getLogger(ProxyRuntime.class); - + /* * 时间更新周期 */ @@ -49,9 +50,8 @@ public class ProxyRuntime { private static final String PROCESSOR_CHECK = "PROCESSOR_CHECK"; private static final String REPLICA_ILDE_CHECK = "REPLICA_ILDE_CHECK"; private static final String REPLICA_HEARTBEAT = "REPLICA_HEARTBEAT"; - - private ProxyConfig proxyConfig; - public static final ProxyRuntime INSTANCE = new ProxyRuntime(); + + private MycatConfig config; private AtomicInteger sessionId = new AtomicInteger(1); private int nioReactorThreads = 2; private boolean traceProtocol = false; @@ -98,7 +98,7 @@ public class ProxyRuntime { public void init() { //心跳调度独立出来,避免被其他任务影响 heartbeatScheduler = Executors.newSingleThreadScheduledExecutor(); - timerExecutor = ExecutorUtil.create("Timer", getProxyConfig().getHeartbeat().getTimerExecutor()); + timerExecutor = ExecutorUtil.create("Timer", getConfig().getHeartbeat().getTimerExecutor()); businessExecutor = ExecutorUtil.create("BusinessExecutor",Runtime.getRuntime().availableProcessors()); listeningExecutorService = MoreExecutors.listeningDecorator(businessExecutor); } @@ -195,12 +195,12 @@ public void setMyCLuster(MyCluster myCLuster) { this.myCLuster = myCLuster; } - public ProxyConfig getProxyConfig() { - return proxyConfig; + public MycatConfig getConfig() { + return config; } - public void setProxyConfig(ProxyConfig proxyConfig) { - this.proxyConfig = proxyConfig; + public void setConfig(MycatConfig config) { + this.config = config; } public static ScheduledExecutorService getSchedulerservice() { diff --git a/source/src/main/resources/balancer.yml b/source/src/main/resources/balancer.yml new file mode 100644 index 0000000..80fac4a --- /dev/null +++ b/source/src/main/resources/balancer.yml @@ -0,0 +1,5 @@ +balancer: + enable: false + ip: 0.0.0.0 + port: 7066 + strategy: RANDOM \ No newline at end of file diff --git a/source/src/main/resources/cluster.yml b/source/src/main/resources/cluster.yml new file mode 100644 index 0000000..b6dbbfe --- /dev/null +++ b/source/src/main/resources/cluster.yml @@ -0,0 +1,7 @@ +cluster: + enable: false + ip: 0.0.0.0 + port: 9066 + myNodeId: leader-1 + allNodes: leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9067,leader-3:127.0.0.1:9068 + prepareDelaySeconds: 30 \ No newline at end of file diff --git a/source/src/main/resources/datasource.yml b/source/src/main/resources/datasource.yml index b8ddf6a..6dbe441 100644 --- a/source/src/main/resources/datasource.yml +++ b/source/src/main/resources/datasource.yml @@ -1,8 +1,8 @@ -mysqlReplicas: +replicas: - name: test - type: MASTER_SLAVE + repType: MASTER_SLAVE switchType: DEFAULT_SWITCH - balance: BALANCE_ALL + balanceType: BALANCE_ALL mysqls: - hostName: test ip: 127.0.0.1 diff --git a/source/src/main/resources/heartbeat.yml b/source/src/main/resources/heartbeat.yml new file mode 100644 index 0000000..15931e5 --- /dev/null +++ b/source/src/main/resources/heartbeat.yml @@ -0,0 +1,7 @@ +heartbeat: + timerExecutor: 2 + replicaHeartbeatPeriod: 10000 + replicaIdleCheckPeriod: 2000 + idleTimeout: 2000 + processorCheckPeriod: 2000 + minSwitchtimeInterval: 120000 \ No newline at end of file diff --git a/source/src/main/resources/mycat.yml b/source/src/main/resources/mycat.yml new file mode 100644 index 0000000..ea7e6dd --- /dev/null +++ b/source/src/main/resources/mycat.yml @@ -0,0 +1,3 @@ +proxy: + ip: 0.0.0.0 + port: 8066 \ No newline at end of file diff --git a/source/src/main/resources/schema.yml b/source/src/main/resources/schema.yml index 16782d0..72e5f40 100644 --- a/source/src/main/resources/schema.yml +++ b/source/src/main/resources/schema.yml @@ -1,28 +1,28 @@ schemas: - name: test - type: DB_IN_ONE_SERVER + schemaType: DB_IN_ONE_SERVER defaultDN: database: test mysqlReplica: test # - name: test2 -# type: DB_IN_MULTI_SERVER +# schemaType: DB_IN_MULTI_SERVER # defaultDN: # database: mysql # mysqlReplica: mysql # tables: # - name: t1 -# type: MASTER +# tableType: MASTER # store: test1:test #db:rep # - name: t2 -# type: SLAVE +# tableType: SLAVE # store: test1 # - name: test3 -# type: SQL_PARSE_ROUTE +# schemaType: SQL_PARSE_ROUTE # defaultDN: # database: test # mysqlReplica: test # tables: # - name: shard1 -# type: SLAVE +# tableType: SLAVE # shardingKey: id # shardingRule: auto-sharding-1 \ No newline at end of file From 5df89e75ea79c153530d2c8f6072e6c0aeba1f3d Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Sun, 24 Sep 2017 16:02:06 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=B0=83=E6=95=B4,?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/mycat/mycat2/ConfigLoader.java | 12 +- .../java/io/mycat/mycat2/MySQLSession.java | 2 +- .../java/io/mycat/mycat2/MycatConfig.java | 116 ++++++++++-------- .../main/java/io/mycat/mycat2/MycatCore.java | 4 +- .../java/io/mycat/mycat2/MycatSession.java | 32 +++-- .../java/io/mycat/mycat2/ProxyStarter.java | 32 ++--- .../io/mycat/mycat2/beans/MySQLMetaBean.java | 34 ++--- .../io/mycat/mycat2/beans/MySQLRepBean.java | 97 ++++++++------- .../mycat2/beans/heartbeat/MySQLDetector.java | 2 +- .../beans/heartbeat/MySQLHeartbeat.java | 47 ++++--- .../java/io/mycat/mycat2/cmds/ComInitDB.java | 11 +- .../mycat2/net/MySQLClientAuthHandler.java | 4 +- .../mycat2/tasks/BackendConCreateTask.java | 11 +- .../mycat2/tasks/BackendHeartbeatTask.java | 25 ++-- .../io/mycat/proxy/MycatReactorThread.java | 30 ++--- .../java/io/mycat/proxy/ProxyRuntime.java | 22 ++-- .../java/io/mycat/proxy/man/MyCluster.java | 5 +- .../proxy/man/cmds/ConfigPacketCommand.java | 26 ++-- .../man/cmds/ConfigUpdatePacketCommand.java | 22 ++-- .../proxy/man/packet/NodeRegInfoPacket.java | 5 +- .../src/main/java/io/mycat/util/YamlUtil.java | 5 - 21 files changed, 288 insertions(+), 256 deletions(-) diff --git a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java index 58ed648..8f371c3 100644 --- a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java +++ b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java @@ -1,8 +1,5 @@ package io.mycat.mycat2; -import io.mycat.mycat2.beans.conf.DatasourceConfig; -import io.mycat.mycat2.beans.conf.ReplicaIndexConfig; -import io.mycat.mycat2.beans.conf.SchemaConfig; import io.mycat.proxy.ConfigEnum; import io.mycat.proxy.Configurable; import io.mycat.proxy.ProxyRuntime; @@ -38,6 +35,9 @@ public void loadAll() throws IOException { loadConfig(false, ConfigEnum.DATASOURCE, null); loadConfig(false, ConfigEnum.SCHEMA, null); + ProxyRuntime.INSTANCE.getConfig().initRepMap(); + ProxyRuntime.INSTANCE.getConfig().initSchemaMap(); + // 清空prepare文件夹 YamlUtil.clearDirectory(DIR_PREPARE, null); } @@ -45,6 +45,12 @@ public void loadAll() throws IOException { public void load(ConfigEnum configEnum, Integer targetVersion) throws IOException { loadConfig(true, configEnum, targetVersion); YamlUtil.clearDirectory(DIR_PREPARE, configEnum.getFileName()); + + if (configEnum == ConfigEnum.SCHEMA) { + ProxyRuntime.INSTANCE.getConfig().initSchemaMap(); + } else if (configEnum == ConfigEnum.DATASOURCE) { + ProxyRuntime.INSTANCE.getConfig().initRepMap(); + } } /** diff --git a/source/src/main/java/io/mycat/mycat2/MySQLSession.java b/source/src/main/java/io/mycat/mycat2/MySQLSession.java index f1a4f2e..0ab24d5 100644 --- a/source/src/main/java/io/mycat/mycat2/MySQLSession.java +++ b/source/src/main/java/io/mycat/mycat2/MySQLSession.java @@ -90,7 +90,7 @@ public void setMySQLMetaBean(MySQLMetaBean metaBean) { @Override public String toString() { - return "MySQLSession [database=" + database + ", ip=" + mysqlMetaBean.getIp() + ",port="+mysqlMetaBean.getPort()+"]"; + return "MySQLSession [database=" + database + ", ip=" + mysqlMetaBean.getDsMetaBean().getIp() + ",port=" + mysqlMetaBean.getDsMetaBean().getPort() + "]"; } } diff --git a/source/src/main/java/io/mycat/mycat2/MycatConfig.java b/source/src/main/java/io/mycat/mycat2/MycatConfig.java index 5541a97..2705698 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatConfig.java +++ b/source/src/main/java/io/mycat/mycat2/MycatConfig.java @@ -1,58 +1,82 @@ package io.mycat.mycat2; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import io.mycat.mycat2.beans.GlobalBean; import io.mycat.mycat2.beans.MySQLRepBean; -import io.mycat.mycat2.beans.conf.ProxyConfig; +import io.mycat.mycat2.beans.conf.DatasourceConfig; import io.mycat.mycat2.beans.conf.SchemaBean; +import io.mycat.mycat2.beans.conf.SchemaConfig; import io.mycat.proxy.ConfigEnum; import io.mycat.proxy.Configurable; public class MycatConfig { - private ProxyConfig proxyConfig; - // 当前节点所用的配置文件的版本 private Map configVersionMap = new HashMap<>(); private Map configMap = new HashMap<>(); - /** - * 系统中所有MySQLRepBean的Map - */ - private Map mysqlRepMap = new HashMap(); - /** - * 系统中所有SchemaBean的Map - */ - private Map mycatSchemaMap = new HashMap(); - /** - * 默认Schema,取配置文件种第一个Schema - */ - private SchemaBean defaultSchemaBean; - - public MycatConfig(ProxyConfig proxyConfig) { - this.proxyConfig = proxyConfig; - } - - public Map getMysqlRepMap() { - return this.mysqlRepMap; + /** + * 系统中所有MySQLRepBean的Map + */ + private Map mysqlRepMap = new HashMap(); + /** + * 系统中所有SchemaBean的Map + */ + private Map mycatSchemaMap = new HashMap(); + /** + * 默认Schema,取配置文件种第一个Schema + */ + private SchemaBean defaultSchemaBean; + + public void initRepMap() { + DatasourceConfig dsConfig = getConfig(ConfigEnum.DATASOURCE); + dsConfig.getReplicas().forEach(replica -> { + MySQLRepBean repBean = new MySQLRepBean(); + repBean.setReplicaBean(replica); + mysqlRepMap.put(replica.getName(), repBean); + }); + } + + public void initSchemaMap() { + SchemaConfig schemaConfig = getConfig(ConfigEnum.SCHEMA); + schemaConfig.getSchemas().forEach(schema -> { + if (defaultSchemaBean == null) { + defaultSchemaBean = schema; + } + mycatSchemaMap.put(schema.getName(), schema); + }); + } + + public MySQLRepBean getMySQLRepBean(String repName) { + return mysqlRepMap.get(repName); + } + + public SchemaBean getSchemaBean(String schemaName) { + return mycatSchemaMap.get(schemaName); + } + + /** + * 获取指定的配置对象 + */ + public T getConfig(ConfigEnum configEnum) { + return (T) configMap.get(configEnum); } - public SchemaBean getMycatSchema(String schema) { - return this.mycatSchemaMap.get(schema); - } - - public SchemaBean getDefaultMycatSchema() { - return this.defaultSchemaBean; + /** + * 添加配置对象,指定版本号,默认版本为1 + * @param configEnum + * @param config + * @param version + */ + public void putConfig(ConfigEnum configEnum, Configurable config, Integer version) { + configMap.put(configEnum, config); + version = version == null ? GlobalBean.INIT_VERSION : version; + configVersionMap.put(configEnum, version); } - public MySQLRepBean getMySQLRepBean(String repName) { - return this.mysqlRepMap.get(repName); - } - - public Collection getMySQLReplicaSet(){ - return this.mysqlRepMap.values(); + public Map getConfigVersionMap() { + return configVersionMap; } public void setConfigVersion(ConfigEnum configEnum, int version) { @@ -64,21 +88,11 @@ public int getConfigVersion(ConfigEnum configEnum) { return oldVersion == null ? GlobalBean.INIT_VERSION : oldVersion; } - public int getNextConfigVersion(ConfigEnum configEnum) { - return getConfigVersion(configEnum) + 1; - } - - public Configurable getConfig(ConfigEnum configEnum) { - return configMap.get(configEnum); - } - - public void putConfig(ConfigEnum configEnum, Configurable config, Integer version) { - configMap.put(configEnum, config); - version = version == null ? GlobalBean.INIT_VERSION : version; - configVersionMap.put(configEnum, version); - } + public Map getMysqlRepMap() { + return mysqlRepMap; + } - public Map getConfigVersionMap() { - return configVersionMap; - } + public SchemaBean getDefaultSchemaBean() { + return defaultSchemaBean; + } } diff --git a/source/src/main/java/io/mycat/mycat2/MycatCore.java b/source/src/main/java/io/mycat/mycat2/MycatCore.java index f24fd8d..b02d91b 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatCore.java +++ b/source/src/main/java/io/mycat/mycat2/MycatCore.java @@ -43,8 +43,8 @@ public class MycatCore { public static void main(String[] args) throws IOException { // mycat.conf的加载不需要在集群内 ProxyConfig proxy = YamlUtil.load(ConfigEnum.PROXY.getFileName(), ProxyConfig.class); - logger.debug("load config for {}", ConfigEnum.PROXY.getFileName()); - MycatConfig conf = new MycatConfig(proxy); + logger.debug("load config for {}", ConfigEnum.PROXY); + MycatConfig conf = new MycatConfig(); ProxyRuntime runtime = ProxyRuntime.INSTANCE; runtime.setConfig(conf); diff --git a/source/src/main/java/io/mycat/mycat2/MycatSession.java b/source/src/main/java/io/mycat/mycat2/MycatSession.java index 17f0ba9..65bf0cf 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatSession.java +++ b/source/src/main/java/io/mycat/mycat2/MycatSession.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import io.mycat.mycat2.beans.conf.SchemaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +20,6 @@ import io.mycat.mycat2.Interceptor.impl.DefaultIntercepor; import io.mycat.mycat2.beans.MySQLMetaBean; import io.mycat.mycat2.beans.MySQLRepBean; -import io.mycat.mycat2.beans.SchemaBean; import io.mycat.mycat2.cmds.strategy.AnnotateRouteCmdStrategy; import io.mycat.mycat2.cmds.strategy.DBINMultiServerCmdStrategy; import io.mycat.mycat2.cmds.strategy.DBInOneServerCmdStrategy; @@ -96,7 +96,7 @@ public void clearSQLCmdMap() { * @return */ public MyCommand getMyCommand(){ - switch(schema.type){ + switch(schema.getSchemaType()){ case DB_IN_ONE_SERVER: return DBInOneServerCmdStrategy.INSTANCE.getMyCommand(this); case DB_IN_MULTI_SERVER: @@ -313,7 +313,7 @@ protected void doTakeReadOwner() { private String getbackendName(){ String backendName = null; - switch(schema.type){ + switch(schema.getSchemaType()){ case DB_IN_ONE_SERVER: backendName = schema.getDefaultDN().getMysqlReplica(); break; @@ -343,7 +343,7 @@ private void putbackendMap(MySQLSession mysqlSession){ list = new ArrayList<>(); backendMap.putIfAbsent(mysqlSession.getMySQLMetaBean().getRepBean(), list); } - logger.debug("add backend connection in mycatSession .{}:{}",mysqlSession.getMySQLMetaBean().getIp(),mysqlSession.getMySQLMetaBean().getPort()); + logger.debug("add backend connection in mycatSession .{}:{}",mysqlSession.getMySQLMetaBean().getDsMetaBean().getIp(),mysqlSession.getMySQLMetaBean().getDsMetaBean().getPort()); list.add(mysqlSession); } @@ -387,12 +387,10 @@ public void getBackend(AsynTaskCallBack callback) throws IOExcepti // 当前连接如果本次不被使用,会被自动放入 currSessionMap 中 if (curBackend != null && canUseforCurrent(curBackend,targetMetaBean,runOnSlave)){ - if (logger.isDebugEnabled()){ - logger.debug("Using cached backend connections for {}。{}:{}" - ,(runOnSlave ? "read" : "write"), - curBackend.getMySQLMetaBean().getIp(), - curBackend.getMySQLMetaBean().getPort()); - } + logger.debug("Using cached backend connections for {}。{}:{}" + ,(runOnSlave ? "read" : "write"), + curBackend.getMySQLMetaBean().getDsMetaBean().getIp(), + curBackend.getMySQLMetaBean().getDsMetaBean().getPort()); reactorThread.syncAndExecute(curBackend,callback); return; } @@ -411,8 +409,8 @@ && canUseforCurrent(curBackend,targetMetaBean,runOnSlave)){ /** * 判断连接是否可以被 当前操作使用 - * @param balanceType * @param backend + * @param targetMetaBean * @param runOnSlave * @return */ @@ -437,7 +435,7 @@ private boolean canUseforCurrent(MySQLSession backend,MySQLMetaBean targetMetaBe * @return */ private MySQLRepBean getMySQLRepBean(String replicaName){ - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); MySQLRepBean repBean = conf.getMySQLRepBean(replicaName); if (repBean == null) { throw new RuntimeException("no such MySQLRepBean " + replicaName); @@ -501,12 +499,10 @@ public MySQLSession getCurrCachedSession(MySQLMetaBean targetMetaBean, boolean r curBackend = null; } backendList.remove(result); - if (logger.isDebugEnabled()){ - logger.debug("Using SessionMap backend connections for {}.{}:{}", - (runOnSlave ? "read" : "write"), - result.getMySQLMetaBean().getIp(), - result.getMySQLMetaBean().getPort()); - } + logger.debug("Using SessionMap backend connections for {}.{}:{}", + (runOnSlave ? "read" : "write"), + result.getMySQLMetaBean().getDsMetaBean().getIp(), + result.getMySQLMetaBean().getDsMetaBean().getPort()); return result; } return result; diff --git a/source/src/main/java/io/mycat/mycat2/ProxyStarter.java b/source/src/main/java/io/mycat/mycat2/ProxyStarter.java index ce02831..5b4602f 100644 --- a/source/src/main/java/io/mycat/mycat2/ProxyStarter.java +++ b/source/src/main/java/io/mycat/mycat2/ProxyStarter.java @@ -2,7 +2,7 @@ import java.io.IOException; -import io.mycat.mycat2.beans.conf.ClusterBean; +import io.mycat.mycat2.beans.conf.*; import io.mycat.mycat2.loadbalance.LocalLoadChecker; import io.mycat.mycat2.loadbalance.RandomStrategy; import io.mycat.proxy.*; @@ -20,11 +20,8 @@ public class ProxyStarter { private static final Logger LOGGER = LoggerFactory.getLogger(ProxyStarter.class); - public static final ProxyStarter INSTANCE = new ProxyStarter(); - private ProxyStarter(){} - public void start() throws IOException { ProxyRuntime runtime = ProxyRuntime.INSTANCE; MycatConfig conf = runtime.getConfig(); @@ -34,7 +31,8 @@ public void start() throws IOException { acceptor.start(); runtime.setAcceptor(acceptor); - ClusterBean clusterBean = conf.getCluster(); + ClusterConfig clusterConfig = conf.getConfig(ConfigEnum.CLUSTER); + ClusterBean clusterBean = clusterConfig.getCluster(); if (clusterBean.isEnable()) { // 集群开启状态,需要等集群启动,主节点确认完配置才能提供服务 acceptor.startServerChannel(clusterBean.getIp(), clusterBean.getPort(), ServerType.CLUSTER); @@ -50,20 +48,26 @@ public void start() throws IOException { public void startProxy(boolean isLeader) throws IOException { ProxyRuntime runtime = ProxyRuntime.INSTANCE; - MycatConfig conf = (MycatConfig) runtime.getProxyConfig(); + MycatConfig conf = runtime.getConfig(); // 加载配置文件信息 ConfigLoader.INSTANCE.loadAll(); NIOAcceptor acceptor = runtime.getAcceptor(); - acceptor.startServerChannel(conf.getProxy().getIp(), conf.getProxy().getPort(), ServerType.MYCAT); + + ProxyConfig proxyConfig = conf.getConfig(ConfigEnum.PROXY); + ProxyBean proxyBean = proxyConfig.getProxy(); + acceptor.startServerChannel(proxyBean.getIp(), proxyBean.getPort(), ServerType.MYCAT); startReactor(); // 初始化 init(conf); - if(conf.getBalancer().isEnable()){ + + BalancerConfig balancerConfig = conf.getConfig(ConfigEnum.BALANCER); + BalancerBean balancerBean = balancerConfig.getBalancer(); + if (balancerBean.isEnable()){ //开启负载均衡服务 runtime.setLocalLoadChecker(new LocalLoadChecker()); runtime.setLoadBalanceStrategy(new RandomStrategy()); - acceptor.startServerChannel(conf.getBalancer().getIp(), conf.getBalancer().getPort(), ServerType.LOAD_BALANCER); + acceptor.startServerChannel(balancerBean.getIp(), balancerBean.getPort(), ServerType.LOAD_BALANCER); } // 主节点才启动心跳,非集群下也启动心跳 @@ -94,13 +98,13 @@ private void startReactor() throws IOException { private void init(MycatConfig conf) { // 初始化连接 - conf.getMysqlRepMap().forEach((key, value) -> { - value.initMaster(); - value.getMysqls().forEach(metaBean -> { + conf.getMysqlRepMap().forEach((repName, repBean) -> { + repBean.initMaster(); + repBean.getMetaBeans().forEach(metaBean -> { try { - metaBean.init(value,ProxyRuntime.INSTANCE.maxdataSourceInitTime); + metaBean.init(repBean,ProxyRuntime.INSTANCE.maxdataSourceInitTime); } catch (IOException e) { - LOGGER.error("error to init metaBean: {}", metaBean.getHostName()); + LOGGER.error("error to init metaBean: {}", metaBean.getDsMetaBean().getHostName()); } }); }); diff --git a/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java b/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java index 158af9c..f7b1257 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java @@ -29,7 +29,6 @@ import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; -import io.mycat.mycat2.beans.conf.DatasourceConfig; import io.mycat.mycat2.beans.conf.DatasourceMetaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,12 +46,13 @@ * 后端mysql连接元数据类,对应datasource.xml配置中的mysql元数据信息 * @author wuzhihui */ -public class MySQLMetaBean extends DatasourceMetaBean { +public class MySQLMetaBean { //默认的重试次数 private static final int MAX_RETRY_COUNT = 5; private static final Logger logger = LoggerFactory.getLogger(MySQLMetaBean.class); + private DatasourceMetaBean dsMetaBean; private volatile boolean slaveNode = true; // 默认为slave节点 private volatile long heartbeatRecoveryTime; // 心跳暂停时间 private DBHeartbeat heartbeat; @@ -70,8 +70,7 @@ public class MySQLMetaBean extends DatasourceMetaBean { public final Map CHARSET_TO_INDEX = new HashMap<>(); public boolean init(MySQLRepBean repBean,long maxwaitTime) throws IOException { - - logger.info("init backend myqsl source ,create connections total " + getMinCon() + " for " + getHostName() + " index :" + repBean.getWriteIndex()); + logger.info("init backend myqsl source ,create connections total " + dsMetaBean.getMinCon() + " for " + dsMetaBean.getHostName() + " index :" + repBean.getWriteIndex()); this.repBean = repBean; heartbeat = new MySQLHeartbeat(this,DBHeartbeat.INIT_STATUS); @@ -79,8 +78,8 @@ public boolean init(MySQLRepBean repBean,long maxwaitTime) throws IOException { MycatReactorThread[] reactorThreads = (MycatReactorThread[]) runtime.getReactorThreads(); int reactorSize = runtime.getNioReactorThreads(); CopyOnWriteArrayList list = new CopyOnWriteArrayList(); - BackendGetConnectionTask getConTask = new BackendGetConnectionTask(list, getMinCon()); - for (int i = 0; i < getMinCon(); i++) { + BackendGetConnectionTask getConTask = new BackendGetConnectionTask(list, dsMetaBean.getMinCon()); + for (int i = 0; i < dsMetaBean.getMinCon(); i++) { MycatReactorThread reactorThread = reactorThreads[i % reactorSize]; reactorThread.addNIOJob(() -> { try { @@ -130,7 +129,7 @@ public void doHeartbeat() { try { heartbeat.heartbeat(); } catch (Exception e) { - logger.error(getHostName() + " heartbeat error.", e); + logger.error(dsMetaBean.getHostName() + " heartbeat error.", e); } } @@ -141,7 +140,7 @@ public void doHeartbeat() { public void clearCons(String reason) { ProxyRuntime runtime = ProxyRuntime.INSTANCE; MycatReactorThread[] reactorThreads = (MycatReactorThread[]) runtime.getReactorThreads(); - Arrays.stream(reactorThreads).forEach(f->f.clearMySQLMetaBeanSession(this,reason)); + Arrays.stream(reactorThreads).forEach(f -> f.clearMySQLMetaBeanSession(this,reason)); } /** @@ -149,11 +148,10 @@ public void clearCons(String reason) { * @return */ public boolean canSelectAsReadNode() { - int slaveBehindMaster = heartbeat.getSlaveBehindMaster(); int dbSynStatus = heartbeat.getDbSynStatus(); - if(!isAlive()){ + if (!isAlive()){ return false; } @@ -161,11 +159,19 @@ public boolean canSelectAsReadNode() { return false; } boolean isSync = dbSynStatus == DBHeartbeat.DB_SYN_NORMAL; - boolean isNotDelay = (slaveThreshold >=0)?(slaveBehindMaster < slaveThreshold):true; + boolean isNotDelay = (slaveThreshold >= 0) ? (slaveBehindMaster < slaveThreshold) : true; return isSync && isNotDelay; } - public boolean isSlaveNode() { + public DatasourceMetaBean getDsMetaBean() { + return dsMetaBean; + } + + public void setDsMetaBean(DatasourceMetaBean dsMetaBean) { + this.dsMetaBean = dsMetaBean; + } + + public boolean isSlaveNode() { return slaveNode; } @@ -211,7 +217,7 @@ public boolean isAlive() { @Override public String toString() { - return "MySQLMetaBean [hostName=" + getHostName() + ", ip=" + getIp() + ", port=" + getPort() + ", user=" + getUser() + ", password=" - + getPassword() + ", maxCon=" + getMaxCon() + ", minCon=" + getMinCon() + ", slaveNode=" + slaveNode + "]"; + return "MySQLMetaBean [hostName=" + dsMetaBean.getHostName() + ", ip=" + dsMetaBean.getIp() + ", port=" + dsMetaBean.getPort() + ", user=" + dsMetaBean.getUser() + ", password=" + + dsMetaBean.getPassword() + ", maxCon=" + dsMetaBean.getMaxCon() + ", minCon=" + dsMetaBean.getMinCon() + ", slaveNode=" + slaveNode + "]"; } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java b/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java index 55f684b..d2a43df 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java @@ -33,6 +33,8 @@ import io.mycat.mycat2.beans.conf.DatasourceConfig; import io.mycat.mycat2.beans.conf.ReplicaBean; +import io.mycat.mycat2.beans.conf.ReplicaIndexConfig; +import io.mycat.proxy.ConfigEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,9 +47,10 @@ * * @author wuzhihui */ -public class MySQLRepBean extends ReplicaBean { +public class MySQLRepBean { private static Logger logger = LoggerFactory.getLogger(MySQLRepBean.class); + private ReplicaBean replicaBean; private String slaveIDs; // 在线数据迁移 虚拟从节点 private boolean tempReadHostAvailable = false; //如果写服务挂掉, 临时读服务是否继续可用 @@ -62,58 +65,57 @@ public class MySQLRepBean extends ReplicaBean { public void initMaster() { // 根据配置replica-index的配置文件修改主节点 MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); - Integer repIndex = conf.getRepIndex(getName()); + ReplicaIndexConfig repIndexConfig = conf.getConfig(ConfigEnum.REPLICA_INDEX); + Integer repIndex = repIndexConfig.getReplicaIndexes().get(replicaBean.getName()); if (repIndex != null && checkIndex(repIndex)) { writeIndex = repIndex; } else { writeIndex = 0; } - metaBeans.add(new MySQLMetaBean()); - writeMetaBean = getMysqls().get(writeIndex); - writeMetaBean.setSlaveNode(false); - readMetaBeans.addAll(mysqls); - readMetaBeans.remove(writeIndex); + replicaBean.getMysqls().forEach(dsMetaBean -> { + MySQLMetaBean metaBean = new MySQLMetaBean(); + metaBean.setDsMetaBean(dsMetaBean); + metaBeans.add(metaBean); + }); + metaBeans.get(writeIndex).setSlaveNode(false); } public void doHeartbeat() { - if (writeMetaBean == null) { + if (metaBeans.get(writeIndex) == null) { return; } - for (MySQLMetaBean source : this.mysqls) { + for (MySQLMetaBean source : metaBeans) { if (source != null) { source.doHeartbeat(); } else { StringBuilder s = new StringBuilder(); - s.append(Alarms.DEFAULT).append(getName()).append(" current dataSource is null!"); + s.append(Alarms.DEFAULT).append(replicaBean.getName()).append(" current dataSource is null!"); logger.error(s.toString()); } } - } private boolean checkIndex(int newIndex){ - return newIndex >= 0 && newIndex < mysqls.size(); + return newIndex >= 0 && newIndex < metaBeans.size(); } public int getNextIndex(){ - MySQLMetaBean metaBean = mysqls.stream().skip(writeIndex+1).findFirst().orElse(null); - if(metaBean!=null){ - return mysqls.indexOf(metaBean); - }else{ - metaBean = mysqls.stream().limit(writeIndex).findFirst().orElse(null); + MySQLMetaBean metaBean = metaBeans.stream().skip(writeIndex + 1).findFirst().orElse(null); + if (metaBean!=null){ + return metaBeans.indexOf(metaBean); + } else { + metaBean = metaBeans.stream().limit(writeIndex).findFirst().orElse(null); if(metaBean!=null){ - return mysqls.indexOf(metaBean); + return metaBeans.indexOf(metaBean); } } return -1; } public void switchSource(int newIndex,long maxwaittime) { - if (getSwitchType() == DatasourceConfig.RepSwitchTypeEnum.NOT_SWITCH) { - logger.debug("not switch datasource ,for switchType is {}", - DatasourceConfig.RepSwitchTypeEnum.NOT_SWITCH); - + if (replicaBean.getSwitchType() == ReplicaBean.RepSwitchTypeEnum.NOT_SWITCH) { + logger.debug("not switch datasource ,for switchType is {}", ReplicaBean.RepSwitchTypeEnum.NOT_SWITCH); switchResult.set(false); return; } @@ -138,12 +140,12 @@ public void switchSource(int newIndex,long maxwaittime) { String reason = "switch datasource"; // init again - MySQLMetaBean newWriteBean = mysqls.get(newIndex); + MySQLMetaBean newWriteBean = metaBeans.get(newIndex); newWriteBean.clearCons(reason); newWriteBean.init(this,maxwaittime); // clear all connections - MySQLMetaBean oldMetaBean = mysqls.get(current); + MySQLMetaBean oldMetaBean = metaBeans.get(current); oldMetaBean.clearCons(reason); // write log logger.warn(switchMessage(current, newIndex, reason)); @@ -167,7 +169,7 @@ public void switchSource(int newIndex,long maxwaittime) { private String switchMessage(int current, int newIndex, String reason) { StringBuilder s = new StringBuilder(); - s.append("[Host=").append(getName()).append(",result=[").append(current).append("->"); + s.append("[Host=").append(replicaBean.getName()).append(",result=[").append(current).append("->"); s.append(newIndex).append("],reason=").append(reason).append(']'); return s.toString(); } @@ -176,17 +178,17 @@ private String switchMessage(int current, int newIndex, String reason) { * 得到当前用于写的MySQLMetaBean */ private MySQLMetaBean getCurWriteMetaBean() { - return writeMetaBean.isAlive()?writeMetaBean:null; + return metaBeans.get(writeIndex).isAlive() ? metaBeans.get(writeIndex) : null; } public MySQLMetaBean getBalanceMetaBean(boolean runOnSlave){ - if(DatasourceConfig.RepTypeEnum.SINGLE_NODE == getRepType()||!runOnSlave){ + if(ReplicaBean.RepTypeEnum.SINGLE_NODE == replicaBean.getRepType()||!runOnSlave){ return getCurWriteMetaBean(); } MySQLMetaBean datas = null; - switch(getBalanceType()){ + switch(replicaBean.getBalanceType()){ case BALANCE_ALL: datas = getLBReadWriteMetaBean(); break; @@ -194,7 +196,7 @@ public MySQLMetaBean getBalanceMetaBean(boolean runOnSlave){ datas = getLBReadMetaBean(); //如果从节点不可用,从主节点获取连接 if(datas==null){ - logger.debug("all slaveNode is Unavailable. use master node for read . balance type is {}",getBalanceType()); + logger.debug("all slaveNode is Unavailable. use master node for read . balance type is {}", replicaBean.getBalanceType()); datas = getCurWriteMetaBean(); } break; @@ -202,19 +204,20 @@ public MySQLMetaBean getBalanceMetaBean(boolean runOnSlave){ datas = getCurWriteMetaBean(); break; default: - logger.debug("current balancetype is not supported!! [{}], use writenode connection .",getBalanceType()); + logger.debug("current balancetype is not supported!! [{}], use writenode connection .", replicaBean.getBalanceType()); datas = getCurWriteMetaBean(); break; } return datas; } + /** * 得到当前用于读的MySQLMetaBean(负载均衡模式,如果支持) * 当前读写节点都承担负载 */ private MySQLMetaBean getLBReadWriteMetaBean() { - List result = mysqls.stream() - .filter(f->f.canSelectAsReadNode()) + List result = metaBeans.stream() + .filter(f -> f.canSelectAsReadNode()) .collect(Collectors.toList()); return result.isEmpty()?null:result.get(ThreadLocalRandom.current().nextInt(result.size())); } @@ -224,12 +227,28 @@ private MySQLMetaBean getLBReadWriteMetaBean() { * @return */ private MySQLMetaBean getLBReadMetaBean(){ - List result = readMetaBeans.stream() - .filter(f->f.canSelectAsReadNode()) + List result = metaBeans.stream() + .filter(f -> f.isSlaveNode() && f.canSelectAsReadNode()) .collect(Collectors.toList()); - return result.isEmpty()?null:result.get(ThreadLocalRandom.current().nextInt(result.size())); + return result.isEmpty() ? null : result.get(ThreadLocalRandom.current().nextInt(result.size())); } - + + public ReplicaBean getReplicaBean() { + return replicaBean; + } + + public void setReplicaBean(ReplicaBean replicaBean) { + this.replicaBean = replicaBean; + } + + public List getMetaBeans() { + return metaBeans; + } + + public void setMetaBeans(List metaBeans) { + this.metaBeans = metaBeans; + } + public String getSlaveIDs() { return slaveIDs; } @@ -246,10 +265,6 @@ public void setTempReadHostAvailable(boolean tempReadHostAvailable) { this.tempReadHostAvailable = tempReadHostAvailable; } - public void setWriteMetaBean(MySQLMetaBean writeMetaBean) { - this.writeMetaBean = writeMetaBean; - } - public int getWriteIndex() { return writeIndex; } @@ -273,4 +288,4 @@ public void setLastSwitchTime(long lastSwitchTime) { public long getLastInitTime() { return lastInitTime; } -} \ No newline at end of file +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java index 6645f91..e4b2da0 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java +++ b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java @@ -95,7 +95,7 @@ public void heartbeat() throws IOException { }else{ heartbeat.setResult(DBHeartbeat.ERROR_STATUS, this, - heartbeat.getSource().getIp()+":"+heartbeat.getSource().getPort() + heartbeat.getSource().getDsMetaBean().getIp()+":"+heartbeat.getSource().getDsMetaBean().getPort() +" connection timeout!!"); } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java index 0a9c4cb..be8fe35 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java +++ b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java @@ -30,7 +30,10 @@ import java.util.Map; import java.util.concurrent.locks.ReentrantLock; -import io.mycat.mycat2.beans.ReplicaIndexBean; +import io.mycat.mycat2.beans.conf.ClusterConfig; +import io.mycat.mycat2.beans.conf.HeartbeatConfig; +import io.mycat.mycat2.beans.conf.ProxyConfig; +import io.mycat.mycat2.beans.conf.ReplicaIndexConfig; import io.mycat.proxy.ConfigEnum; import io.mycat.proxy.man.cmds.ConfigUpdatePacketCommand; import io.mycat.util.YamlUtil; @@ -125,7 +128,7 @@ public void heartbeat() { lock.lock(); try { if (isChecking.compareAndSet(false, true)) { - logger.debug("backend mysql heartbeat begin.{}:{}",source.getIp(),source.getPort()); + logger.debug("backend mysql heartbeat begin.{}:{}", source.getDsMetaBean().getIp(), source.getDsMetaBean().getPort()); MySQLDetector detector = this.detector; if (detector == null || detector.isQuit()) { try { @@ -190,9 +193,10 @@ private void setOk(MySQLDetector detector) { } break; case DBHeartbeat.INIT_STATUS: - logger.info("current repl status [INIT_STATUS ---> OK_STATUS ]. update lastSwitchTime .{}:{}", source.getIp(), source.getPort()); - MycatConfig conf = (MycatConfig)ProxyRuntime.INSTANCE.getProxyConfig(); - source.getRepBean().setLastSwitchTime(System.currentTimeMillis() - conf.getHeartbeat().getMinSwitchtimeInterval()); + logger.info("current repl status [INIT_STATUS ---> OK_STATUS ]. update lastSwitchTime .{}:{}", source.getDsMetaBean().getIp(), source.getDsMetaBean().getPort()); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + HeartbeatConfig heartbeatConfig = conf.getConfig(ConfigEnum.HEARTBEAT); + source.getRepBean().setLastSwitchTime(System.currentTimeMillis() - heartbeatConfig.getHeartbeat().getMinSwitchtimeInterval()); case DBHeartbeat.OK_STATUS: default: this.status = OK_STATUS; @@ -214,9 +218,10 @@ private void setError(MySQLDetector detector, String msg) { //写节点 尝试多次次失败后, 需要通知集群 logger.debug("heartbeat to backend session error, notify the cluster if needed"); - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + HeartbeatConfig heartbeatConfig = conf.getConfig(ConfigEnum.HEARTBEAT); long curTime = System.currentTimeMillis(); - long minSwitchTimeInterval = conf.getHeartbeat().getMinSwitchtimeInterval(); + long minSwitchTimeInterval = heartbeatConfig.getHeartbeat().getMinSwitchtimeInterval(); if (((curTime - source.getRepBean().getLastSwitchTime()) < minSwitchTimeInterval) || (curTime - source.getRepBean().getLastInitTime()) < minSwitchTimeInterval) { if (logger.isDebugEnabled()) { @@ -229,21 +234,23 @@ private void setError(MySQLDetector detector, String msg) { if (next == -1) { logger.error("all metaBean in replica is invalid !!!"); } else { - String repName = source.getRepBean().getName(); - if (ProxyRuntime.INSTANCE.getProxyConfig().getCluster().isEnable()) { - ReplicaIndexBean bean = new ReplicaIndexBean(); - Map map = new HashMap(conf.getRepIndexMap()); + String repName = source.getRepBean().getReplicaBean().getName(); + ClusterConfig clusterConfig = conf.getConfig(ConfigEnum.CLUSTER); + ReplicaIndexConfig curRepIndexConfig = conf.getConfig(ConfigEnum.REPLICA_INDEX); + if (clusterConfig.getCluster().isEnable()) { + ReplicaIndexConfig newRepIndexConfig = new ReplicaIndexConfig(); + Map map = new HashMap(curRepIndexConfig.getReplicaIndexes()); map.put(repName, next); - bean.setReplicaIndexes(map); - ConfigUpdatePacketCommand.INSTANCE.sendPreparePacket(ConfigEnum.REPLICA_INDEX, bean, repName); + newRepIndexConfig.setReplicaIndexes(map); + ConfigUpdatePacketCommand.INSTANCE.sendPreparePacket(ConfigEnum.REPLICA_INDEX, newRepIndexConfig, repName); } else { // 非集群下直接更新replica-index信息 - byte configType = ConfigEnum.REPLICA_INDEX.getType(); - conf.getRepIndexMap().put(repName, next); - int curVersion = conf.getConfigVersion(configType); - conf.setConfigVersion(configType, curVersion + 1); - YamlUtil.archiveAndDump(ConfigEnum.REPLICA_INDEX.getFileName(), curVersion, conf.getConfig(configType)); - ProxyRuntime.INSTANCE.startSwitchDataSource(source.getRepBean().getName(), next); + ConfigEnum configEnum = ConfigEnum.REPLICA_INDEX; + curRepIndexConfig.getReplicaIndexes().put(repName, next); + int curVersion = conf.getConfigVersion(configEnum); + conf.setConfigVersion(configEnum, curVersion + 1); + YamlUtil.archiveAndDump(configEnum.getFileName(), curVersion, conf.getConfig(configEnum)); + ProxyRuntime.INSTANCE.startSwitchDataSource(source.getRepBean().getReplicaBean().getName(), next); } } } @@ -317,4 +324,4 @@ private void setTimeout(MySQLDetector detector) { // } // } // } -} \ No newline at end of file +} diff --git a/source/src/main/java/io/mycat/mycat2/cmds/ComInitDB.java b/source/src/main/java/io/mycat/mycat2/cmds/ComInitDB.java index 43e65fa..3c0e1ec 100644 --- a/source/src/main/java/io/mycat/mycat2/cmds/ComInitDB.java +++ b/source/src/main/java/io/mycat/mycat2/cmds/ComInitDB.java @@ -2,13 +2,12 @@ import java.io.IOException; +import io.mycat.mycat2.beans.conf.SchemaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.mycat.mycat2.MycatConfig; import io.mycat.mycat2.MycatSession; -import io.mycat.mycat2.beans.SchemaBean; -import io.mycat.mycat2.beans.SchemaBean.SchemaType; import io.mycat.mycat2.sqlparser.BufferSQLParser; import io.mycat.mysql.packet.ErrorPacket; import io.mycat.mysql.packet.MySQLPacket; @@ -34,10 +33,10 @@ public boolean procssSQL(MycatSession session) throws IOException { String schema = session.sqlContext.getBuffer().getString(offset, len); - MycatConfig config = (MycatConfig)ProxyRuntime.INSTANCE.getProxyConfig(); - SchemaBean schemaBean = config.getMycatSchema(schema); + MycatConfig config = ProxyRuntime.INSTANCE.getConfig(); + SchemaBean schemaBean = config.getSchemaBean(schema); - if (schemaBean == null && SchemaType.DB_IN_ONE_SERVER!=session.schema.getType()) { + if (schemaBean == null && SchemaBean.SchemaTypeEnum.DB_IN_ONE_SERVER != session.schema.getSchemaType()) { ErrorPacket error = new ErrorPacket(); error.errno = ErrorCode.ER_BAD_DB_ERROR; error.packetId = session.proxyBuffer.getByte(session.curMSQLPackgInf.startPos @@ -49,7 +48,7 @@ public boolean procssSQL(MycatSession session) throws IOException { session.schema = schemaBean; session.responseOKOrError(OKPacket.OK); return false; - }else if(SchemaType.DB_IN_ONE_SERVER==session.schema.getType()){ + }else if(SchemaBean.SchemaTypeEnum.DB_IN_ONE_SERVER==session.schema.getSchemaType()){ session.schema.getDefaultDN().setDatabase(schema); return super.procssSQL(session); }else{ diff --git a/source/src/main/java/io/mycat/mycat2/net/MySQLClientAuthHandler.java b/source/src/main/java/io/mycat/mycat2/net/MySQLClientAuthHandler.java index 166e9ba..a1ddd0c 100644 --- a/source/src/main/java/io/mycat/mycat2/net/MySQLClientAuthHandler.java +++ b/source/src/main/java/io/mycat/mycat2/net/MySQLClientAuthHandler.java @@ -54,8 +54,8 @@ public void onSocketRead(MycatSession session) throws IOException { // check schema logger.debug("Check database. " + auth.database); //TODO ...set schema - MycatConfig mycatConf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); - session.schema=mycatConf.getDefaultMycatSchema(); + MycatConfig mycatConf = ProxyRuntime.INSTANCE.getConfig(); + session.schema = mycatConf.getDefaultSchemaBean(); boolean succ = success(session,auth); if (succ) { session.proxyBuffer.reset(); diff --git a/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java b/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java index 9dc5e73..324396d 100644 --- a/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java +++ b/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java @@ -8,19 +8,18 @@ import java.security.NoSuchAlgorithmException; import io.mycat.mycat2.beans.MySQLMetaBean; +import io.mycat.mycat2.beans.conf.SchemaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.mycat.mycat2.AbstractMySQLSession.CurrPacketType; import io.mycat.mycat2.MySQLSession; -import io.mycat.mycat2.beans.SchemaBean; import io.mycat.mysql.Capabilities; import io.mycat.mysql.packet.AuthPacket; import io.mycat.mysql.packet.ErrorPacket; import io.mycat.mysql.packet.HandshakePacket; import io.mycat.mysql.packet.MySQLPacket; import io.mycat.proxy.BufferPool; -import io.mycat.util.CharsetUtil; import io.mycat.util.ParseUtil; import io.mycat.util.SecurityUtil; @@ -40,8 +39,8 @@ public class BackendConCreateTask extends AbstractBackendIOTask { public BackendConCreateTask(BufferPool bufPool, Selector nioSelector, MySQLMetaBean mySQLMetaBean, SchemaBean schema,AsynTaskCallBack callBack) throws IOException { - String serverIP = mySQLMetaBean.getIp(); - int serverPort = mySQLMetaBean.getPort(); + String serverIP = mySQLMetaBean.getDsMetaBean().getIp(); + int serverPort = mySQLMetaBean.getDsMetaBean().getPort(); logger.info("Connecting to backend MySQL Server " + serverIP + ":" + serverPort); InetSocketAddress serverAddress = new InetSocketAddress(serverIP, serverPort); SocketChannel backendChannel = SocketChannel.open(); @@ -75,9 +74,9 @@ public void onSocketRead(MySQLSession session) throws IOException { packet.clientFlags = initClientFlags(); packet.maxPacketSize = 1024 * 1000; packet.charsetIndex = charsetIndex; - packet.user = mySQLMetaBean.getUser(); + packet.user = mySQLMetaBean.getDsMetaBean().getUser(); try { - packet.password = passwd(mySQLMetaBean.getPassword(), handshake); + packet.password = passwd(mySQLMetaBean.getDsMetaBean().getPassword(), handshake); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage()); } diff --git a/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java b/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java index 451ca8e..715f1e4 100644 --- a/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java +++ b/source/src/main/java/io/mycat/mycat2/tasks/BackendHeartbeatTask.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; +import io.mycat.mycat2.beans.conf.ReplicaBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +15,6 @@ import io.mycat.mycat2.beans.MySQLMetaBean; import io.mycat.mycat2.beans.MySQLPackageInf; import io.mycat.mycat2.beans.MySQLRepBean; -import io.mycat.mycat2.beans.MySQLRepBean.RepTypeEnum; import io.mycat.mycat2.beans.heartbeat.DBHeartbeat; import io.mycat.mycat2.beans.heartbeat.MySQLDetector; import io.mycat.mycat2.beans.heartbeat.MySQLHeartbeat; @@ -24,7 +24,6 @@ import io.mycat.proxy.ProxyBuffer; public class BackendHeartbeatTask extends BackendIOTaskWithResultSet { - private static Logger logger = LoggerFactory.getLogger(BackendHeartbeatTask.class); private int fieldCount; @@ -39,7 +38,7 @@ public class BackendHeartbeatTask extends BackendIOTaskWithResultSet list != null) .reduce(0, (sum, list) -> sum += list.size(), (sum1, sum2) -> sum1 + sum2); int backendCounts = getUsingBackendConCounts(mySQLMetaBean); - logger.debug("all session backend count is {},reactor backend count is {},metabean max con is {}",backendCounts,count,mySQLMetaBean.getMaxCon()); - if (count + backendCounts + 1 > mySQLMetaBean.getMaxCon()) { - callBack.finished(null, null, false, "backend connection is full for " + mySQLMetaBean.getIp() + ":"+mySQLMetaBean.getPort()); + logger.debug("all session backend count is {},reactor backend count is {},metabean max con is {}",backendCounts,count,mySQLMetaBean.getDsMetaBean().getMaxCon()); + if (count + backendCounts + 1 > mySQLMetaBean.getDsMetaBean().getMaxCon()) { + callBack.finished(null, null, false, "backend connection is full for " + mySQLMetaBean.getDsMetaBean().getIp() + ":" + mySQLMetaBean.getDsMetaBean().getPort()); return; } try { @@ -92,8 +92,6 @@ public void createSession(MySQLMetaBean mySQLMetaBean, SchemaBean schema, AsynTa * 3. reactor thread中空闲的backend * 4. 连接池中的 backend * 5. 是否可以新建连接 - * @param runOnSlave - * @param backendName * @return * @throws IOException */ @@ -107,12 +105,10 @@ public void getMySQLSession(MycatSession currMycatSession,boolean runOnSlave,MyS .map(mycatSession->mycatSession.getCurrCachedSession(targetMetaBean, runOnSlave,true)) .filter(session -> session != null).findFirst().orElse(null); if (mysqlSession != null) { - if (logger.isDebugEnabled()) { - logger.debug("Use reactor cached backend connections for {}.{}:{}", - (runOnSlave ? "read" : "write"), - mysqlSession.getMySQLMetaBean().getIp(), - mysqlSession.getMySQLMetaBean().getPort()); - } + logger.debug("Use reactor cached backend connections for {}.{}:{}", + (runOnSlave ? "read" : "write"), + mysqlSession.getMySQLMetaBean().getDsMetaBean().getIp(), + mysqlSession.getMySQLMetaBean().getDsMetaBean().getPort()); mysqlSession.getMycatSession().unbindBeckend(mysqlSession); currMycatSession.bindBackend(mysqlSession); syncAndExecute(mysqlSession,callback); @@ -125,12 +121,10 @@ public void getMySQLSession(MycatSession currMycatSession,boolean runOnSlave,MyS if (mySQLSessionList != null && !mySQLSessionList.isEmpty()) { mysqlSession = mySQLSessionList.removeLast(); if(mysqlSession!=null){ - if(logger.isDebugEnabled()){ - logger.debug("Using the existing session in the datasource for {}. {}:{}", - (runOnSlave ? "read" : "write"), - mysqlSession.getMySQLMetaBean().getIp(), - mysqlSession.getMySQLMetaBean().getPort()); - } + logger.debug("Using the existing session in the datasource for {}. {}:{}", + (runOnSlave ? "read" : "write"), + mysqlSession.getMySQLMetaBean().getDsMetaBean().getIp(), + mysqlSession.getMySQLMetaBean().getDsMetaBean().getPort()); currMycatSession.bindBackend(mysqlSession); syncAndExecute(mysqlSession,callback); return; diff --git a/source/src/main/java/io/mycat/proxy/ProxyRuntime.java b/source/src/main/java/io/mycat/proxy/ProxyRuntime.java index e93ff20..8f52ee1 100644 --- a/source/src/main/java/io/mycat/proxy/ProxyRuntime.java +++ b/source/src/main/java/io/mycat/proxy/ProxyRuntime.java @@ -1,10 +1,10 @@ package io.mycat.proxy; + /** * 运行时环境,单例方式访问 * @author wuzhihui * */ - import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; @@ -14,11 +14,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import io.mycat.mycat2.ConfigLoader; -import io.mycat.mycat2.beans.ReplicaIndexBean; -import io.mycat.mycat2.beans.conf.ProxyConfig; -import io.mycat.proxy.man.cmds.ConfigUpdatePacketCommand; -import io.mycat.util.YamlUtil; +import io.mycat.mycat2.beans.conf.HeartbeatConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,7 +94,8 @@ public class ProxyRuntime { public void init() { //心跳调度独立出来,避免被其他任务影响 heartbeatScheduler = Executors.newSingleThreadScheduledExecutor(); - timerExecutor = ExecutorUtil.create("Timer", getConfig().getHeartbeat().getTimerExecutor()); + HeartbeatConfig heartbeatConfig = config.getConfig(ConfigEnum.HEARTBEAT); + timerExecutor = ExecutorUtil.create("Timer", heartbeatConfig.getHeartbeat().getTimerExecutor()); businessExecutor = ExecutorUtil.create("BusinessExecutor",Runtime.getRuntime().availableProcessors()); listeningExecutorService = MoreExecutors.listeningDecorator(businessExecutor); } @@ -118,7 +115,8 @@ public ProxyReactorThread getProxyReactorThread(ReactorEnv reactorEnv){ */ public void startHeartBeatScheduler(){ if(heartBeatTasks.get(REPLICA_HEARTBEAT)==null){ - long replicaHeartbeat = getProxyConfig().getHeartbeat().getReplicaHeartbeatPeriod(); + HeartbeatConfig heartbeatConfig = config.getConfig(ConfigEnum.HEARTBEAT); + long replicaHeartbeat = heartbeatConfig.getHeartbeat().getReplicaHeartbeatPeriod(); heartBeatTasks.put(REPLICA_HEARTBEAT, heartbeatScheduler.scheduleAtFixedRate(replicaHeartbeat(), 0, @@ -139,7 +137,6 @@ public void addDelayedJob(Runnable job, int delayedSeconds) { * 切换 metaBean 名称 */ public void startSwitchDataSource(String replBean,Integer writeIndex){ - MycatConfig config = (MycatConfig) getProxyConfig(); MySQLRepBean repBean = config.getMySQLRepBean(replBean); if (repBean != null){ @@ -178,12 +175,7 @@ public void run() { private Runnable replicaHeartbeat() { return ()->{ ProxyReactorThread reactor = getReactorThreads()[ThreadLocalRandom.current().nextInt(getReactorThreads().length)]; - reactor.addNIOJob(()->{ - MycatConfig config = (MycatConfig) getProxyConfig(); - config.getMySQLReplicaSet() - .stream() - .forEach(f->f.doHeartbeat()); - }); + reactor.addNIOJob(()-> config.getMysqlRepMap().values().stream().forEach(f -> f.doHeartbeat())); }; } diff --git a/source/src/main/java/io/mycat/proxy/man/MyCluster.java b/source/src/main/java/io/mycat/proxy/man/MyCluster.java index 0c65d71..ecc9b5f 100644 --- a/source/src/main/java/io/mycat/proxy/man/MyCluster.java +++ b/source/src/main/java/io/mycat/proxy/man/MyCluster.java @@ -10,6 +10,8 @@ import java.util.concurrent.ConcurrentHashMap; import io.mycat.mycat2.ProxyStarter; +import io.mycat.mycat2.beans.conf.ProxyConfig; +import io.mycat.proxy.ConfigEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,7 +70,8 @@ public MyCluster(Selector nioSelector, String myNodeId, ArrayList a } myNode.setState(NodeState.Online); this.myNode = myNode; - this.myNode.proxyPort = ProxyRuntime.INSTANCE.getProxyConfig().getProxy().getPort(); + ProxyConfig proxyConfig = ProxyRuntime.INSTANCE.getConfig().getConfig(ConfigEnum.PROXY); + this.myNode.proxyPort = proxyConfig.getProxy().getPort(); } /** diff --git a/source/src/main/java/io/mycat/proxy/man/cmds/ConfigPacketCommand.java b/source/src/main/java/io/mycat/proxy/man/cmds/ConfigPacketCommand.java index 1b40b0f..90f75f5 100644 --- a/source/src/main/java/io/mycat/proxy/man/cmds/ConfigPacketCommand.java +++ b/source/src/main/java/io/mycat/proxy/man/cmds/ConfigPacketCommand.java @@ -3,7 +3,6 @@ import io.mycat.mycat2.MycatConfig; import io.mycat.mycat2.ProxyStarter; import io.mycat.proxy.ConfigEnum; -import io.mycat.proxy.ProxyConfig; import io.mycat.proxy.ProxyRuntime; import io.mycat.proxy.man.AdminCommand; import io.mycat.proxy.man.AdminSession; @@ -58,13 +57,12 @@ public void handlerPkg(AdminSession session, byte cmdType) throws IOException { */ private void handleConfigVersionReq(AdminSession session) throws IOException { LOGGER.debug("receive config version request package from {}", session.getNodeId()); - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); - Map configVersionMap = conf.getConfigVersionMap(); - ConfigVersionResPacket versionResPacket = - new ConfigVersionResPacket(configVersionMap.size()); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + Map configVersionMap = conf.getConfigVersionMap(); + ConfigVersionResPacket versionResPacket = new ConfigVersionResPacket(configVersionMap.size()); int i = 0; - for (Map.Entry entry : configVersionMap.entrySet()) { - versionResPacket.getConfTypes()[i] = entry.getKey(); + for (Map.Entry entry : configVersionMap.entrySet()) { + versionResPacket.getConfTypes()[i] = entry.getKey().getType(); versionResPacket.getConfVersions()[i] = entry.getValue(); i++; } @@ -83,10 +81,11 @@ private void handleConfigVersionRes(AdminSession session) throws IOException { int confCount = respPacket.getConfCount(); byte[] confTypes = respPacket.getConfTypes(); int[] confVersions = respPacket.getConfVersions(); - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); List confTypeList = new ArrayList<>(); for (int i = 0; i < confCount; i++) { - if (conf.getConfigVersion(confTypes[i]) != confVersions[i]) { + ConfigEnum configEnum = ConfigEnum.getConfigEnum(confTypes[i]); + if (conf.getConfigVersion(configEnum) != confVersions[i]) { // 配置文件版本不相同,需要加载 confTypeList.add(confTypes[i]); } @@ -115,7 +114,7 @@ private void handleConfigVersionRes(AdminSession session) throws IOException { */ private void handleConfigReq(AdminSession session) throws IOException { LOGGER.debug("receive config request packet from {}", session.getNodeId()); - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); ConfigReqPacket reqPacket = new ConfigReqPacket(); reqPacket.resolve(session.readingBuffer); int count = reqPacket.getConfCount(); @@ -127,10 +126,9 @@ private void handleConfigReq(AdminSession session) throws IOException { LOGGER.warn("config type is error: {}", type); continue; } - byte confType = configEnum.getType(); - int confVersion = conf.getConfigVersion(confType); - String confMsg = YamlUtil.dump(conf.getConfig(confType)); - ConfigResPacket resPacket = new ConfigResPacket(confType, confVersion, confMsg); + int confVersion = conf.getConfigVersion(configEnum); + String confMsg = YamlUtil.dump(conf.getConfig(configEnum)); + ConfigResPacket resPacket = new ConfigResPacket(configEnum.getType(), confVersion, confMsg); session.answerClientNow(resPacket); } } diff --git a/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java b/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java index 5de0052..e8d1c75 100644 --- a/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java +++ b/source/src/main/java/io/mycat/proxy/man/cmds/ConfigUpdatePacketCommand.java @@ -2,7 +2,8 @@ import io.mycat.mycat2.ConfigLoader; import io.mycat.mycat2.MycatConfig; -import io.mycat.mycat2.beans.ReplicaIndexBean; +import io.mycat.mycat2.beans.conf.ClusterConfig; +import io.mycat.mycat2.beans.conf.ReplicaIndexConfig; import io.mycat.proxy.ConfigEnum; import io.mycat.proxy.ProxyRuntime; import io.mycat.proxy.man.*; @@ -14,8 +15,6 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; /** * Desc: 用来处理配置更新相关报文 @@ -48,7 +47,7 @@ public void handlerPkg(AdminSession session, byte cmdType) throws IOException { } public boolean sendPreparePacket(ConfigEnum configEnum, Object bean, String attach) { - MycatConfig config = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); + MycatConfig config = ProxyRuntime.INSTANCE.getConfig(); MyCluster cluster = ProxyRuntime.INSTANCE.getMyCLuster(); byte type = configEnum.getType(); @@ -58,7 +57,7 @@ public boolean sendPreparePacket(ConfigEnum configEnum, Object bean, String atta } // 获取新版本 - int newVersion = config.getNextConfigVersion(type); + int newVersion = config.getConfigVersion(configEnum) + 1; // 生成yml文件内容 String content = YamlUtil.dump(bean); // 存储在本地prepare文件夹下 @@ -71,6 +70,7 @@ public boolean sendPreparePacket(ConfigEnum configEnum, Object bean, String atta // 设置延迟任务,确认是否超时回复 ProxyRuntime runtime = ProxyRuntime.INSTANCE; + ClusterConfig clusterConfig = config.getConfig(ConfigEnum.CLUSTER); runtime.addDelayedJob(() -> { ConfigConfirmBean confirmBean = cluster.configConfirmMap.get(type); if (confirmBean == null || confirmBean.confirmVersion != newVersion) { @@ -83,7 +83,7 @@ public boolean sendPreparePacket(ConfigEnum configEnum, Object bean, String atta //todo config update 命令处理需要给前端返回 } cluster.configConfirmMap.remove(type); - }, runtime.getProxyConfig().getCluster().getPrepareDelaySeconds()); + }, clusterConfig.getCluster().getPrepareDelaySeconds()); return true; } @@ -128,8 +128,9 @@ private void handleConfirm(AdminSession session) throws IOException { cluster.configConfirmMap.remove(configType); if (configEnum == ConfigEnum.REPLICA_INDEX) { - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); - ProxyRuntime.INSTANCE.startSwitchDataSource(attach, conf.getRepIndex(attach)); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + ReplicaIndexConfig repIndexConfig = conf.getConfig(ConfigEnum.REPLICA_INDEX); + ProxyRuntime.INSTANCE.startSwitchDataSource(attach, repIndexConfig.getReplicaIndexes().get(attach)); } } } @@ -143,9 +144,10 @@ private void handleCommit(AdminSession session) throws IOException { ConfigLoader.INSTANCE.load(configEnum, commitPacket.getConfVersion()); if (configEnum == ConfigEnum.REPLICA_INDEX) { - MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig(); + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + ReplicaIndexConfig repIndexConfig = conf.getConfig(ConfigEnum.REPLICA_INDEX); String attach = commitPacket.getAttach(); - ProxyRuntime.INSTANCE.startSwitchDataSource(attach, conf.getRepIndex(attach)); + ProxyRuntime.INSTANCE.startSwitchDataSource(attach, repIndexConfig.getReplicaIndexes().get(attach)); } } diff --git a/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java b/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java index 72e7ded..ccd6639 100644 --- a/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java +++ b/source/src/main/java/io/mycat/proxy/man/packet/NodeRegInfoPacket.java @@ -1,5 +1,7 @@ package io.mycat.proxy.man.packet; +import io.mycat.mycat2.beans.conf.ProxyConfig; +import io.mycat.proxy.ConfigEnum; import io.mycat.proxy.ProxyBuffer; import io.mycat.proxy.ProxyRuntime; import io.mycat.proxy.man.ManagePacket; @@ -29,7 +31,8 @@ public NodeRegInfoPacket(String nodeId, MyCluster.ClusterState myClusterState,lo this.lastClusterStateTime=lastClusterStateTime; setMyLeader(myLeader); this.startupTime = startupTime; - this.proxyPort = ProxyRuntime.INSTANCE.getProxyConfig().getProxy().getPort(); + ProxyConfig proxyConfig = ProxyRuntime.INSTANCE.getConfig().getConfig(ConfigEnum.PROXY); + this.proxyPort = proxyConfig.getProxy().getPort(); } public NodeRegInfoPacket() { diff --git a/source/src/main/java/io/mycat/util/YamlUtil.java b/source/src/main/java/io/mycat/util/YamlUtil.java index 59ea1c4..2bcfc3b 100644 --- a/source/src/main/java/io/mycat/util/YamlUtil.java +++ b/source/src/main/java/io/mycat/util/YamlUtil.java @@ -1,7 +1,6 @@ package io.mycat.util; import io.mycat.mycat2.ConfigLoader; -import io.mycat.mycat2.beans.ReplicaConfBean; import io.mycat.proxy.Configurable; import io.mycat.proxy.ProxyRuntime; import org.slf4j.Logger; @@ -13,11 +12,7 @@ import java.io.*; import java.net.URL; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; import java.nio.file.*; -import java.util.EnumSet; -import java.util.Map; import java.util.stream.Stream; /** From 51de16987e8f43032b5e44090632f72f07c2f087 Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Sun, 24 Sep 2017 16:43:39 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=AD=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/mycat/mycat2/ConfigLoader.java | 15 ++++++++++++--- .../src/main/java/io/mycat/mycat2/MycatCore.java | 10 +++------- .../java/io/mycat/mycat2/beans/GlobalBean.java | 2 ++ .../java/io/mycat/mycat2/beans/MySQLMetaBean.java | 15 +-------------- .../mycat/mycat2/beans/conf/DatasourceConfig.java | 1 - .../mycat2/beans/conf/DatasourceMetaBean.java | 13 ++++++++++++- .../mycat2/beans/heartbeat/MySQLDetector.java | 2 +- .../mycat2/beans/heartbeat/MySQLHeartbeat.java | 2 +- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java index 8f371c3..a64752f 100644 --- a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java +++ b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java @@ -24,7 +24,12 @@ public class ConfigLoader { public static final String DIR_PREPARE = "prepare" + File.separator; public static final String DIR_ARCHIVE = "archive" + File.separator; - private ConfigLoader() {} + public void loadCore() throws IOException { + ConfigLoader.INSTANCE.load(ConfigEnum.PROXY, null); + ConfigLoader.INSTANCE.load(ConfigEnum.HEARTBEAT, null); + ConfigLoader.INSTANCE.load(ConfigEnum.CLUSTER, null); + ConfigLoader.INSTANCE.load(ConfigEnum.BALANCER, null); + } public void loadAll() throws IOException { // 保证文件夹存在 @@ -43,8 +48,12 @@ public void loadAll() throws IOException { } public void load(ConfigEnum configEnum, Integer targetVersion) throws IOException { - loadConfig(true, configEnum, targetVersion); - YamlUtil.clearDirectory(DIR_PREPARE, configEnum.getFileName()); + if (targetVersion != null) { + loadConfig(true, configEnum, targetVersion); + YamlUtil.clearDirectory(DIR_PREPARE, configEnum.getFileName()); + } else { + loadConfig(false, configEnum, targetVersion); + } if (configEnum == ConfigEnum.SCHEMA) { ProxyRuntime.INSTANCE.getConfig().initSchemaMap(); diff --git a/source/src/main/java/io/mycat/mycat2/MycatCore.java b/source/src/main/java/io/mycat/mycat2/MycatCore.java index b02d91b..dacc6c7 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatCore.java +++ b/source/src/main/java/io/mycat/mycat2/MycatCore.java @@ -38,17 +38,13 @@ * @author wuzhihui */ public class MycatCore { - private static final Logger logger = LoggerFactory.getLogger(MycatCore.class); - public static void main(String[] args) throws IOException { - // mycat.conf的加载不需要在集群内 - ProxyConfig proxy = YamlUtil.load(ConfigEnum.PROXY.getFileName(), ProxyConfig.class); - logger.debug("load config for {}", ConfigEnum.PROXY); - MycatConfig conf = new MycatConfig(); - ProxyRuntime runtime = ProxyRuntime.INSTANCE; + MycatConfig conf = new MycatConfig(); runtime.setConfig(conf); + ConfigLoader.INSTANCE.loadCore(); + int cpus = Runtime.getRuntime().availableProcessors(); // int cpus = 1; runtime.setNioReactorThreads(cpus); diff --git a/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java b/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java index 7509776..fb432e6 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/GlobalBean.java @@ -26,4 +26,6 @@ public class GlobalBean { public static final String[] MYSQL_CLUSTER_STAUTS_COLMS = {"Variable_name", "Value"}; public static final int INIT_VERSION = 1; + // 默认的重试次数 + public static final int MAX_RETRY_COUNT = 5; } diff --git a/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java b/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java index f7b1257..6889a48 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java @@ -47,9 +47,6 @@ * @author wuzhihui */ public class MySQLMetaBean { - //默认的重试次数 - private static final int MAX_RETRY_COUNT = 5; - private static final Logger logger = LoggerFactory.getLogger(MySQLMetaBean.class); private DatasourceMetaBean dsMetaBean; @@ -57,9 +54,7 @@ public class MySQLMetaBean { private volatile long heartbeatRecoveryTime; // 心跳暂停时间 private DBHeartbeat heartbeat; private MySQLRepBean repBean; - - private int maxRetryCount = MAX_RETRY_COUNT; //重试次数 - + private int slaveThreshold = -1; public boolean charsetLoaded = false; @@ -195,14 +190,6 @@ public void setSlaveThreshold(int slaveThreshold) { this.slaveThreshold = slaveThreshold; } - public int getMaxRetryCount() { - return maxRetryCount; - } - - public void setMaxRetryCount(int maxRetryCount) { - this.maxRetryCount = maxRetryCount; - } - public DBHeartbeat getHeartbeat() { return heartbeat; } diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java index c702f49..6bdad2d 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceConfig.java @@ -1,6 +1,5 @@ package io.mycat.mycat2.beans.conf; -import io.mycat.mycat2.beans.GlobalBean; import io.mycat.proxy.Configurable; import java.util.List; diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java index 60fd150..39b6b95 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/DatasourceMetaBean.java @@ -1,5 +1,7 @@ package io.mycat.mycat2.beans.conf; +import io.mycat.mycat2.beans.GlobalBean; + /** * Desc: * @@ -14,6 +16,7 @@ public class DatasourceMetaBean { private String password; private int maxCon = 1000; private int minCon = 1; + private int maxRetryCount = GlobalBean.MAX_RETRY_COUNT; public String getHostName() { return hostName; @@ -71,9 +74,17 @@ public void setMinCon(int minCon) { this.minCon = minCon; } + public int getMaxRetryCount() { + return maxRetryCount; + } + + public void setMaxRetryCount(int maxRetryCount) { + this.maxRetryCount = maxRetryCount; + } + @Override public String toString() { return "DatasourceMetaBean{" + "hostName='" + hostName + '\'' + ", ip='" + ip + '\'' + ", port=" + port + ", user='" + user + '\'' - + ", password='" + password + '\'' + ", maxCon=" + maxCon + ", minCon=" + minCon + '}'; + + ", password='" + password + '\'' + ", maxCon=" + maxCon + ", minCon=" + minCon + ", maxRetryCount=" + maxRetryCount + '}'; } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java index e4b2da0..005001a 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java +++ b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLDetector.java @@ -90,7 +90,7 @@ public void heartbeat() throws IOException { heartbeatTask.doHeartbeat(); }else{ //连接创建 失败. 如果是主节点,需要重试.并在达到重试次数后,通知集群 - if(heartbeat.incrErrorCount() < heartbeat.getSource().getMaxRetryCount()){ + if(heartbeat.incrErrorCount() < heartbeat.getSource().getDsMetaBean().getMaxRetryCount()){ heartbeat(); }else{ heartbeat.setResult(DBHeartbeat.ERROR_STATUS, diff --git a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java index be8fe35..9101c01 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java +++ b/source/src/main/java/io/mycat/mycat2/beans/heartbeat/MySQLHeartbeat.java @@ -207,7 +207,7 @@ private void setOk(MySQLDetector detector) { private void setError(MySQLDetector detector, String msg) { // should continues check error status - if (++errorCount < source.getMaxRetryCount()) { + if (++errorCount < source.getDsMetaBean().getMaxRetryCount()) { if (detector != null && !detector.isQuit()) { heartbeat(); // error count not enough, heart beat again } From de853090da12cb6193f363df05c1515e5a4d6b61 Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Sun, 24 Sep 2017 18:06:32 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/mycat/mycat2/MycatCore.java | 50 +++++++++++++++++-- .../java/io/mycat/mycat2/beans/ArgsBean.java | 19 +++++++ .../mycat/mycat2/beans/conf/BalancerBean.java | 6 +++ source/src/main/resources/datasource.yml | 18 +++---- source/src/main/resources/mycat1.yml | 25 ---------- source/src/main/resources/mycat2.yml | 13 ----- source/src/main/resources/mycat3.yml | 14 ------ 7 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 source/src/main/java/io/mycat/mycat2/beans/ArgsBean.java delete mode 100644 source/src/main/resources/mycat1.yml delete mode 100644 source/src/main/resources/mycat2.yml delete mode 100644 source/src/main/resources/mycat3.yml diff --git a/source/src/main/java/io/mycat/mycat2/MycatCore.java b/source/src/main/java/io/mycat/mycat2/MycatCore.java index dacc6c7..6a83486 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatCore.java +++ b/source/src/main/java/io/mycat/mycat2/MycatCore.java @@ -25,6 +25,10 @@ import java.io.IOException; +import io.mycat.mycat2.beans.ArgsBean; +import io.mycat.mycat2.beans.conf.BalancerBean; +import io.mycat.mycat2.beans.conf.BalancerConfig; +import io.mycat.mycat2.beans.conf.ClusterConfig; import io.mycat.mycat2.beans.conf.ProxyConfig; import io.mycat.proxy.ConfigEnum; import org.slf4j.Logger; @@ -40,13 +44,12 @@ public class MycatCore { public static void main(String[] args) throws IOException { ProxyRuntime runtime = ProxyRuntime.INSTANCE; - MycatConfig conf = new MycatConfig(); - runtime.setConfig(conf); + runtime.setConfig(new MycatConfig()); ConfigLoader.INSTANCE.loadCore(); + solveArgs(args); int cpus = Runtime.getRuntime().availableProcessors(); -// int cpus = 1; runtime.setNioReactorThreads(cpus); runtime.setReactorThreads(new MycatReactorThread[cpus]); @@ -60,4 +63,45 @@ public static void main(String[] args) throws IOException { ProxyStarter.INSTANCE.start(); } + + private static void solveArgs(String[] args) { + int lenght = args.length; + + MycatConfig conf = ProxyRuntime.INSTANCE.getConfig(); + ProxyConfig proxyConfig = conf.getConfig(ConfigEnum.PROXY); + ClusterConfig clusterConfig = conf.getConfig(ConfigEnum.CLUSTER); + BalancerConfig balancerConfig= conf.getConfig(ConfigEnum.BALANCER); + + for (int i = 0; i < lenght; i++) { + switch(args[i]) { + case ArgsBean.PROXY_PORT: + proxyConfig.getProxy().setPort(Integer.parseInt(args[++i])); + break; + case ArgsBean.CLUSTER_ENABLE: + clusterConfig.getCluster().setEnable(Boolean.parseBoolean(args[++i])); + break; + case ArgsBean.CLUSTER_PORT: + clusterConfig.getCluster().setPort(Integer.parseInt(args[++i])); + break; + case ArgsBean.CLUSTER_MY_NODE_ID: + clusterConfig.getCluster().setMyNodeId(args[++i]); + break; + case ArgsBean.BALANCER_ENABLE: + balancerConfig.getBalancer().setEnable(Boolean.parseBoolean(args[++i])); + break; + case ArgsBean.BALANCER_PORT: + balancerConfig.getBalancer().setPort(Integer.parseInt(args[++i])); + break; + case ArgsBean.BALANCER_STRATEGY: + BalancerBean.BalancerStrategyEnum strategy = BalancerBean.BalancerStrategyEnum.getEnum(args[++i]); + if (strategy == null) { + throw new IllegalArgumentException("no such balancer strategy"); + } + balancerConfig.getBalancer().setStrategy(strategy); + break; + default: + break; + } + } + } } diff --git a/source/src/main/java/io/mycat/mycat2/beans/ArgsBean.java b/source/src/main/java/io/mycat/mycat2/beans/ArgsBean.java new file mode 100644 index 0000000..47824c4 --- /dev/null +++ b/source/src/main/java/io/mycat/mycat2/beans/ArgsBean.java @@ -0,0 +1,19 @@ +package io.mycat.mycat2.beans; + +/** + * Desc: 命令行参数 + * + * @date: 24/09/2017 + * @author: gaozhiwen + */ +public class ArgsBean { + public static final String PROXY_PORT = "-mycat.proxy.port"; + + public static final String CLUSTER_ENABLE = "-mycat.cluster.enable"; + public static final String CLUSTER_PORT = "-mycat.cluster.port"; + public static final String CLUSTER_MY_NODE_ID = "-mycat.cluster.myNodeId"; + + public static final String BALANCER_ENABLE = "-mycat.balancer.enable"; + public static final String BALANCER_PORT = "-mycat.balancer.port"; + public static final String BALANCER_STRATEGY = "-mycat.proxy.strategy"; +} diff --git a/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java b/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java index a7f6a54..f36b1e0 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/conf/BalancerBean.java @@ -1,5 +1,7 @@ package io.mycat.mycat2.beans.conf; +import java.util.stream.Stream; + /** * Desc: 负载均衡配置类 * @@ -9,6 +11,10 @@ public class BalancerBean { public enum BalancerStrategyEnum { RANDOM, ROUND_ROBIN, WEIGHT_RANDOM, WEIGHT_ROUND_ROBIN, RESPONSE_TIME, LEAST_CONNECTION, CAPACITY; + + public static BalancerStrategyEnum getEnum(String enumName) { + return Stream.of(BalancerStrategyEnum.values()).filter(strategy -> strategy.name().equalsIgnoreCase(enumName)).findFirst().orElse(null); + } } private boolean enable = false; diff --git a/source/src/main/resources/datasource.yml b/source/src/main/resources/datasource.yml index 6dbe441..4d46c0e 100644 --- a/source/src/main/resources/datasource.yml +++ b/source/src/main/resources/datasource.yml @@ -1,6 +1,6 @@ replicas: - name: test - repType: MASTER_SLAVE + repType: SINGLE_NODE switchType: DEFAULT_SWITCH balanceType: BALANCE_ALL mysqls: @@ -12,11 +12,11 @@ replicas: minCon: 1 maxCon: 10 maxRetryCount: 3 - - hostName: test - ip: 127.0.0.1 - port: 3307 - user: root - password: 123456 - minCon: 1 - maxCon: 10 - maxRetryCount: 3 #心跳重试次数 \ No newline at end of file +# - hostName: test +# ip: 127.0.0.1 +# port: 3307 +# user: root +# password: 123456 +# minCon: 1 +# maxCon: 10 +# maxRetryCount: 3 #心跳重试次数 \ No newline at end of file diff --git a/source/src/main/resources/mycat1.yml b/source/src/main/resources/mycat1.yml deleted file mode 100644 index 5585552..0000000 --- a/source/src/main/resources/mycat1.yml +++ /dev/null @@ -1,25 +0,0 @@ -proxy: - ip: 0.0.0.0 - port: 8066 - -cluster: - enable: false - ip: 0.0.0.0 - port: 9066 - myNodeId: leader-1 - allNodes: leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9067,leader-3:127.0.0.1:9068 - prepareDelaySeconds: 30 - -balancer: - enable: false - ip: 0.0.0.0 - port: 7066 - strategy: RANDOM - -heartbeat: - timerExecutor: 2 - replicaHeartbeatPeriod: 10000 - replicaIdleCheckPeriod: 2000 - idleTimeout: 2000 - processorCheckPeriod: 2000 - minSwitchtimeInterval: 120000 \ No newline at end of file diff --git a/source/src/main/resources/mycat2.yml b/source/src/main/resources/mycat2.yml deleted file mode 100644 index 42078ff..0000000 --- a/source/src/main/resources/mycat2.yml +++ /dev/null @@ -1,13 +0,0 @@ -bindIP: 0.0.0.0 -bindPort: 8067 -clusterEnable: true -clusterIP: 0.0.0.0 -clusterPort: 9067 -myNodeId: leader-2 -allNodeInfs: leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9067,leader-3:127.0.0.1:9068 -timerExecutor: 2 -replicaHeartbeatPeriod: 10000 -replicaIdleCheckPeriod: 2000 -idleTimeout: 2000 -processorCheckPeriod: 2000 -minSwitchtimeInterval: 120000 diff --git a/source/src/main/resources/mycat3.yml b/source/src/main/resources/mycat3.yml deleted file mode 100644 index debc818..0000000 --- a/source/src/main/resources/mycat3.yml +++ /dev/null @@ -1,14 +0,0 @@ -bindIP: 0.0.0.0 -bindPort: 8068 -clusterEnable: true -clusterIP: 0.0.0.0 -clusterPort: 9068 -myNodeId: leader-3 -allNodeInfs: leader-1:127.0.0.1:9066,leader-2:127.0.0.1:9067,leader-3:127.0.0.1:9068 -timerExecutor: 2 -replicaHeartbeatPeriod: 10000 -replicaIdleCheckPeriod: 2000 -idleTimeout: 2000 -processorCheckPeriod: 2000 -minSwitchtimeInterval: 120000 - From 4003a1e4d1f25e061948a5ba5c354d515ddc1049 Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Sun, 24 Sep 2017 20:32:43 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E9=9B=86=E7=BE=A4=E4=B8=BB=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=8C=82=E6=8E=89=E5=90=8E=EF=BC=8C=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E9=87=8D=E6=96=B0=E9=80=89=E4=B8=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mycat/proxy/man/DefaultAdminSessionHandler.java | 7 +++++-- .../src/main/java/io/mycat/proxy/man/MyCluster.java | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/source/src/main/java/io/mycat/proxy/man/DefaultAdminSessionHandler.java b/source/src/main/java/io/mycat/proxy/man/DefaultAdminSessionHandler.java index dee0ae2..4abaff2 100644 --- a/source/src/main/java/io/mycat/proxy/man/DefaultAdminSessionHandler.java +++ b/source/src/main/java/io/mycat/proxy/man/DefaultAdminSessionHandler.java @@ -53,8 +53,11 @@ public void onSocketRead(final AdminSession session) throws IOException { */ public void onSocketClosed(AdminSession userSession, boolean normal) { logger.info("front socket closed "); - userSession.cluster().onClusterNodeDown(userSession.getNodeId(), userSession); - + try { + userSession.cluster().onClusterNodeDown(userSession.getNodeId(), userSession); + } catch (IOException e) { + logger.error("error to handle cluster node down", e); + } } @Override diff --git a/source/src/main/java/io/mycat/proxy/man/MyCluster.java b/source/src/main/java/io/mycat/proxy/man/MyCluster.java index ecc9b5f..208bc27 100644 --- a/source/src/main/java/io/mycat/proxy/man/MyCluster.java +++ b/source/src/main/java/io/mycat/proxy/man/MyCluster.java @@ -236,7 +236,7 @@ private AdminSession findSession(String nodeId) { return null; } - public void onClusterNodeDown(String nodeId, AdminSession session) { + public void onClusterNodeDown(String nodeId, AdminSession session) throws IOException { ClusterNode theNode = allNodes.get(nodeId); theNode.setState(NodeState.Offline); logger.info("Node offline " + theNode.id + " at " + theNode.ip + ":" + theNode.port + " started at " @@ -247,6 +247,17 @@ public void onClusterNodeDown(String nodeId, AdminSession session) { // 当前集群失去主节点,关闭proxy服务 ProxyStarter.INSTANCE.stopProxy(); + + if (checkIfLeader()) { + logger.info("My Leader crashed, I'm smallest alive node, and exceeded 1/2 nodes alive, so I'm the King now!"); + // 集群主已产生,继续加载配置,提供服务 + ProxyStarter.INSTANCE.startProxy(true); + + this.setClusterState(ClusterState.Clustered); + this.myLeader = this.myNode; + JoinCLusterNotifyPacket joinReps = createJoinNotifyPkg(session,JoinCLusterNotifyPacket.JOIN_STATE_NEED_ACK); + notifyAllNodes(session,joinReps); + } } else if (myLeader == myNode) { if (checkIfNeedDismissCluster()) { logger.warn("Less than 1/2 mumbers in my Kingdom ,so I quit"); From c8cd962938e1e75fc92f255e3a2bf16f69fae9ab Mon Sep 17 00:00:00 2001 From: gaulzhw Date: Sun, 24 Sep 2017 21:23:52 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 22 +++++++++++++--------- source/pom.xml | 6 +++--- source/src/main/resources/datasource.yml | 18 +++++++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3834677..90d7de7 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,13 @@ Genel TCP Proxy using Java NIO ,simple and fast ### 集群配置说明 - 非集群模式 - - 修改`mycat.yml`中的`clusterEnable: false` + - 指定`mycat.yml`中的ip和port + - 修改`cluster.yml`中的`enable: false`,设置cluster的port - 集群模式 - - 修改`mycat.yml`中的`clusterEnable: true` - - `myNodeId`需要在集群中唯一 + - 开启集群:`cluster.yml`中的`enable: true`,设置cluster的port,`myNodeId`需要在集群中唯一 + - 负载均衡:`balancer.yml`中的`enable: true`,设置balancer的port及strategy - 集群模式下,只有在集群状态下才提供代理服务,在脱离集群状态下将暂时无法提供代理服务 @@ -57,12 +58,15 @@ Genel TCP Proxy using Java NIO ,simple and fast #### 四 启动第二个mycat,并自动加入集群。 4.1 集群相关配置文件 - `conf` 目录下, mycat1.yml,mycat2.yml,mycat3.yml 三个配置文件,分别为集群中三个节点的配置文件。
- 配置文件名称格式为: `mycat[unique id]. conf`
+ `conf` 目录下, 修改mycat.yml,cluster.yml,balancer.yml 三个配置文件。
配置文件中 `cluster.allnodes` 属性 需要将集群中,所有节点的信息配置上。
- - `conf` 目录下, 设置 `wrapper.conf` 配置文件中,
- `wrapper.app.parameter.2` 参数的值 为 mycat[`unique id`]. yml 中的 `unique id` 。
- 确定从哪个mycat[unique id]. conf 配置文件中 读取配置。
4.2 需要注意的是,每个节点一套mycat程序。
4.3 配置完成后 按照第三步 启动mycat. 新启动的mycat 将自动加入集群中。
+ +### 五 在IDEA中调试集群 + + IDEA中调试可以设置启动参数,支持的启动参数: + -mycat.proxy.port 8067 + -mycat.cluster.enable true + -mycat.cluster.port 9067 + -mycat.cluster.myNodeId leader-2 \ No newline at end of file diff --git a/source/pom.xml b/source/pom.xml index d92eca9..55f6006 100644 --- a/source/pom.xml +++ b/source/pom.xml @@ -235,9 +235,9 @@ mycat io.mycat.mycat2.MycatCore - - 1 - + + + jsw diff --git a/source/src/main/resources/datasource.yml b/source/src/main/resources/datasource.yml index 4d46c0e..6dbe441 100644 --- a/source/src/main/resources/datasource.yml +++ b/source/src/main/resources/datasource.yml @@ -1,6 +1,6 @@ replicas: - name: test - repType: SINGLE_NODE + repType: MASTER_SLAVE switchType: DEFAULT_SWITCH balanceType: BALANCE_ALL mysqls: @@ -12,11 +12,11 @@ replicas: minCon: 1 maxCon: 10 maxRetryCount: 3 -# - hostName: test -# ip: 127.0.0.1 -# port: 3307 -# user: root -# password: 123456 -# minCon: 1 -# maxCon: 10 -# maxRetryCount: 3 #心跳重试次数 \ No newline at end of file + - hostName: test + ip: 127.0.0.1 + port: 3307 + user: root + password: 123456 + minCon: 1 + maxCon: 10 + maxRetryCount: 3 #心跳重试次数 \ No newline at end of file