Search in sources :

Example 26 with ConfigException

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

the class XMLSchemaLoader method checkRuleSuitTable.

/**
	 * 检查分片表分片规则配置, 目前主要检查分片表分片算法定义与分片dataNode是否匹配<br>
	 * 例如分片表定义如下:<br>
	 * {@code
	 * <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2"
			   rule="mod-long" />
	 * }
	 * <br>
	 * 分片算法如下:<br>
	 * {@code
	 * <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
		<!-- how many data nodes -->
		<property name="count">3</property>
	   </function>
	 * }
	 * <br>
	 * shard table datanode(2) < function count(3) 此时检测为不匹配
	 */
private void checkRuleSuitTable(TableConfig tableConf) {
    AbstractPartitionAlgorithm function = tableConf.getRule().getRuleAlgorithm();
    int suitValue = function.suitableFor(tableConf);
    switch(suitValue) {
        case -1:
            // 少节点,给提示并抛异常
            throw new ConfigException("Illegal table conf : table [ " + tableConf.getName() + " ] rule function [ " + tableConf.getRule().getFunctionName() + " ] partition size : " + tableConf.getRule().getRuleAlgorithm().getPartitionNum() + " > table datanode size : " + tableConf.getDataNodes().size() + ", please make sure table datanode size = function partition size");
        case 0:
            // table datanode size == rule function partition size
            break;
        case 1:
            // 有些节点是多余的,给出warn log
            LOGGER.warn("table conf : table [ {} ] rule function [ {} ] partition size : {} < table datanode size : {} , this cause some datanode to be redundant", new String[] { tableConf.getName(), tableConf.getRule().getFunctionName(), String.valueOf(tableConf.getRule().getRuleAlgorithm().getPartitionNum()), String.valueOf(tableConf.getDataNodes().size()) });
            break;
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) ConfigException(io.mycat.config.util.ConfigException)

Example 27 with ConfigException

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

the class XMLSchemaLoader method createDataNode.

private void createDataNode(String dnName, String database, String host) {
    DataNodeConfig conf = new DataNodeConfig(dnName, database, host);
    if (dataNodes.containsKey(conf.getName())) {
        throw new ConfigException("dataNode " + conf.getName() + " duplicated!");
    }
    if (!dataHosts.containsKey(host)) {
        throw new ConfigException("dataNode " + dnName + " reference dataHost:" + host + " not exists!");
    }
    dataHosts.get(host).addDataNode(conf.getName());
    dataNodes.put(conf.getName(), conf);
}
Also used : ConfigException(io.mycat.config.util.ConfigException) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Example 28 with ConfigException

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

the class ClusterConfig method loadNode.

private static Map<String, MycatNodeConfig> loadNode(Element root, int port) {
    Map<String, MycatNodeConfig> nodes = new HashMap<String, MycatNodeConfig>();
    NodeList list = root.getElementsByTagName("node");
    Set<String> hostSet = new HashSet<String>();
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            String name = element.getAttribute("name").trim();
            if (nodes.containsKey(name)) {
                throw new ConfigException("node name duplicated :" + name);
            }
            Map<String, Object> props = ConfigUtil.loadElements(element);
            String host = (String) props.get("host");
            if (null == host || "".equals(host)) {
                throw new ConfigException("host empty in node: " + name);
            }
            if (hostSet.contains(host)) {
                throw new ConfigException("node host duplicated :" + host);
            }
            String wei = (String) props.get("weight");
            if (null == wei || "".equals(wei)) {
                throw new ConfigException("weight should not be null in host:" + host);
            }
            int weight = Integer.parseInt(wei);
            if (weight <= 0) {
                throw new ConfigException("weight should be > 0 in host:" + host + " weight:" + weight);
            }
            MycatNodeConfig conf = new MycatNodeConfig(name, host, port, weight);
            nodes.put(name, conf);
            hostSet.add(host);
        }
    }
    return nodes;
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) HashSet(java.util.HashSet)

Example 29 with ConfigException

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

the class ClusterConfig method loadGroup.

private static Map<String, List<String>> loadGroup(Element root, Map<String, MycatNodeConfig> nodes) {
    Map<String, List<String>> groups = new HashMap<String, List<String>>();
    NodeList list = root.getElementsByTagName("group");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            String groupName = e.getAttribute("name").trim();
            if (groups.containsKey(groupName)) {
                throw new ConfigException("group duplicated : " + groupName);
            }
            Map<String, Object> props = ConfigUtil.loadElements(e);
            String value = (String) props.get("nodeList");
            if (null == value || "".equals(value)) {
                throw new ConfigException("group should contain 'nodeList'");
            }
            String[] sList = SplitUtil.split(value, ',', true);
            if (null == sList || sList.length == 0) {
                throw new ConfigException("group should contain 'nodeList'");
            }
            for (String s : sList) {
                if (!nodes.containsKey(s)) {
                    throw new ConfigException("[ node :" + s + "] in [ group:" + groupName + "] doesn't exist!");
                }
            }
            List<String> nodeList = Arrays.asList(sList);
            groups.put(groupName, nodeList);
        }
    }
    if (!groups.containsKey("default")) {
        List<String> nodeList = new ArrayList<String>(nodes.keySet());
        groups.put("default", nodeList);
    }
    return groups;
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(io.mycat.config.util.ConfigException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with ConfigException

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

the class ConfigComparer method loadMigratorTables.

/*
	 * 加载迁移表信息,tables大小为0表示迁移schema下所有表
	 */
private void loadMigratorTables(String schemaName, String[] tables) {
    if (!DataMigratorUtil.isKeyExistIgnoreCase(oldSchemas, schemaName)) {
        throw new ConfigException("oldSchema:" + schemaName + " is not exists!");
    }
    if (!DataMigratorUtil.isKeyExistIgnoreCase(newSchemas, schemaName)) {
        throw new ConfigException("newSchema:" + schemaName + " is not exists!");
    }
    Map<String, TableConfig> oldTables = DataMigratorUtil.getValueIgnoreCase(oldSchemas, schemaName).getTables();
    Map<String, TableConfig> newTables = DataMigratorUtil.getValueIgnoreCase(newSchemas, schemaName).getTables();
    if (tables.length > 0) {
        //指定schema下的表进行迁移
        for (int i = 0; i < tables.length; i++) {
            TableConfig oldTable = DataMigratorUtil.getValueIgnoreCase(oldTables, tables[i]);
            TableConfig newTable = DataMigratorUtil.getValueIgnoreCase(newTables, tables[i]);
            loadMigratorTable(oldTable, newTable, schemaName, tables[i]);
        }
    } else {
        //迁移schema下所有的表
        //校验新旧schema中的table配置是否一致
        Set<String> oldSet = oldTables.keySet();
        Set<String> newSet = newTables.keySet();
        if (!oldSet.equals(newSet)) {
            throw new ConfigException("new & old table config is not equal!");
        }
        for (String tableName : oldSet) {
            TableConfig oldTable = oldTables.get(tableName);
            TableConfig newTable = newTables.get(tableName);
            loadMigratorTable(oldTable, newTable, schemaName, tableName);
        }
    }
}
Also used : TableConfig(io.mycat.config.model.TableConfig) 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