use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class MigrateUtils method execulteCount.
public static long execulteCount(String sql, String toDn) throws SQLException, IOException {
PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(toDn);
PhysicalDBPool dbPool = dbNode.getDbPool();
PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
DBHostConfig config = datasource.getConfig();
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://" + config.getUrl() + "/" + dbNode.getDatabase(), config.getUser(), config.getPassword());
List<Map<String, Object>> result = JdbcUtils.executeQuery(con, sql, new ArrayList<>());
if (result.size() == 1) {
return (long) result.get(0).get("count");
}
} finally {
JdbcUtils.close(con);
}
return 0;
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class MigrateUtils method execulteSql.
public static void execulteSql(String sql, String toDn) throws SQLException, IOException {
PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(toDn);
PhysicalDBPool dbPool = dbNode.getDbPool();
PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
DBHostConfig config = datasource.getConfig();
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://" + config.getUrl() + "/" + dbNode.getDatabase(), config.getUser(), config.getPassword());
JdbcUtils.execute(con, sql, new ArrayList<>());
} finally {
JdbcUtils.close(con);
}
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class HintDataNodeHandler method route.
@Override
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue, int hintSqlType, Map hintMap) throws SQLNonTransientException {
String stmt = realSQL;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("route datanode sql hint from " + stmt);
}
RouteResultset rrs = new RouteResultset(stmt, sqlType);
PhysicalDBNode dataNode = MycatServer.getInstance().getConfig().getDataNodes().get(hintSQLValue);
if (dataNode != null) {
rrs = RouterUtil.routeToSingleNode(rrs, dataNode.getName(), stmt);
} else {
String msg = "can't find hint datanode:" + hintSQLValue;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
return rrs;
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class RouterUtil method getRandomDataNode.
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.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class MigrateHandler method getSlaveIdFromZKForDataNode.
private static int getSlaveIdFromZKForDataNode(String dataNode) {
PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(dataNode);
String slaveIDs = dbNode.getDbPool().getSlaveIDs();
if (Strings.isNullOrEmpty(slaveIDs))
throw new RuntimeException("dataHost:" + dbNode.getDbPool().getHostName() + " do not config the salveIDs field");
List<Integer> allSlaveIDList = parseSlaveIDs(slaveIDs);
String taskPath = ZKUtils.getZKBasePath() + "slaveIDs/" + dbNode.getDbPool().getHostName();
try {
slaveIDsLock.acquire(30, TimeUnit.SECONDS);
Set<Integer> zkSlaveIdsSet = new HashSet<>();
if (ZKUtils.getConnection().checkExists().forPath(taskPath) != null) {
List<String> zkHasSlaveIDs = ZKUtils.getConnection().getChildren().forPath(taskPath);
for (String zkHasSlaveID : zkHasSlaveIDs) {
zkSlaveIdsSet.add(Integer.parseInt(zkHasSlaveID));
}
}
for (Integer integer : allSlaveIDList) {
if (!zkSlaveIdsSet.contains(integer)) {
ZKUtils.getConnection().create().creatingParentsIfNeeded().forPath(taskPath + "/" + integer);
return integer;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
slaveIDsLock.release();
} catch (Exception e) {
LOGGER.error("error:", e);
}
}
throw new RuntimeException("cannot get the slaveID for dataHost :" + dbNode.getDbPool().getHostName());
}
Aggregations