Search in sources :

Example 11 with DBHostConfig

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);
        }
    }
}
Also used : PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DBHostConfig(io.mycat.config.model.DBHostConfig) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 12 with DBHostConfig

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;
}
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 13 with DBHostConfig

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);
    }
}
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 14 with DBHostConfig

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;
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) ArrayList(java.util.ArrayList) DataHostConfig(io.mycat.config.model.DataHostConfig) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Aggregations

DBHostConfig (io.mycat.config.model.DBHostConfig)14 Connection (java.sql.Connection)7 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)6 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)6 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)4 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)3 DataHostConfig (io.mycat.config.model.DataHostConfig)2 ConfigException (io.mycat.config.util.ConfigException)2 NIOConnector (io.mycat.net.NIOConnector)2 InetSocketAddress (java.net.InetSocketAddress)2 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)2 NetworkChannel (java.nio.channels.NetworkChannel)2 Statement (java.sql.Statement)2 DataNodeConfig (io.mycat.config.model.DataNodeConfig)1 NIOProcessor (io.mycat.net.NIOProcessor)1 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1