use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class PostgreSQLHeartbeat method switchSourceIfNeed.
private void switchSourceIfNeed(String reason) {
int switchType = source.getHostConfig().getSwitchType();
if (switchType == DataHostConfig.NOT_SWITCH_DS) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("not switch datasource ,for switchType is " + DataHostConfig.NOT_SWITCH_DS);
return;
}
return;
}
PhysicalDBPool pool = this.source.getDbPool();
int curDatasourceHB = pool.getSource().getHeartbeat().getStatus();
// read node can't switch ,only write node can switch
if (pool.getWriteType() == PhysicalDBPool.WRITE_ONLYONE_NODE && !source.isReadNode() && curDatasourceHB != DBHeartbeat.OK_STATUS && pool.getSources().length > 1) {
synchronized (pool) {
// try to see if need switch datasource
curDatasourceHB = pool.getSource().getHeartbeat().getStatus();
if (curDatasourceHB != DBHeartbeat.INIT_STATUS && curDatasourceHB != DBHeartbeat.OK_STATUS) {
int curIndex = pool.getActivedIndex();
int nextId = pool.next(curIndex);
PhysicalDatasource[] allWriteNodes = pool.getSources();
while (true) {
if (nextId == curIndex) {
break;
}
PhysicalDatasource theSource = allWriteNodes[nextId];
DBHeartbeat theSourceHB = theSource.getHeartbeat();
int theSourceHBStatus = theSourceHB.getStatus();
if (theSourceHBStatus == DBHeartbeat.OK_STATUS) {
if (switchType == DataHostConfig.SYN_STATUS_SWITCH_DS) {
if (Integer.valueOf(0).equals(theSourceHB.getSlaveBehindMaster())) {
LOGGER.info("try to switch datasource ,slave is synchronized to master " + theSource.getConfig());
pool.switchSource(nextId, true, reason);
break;
} else {
LOGGER.warn("ignored datasource ,slave is not synchronized to master , slave behind master :" + theSourceHB.getSlaveBehindMaster() + " " + theSource.getConfig());
}
} else {
// normal switch
LOGGER.info("try to switch datasource ,not checked slave synchronize status " + theSource.getConfig());
pool.switchSource(nextId, true, reason);
break;
}
}
nextId = pool.next(nextId);
}
}
}
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class RouterUtil method getRandomDataNode.
@Deprecated
private static String getRandomDataNode(TableConfig tc) {
// 写节点不可用,意味着读节点也不可用。
// 直接使用下一个 dataHost
String randomDn = tc.getRandomDataNode();
MycatConfig mycatConfig = MycatServer.getInstance().getConfig();
if (mycatConfig != null) {
PhysicalDBNode physicalDBNode = mycatConfig.getDataNodes().get(randomDn);
if (physicalDBNode != null) {
if (physicalDBNode.getDbPool().getSource().isAlive()) {
for (PhysicalDBPool pool : MycatServer.getInstance().getConfig().getDataHosts().values()) {
if (pool.getSource().getHostConfig().containDataNode(randomDn)) {
continue;
}
if (pool.getSource().isAlive()) {
return pool.getSource().getHostConfig().getRandomDataNode();
}
}
}
}
}
// all fail return default
return randomDn;
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class RouterUtil method getAliveRandomDataNode.
/**
* 解决getRandomDataNode方法获取错误节点的问题.
* @param tc
* @return
*/
private static String getAliveRandomDataNode(TableConfig tc) {
List<String> randomDns = tc.getDataNodes();
MycatConfig mycatConfig = MycatServer.getInstance().getConfig();
if (mycatConfig != null) {
for (String randomDn : randomDns) {
PhysicalDBNode physicalDBNode = mycatConfig.getDataNodes().get(randomDn);
if (physicalDBNode != null) {
if (physicalDBNode.getDbPool().getSource().isAlive()) {
for (PhysicalDBPool pool : MycatServer.getInstance().getConfig().getDataHosts().values()) {
PhysicalDatasource source = pool.getSource();
if (source.getHostConfig().containDataNode(randomDn) && pool.getSource().isAlive()) {
return randomDn;
}
}
}
}
}
}
// all fail return default
return tc.getRandomDataNode();
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class GlobalTableUtil method consistencyCheck.
public static void consistencyCheck() {
MycatConfig config = MycatServer.getInstance().getConfig();
for (String key : globalTableMap.keySet()) {
TableConfig table = globalTableMap.get(key);
// <table name="travelrecord" dataNode="dn1,dn2,dn3"
List<String> dataNodeList = table.getDataNodes();
// 记录本次已经执行的datanode
// 多个 datanode 对应到同一个 PhysicalDatasource 只执行一次
Map<String, String> executedMap = new HashMap<>();
for (String nodeName : dataNodeList) {
Map<String, PhysicalDBNode> map = config.getDataNodes();
for (String k2 : map.keySet()) {
// <dataNode name="dn1" dataHost="localhost1" database="db1" />
PhysicalDBNode dBnode = map.get(k2);
if (nodeName.equals(dBnode.getName())) {
// dn1,dn2,dn3
PhysicalDBPool pool = dBnode.getDbPool();
Collection<PhysicalDatasource> allDS = pool.genAllDataSources();
for (PhysicalDatasource pds : allDS) {
if (pds instanceof MySQLDataSource) {
MySQLDataSource mds = (MySQLDataSource) pds;
if (executedMap.get(pds.getName()) == null) {
MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, table.getName());
isInnerColumnCheckFinished = 0;
checker.checkInnerColumnExist();
while (isInnerColumnCheckFinished <= 0) {
LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
LOGGER.warn(e.getMessage());
}
}
LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
// 一种 check 完成之后,再进行另一种 check
checker = new MySQLConsistencyChecker(mds, table.getName());
isColumnCountCheckFinished = 0;
checker.checkRecordCout();
while (isColumnCountCheckFinished <= 0) {
LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
LOGGER.warn(e.getMessage());
}
}
LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
checker = new MySQLConsistencyChecker(mds, table.getName());
checker.checkMaxTimeStamp();
executedMap.put(pds.getName(), nodeName);
}
}
}
}
}
}
}
}
Aggregations