use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
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_plus by coderczp.
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);
}
}
}
}
}
}
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class ConfigInitializer method initDataHosts.
private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) {
Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts();
boolean isBooster = "booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE));
// 根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool
Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>(nodeConfs.size());
for (DataHostConfig conf : nodeConfs.values()) {
if (isBooster) {
conf.setMinCon(2);
}
// 建立PhysicalDBPool
PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader);
nodes.put(pool.getHostName(), pool);
}
return nodes;
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class ConfigInitializer method testConnection.
public void testConnection() {
// 实际链路的连接测试
if (this.dataNodes != null && this.dataHosts != null) {
Map<String, Boolean> map = new HashMap<String, Boolean>();
for (PhysicalDBNode dataNode : dataNodes.values()) {
String database = dataNode.getDatabase();
PhysicalDBPool pool = dataNode.getDbPool();
for (PhysicalDatasource ds : pool.getAllDataSources()) {
String key = ds.getName() + "_" + database;
if (map.get(key) == null) {
map.put(key, false);
boolean isConnected = false;
try {
isConnected = ds.testConnection(database);
map.put(key, isConnected);
} catch (IOException e) {
LOGGER.warn("test conn error:", e);
}
}
}
}
//
boolean isConnectivity = true;
for (Map.Entry<String, Boolean> entry : map.entrySet()) {
String key = entry.getKey();
Boolean value = entry.getValue();
if (!value && isConnectivity) {
LOGGER.warn("SelfCheck### test " + key + " database connection failed ");
isConnectivity = false;
} else {
LOGGER.info("SelfCheck### test " + key + " database connection success ");
}
}
if (!isConnectivity) {
throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!");
}
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class ShowDataNode method getRow.
private static RowDataPacket getRow(PhysicalDBNode node, String charset) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(node.getName(), charset));
row.add(StringUtil.encode(node.getDbPool().getHostName() + '/' + node.getDatabase(), charset));
PhysicalDBPool pool = node.getDbPool();
PhysicalDatasource ds = pool.getSource();
if (ds != null) {
int active = ds.getActiveCountForSchema(node.getDatabase());
int idle = ds.getIdleCountForSchema(node.getDatabase());
row.add(IntegerUtil.toBytes(pool.getActivedIndex()));
row.add(StringUtil.encode(ds.getConfig().getDbType(), charset));
row.add(IntegerUtil.toBytes(active));
row.add(IntegerUtil.toBytes(idle));
row.add(IntegerUtil.toBytes(ds.getSize()));
} else {
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
}
row.add(LongUtil.toBytes(ds.getExecuteCountForSchema(node.getDatabase())));
row.add(StringUtil.encode(nf.format(0), charset));
row.add(StringUtil.encode(nf.format(0), charset));
row.add(LongUtil.toBytes(0));
long recoveryTime = pool.getSource().getHeartbeatRecoveryTime() - TimeUtil.currentTimeMillis();
row.add(LongUtil.toBytes(recoveryTime > 0 ? recoveryTime / 1000L : -1L));
return row;
}
Aggregations