Search in sources :

Example 1 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class MySQLDataNode method setHeartbeat.

private void setHeartbeat(String heartbeat) {
    if (heartbeat == null) {
        heartbeatAST = null;
        placeHolderToStringer = null;
        return;
    }
    try {
        final Set<PlaceHolder> plist = new HashSet<PlaceHolder>(1, 1);
        SQLStatement ast = SQLParserDelegate.parse(heartbeat);
        ast.accept(new EmptySQLASTVisitor() {

            @Override
            public void visit(PlaceHolder node) {
                plist.add(node);
            }
        });
        if (plist.isEmpty()) {
            heartbeatAST = null;
            placeHolderToStringer = null;
            return;
        }
        Map<PlaceHolder, Object> phm = new HashMap<PlaceHolder, Object>(plist.size(), 1);
        for (PlaceHolder ph : plist) {
            final String content = ph.getName();
            final int low = Integer.parseInt(content.substring(content.indexOf('(') + 1, content.indexOf(',')).trim());
            final int high = Integer.parseInt(content.substring(content.indexOf(',') + 1, content.indexOf(')')).trim());
            phm.put(ph, new Object() {

                private Random rnd = new Random();

                @Override
                public String toString() {
                    return String.valueOf(rnd.nextInt(high - low + 1) + low);
                }
            });
        }
        heartbeatAST = ast;
        placeHolderToStringer = phm;
    } catch (SQLSyntaxErrorException e) {
        throw new ConfigException("heartbeat syntax err: " + heartbeat, e);
    }
}
Also used : EmptySQLASTVisitor(com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor) PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) HashMap(java.util.HashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ConfigException(com.alibaba.cobar.config.util.ConfigException) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) Random(java.util.Random) HashSet(java.util.HashSet)

Example 2 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class RouteRuleInitializer method initRouteRule.

public static void initRouteRule(SchemaLoader loader) throws SQLSyntaxErrorException {
    Map<String, RuleAlgorithm> functions = loader.getFunctions();
    MySQLFunctionManager functionManager = new MySQLFunctionManager(true);
    buildFuncManager(functionManager, functions);
    for (RuleConfig conf : loader.listRuleConfig()) {
        String algorithmString = conf.getAlgorithm();
        MySQLLexer lexer = new MySQLLexer(algorithmString);
        MySQLExprParser parser = new MySQLExprParser(lexer, functionManager, false, MySQLParser.DEFAULT_CHARSET);
        Expression expression = parser.expression();
        if (lexer.token() != MySQLToken.EOF) {
            throw new ConfigException("route algorithm not end with EOF: " + algorithmString);
        }
        RuleAlgorithm algorithm;
        if (expression instanceof RuleAlgorithm) {
            algorithm = (RuleAlgorithm) expression;
        } else {
            algorithm = new ExpressionAdapter(expression);
        }
        conf.setRuleAlgorithm(algorithm);
    }
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) RuleAlgorithm(com.alibaba.cobar.config.model.rule.RuleAlgorithm) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) MySQLFunctionManager(com.alibaba.cobar.parser.recognizer.mysql.MySQLFunctionManager) ConfigException(com.alibaba.cobar.config.util.ConfigException) RuleConfig(com.alibaba.cobar.config.model.rule.RuleConfig) ExpressionAdapter(com.alibaba.cobar.route.function.ExpressionAdapter)

Example 3 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class XMLSchemaLoader method loadTables.

private Map<String, TableConfig> loadTables(Element node) {
    Map<String, TableConfig> tables = new HashMap<String, TableConfig>();
    NodeList nodeList = node.getElementsByTagName("table");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element tableElement = (Element) nodeList.item(i);
        String name = tableElement.getAttribute("name").toUpperCase();
        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"));
        }
        String[] tableNames = SplitUtil.split(name, ',', true);
        for (String tableName : tableNames) {
            TableConfig table = new TableConfig(tableName, dataNode, tableRule, ruleRequired);
            checkDataNodeExists(table.getDataNodes());
            if (tables.containsKey(table.getName())) {
                throw new ConfigException("table " + tableName + " duplicated!");
            }
            tables.put(table.getName(), table);
        }
    }
    return tables;
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) TableConfig(com.alibaba.cobar.config.model.TableConfig) ConfigException(com.alibaba.cobar.config.util.ConfigException) TableRuleConfig(com.alibaba.cobar.config.model.rule.TableRuleConfig)

Example 4 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

the class XMLSchemaLoader method load.

private void load(String dtdFile, String xmlFile) {
    InputStream dtd = null;
    InputStream xml = null;
    try {
        dtd = XMLSchemaLoader.class.getResourceAsStream(dtdFile);
        xml = XMLSchemaLoader.class.getResourceAsStream(xmlFile);
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        loadDataSources(root);
        loadDataNodes(root);
        loadSchemas(root);
    } catch (ConfigException e) {
        throw e;
    } catch (Throwable 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(com.alibaba.cobar.config.util.ConfigException) IOException(java.io.IOException)

Example 5 with ConfigException

use of com.alibaba.cobar.config.util.ConfigException in project cobar by alibaba.

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");
        // 在非空的情况下检查dataNode是否存在
        if (dataNode != null && dataNode.length() != 0) {
            checkDataNodeExists(dataNode);
        } else {
            // 确保非空
            dataNode = "";
        }
        String group = "default";
        if (schemaElement.hasAttribute("group")) {
            group = schemaElement.getAttribute("group").trim();
        }
        Map<String, TableConfig> tables = loadTables(schemaElement);
        if (schemas.containsKey(name)) {
            throw new ConfigException("schema " + name + " duplicated!");
        }
        boolean keepSqlSchema = false;
        if (schemaElement.hasAttribute("keepSqlSchema")) {
            keepSqlSchema = Boolean.parseBoolean(schemaElement.getAttribute("keepSqlSchema").trim());
        }
        schemas.put(name, new SchemaConfig(name, dataNode, group, keepSqlSchema, tables));
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) TableConfig(com.alibaba.cobar.config.model.TableConfig) ConfigException(com.alibaba.cobar.config.util.ConfigException)

Aggregations

ConfigException (com.alibaba.cobar.config.util.ConfigException)16 Element (org.w3c.dom.Element)13 NodeList (org.w3c.dom.NodeList)10 Node (org.w3c.dom.Node)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 HashSet (java.util.HashSet)3 DataNodeConfig (com.alibaba.cobar.config.model.DataNodeConfig)2 TableConfig (com.alibaba.cobar.config.model.TableConfig)2 UserConfig (com.alibaba.cobar.config.model.UserConfig)2 RuleAlgorithm (com.alibaba.cobar.config.model.rule.RuleAlgorithm)2 RuleConfig (com.alibaba.cobar.config.model.rule.RuleConfig)2 TableRuleConfig (com.alibaba.cobar.config.model.rule.TableRuleConfig)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 ClusterConfig (com.alibaba.cobar.config.model.ClusterConfig)1 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)1 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)1