Skip to content

Commit

Permalink
修复从节点无法联通的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yanjunli committed Sep 23, 2017
1 parent ed7fa1b commit b218c17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/src/main/java/io/mycat/mycat2/ProxyStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void init(MycatConfig conf) {
value.initMaster();
value.getMysqls().forEach(metaBean -> {
try {
metaBean.init(value,ProxyRuntime.INSTANCE.maxdataSourceInitTime);
metaBean.init(value,ProxyRuntime.INSTANCE.maxdataSourceInitTime,value.getDataSourceInitStatus());
} catch (IOException e) {
LOGGER.error("error to init metaBean: {}", metaBean.getHostName());
}
Expand Down
4 changes: 2 additions & 2 deletions source/src/main/java/io/mycat/mycat2/beans/MySQLMetaBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public class MySQLMetaBean {
/** charsetName 到 默认collationIndex 的映射 */
public final Map<String, Integer> CHARSET_TO_INDEX = new HashMap<>();

public boolean init(MySQLRepBean repBean,long maxwaitTime) throws IOException {
public boolean init(MySQLRepBean repBean,long maxwaitTime,int status) throws IOException {

logger.info("init backend myqsl source ,create connections total " + minCon + " for " + hostName + " index :" + repBean.getWriteIndex());

this.repBean = repBean;
heartbeat = new MySQLHeartbeat(this,DBHeartbeat.INIT_STATUS);
heartbeat = new MySQLHeartbeat(this,status);
ProxyRuntime runtime = ProxyRuntime.INSTANCE;
MycatReactorThread[] reactorThreads = (MycatReactorThread[]) runtime.getReactorThreads();
int reactorSize = runtime.getNioReactorThreads();
Expand Down
16 changes: 14 additions & 2 deletions source/src/main/java/io/mycat/mycat2/beans/MySQLRepBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import org.slf4j.LoggerFactory;

import io.mycat.mycat2.MycatConfig;
import io.mycat.mycat2.beans.heartbeat.DBHeartbeat;
import io.mycat.mysql.Alarms;
import io.mycat.proxy.ProxyRuntime;
import io.mycat.proxy.man.MyCluster;

/**
* 表示一組MySQL Server复制集群,如主从或者多主
Expand Down Expand Up @@ -239,11 +241,11 @@ public void switchSource(int newIndex,long maxwaittime) {
if (current != newIndex) {

String reason = "switch datasource";

// init again
MySQLMetaBean newWriteBean = mysqls.get(newIndex);
newWriteBean.clearCons(reason);
newWriteBean.init(this,maxwaittime);
newWriteBean.init(this,maxwaittime,getDataSourceInitStatus());

// clear all connections
MySQLMetaBean oldMetaBean = mysqls.get(current);
Expand All @@ -268,6 +270,16 @@ public void switchSource(int newIndex,long maxwaittime) {
}
}

public int getDataSourceInitStatus(){
int initstatus = DBHeartbeat.OK_STATUS;
MyCluster myCluster = ProxyRuntime.INSTANCE.getMyCLuster();

if(myCluster.getMyLeader()==myCluster.getMyNode()){
initstatus = DBHeartbeat.INIT_STATUS;
}
return initstatus;
}

private String switchMessage(int current, int newIndex, String reason) {
StringBuilder s = new StringBuilder();
s.append("[Host=").append(name).append(",result=[").append(current).append("->");
Expand Down

0 comments on commit b218c17

Please sign in to comment.