Search in sources :

Example 1 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource 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();
    }
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) File(java.io.File) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 2 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource 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);
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) DBHostConfig(io.mycat.config.model.DBHostConfig) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) Connection(java.sql.Connection) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 3 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource 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);
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) DBHostConfig(io.mycat.config.model.DBHostConfig) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) Connection(java.sql.Connection) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool)

Example 4 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat_plus by coderczp.

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!");
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) ConfigException(io.mycat.config.util.ConfigException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat_plus by coderczp.

the class ConfigInitializer method getPhysicalDBPool.

private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) {
    String name = conf.getName();
    // 数据库类型,我们这里只讨论MySQL
    String dbType = conf.getDbType();
    // 连接数据库驱动,我们这里只讨论MyCat自己实现的native
    String dbDriver = conf.getDbDriver();
    // 针对所有写节点创建PhysicalDatasource
    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());
    // 对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表
    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());
    pool.setSlaveIDs(conf.getSlaveIDs());
    return pool;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)52 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)43 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)20 MycatConfig (io.mycat.config.MycatConfig)20 RowDataPacket (io.mycat.net.mysql.RowDataPacket)14 DBHostConfig (io.mycat.config.model.DBHostConfig)12 LinkedList (java.util.LinkedList)12 DBHeartbeat (io.mycat.backend.heartbeat.DBHeartbeat)11 HashMap (java.util.HashMap)10 MySQLDataSource (io.mycat.backend.mysql.nio.MySQLDataSource)8 Connection (java.sql.Connection)8 Map (java.util.Map)8 ConfigException (io.mycat.config.util.ConfigException)6 DataSourceSyncRecorder (io.mycat.statistic.DataSourceSyncRecorder)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 MySQLConsistencyChecker (io.mycat.backend.heartbeat.MySQLConsistencyChecker)4 JDBCDatasource (io.mycat.backend.jdbc.JDBCDatasource)4 TableConfig (io.mycat.config.model.TableConfig)4 SimpleDateFormat (java.text.SimpleDateFormat)4