use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
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.config.model.DBHostConfig 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.config.model.DBHostConfig 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.config.model.DBHostConfig in project Mycat-Server by MyCATApache.
the class ConfigComparer method getDataNodes.
//获取拆分表对应节点列表,具体到实例地址、库
private List<DataNode> getDataNodes(TableConfig tableConfig, Map<String, DataNodeConfig> dnConfig, Map<String, DataHostConfig> dhConfig) {
List<DataNode> dataNodes = new ArrayList<DataNode>();
//TO-DO
ArrayList<String> dataNodeNames = tableConfig.getDataNodes();
int i = 0;
for (String name : dataNodeNames) {
DataNodeConfig config = dnConfig.get(name);
String db = config.getDatabase();
String dataHost = config.getDataHost();
DataHostConfig dh = dhConfig.get(dataHost);
String dbType = dh.getDbType();
DBHostConfig[] writeHosts = dh.getWriteHosts();
DBHostConfig currentWriteHost;
if (isAwaysUseMaster) {
currentWriteHost = writeHosts[0];
} else {
//迁移数据发生在当前切换后的数据源
currentWriteHost = writeHosts[Integer.valueOf(dnIndexProps.getProperty(dh.getName()))];
}
DataNode dn = new DataNode(name, currentWriteHost.getIp(), currentWriteHost.getPort(), currentWriteHost.getUser(), currentWriteHost.getPassword(), db, dbType, i++);
dataNodes.add(dn);
}
return dataNodes;
}
Aggregations