Search in sources :

Example 1 with DataHostConfig

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

the class XMLSchemaLoader method getDbType.

private Set<String> getDbType(String dataNode) {
    Set<String> dbTypes = new HashSet<>();
    String[] dataNodeArr = SplitUtil.split(dataNode, ',', '$', '-');
    for (String node : dataNodeArr) {
        DataNodeConfig datanode = dataNodes.get(node);
        DataHostConfig datahost = dataHosts.get(datanode.getDataHost());
        dbTypes.add(datahost.getDbType());
    }
    return dbTypes;
}
Also used : DataHostConfig(io.mycat.config.model.DataHostConfig) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Example 2 with DataHostConfig

use of io.mycat.config.model.DataHostConfig 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 3 with DataHostConfig

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

the class XMLSchemaLoader method getDataNodeDbTypeMap.

private Set<String> getDataNodeDbTypeMap(String dataNode) {
    Set<String> dbTypes = new HashSet<>();
    String[] dataNodeArr = SplitUtil.split(dataNode, ',', '$', '-');
    for (String node : dataNodeArr) {
        DataNodeConfig datanode = dataNodes.get(node);
        DataHostConfig datahost = dataHosts.get(datanode.getDataHost());
        dbTypes.add(datahost.getDbType());
    }
    return dbTypes;
}
Also used : DataHostConfig(io.mycat.config.model.DataHostConfig) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Example 4 with DataHostConfig

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

the class ConfigInitializer method initDataHosts.

private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) {
    Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts();
    boolean isBooster = "booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE));
    //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool
    Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>(nodeConfs.size());
    for (DataHostConfig conf : nodeConfs.values()) {
        if (isBooster) {
            conf.setMinCon(2);
        }
        //建立PhysicalDBPool
        PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader);
        nodes.put(pool.getHostName(), pool);
    }
    return nodes;
}
Also used : HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) DataHostConfig(io.mycat.config.model.DataHostConfig)

Example 5 with DataHostConfig

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

the class ConfigTest method initDataHosts.

private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) {
    Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts();
    Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>(nodeConfs.size());
    for (DataHostConfig conf : nodeConfs.values()) {
        PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader);
        nodes.put(pool.getHostName(), pool);
    }
    return nodes;
}
Also used : HashMap(java.util.HashMap) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) DataHostConfig(io.mycat.config.model.DataHostConfig)

Aggregations

DataHostConfig (io.mycat.config.model.DataHostConfig)8 DataNodeConfig (io.mycat.config.model.DataNodeConfig)4 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)3 DBHostConfig (io.mycat.config.model.DBHostConfig)2 ConfigException (io.mycat.config.util.ConfigException)2 HashMap (java.util.HashMap)2 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 SchemaConfig (io.mycat.config.model.SchemaConfig)1 TableConfig (io.mycat.config.model.TableConfig)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1