Search in sources :

Example 6 with ConfigException

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

the class XMLSchemaLoader method loadTables.

private Map<String, TableConfig> loadTables(Element node) {
    // Map<String, TableConfig> tables = new HashMap<String, TableConfig>();
    // 支持表名中包含引号[`] BEN GONG
    Map<String, TableConfig> tables = new TableConfigMap();
    NodeList nodeList = node.getElementsByTagName("table");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element tableElement = (Element) nodeList.item(i);
        String tableNameElement = tableElement.getAttribute("name").toUpperCase();
        //TODO:路由, 增加对动态日期表的支持
        String tableNameSuffixElement = tableElement.getAttribute("nameSuffix").toUpperCase();
        if (!"".equals(tableNameSuffixElement)) {
            if (tableNameElement.split(",").length > 1) {
                throw new ConfigException("nameSuffix " + tableNameSuffixElement + ", require name parameter cannot multiple breaks!");
            }
            //前缀用来标明日期格式
            tableNameElement = doTableNameSuffix(tableNameElement, tableNameSuffixElement);
        }
        //记录主键,用于之后路由分析,以及启用自增长主键
        String[] tableNames = tableNameElement.split(",");
        String primaryKey = tableElement.hasAttribute("primaryKey") ? tableElement.getAttribute("primaryKey").toUpperCase() : null;
        //记录是否主键自增,默认不是,(启用全局sequence handler)
        boolean autoIncrement = false;
        if (tableElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(tableElement.getAttribute("autoIncrement"));
        }
        //记录是否需要加返回结果集限制,默认需要加
        boolean needAddLimit = true;
        if (tableElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(tableElement.getAttribute("needAddLimit"));
        }
        //记录type,是否为global
        String tableTypeStr = tableElement.hasAttribute("type") ? tableElement.getAttribute("type") : null;
        int tableType = TableConfig.TYPE_GLOBAL_DEFAULT;
        if ("global".equalsIgnoreCase(tableTypeStr)) {
            tableType = TableConfig.TYPE_GLOBAL_TABLE;
        }
        //记录dataNode,就是分布在哪些dataNode上
        String dataNode = tableElement.getAttribute("dataNode");
        TableRuleConfig tableRule = null;
        if (tableElement.hasAttribute("rule")) {
            String ruleName = tableElement.getAttribute("rule");
            tableRule = tableRules.get(ruleName);
            if (tableRule == null) {
                throw new ConfigException("rule " + ruleName + " is not found!");
            }
        }
        boolean ruleRequired = false;
        //记录是否绑定有分片规则
        if (tableElement.hasAttribute("ruleRequired")) {
            ruleRequired = Boolean.parseBoolean(tableElement.getAttribute("ruleRequired"));
        }
        if (tableNames == null) {
            throw new ConfigException("table name is not found!");
        }
        //distribute函数,重新编排dataNode
        String distPrex = "distribute(";
        boolean distTableDns = dataNode.startsWith(distPrex);
        if (distTableDns) {
            dataNode = dataNode.substring(distPrex.length(), dataNode.length() - 1);
        }
        //分表功能
        String subTables = tableElement.getAttribute("subTables");
        for (int j = 0; j < tableNames.length; j++) {
            String tableName = tableNames[j];
            TableRuleConfig tableRuleConfig = tableRule;
            if (tableRuleConfig != null) {
                //对于实现TableRuleAware的function进行特殊处理  根据每个表新建个实例
                RuleConfig rule = tableRuleConfig.getRule();
                if (rule.getRuleAlgorithm() instanceof TableRuleAware) {
                    tableRuleConfig = (TableRuleConfig) ObjectUtil.copyObject(tableRuleConfig);
                    tableRules.remove(tableRuleConfig.getName());
                    String newRuleName = tableRuleConfig.getName() + "_" + tableName;
                    tableRuleConfig.setName(newRuleName);
                    TableRuleAware tableRuleAware = (TableRuleAware) tableRuleConfig.getRule().getRuleAlgorithm();
                    tableRuleAware.setRuleName(newRuleName);
                    tableRuleAware.setTableName(tableName);
                    tableRuleConfig.getRule().getRuleAlgorithm().init();
                    tableRules.put(newRuleName, tableRuleConfig);
                }
            }
            TableConfig table = new TableConfig(tableName, primaryKey, autoIncrement, needAddLimit, tableType, dataNode, getDbType(dataNode), (tableRuleConfig != null) ? tableRuleConfig.getRule() : null, ruleRequired, null, false, null, null, subTables);
            checkDataNodeExists(table.getDataNodes());
            // 检查分片表分片规则配置是否合法
            if (table.getRule() != null) {
                checkRuleSuitTable(table);
            }
            if (distTableDns) {
                distributeDataNodes(table.getDataNodes());
            }
            //检查去重
            if (tables.containsKey(table.getName())) {
                throw new ConfigException("table " + tableName + " duplicated!");
            }
            //放入map
            tables.put(table.getName(), table);
        }
        //只有tableName配置的是单个表(没有逗号)的时候才能有子表
        if (tableNames.length == 1) {
            TableConfig table = tables.get(tableNames[0]);
            // process child tables
            processChildTables(tables, table, dataNode, tableElement);
        }
    }
    return tables;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) TableRuleAware(io.mycat.route.function.TableRuleAware) ConfigException(io.mycat.config.util.ConfigException) TableConfigMap(io.mycat.config.model.TableConfigMap) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig) TableConfig(io.mycat.config.model.TableConfig) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig)

Example 7 with ConfigException

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

the class XMLServerLoader method loadUsers.

private void loadUsers(Element root) {
    NodeList list = root.getElementsByTagName("user");
    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");
            UserConfig user = new UserConfig();
            Map<String, Object> props = ConfigUtil.loadElements(e);
            String password = (String) props.get("password");
            String usingDecrypt = (String) props.get("usingDecrypt");
            String passwordDecrypt = DecryptUtil.mycatDecrypt(usingDecrypt, name, password);
            user.setName(name);
            user.setPassword(passwordDecrypt);
            user.setEncryptPassword(password);
            String benchmark = (String) props.get("benchmark");
            if (null != benchmark) {
                user.setBenchmark(Integer.parseInt(benchmark));
            }
            String readOnly = (String) props.get("readOnly");
            if (null != readOnly) {
                user.setReadOnly(Boolean.parseBoolean(readOnly));
            }
            String schemas = (String) props.get("schemas");
            if (schemas != null) {
                String[] strArray = SplitUtil.split(schemas, ',', true);
                user.setSchemas(new HashSet<String>(Arrays.asList(strArray)));
            }
            //加载用户 DML 权限
            loadPrivileges(user, e);
            if (users.containsKey(name)) {
                throw new ConfigException("user " + name + " duplicated!");
            }
            users.put(name, user);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) UserConfig(io.mycat.config.model.UserConfig)

Example 8 with ConfigException

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

the class XMLServerLoader method loadSystem.

private void loadSystem(Element root) throws IllegalAccessException, InvocationTargetException {
    NodeList list = root.getElementsByTagName("system");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Map<String, Object> props = ConfigUtil.loadElements((Element) node);
            ParameterMapping.mapping(system, props);
        }
    }
    if (system.getFakeMySQLVersion() != null) {
        boolean validVersion = false;
        String majorMySQLVersion = system.getFakeMySQLVersion();
        /*
             * 注意!!! 目前MySQL官方主版本号仍然是5.x, 以后万一前面的大版本号变成2位数字,
             * 比如 10.x...,下面获取主版本的代码要做修改
             */
        majorMySQLVersion = majorMySQLVersion.substring(0, majorMySQLVersion.indexOf(".", 2));
        for (String ver : SystemConfig.MySQLVersions) {
            // 这里只是比较mysql前面的大版本号
            if (majorMySQLVersion.equals(ver)) {
                validVersion = true;
            }
        }
        if (validVersion) {
            Versions.setServerVersion(system.getFakeMySQLVersion());
        } else {
            throw new ConfigException("The specified MySQL Version (" + system.getFakeMySQLVersion() + ") is not valid.");
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException)

Example 9 with ConfigException

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

the class XMLServerLoader method load.

private void load() {
    //读取server.xml配置
    InputStream dtd = null;
    InputStream xml = null;
    try {
        dtd = XMLServerLoader.class.getResourceAsStream("/server.dtd");
        xml = XMLServerLoader.class.getResourceAsStream("/server.xml");
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        //加载System标签
        loadSystem(root);
        //加载User标签
        loadUsers(root);
        //加载集群配置
        this.cluster = new ClusterConfig(root, system.getServerPort());
        //加载全局SQL防火墙
        loadFirewall(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) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigException(io.mycat.config.util.ConfigException) ClusterConfig(io.mycat.config.model.ClusterConfig)

Example 10 with ConfigException

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

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 if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")) {
        for (int i = 0; i < nodes.length; i++) {
            nodes[i].setIdleTimeout(system.getIdleTimeout());
            PostgreSQLDataSource ds = new PostgreSQLDataSource(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) PostgreSQLDataSource(io.mycat.backend.postgresql.PostgreSQLDataSource) MySQLDataSource(io.mycat.backend.mysql.nio.MySQLDataSource) JDBCDatasource(io.mycat.backend.jdbc.JDBCDatasource) 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