use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
the class RollbackConfig method rollback.
private static boolean rollback() {
MycatConfig conf = MycatServer.getInstance().getConfig();
Map<String, UserConfig> users = conf.getBackupUsers();
Map<String, SchemaConfig> schemas = conf.getBackupSchemas();
Map<String, PhysicalDBNode> dataNodes = conf.getBackupDataNodes();
Map<String, PhysicalDBPool> dataHosts = conf.getBackupDataHosts();
MycatCluster cluster = conf.getBackupCluster();
FirewallConfig firewall = conf.getBackupFirewall();
// 检查可回滚状态
if (!conf.canRollback()) {
return false;
}
// 如果回滚已经存在的pool
boolean rollbackStatus = true;
Map<String, PhysicalDBPool> cNodes = conf.getDataHosts();
for (PhysicalDBPool dn : dataHosts.values()) {
dn.init(dn.getActivedIndex());
if (!dn.isInitSuccess()) {
rollbackStatus = false;
break;
}
}
// 如果回滚不成功,则清理已初始化的资源。
if (!rollbackStatus) {
for (PhysicalDBPool dn : dataHosts.values()) {
dn.clearDataSources("rollbackup config");
dn.stopHeartbeat();
}
return false;
}
// 应用回滚
conf.rollback(users, schemas, dataNodes, dataHosts, cluster, firewall);
// 处理旧的资源
for (PhysicalDBPool dn : cNodes.values()) {
dn.clearDataSources("clear old config ");
dn.stopHeartbeat();
}
// 清理缓存
MycatServer.getInstance().getCacheService().clearCache();
return true;
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
the class MigrateDumpRunner method run.
@Override
public void run() {
try {
String mysqldump = "?mysqldump -h? -P? -u? -p? ? ? --single-transaction -q --default-character-set=utf8mb4 --hex-blob --where=\"?\" --master-data=1 -T \"?\" --fields-enclosed-by=\\\" --fields-terminated-by=, --lines-terminated-by=\\n --fields-escaped-by=\\\\ ";
PhysicalDBPool dbPool = MycatServer.getInstance().getConfig().getDataNodes().get(task.getFrom()).getDbPool();
PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
DBHostConfig config = datasource.getConfig();
File file = null;
String spath = querySecurePath(config);
if (Strings.isNullOrEmpty(spath) || "NULL".equalsIgnoreCase(spath) || "empty".equalsIgnoreCase(spath)) {
file = new File(SystemConfig.getHomePath() + File.separator + "temp", "dump");
// task.getFrom() + "_" + task.getTo() + Thread.currentThread().getId() + System.currentTimeMillis() + "");
} else {
spath += Thread.currentThread().getId() + System.currentTimeMillis();
file = new File(spath);
}
file.mkdirs();
String encose = OperatingSystem.isWindows() ? "\\" : "";
String finalCmd = DataMigratorUtil.paramsAssignment(mysqldump, "?", "", config.getIp(), String.valueOf(config.getPort()), config.getUser(), config.getPassword(), MigrateUtils.getDatabaseFromDataNode(task.getFrom()), task.getTable(), makeWhere(task), file.getPath());
List<String> args = Arrays.asList("mysqldump", "-h" + config.getIp(), "-P" + String.valueOf(config.getPort()), "-u" + config.getUser(), "-p" + config.getPassword(), MigrateUtils.getDatabaseFromDataNode(task.getFrom()), task.getTable(), "--single-transaction", "-q", "--default-character-set=utf8mb4", "--hex-blob", "--where=" + makeWhere(task), "--master-data=1", "-T" + file.getPath(), "--fields-enclosed-by=" + encose + "\"", "--fields-terminated-by=,", "--lines-terminated-by=\\n", "--fields-escaped-by=\\\\");
String result = ProcessUtil.execReturnString(args);
int logIndex = result.indexOf("MASTER_LOG_FILE='");
int logPosIndex = result.indexOf("MASTER_LOG_POS=");
String logFile = result.substring(logIndex + 17, logIndex + 17 + result.substring(logIndex + 17).indexOf("'"));
String logPos = result.substring(logPosIndex + 15, logPosIndex + 15 + result.substring(logPosIndex + 15).indexOf(";"));
task.setBinlogFile(logFile);
task.setPos(Integer.parseInt(logPos));
File dataFile = new File(file, task.getTable() + ".txt");
File sqlFile = new File(file, task.getTable() + ".sql");
List<String> createTable = Files.readLines(sqlFile, Charset.forName("UTF-8"));
exeCreateTableToDn(extractCreateSql(createTable), task.getTo(), task.getTable());
if (dataFile.length() > 0) {
loaddataToDn(dataFile, task.getTo(), task.getTable());
}
pushMsgToZK(task.getZkpath(), task.getFrom() + "-" + task.getTo(), 1, "sucess", logFile, logPos);
DataMigratorUtil.deleteDir(file);
sucessTask.getAndIncrement();
} catch (Exception e) {
try {
pushMsgToZK(task.getZkpath(), task.getFrom() + "-" + task.getTo(), 0, e.getMessage(), "", "");
} catch (Exception e1) {
}
LOGGER.error("error:", e);
} finally {
latch.countDown();
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat_plus by coderczp.
the class MigrateDumpRunner method exeCreateTableToDn.
private void exeCreateTableToDn(String sql, String toDn, String table) throws SQLException {
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.PhysicalDBPool in project Mycat_plus by coderczp.
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.PhysicalDBPool in project Mycat_plus by coderczp.
the class ConfigInitializer method initDataNodes.
private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) {
Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes();
Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>(nodeConfs.size());
for (DataNodeConfig conf : nodeConfs.values()) {
PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost());
if (pool == null) {
throw new ConfigException("dataHost not exists " + conf.getDataHost());
}
PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool);
nodes.put(dataNode.getName(), dataNode);
}
return nodes;
}
Aggregations