Search in sources :

Example 11 with ConfigException

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

the class ConfigInitializer method testConnection.

public void testConnection(boolean isStart) {
    if (this.dataNodes != null && this.dataHosts != null) {
        Map<String, Boolean> map = new HashMap<>();
        for (PhysicalDBNode dataNode : dataNodes.values()) {
            String database = dataNode.getDatabase();
            PhysicalDBPool pool = dataNode.getDbPool();
            if (isStart) {
                // start for first time, 1.you can set write host as empty
                if (pool.getSources() == null || pool.getSources().length == 0) {
                    continue;
                }
                DBHostConfig wHost = pool.getSource().getConfig();
                // start for first time, 2.you can set write host as yourself
                if (("localhost".equalsIgnoreCase(wHost.getIp()) || "127.0.0.1".equalsIgnoreCase(wHost.getIp())) && wHost.getPort() == this.system.getServerPort()) {
                    continue;
                }
            }
            for (PhysicalDatasource ds : pool.getAllDataSources()) {
                String key = ds.getName() + "_" + database;
                if (map.get(key) == null) {
                    map.put(key, false);
                    try {
                        boolean isConnected = ds.testConnection(database);
                        map.put(key, isConnected);
                    } catch (IOException e) {
                        LOGGER.info("test conn " + key + " error:", e);
                    }
                }
            }
        }
        boolean isConnectivity = true;
        for (Map.Entry<String, Boolean> entry : map.entrySet()) {
            String key = entry.getKey();
            Boolean value = entry.getValue();
            if (!value && isConnectivity) {
                LOGGER.info("SelfCheck### test " + key + " database connection failed ");
                isConnectivity = false;
            } else {
                LOGGER.info("SelfCheck### test " + key + " database connection success ");
            }
        }
        if (!isConnectivity) {
            throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!");
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ConfigException(com.actiontech.dble.config.util.ConfigException) IOException(java.io.IOException) PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource)

Example 12 with ConfigException

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

the class SystemConfigLoader method load.

public void load(Element root, XMLServerLoader xsl, boolean isLowerCaseTableNames) throws IllegalAccessException, InvocationTargetException {
    SystemConfig system = xsl.getSystem();
    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();
        int pos = majorMySQLVersion.indexOf(".") + 1;
        majorMySQLVersion = majorMySQLVersion.substring(0, majorMySQLVersion.indexOf(".", pos));
        for (String ver : SystemConfig.MYSQL_VERSIONS) {
            // version is x.y.z ,just compare the x.y
            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 : SystemConfig(com.actiontech.dble.config.model.SystemConfig) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException)

Example 13 with ConfigException

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

the class UserConfigLoader method load.

public void load(Element root, XMLServerLoader xsl, boolean isLowerCaseTableNames) throws IllegalAccessException, InvocationTargetException {
    Map<String, UserConfig> users = xsl.getUsers();
    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.decrypt(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 manager = (String) props.get("manager");
            if (null != manager) {
                user.setManager(Boolean.parseBoolean(manager));
                user.setSchemas(new HashSet<String>(0));
            }
            String schemas = (String) props.get("schemas");
            if (user.isManager() && schemas != null) {
                throw new ConfigException("manager user can't set any schema!");
            } else if (!user.isManager()) {
                if (schemas != null) {
                    if (isLowerCaseTableNames) {
                        schemas = schemas.toLowerCase();
                    }
                    String[] strArray = SplitUtil.split(schemas, ',', true);
                    user.setSchemas(new HashSet<>(Arrays.asList(strArray)));
                }
                // load DML
                loadPrivileges(user, isLowerCaseTableNames, 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(com.actiontech.dble.config.util.ConfigException) UserConfig(com.actiontech.dble.config.model.UserConfig) HashSet(java.util.HashSet)

Example 14 with ConfigException

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

the class XMLRuleLoader method loadTableRules.

/**
 * tableRule tag:
 * <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 {
    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 (StringUtil.isEmpty(name)) {
                throw new ConfigException("name is null or empty");
            }
            if (tableRules.containsKey(name)) {
                throw new ConfigException("table rule " + name + " duplicated!");
            }
            NodeList ruleNodes = e.getElementsByTagName("rule");
            int length = ruleNodes.getLength();
            if (length > 1) {
                throw new ConfigException("only one rule can defined :" + name);
            }
            // rule has only one element now. Maybe it will not contains one rule in feature
            // RuleConfig:rule->function
            RuleConfig rule = loadRule((Element) ruleNodes.item(0));
            tableRules.put(name, new TableRuleConfig(name, rule));
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) RuleConfig(com.actiontech.dble.config.model.rule.RuleConfig) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig)

Example 15 with ConfigException

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

the class XMLRuleLoader method load.

private void load(String dtdFile, String xmlFile) {
    InputStream dtd = null;
    InputStream xml = null;
    try {
        dtd = ResourceUtil.getResourceAsStream(dtdFile);
        xml = ResourceUtil.getResourceAsStream(xmlFile);
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        loadFunctions(root);
        loadTableRules(root);
    } 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) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigException(com.actiontech.dble.config.util.ConfigException)

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