Search in sources :

Example 6 with ConfigException

use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.

the class XMLSchemaLoader method processChildTables.

private void processChildTables(Map<String, TableConfig> tables, TableConfig parentTable, String strDatoNodes, Element tableNode, boolean isLowerCaseNames) {
    // parse child tables
    NodeList childNodeList = tableNode.getChildNodes();
    for (int j = 0; j < childNodeList.getLength(); j++) {
        Node theNode = childNodeList.item(j);
        if (!theNode.getNodeName().equals("childTable")) {
            continue;
        }
        Element childTbElement = (Element) theNode;
        String cdTbName = childTbElement.getAttribute("name");
        if (isLowerCaseNames) {
            cdTbName = cdTbName.toLowerCase();
        }
        String primaryKey = childTbElement.hasAttribute("primaryKey") ? childTbElement.getAttribute("primaryKey").toUpperCase() : null;
        boolean autoIncrement = false;
        if (childTbElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(childTbElement.getAttribute("autoIncrement"));
        }
        boolean needAddLimit = true;
        if (childTbElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(childTbElement.getAttribute("needAddLimit"));
        }
        // join key ,the parent's column
        String joinKey = childTbElement.getAttribute("joinKey").toUpperCase();
        String parentKey = childTbElement.getAttribute("parentKey").toUpperCase();
        TableConfig table = new TableConfig(cdTbName, primaryKey, autoIncrement, needAddLimit, TableTypeEnum.TYPE_SHARDING_TABLE, strDatoNodes, null, false, parentTable, joinKey, parentKey);
        if (tables.containsKey(table.getName())) {
            throw new ConfigException("table " + table.getName() + " duplicated!");
        }
        tables.put(table.getName(), table);
        // child table may also have children
        processChildTables(tables, table, strDatoNodes, childTbElement, isLowerCaseNames);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException)

Example 7 with ConfigException

use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.

the class XMLSchemaLoader method loadTables.

private Map<String, TableConfig> loadTables(Element node, boolean isLowerCaseNames) {
    // supprot [`] BEN GONG in table name
    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);
        // primaryKey used for cache and autoincrement
        String primaryKey = tableElement.hasAttribute("primaryKey") ? tableElement.getAttribute("primaryKey").toUpperCase() : null;
        // if autoIncrement,it will use sequence handler
        boolean autoIncrement = false;
        if (tableElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(tableElement.getAttribute("autoIncrement"));
        }
        // limit size of the table
        boolean needAddLimit = true;
        if (tableElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(tableElement.getAttribute("needAddLimit"));
        }
        // table type is global or not
        String tableTypeStr = tableElement.hasAttribute("type") ? tableElement.getAttribute("type") : null;
        TableTypeEnum tableType = TableTypeEnum.TYPE_SHARDING_TABLE;
        if ("global".equalsIgnoreCase(tableTypeStr)) {
            tableType = TableTypeEnum.TYPE_GLOBAL_TABLE;
        }
        // dataNode of table
        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;
        // ruleRequired?
        if (tableElement.hasAttribute("ruleRequired")) {
            ruleRequired = Boolean.parseBoolean(tableElement.getAttribute("ruleRequired"));
        }
        String dataNode = tableElement.getAttribute("dataNode");
        // distribute function
        String distPrex = "distribute(";
        boolean distTableDns = dataNode.startsWith(distPrex);
        if (distTableDns) {
            dataNode = dataNode.substring(distPrex.length(), dataNode.length() - 1);
        }
        String tableNameElement = tableElement.getAttribute("name");
        if (isLowerCaseNames) {
            tableNameElement = tableNameElement.toLowerCase();
        }
        String[] tableNames = tableNameElement.split(",");
        if (tableNames == null) {
            throw new ConfigException("table name is not found!");
        }
        for (String tableName : tableNames) {
            TableConfig table = new TableConfig(tableName, primaryKey, autoIncrement, needAddLimit, tableType, dataNode, (tableRule != null) ? tableRule.getRule() : null, ruleRequired);
            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!");
            }
            tables.put(table.getName(), table);
        }
        // child table must know its unique father
        if (tableNames.length == 1) {
            TableConfig parentTable = tables.get(tableNames[0]);
            // process child tables
            processChildTables(tables, parentTable, dataNode, tableElement, isLowerCaseNames);
        }
    }
    return tables;
}
Also used : TableTypeEnum(com.actiontech.dble.config.model.TableConfig.TableTypeEnum) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig)

Example 8 with ConfigException

use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.

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"));
        /**
         * balance type for read
         * 1. balance="0", read will be send to all writeHost.
         * 2. balance="1", read will be send to all readHost and stand by writeHost
         * 3. balance="2", read will be send to all readHost and all writeHost
         * 4. balance="3", read will be send to all readHost
         */
        final int balance = Integer.parseInt(element.getAttribute("balance"));
        /**
         * switchType
         * -1 No switch
         * 1 switch automatically
         * 2 switch according to the second behind of slave ,the heartbeat query is show slave status
         * 3 switch according to Galera Cluster for MySQL
         */
        String switchTypeStr = element.getAttribute("switchType");
        int switchType = switchTypeStr.equals("") ? -1 : Integer.parseInt(switchTypeStr);
        // slave delay threshold
        String slaveThresholdStr = element.getAttribute("slaveThreshold");
        int slaveThreshold = slaveThresholdStr.equals("") ? -1 : Integer.parseInt(slaveThresholdStr);
        // tempReadHostAvailable >0 means read service is still work even write host crash
        String tempReadHostAvailableStr = element.getAttribute("tempReadHostAvailable");
        boolean tempReadHostAvailable = !tempReadHostAvailableStr.equals("") && Integer.parseInt(tempReadHostAvailableStr) > 0;
        final String heartbeatSQL = element.getElementsByTagName("heartbeat").item(0).getTextContent();
        NodeList writeNodes = element.getElementsByTagName("writeHost");
        DBHostConfig[] writeDbConfs = new DBHostConfig[writeNodes.getLength()];
        Map<Integer, DBHostConfig[]> readHostsMap = new HashMap<>(2);
        for (int w = 0; w < writeDbConfs.length; w++) {
            Element writeNode = (Element) writeNodes.item(w);
            writeDbConfs[w] = createDBHostConf(name, writeNode, maxCon, minCon);
            NodeList readNodes = writeNode.getElementsByTagName("readHost");
            // for every readHost
            if (readNodes.getLength() != 0) {
                DBHostConfig[] readDbConfs = new DBHostConfig[readNodes.getLength()];
                for (int r = 0; r < readDbConfs.length; r++) {
                    Element readNode = (Element) readNodes.item(r);
                    readDbConfs[r] = createDBHostConf(name, readNode, maxCon, minCon);
                }
                readHostsMap.put(w, readDbConfs);
            }
        }
        DataHostConfig hostConf = new DataHostConfig(name, writeDbConfs, readHostsMap, switchType, slaveThreshold, tempReadHostAvailable);
        hostConf.setMaxCon(maxCon);
        hostConf.setMinCon(minCon);
        hostConf.setBalance(balance);
        hostConf.setHearbeatSQL(heartbeatSQL);
        dataHosts.put(hostConf.getName(), hostConf);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException)

Example 9 with ConfigException

use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.

the class XMLSchemaLoader method loadDataNodes.

private void loadDataNodes(Element root) {
    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");
        if (lowerCaseNames) {
            databaseStr = databaseStr.toLowerCase();
        }
        String host = element.getAttribute("dataHost");
        if (empty(dnNamePre) || empty(databaseStr) || empty(host)) {
            throw new ConfigException("dataNode " + dnNamePre + " define error ,attribute can't be empty");
        }
        // dnNamePre(name),databaseStr(database),host(dataHost) can use ',', '$', '-' to configure multi nodes
        // but the database size *dataHost size must equal the size of name
        // every dataHost has all database in its tag
        // eg:<dataNode name="dn1$0-75" dataHost="localhost$1-10" database="db$0-75" />
        // means:localhost1 has database of dn1$0-75,localhost2has database of dn1$0-75(name is db$76-151)
        String[] dnNames = SplitUtil.split(dnNamePre, ',', '$', '-');
        String[] databases = SplitUtil.split(databaseStr, ',', '$', '-');
        String[] hostStrings = 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(com.actiontech.dble.config.util.ConfigException)

Example 10 with ConfigException

use of com.actiontech.dble.config.util.ConfigException in project dble by actiontech.

the class XMLServerLoader method load.

public void load(Loader loader, boolean isLowerCaseTableNames) {
    // read server.xml
    InputStream dtd = null;
    InputStream xml = null;
    try {
        dtd = ResourceUtil.getResourceAsStream("/server.dtd");
        xml = ResourceUtil.getResourceAsStream("/server.xml");
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        loader.load(root, this, isLowerCaseTableNames);
    } catch (ConfigException e) {
        throw e;
    } catch (Exception e) {
        throw new ConfigException(e);
    } finally {
        if (dtd != null) {
            try {
                dtd.close();
            } catch (IOException e) {
            // ignore error
            }
        }
        if (xml != null) {
            try {
                xml.close();
            } catch (IOException e) {
            // ignore error
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) IOException(java.io.IOException) ConfigException(com.actiontech.dble.config.util.ConfigException) IOException(java.io.IOException)

Aggregations

ConfigException (com.actiontech.dble.config.util.ConfigException)18 Element (org.w3c.dom.Element)14 NodeList (org.w3c.dom.NodeList)10 Node (org.w3c.dom.Node)6 IOException (java.io.IOException)4 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)3 TableRuleConfig (com.actiontech.dble.config.model.rule.TableRuleConfig)3 InputStream (java.io.InputStream)3 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)2 UserConfig (com.actiontech.dble.config.model.UserConfig)2 RuleConfig (com.actiontech.dble.config.model.rule.RuleConfig)2 PhysicalDatasource (com.actiontech.dble.backend.datasource.PhysicalDatasource)1 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)1 SystemConfig (com.actiontech.dble.config.model.SystemConfig)1 TableTypeEnum (com.actiontech.dble.config.model.TableConfig.TableTypeEnum)1 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)1 WallConfig (com.alibaba.druid.wall.WallConfig)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 ArrayList (java.util.ArrayList)1