Search in sources :

Example 26 with PhysicalDatasource

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

the class ConfigInitializer method createDataSource.

private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) {
    PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length];
    if (dbType.equals("mysql") && dbDriver.equals("native")) {
        for (int i = 0; i < nodes.length; i++) {
            // 设置最大idle时间,默认为30分钟
            nodes[i].setIdleTimeout(system.getIdleTimeout());
            MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead);
            dataSources[i] = ds;
        }
    } else if (dbDriver.equals("jdbc")) {
        for (int i = 0; i < nodes.length; i++) {
            nodes[i].setIdleTimeout(system.getIdleTimeout());
            JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead);
            dataSources[i] = ds;
        }
    } else {
        throw new ConfigException("not supported yet !" + hostName);
    }
    return dataSources;
}
Also used : PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) MySQLDataSource(io.mycat.backend.mysql.nio.MySQLDataSource) JDBCDatasource(io.mycat.backend.jdbc.JDBCDatasource) ConfigException(io.mycat.config.util.ConfigException)

Example 27 with PhysicalDatasource

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

the class ReloadConfig method reload_all.

public static boolean reload_all() {
    /**
     *  1、载入新的配置
     *  1.1、ConfigInitializer 初始化,基本自检
     *  1.2、DataNode/DataHost 实际链路检测
     */
    ConfigInitializer loader = new ConfigInitializer(true);
    Map<String, UserConfig> newUsers = loader.getUsers();
    Map<String, SchemaConfig> newSchemas = loader.getSchemas();
    Map<String, PhysicalDBNode> newDataNodes = loader.getDataNodes();
    Map<String, PhysicalDBPool> newDataHosts = loader.getDataHosts();
    MycatCluster newCluster = loader.getCluster();
    FirewallConfig newFirewall = loader.getFirewall();
    /**
     * 1.2、实际链路检测
     */
    loader.testConnection();
    /**
     *  2、承接
     *  2.1、老的 dataSource 继续承接新建请求
     *  2.2、新的 dataSource 开始初始化, 完毕后交由 2.3
     *  2.3、新的 dataSource 开始承接新建请求
     *  2.4、老的 dataSource 内部的事务执行完毕, 相继关闭
     *  2.5、老的 dataSource 超过阀值的,强制关闭
     */
    MycatConfig config = MycatServer.getInstance().getConfig();
    /**
     * 2.1 、老的 dataSource 继续承接新建请求, 此处什么也不需要做
     */
    boolean isReloadStatusOK = true;
    /**
     * 2.2、新的 dataHosts 初始化
     */
    for (PhysicalDBPool dbPool : newDataHosts.values()) {
        String hostName = dbPool.getHostName();
        // 设置 schemas
        ArrayList<String> dnSchemas = new ArrayList<String>(30);
        for (PhysicalDBNode dn : newDataNodes.values()) {
            if (dn.getDbPool().getHostName().equals(hostName)) {
                dnSchemas.add(dn.getDatabase());
            }
        }
        dbPool.setSchemas(dnSchemas.toArray(new String[dnSchemas.size()]));
        // 获取 data host
        String dnIndex = DnPropertyUtil.loadDnIndexProps().getProperty(dbPool.getHostName(), "0");
        if (!"0".equals(dnIndex)) {
            LOGGER.info("init datahost: " + dbPool.getHostName() + "  to use datasource index:" + dnIndex);
        }
        dbPool.init(Integer.valueOf(dnIndex));
        if (!dbPool.isInitSuccess()) {
            isReloadStatusOK = false;
            break;
        }
    }
    /**
     *  TODO: 确认初始化情况
     *
     *  新的 dataHosts 是否初始化成功
     */
    if (isReloadStatusOK) {
        /**
         * 2.3、 在老的配置上,应用新的配置,开始准备承接任务
         */
        config.reload(newUsers, newSchemas, newDataNodes, newDataHosts, newCluster, newFirewall, true);
        /**
         * 2.4、 处理旧的资源
         */
        LOGGER.warn("1、clear old backend connection(size): " + NIOProcessor.backends_old.size());
        // 清除前一次 reload 转移出去的 old Cons
        Iterator<BackendConnection> iter = NIOProcessor.backends_old.iterator();
        while (iter.hasNext()) {
            BackendConnection con = iter.next();
            con.close("clear old datasources");
            iter.remove();
        }
        Map<String, PhysicalDBPool> oldDataHosts = config.getBackupDataHosts();
        for (PhysicalDBPool dbPool : oldDataHosts.values()) {
            dbPool.stopHeartbeat();
            // 提取数据源下的所有连接
            for (PhysicalDatasource ds : dbPool.getAllDataSources()) {
                // 
                for (NIOProcessor processor : MycatServer.getInstance().getProcessors()) {
                    for (BackendConnection con : processor.getBackends().values()) {
                        if (con instanceof MySQLConnection) {
                            MySQLConnection mysqlCon = (MySQLConnection) con;
                            if (mysqlCon.getPool() == ds) {
                                NIOProcessor.backends_old.add(con);
                            }
                        } else if (con instanceof JDBCConnection) {
                            JDBCConnection jdbcCon = (JDBCConnection) con;
                            if (jdbcCon.getPool() == ds) {
                                NIOProcessor.backends_old.add(con);
                            }
                        }
                    }
                }
            }
        }
        LOGGER.warn("2、to be recycled old backend connection(size): " + NIOProcessor.backends_old.size());
        // 清理缓存
        MycatServer.getInstance().getCacheService().clearCache();
        MycatServer.getInstance().initRuleData();
        return true;
    } else {
        // 如果重载不成功,则清理已初始化的资源。
        LOGGER.warn("reload failed, clear previously created datasources ");
        for (PhysicalDBPool dbPool : newDataHosts.values()) {
            dbPool.clearDataSources("reload config");
            dbPool.stopHeartbeat();
        }
        return false;
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) SchemaConfig(io.mycat.config.model.SchemaConfig) ConfigInitializer(io.mycat.config.ConfigInitializer) MycatCluster(io.mycat.config.MycatCluster) ArrayList(java.util.ArrayList) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) UserConfig(io.mycat.config.model.UserConfig) FirewallConfig(io.mycat.config.model.FirewallConfig) MycatConfig(io.mycat.config.MycatConfig) NIOProcessor(io.mycat.net.NIOProcessor) PhysicalDatasource(io.mycat.backend.datasource.PhysicalDatasource) JDBCConnection(io.mycat.backend.jdbc.JDBCConnection) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Example 28 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource 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);
    }
}
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 29 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource 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);
        }
    }
}
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 30 with PhysicalDatasource

use of io.mycat.backend.datasource.PhysicalDatasource 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;
}
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)

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