Search in sources :

Example 6 with DBHostConfig

use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.

the class XMLSchemaLoader method loadDataHosts.

private void loadDataHosts(Element root) {
    NodeList list = root.getElementsByTagName("dataHost");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Element element = (Element) list.item(i);
        String name = element.getAttribute("name");
        //判断是否重复
        if (dataHosts.containsKey(name)) {
            throw new ConfigException("dataHost name " + name + "duplicated!");
        }
        //读取最大连接数
        int maxCon = Integer.parseInt(element.getAttribute("maxCon"));
        //读取最小连接数
        int minCon = Integer.parseInt(element.getAttribute("minCon"));
        /**
			 * 读取负载均衡配置
			 * 1. balance="0", 不开启分离机制,所有读操作都发送到当前可用的 writeHost 上。
			 * 2. balance="1",全部的 readHost 和 stand by writeHost 参不 select 的负载均衡
			 * 3. balance="2",所有读操作都随机的在 writeHost、readhost 上分发。
			 * 4. balance="3",所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力
			 */
        int balance = Integer.parseInt(element.getAttribute("balance"));
        /**
			 * 读取切换类型
			 * -1 表示不自动切换
			 * 1 默认值,自动切换
			 * 2 基于MySQL主从同步的状态决定是否切换
			 * 心跳询句为 show slave status
			 * 3 基于 MySQL galary cluster 的切换机制
			 */
        String switchTypeStr = element.getAttribute("switchType");
        int switchType = switchTypeStr.equals("") ? -1 : Integer.parseInt(switchTypeStr);
        //读取从延迟界限
        String slaveThresholdStr = element.getAttribute("slaveThreshold");
        int slaveThreshold = slaveThresholdStr.equals("") ? -1 : Integer.parseInt(slaveThresholdStr);
        //如果 tempReadHostAvailable 设置大于 0 则表示写主机如果挂掉, 临时的读服务依然可用
        String tempReadHostAvailableStr = element.getAttribute("tempReadHostAvailable");
        boolean tempReadHostAvailable = !tempReadHostAvailableStr.equals("") && Integer.parseInt(tempReadHostAvailableStr) > 0;
        /**
			 * 读取 写类型
			 * 这里只支持 0 - 所有写操作仅配置的第一个 writeHost
			 */
        String writeTypStr = element.getAttribute("writeType");
        int writeType = "".equals(writeTypStr) ? PhysicalDBPool.WRITE_ONLYONE_NODE : Integer.parseInt(writeTypStr);
        String dbDriver = element.getAttribute("dbDriver");
        String dbType = element.getAttribute("dbType");
        String filters = element.getAttribute("filters");
        String logTimeStr = element.getAttribute("logTime");
        String slaveIDs = element.getAttribute("slaveIDs");
        long logTime = "".equals(logTimeStr) ? PhysicalDBPool.LONG_TIME : Long.parseLong(logTimeStr);
        //读取心跳语句
        String heartbeatSQL = element.getElementsByTagName("heartbeat").item(0).getTextContent();
        //读取 初始化sql配置,用于oracle
        NodeList connectionInitSqlList = element.getElementsByTagName("connectionInitSql");
        String initConSQL = null;
        if (connectionInitSqlList.getLength() > 0) {
            initConSQL = connectionInitSqlList.item(0).getTextContent();
        }
        //读取writeHost
        NodeList writeNodes = element.getElementsByTagName("writeHost");
        DBHostConfig[] writeDbConfs = new DBHostConfig[writeNodes.getLength()];
        Map<Integer, DBHostConfig[]> readHostsMap = new HashMap<Integer, DBHostConfig[]>(2);
        Set<String> writeHostNameSet = new HashSet<String>(writeNodes.getLength());
        for (int w = 0; w < writeDbConfs.length; w++) {
            Element writeNode = (Element) writeNodes.item(w);
            writeDbConfs[w] = createDBHostConf(name, writeNode, dbType, dbDriver, maxCon, minCon, filters, logTime);
            if (writeHostNameSet.contains(writeDbConfs[w].getHostName())) {
                throw new ConfigException("writeHost " + writeDbConfs[w].getHostName() + " duplicated!");
            } else {
                writeHostNameSet.add(writeDbConfs[w].getHostName());
            }
            NodeList readNodes = writeNode.getElementsByTagName("readHost");
            //读取对应的每一个readHost
            if (readNodes.getLength() != 0) {
                DBHostConfig[] readDbConfs = new DBHostConfig[readNodes.getLength()];
                Set<String> readHostNameSet = new HashSet<String>(readNodes.getLength());
                for (int r = 0; r < readDbConfs.length; r++) {
                    Element readNode = (Element) readNodes.item(r);
                    readDbConfs[r] = createDBHostConf(name, readNode, dbType, dbDriver, maxCon, minCon, filters, logTime);
                    if (readHostNameSet.contains(readDbConfs[r].getHostName())) {
                        throw new ConfigException("readHost " + readDbConfs[r].getHostName() + " duplicated!");
                    } else {
                        readHostNameSet.add(readDbConfs[r].getHostName());
                    }
                }
                readHostsMap.put(w, readDbConfs);
            }
        }
        DataHostConfig hostConf = new DataHostConfig(name, dbType, dbDriver, writeDbConfs, readHostsMap, switchType, slaveThreshold, tempReadHostAvailable);
        hostConf.setMaxCon(maxCon);
        hostConf.setMinCon(minCon);
        hostConf.setBalance(balance);
        hostConf.setWriteType(writeType);
        hostConf.setHearbeatSQL(heartbeatSQL);
        hostConf.setConnectionInitSql(initConSQL);
        hostConf.setFilters(filters);
        hostConf.setLogTime(logTime);
        hostConf.setSlaveIDs(slaveIDs);
        dataHosts.put(hostConf.getName(), hostConf);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) DBHostConfig(io.mycat.config.model.DBHostConfig) DataHostConfig(io.mycat.config.model.DataHostConfig)

Example 7 with DBHostConfig

use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.

the class JDBCDatasource method createNewConnection.

@Override
public void createNewConnection(ResponseHandler handler, String schema) throws IOException {
    DBHostConfig cfg = getConfig();
    JDBCConnection c = new JDBCConnection();
    c.setHost(cfg.getIp());
    c.setPort(cfg.getPort());
    c.setPool(this);
    c.setSchema(schema);
    c.setDbType(cfg.getDbType());
    NIOProcessor processor = (NIOProcessor) MycatServer.getInstance().nextProcessor();
    c.setProcessor(processor);
    //复用mysql的Backend的ID,需要在process中存储
    c.setId(NIOConnector.ID_GENERATOR.getId());
    processor.addBackend(c);
    try {
        Connection con = getConnection();
        // c.setIdleTimeout(pool.getConfig().getIdleTimeout());
        c.setCon(con);
        // notify handler
        handler.connectionAcquired(c);
    } catch (Exception e) {
        handler.connectionError(e, c);
    }
}
Also used : DBHostConfig(io.mycat.config.model.DBHostConfig) Connection(java.sql.Connection) NIOProcessor(io.mycat.net.NIOProcessor) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 8 with DBHostConfig

use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.

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 9 with DBHostConfig

use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.

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", 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 10 with DBHostConfig

use of io.mycat.config.model.DBHostConfig in project Mycat-Server by MyCATApache.

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)

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