use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
the class MigrateDumpRunner method loaddataToDn.
private void loaddataToDn(File loaddataFile, String toDn, String table) 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());
String sql = "load data local infile '" + loaddataFile.getPath().replace("\\", "//") + "' replace into table " + table + " character set 'utf8mb4' fields terminated by ',' enclosed by '\"' ESCAPED BY '\\\\' lines terminated by '\\n'";
JdbcUtils.execute(con, sql, new ArrayList<>());
} finally {
JdbcUtils.close(con);
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
the class MigrateMainRunner method run.
@Override
public void run() {
AtomicInteger sucessTask = new AtomicInteger(0);
CountDownLatch downLatch = new CountDownLatch(migrateTaskList.size());
for (MigrateTask migrateTask : migrateTaskList) {
MycatServer.getInstance().getBusinessExecutor().submit(new MigrateDumpRunner(migrateTask, downLatch, sucessTask));
}
try {
downLatch.await(2, TimeUnit.HOURS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 同一个dataHost的任务合并执行,避免过多流量浪费
if (sucessTask.get() == migrateTaskList.size()) {
long binlogFileNum = -1;
String binlogFile = "";
long pos = -1;
for (MigrateTask migrateTask : migrateTaskList) {
if (binlogFileNum == -1) {
binlogFileNum = Integer.parseInt(migrateTask.getBinlogFile().substring(migrateTask.getBinlogFile().lastIndexOf(".") + 1));
binlogFile = migrateTask.getBinlogFile();
pos = migrateTask.getPos();
} else {
int tempBinlogFileNum = Integer.parseInt(migrateTask.getBinlogFile().substring(migrateTask.getBinlogFile().lastIndexOf(".") + 1));
if (tempBinlogFileNum <= binlogFileNum && migrateTask.getPos() <= pos) {
binlogFileNum = tempBinlogFileNum;
binlogFile = migrateTask.getBinlogFile();
pos = migrateTask.getPos();
}
}
}
String taskPath = migrateTaskList.get(0).getZkpath();
taskPath = taskPath.substring(0, taskPath.lastIndexOf("/"));
String taskID = taskPath.substring(taskPath.lastIndexOf('/') + 1, taskPath.length());
// 开始增量数据迁移
PhysicalDBPool dbPool = MycatServer.getInstance().getConfig().getDataHosts().get(dataHost);
PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
DBHostConfig config = datasource.getConfig();
BinlogStream stream = new BinlogStream(config.getUrl().substring(0, config.getUrl().indexOf(":")), config.getPort(), config.getUser(), config.getPassword());
try {
stream.setSlaveID(migrateTaskList.get(0).getSlaveId());
stream.setBinglogFile(binlogFile);
stream.setBinlogPos(pos);
stream.setMigrateTaskList(migrateTaskList);
BinlogStreamHoder.binlogStreamMap.put(taskID, stream);
stream.connect();
} catch (IOException e) {
LOGGER.error("error:", e);
}
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
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.PhysicalDBPool in project Mycat_plus by coderczp.
the class GlobalTableUtil method reGetColumnsForTable.
/**
* 重新获得table 的列list
* @param tableName
*/
private static void reGetColumnsForTable(String tableName) {
MycatConfig config = MycatServer.getInstance().getConfig();
if (globalTableMap != null && globalTableMap.get(tableName.toUpperCase()) != null) {
TableConfig tableConfig = globalTableMap.get(tableName.toUpperCase());
if (// consistencyCheck 在运行中
tableConfig == null || isInnerColumnCheckFinished != 1)
return;
String nodeName = tableConfig.getDataNodes().get(0);
Map<String, PhysicalDBNode> map = config.getDataNodes();
for (String k2 : map.keySet()) {
PhysicalDBNode dBnode = map.get(k2);
if (nodeName.equals(dBnode.getName())) {
PhysicalDBPool pool = dBnode.getDbPool();
List<PhysicalDatasource> dsList = (List<PhysicalDatasource>) pool.genAllDataSources();
for (PhysicalDatasource ds : dsList) {
if (ds instanceof MySQLDataSource) {
MySQLDataSource mds = (MySQLDataSource) dsList.get(0);
MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, tableConfig.getName());
checker.checkInnerColumnExist();
// 运行一次就行了,不需要像consistencyCheck那样每个db都运行一次
return;
}
}
}
}
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
the class ConfigTest method getPhysicalDBPool.
private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) {
String name = conf.getName();
String dbType = conf.getDbType();
String dbDriver = conf.getDbDriver();
PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false);
Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts();
Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>(readHostsMap.size());
for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) {
PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true);
readSourcesMap.put(entry.getKey(), readSources);
}
PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType());
return pool;
}
Aggregations