Search in sources :

Example 21 with ConfigException

use of io.mycat.config.util.ConfigException in project Mycat-Server by MyCATApache.

the class XMLRuleLoader method loadTableRules.

/**
	 * tableRule标签结构:
	 * <tableRule name="sharding-by-month">
	 *     <rule>
	 *         <columns>create_date</columns>
	 *         <algorithm>partbymonth</algorithm>
	 *     </rule>
	 * </tableRule>
	 * @param root
	 * @throws SQLSyntaxErrorException
     */
private void loadTableRules(Element root) throws SQLSyntaxErrorException {
    //获取每个tableRule标签
    NodeList list = root.getElementsByTagName("tableRule");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            //先判断是否重复
            String name = e.getAttribute("name");
            if (tableRules.containsKey(name)) {
                throw new ConfigException("table rule " + name + " duplicated!");
            }
            //获取rule标签
            NodeList ruleNodes = e.getElementsByTagName("rule");
            int length = ruleNodes.getLength();
            if (length > 1) {
                throw new ConfigException("only one rule can defined :" + name);
            }
            //目前只处理第一个,未来可能有多列复合逻辑需求
            //RuleConfig是保存着rule与function对应关系的对象
            RuleConfig rule = loadRule((Element) ruleNodes.item(0));
            String funName = rule.getFunctionName();
            //判断function是否存在,获取function
            AbstractPartitionAlgorithm func = functions.get(funName);
            if (func == null) {
                throw new ConfigException("can't find function of name :" + funName);
            }
            rule.setRuleAlgorithm(func);
            //保存到tableRules
            tableRules.put(name, new TableRuleConfig(name, rule));
        }
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig)

Example 22 with ConfigException

use of io.mycat.config.util.ConfigException in project Mycat-Server by MyCATApache.

the class XMLRuleLoader method load.

private void load(String dtdFile, String xmlFile) {
    InputStream dtd = null;
    InputStream xml = null;
    try {
        dtd = XMLRuleLoader.class.getResourceAsStream(dtdFile);
        xml = XMLRuleLoader.class.getResourceAsStream(xmlFile);
        //读取出语意树
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        //加载Function
        loadFunctions(root);
        //加载TableRule
        loadTableRules(root);
    } catch (ConfigException e) {
        throw e;
    } catch (Exception e) {
        throw new ConfigException(e);
    } finally {
        if (dtd != null) {
            try {
                dtd.close();
            } catch (IOException e) {
            }
        }
        if (xml != null) {
            try {
                xml.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) IOException(java.io.IOException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigException(io.mycat.config.util.ConfigException)

Example 23 with ConfigException

use of io.mycat.config.util.ConfigException in project Mycat-Server by MyCATApache.

the class XMLRuleLoader method loadFunctions.

/**
	 * function标签结构:
	 * <function name="partbymonth" class="io.mycat.route.function.PartitionByMonth">
	 *     <property name="dateFormat">yyyy-MM-dd</property>
	 *     <property name="sBeginDate">2015-01-01</property>
	 * </function>
	 * @param root
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
     */
private void loadFunctions(Element root) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
    NodeList list = root.getElementsByTagName("function");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            //获取name标签
            String name = e.getAttribute("name");
            //如果Map已有,则function重复
            if (functions.containsKey(name)) {
                throw new ConfigException("rule function " + name + " duplicated!");
            }
            //获取class标签
            String clazz = e.getAttribute("class");
            //根据class利用反射新建分片算法
            AbstractPartitionAlgorithm function = createFunction(name, clazz);
            //根据读取参数配置分片算法
            ParameterMapping.mapping(function, ConfigUtil.loadElements(e));
            //每个AbstractPartitionAlgorithm可能会实现init来初始化
            function.init();
            //放入functions map
            functions.put(name, function);
        }
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException)

Example 24 with ConfigException

use of io.mycat.config.util.ConfigException in project Mycat-Server by MyCATApache.

the class XMLSchemaLoader method loadSchemas.

private void loadSchemas(Element root) {
    NodeList list = root.getElementsByTagName("schema");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Element schemaElement = (Element) list.item(i);
        //读取各个属性
        String name = schemaElement.getAttribute("name");
        String dataNode = schemaElement.getAttribute("dataNode");
        String checkSQLSchemaStr = schemaElement.getAttribute("checkSQLschema");
        String sqlMaxLimitStr = schemaElement.getAttribute("sqlMaxLimit");
        int sqlMaxLimit = -1;
        //读取sql返回结果集限制
        if (sqlMaxLimitStr != null && !sqlMaxLimitStr.isEmpty()) {
            sqlMaxLimit = Integer.parseInt(sqlMaxLimitStr);
        }
        // check dataNode already exists or not,看schema标签中是否有datanode
        String defaultDbType = null;
        //校验检查并添加dataNode
        if (dataNode != null && !dataNode.isEmpty()) {
            List<String> dataNodeLst = new ArrayList<String>(1);
            dataNodeLst.add(dataNode);
            checkDataNodeExists(dataNodeLst);
            String dataHost = dataNodes.get(dataNode).getDataHost();
            defaultDbType = dataHosts.get(dataHost).getDbType();
        } else {
            dataNode = null;
        }
        //加载schema下所有tables
        Map<String, TableConfig> tables = loadTables(schemaElement);
        //判断schema是否重复
        if (schemas.containsKey(name)) {
            throw new ConfigException("schema " + name + " duplicated!");
        }
        // 设置了table的不需要设置dataNode属性,没有设置table的必须设置dataNode属性
        if (dataNode == null && tables.size() == 0) {
            throw new ConfigException("schema " + name + " didn't config tables,so you must set dataNode property!");
        }
        SchemaConfig schemaConfig = new SchemaConfig(name, dataNode, tables, sqlMaxLimit, "true".equalsIgnoreCase(checkSQLSchemaStr));
        //设定DB类型,这对之后的sql语句路由解析有帮助
        if (defaultDbType != null) {
            schemaConfig.setDefaultDataNodeDbType(defaultDbType);
            if (!"mysql".equalsIgnoreCase(defaultDbType)) {
                schemaConfig.setNeedSupportMultiDBType(true);
            }
        }
        // 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析
        for (TableConfig tableConfig : tables.values()) {
            if (isHasMultiDbType(tableConfig)) {
                schemaConfig.setNeedSupportMultiDBType(true);
                break;
            }
        }
        //记录每种dataNode的DB类型
        Map<String, String> dataNodeDbTypeMap = new HashMap<>();
        for (String dataNodeName : dataNodes.keySet()) {
            DataNodeConfig dataNodeConfig = dataNodes.get(dataNodeName);
            String dataHost = dataNodeConfig.getDataHost();
            DataHostConfig dataHostConfig = dataHosts.get(dataHost);
            if (dataHostConfig != null) {
                String dbType = dataHostConfig.getDbType();
                dataNodeDbTypeMap.put(dataNodeName, dbType);
            }
        }
        schemaConfig.setDataNodeDbTypeMap(dataNodeDbTypeMap);
        schemas.put(name, schemaConfig);
    }
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) TableConfig(io.mycat.config.model.TableConfig) DataHostConfig(io.mycat.config.model.DataHostConfig) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Example 25 with ConfigException

use of io.mycat.config.util.ConfigException in project Mycat-Server by MyCATApache.

the class XMLSchemaLoader method loadDataNodes.

private void loadDataNodes(Element root) {
    //读取DataNode分支
    NodeList list = root.getElementsByTagName("dataNode");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Element element = (Element) list.item(i);
        String dnNamePre = element.getAttribute("name");
        String databaseStr = element.getAttribute("database");
        String host = element.getAttribute("dataHost");
        //字符串不为空
        if (empty(dnNamePre) || empty(databaseStr) || empty(host)) {
            throw new ConfigException("dataNode " + dnNamePre + " define error ,attribute can't be empty");
        }
        //dnNames(name),databases(database),hostStrings(dataHost)都可以配置多个,以',', '$', '-'区分,但是需要保证database的个数*dataHost的个数=name的个数
        //多个dataHost与多个database如果写在一个标签,则每个dataHost拥有所有database
        //例如:<dataNode name="dn1$0-75" dataHost="localhost$1-10" database="db$0-759" />
        //则为:localhost1拥有dn1$0-75,localhost2也拥有dn1$0-75(对应db$76-151)
        String[] dnNames = io.mycat.util.SplitUtil.split(dnNamePre, ',', '$', '-');
        String[] databases = io.mycat.util.SplitUtil.split(databaseStr, ',', '$', '-');
        String[] hostStrings = io.mycat.util.SplitUtil.split(host, ',', '$', '-');
        if (dnNames.length > 1 && dnNames.length != databases.length * hostStrings.length) {
            throw new ConfigException("dataNode " + dnNamePre + " define error ,dnNames.length must be=databases.length*hostStrings.length");
        }
        if (dnNames.length > 1) {
            List<String[]> mhdList = mergerHostDatabase(hostStrings, databases);
            for (int k = 0; k < dnNames.length; k++) {
                String[] hd = mhdList.get(k);
                String dnName = dnNames[k];
                String databaseName = hd[1];
                String hostName = hd[0];
                createDataNode(dnName, databaseName, hostName);
            }
        } else {
            createDataNode(dnNamePre, databaseStr, host);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException)

Aggregations

ConfigException (io.mycat.config.util.ConfigException)30 Element (org.w3c.dom.Element)16 NodeList (org.w3c.dom.NodeList)12 IOException (java.io.IOException)9 Node (org.w3c.dom.Node)8 HashMap (java.util.HashMap)5 TableConfig (io.mycat.config.model.TableConfig)4 RuleConfig (io.mycat.config.model.rule.RuleConfig)4 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)4 InputStream (java.io.InputStream)4 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)3 DataNodeConfig (io.mycat.config.model.DataNodeConfig)3 TableRuleConfig (io.mycat.config.model.rule.TableRuleConfig)3 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)2 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 JDBCDatasource (io.mycat.backend.jdbc.JDBCDatasource)2 MySQLDataSource (io.mycat.backend.mysql.nio.MySQLDataSource)2 XMLSchemaLoader (io.mycat.config.loader.xml.XMLSchemaLoader)2 DBHostConfig (io.mycat.config.model.DBHostConfig)2 DataHostConfig (io.mycat.config.model.DataHostConfig)2