Search in sources :

Example 1 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerConnection method execute.

public void execute(String sql, int type) {
    // 状态检查
    if (txInterrupted) {
        writeErrMessage(ErrorCode.ER_YES, "Transaction error, need to rollback.");
        return;
    }
    // 检查当前使用的DB
    String db = this.schema;
    if (db == null) {
        writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return;
    }
    SchemaConfig schema = CobarServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return;
    }
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = ServerRouter.route(schema, sql, this.charset, this);
    } catch (SQLNonTransientException e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(sql).toString(), e);
        String msg = e.getMessage();
        writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return;
    }
    // session执行
    session.execute(rrs, type);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Example 2 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ShowSlow method schema.

public static void schema(ManagerConnection c, String name) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c);
    }
    // write eof
    buffer = eof.write(buffer, c);
    // write rows
    byte packetId = eof.packetId;
    CobarConfig conf = CobarServer.getInstance().getConfig();
    SchemaConfig schema = conf.getSchemas().get(name);
    if (schema != null) {
        SQLRecorder recorder = new SQLRecorder(conf.getSystem().getSqlRecordCount());
        Map<String, MySQLDataNode> dataNodes = conf.getDataNodes();
        for (String n : schema.getAllDataNodes()) {
            MySQLDataNode dn = dataNodes.get(n);
            MySQLDataSource ds = null;
            if (dn != null && (ds = dn.getSource()) != null) {
                for (SQLRecord r : ds.getSqlRecorder().getRecords()) {
                    if (r != null && recorder.check(r.executeTime)) {
                        recorder.add(r);
                    }
                }
            }
        }
        SQLRecord[] records = recorder.getRecords();
        for (int i = records.length - 1; i >= 0; i--) {
            if (records[i] != null) {
                RowDataPacket row = getRow(records[i], c.getCharset());
                row.packetId = ++packetId;
                buffer = row.write(buffer, c);
            }
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // write buffer
    c.write(buffer);
}
Also used : MySQLDataNode(com.alibaba.cobar.mysql.MySQLDataNode) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) SQLRecorder(com.alibaba.cobar.statistic.SQLRecorder) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) CobarConfig(com.alibaba.cobar.CobarConfig) SQLRecord(com.alibaba.cobar.statistic.SQLRecord) ByteBuffer(java.nio.ByteBuffer) MySQLDataSource(com.alibaba.cobar.mysql.MySQLDataSource) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 3 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ExplainHandler method getRouteResultset.

private static RouteResultset getRouteResultset(ServerConnection c, String stmt) {
    String db = c.getSchema();
    if (db == null) {
        c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return null;
    }
    SchemaConfig schema = CobarServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        c.writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return null;
    }
    try {
        return ServerRouter.route(schema, stmt, c.getCharset(), c);
    } catch (SQLNonTransientException e) {
        StringBuilder s = new StringBuilder();
        logger.warn(s.append(c).append(stmt).toString(), e);
        String msg = e.getMessage();
        c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 4 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig 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)

Example 5 with SchemaConfig

use of com.alibaba.cobar.config.model.SchemaConfig in project cobar by alibaba.

the class ServerRouteTest method testDuplicatePartitionKey.

public void testDuplicatePartitionKey() throws Exception {
    String sql = "select * from offer.wp_image where member_id in ('pavarotti17', 'qaa') or offer.wp_image.member_id='1qq' or member_id='1qq'";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 3);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image WHERE member_id IN ('pavarotti17') OR FALSE OR FALSE").addExpectSQL(1, "SELECT * FROM wp_image WHERE member_id IN ('qaa') OR FALSE OR FALSE").addExpectSQL(2, "SELECT * FROM wp_image WHERE FALSE OR wp_image.member_id = '1qq' OR member_id = '1qq'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "insert into wp_image (id, member_id, gmt) values (1,'pavarotti17',now()),(2,'pavarotti17',now()),(3,'qaa',now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "INSERT INTO wp_image (id, member_id, gmt) VALUES (2, 'pavarotti17', NOW()), (1, 'pavarotti17', NOW())", "INSERT INTO wp_image (id, member_id, gmt) VALUES (1, 'pavarotti17', NOW()), (2, 'pavarotti17', NOW())").addExpectSQL(1, "INSERT INTO wp_image (id, member_id, gmt) VALUES (3, 'qaa', NOW())");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from offer.wp_image where member_id in ('pavarotti17','pavarotti17', 'qaa') or offer.wp_image.member_id='pavarotti17'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(2, rrs.getNodes().length);
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image WHERE member_id IN ('pavarotti17', 'pavarotti17') OR wp_image.member_id = 'pavarotti17'").addExpectSQL(1, "SELECT * FROM wp_image WHERE member_id IN ('qaa') OR FALSE");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from offer.`wp_image` where `member_id` in ('pavarotti17','pavarotti17', 'qaa') or member_id in ('pavarotti17','1qq','pavarotti17') or offer.wp_image.member_id='pavarotti17'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(3, rrs.getNodes().length);
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM `wp_image` WHERE `member_id` IN ('pavarotti17', 'pavarotti17') OR member_id IN ('pavarotti17', 'pavarotti17') OR wp_image.member_id = 'pavarotti17'").addExpectSQL(1, "SELECT * FROM `wp_image` WHERE `member_id` IN ('qaa') OR FALSE OR FALSE").addExpectSQL(2, "SELECT * FROM `wp_image` WHERE FALSE OR member_id IN ('1qq') OR FALSE");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "insert into offer_detail (offer_id, gmt) values (123,now()),(123,now()+1),(234,now()),(123,now()),(345,now()),(122+1,now()),(456,now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("detail_dn[29]", "detail_dn[43]", "detail_dn[57]", "detail_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "INSERT INTO offer_detail (offer_id, gmt) VALUES (234, NOW())").addExpectSQL(1, "INSERT INTO offer_detail (offer_id, gmt) VALUES (345, NOW())").addExpectSQL(2, "INSERT INTO offer_detail (offer_id, gmt) VALUES (456, NOW())").addExpectSQL(3, "INSERT INTO offer_detail (offer_id, gmt) VALUES ", new PermutationGenerator("(123, NOW())", "(123, NOW() + 1)", "(122 + 1, NOW())", "(123, NOW())").setDelimiter(", "), "");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "insert into offer (offer_id, group_id, gmt) values " + "(123, 123, now()),(123, 234, now()),(123, 345, now()),(123, 456, now())" + ",(234, 123, now()),(234, 234, now()),(234, 345, now()),(234, 456, now())" + ",(345, 123, now()),(345, 234, now()),(345, 345, now()),(345, 456, now())" + ",(456, 123, now()),(456, 234, now()),(456, 345, now()),(456, 456, now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 7);
    nameAsserter = new NodeNameAsserter("offer_dn[58]", "offer_dn[100]", "offer_dn[86]", "offer_dn[72]", "offer_dn[114]", "offer_dn[44]", "offer_dn[30]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(345, 123, NOW())", "(123, 345, NOW())", "(234, 234, NOW())").setDelimiter(", "), "").addExpectSQL(1, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(345, 456, NOW())", "(456, 345, NOW())").setDelimiter(", "), "").addExpectSQL(2, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(456, 234, NOW())", "(234, 456, NOW())", "(345, 345, NOW())").setDelimiter(", "), "").addExpectSQL(3, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(123, 456, NOW())", "(345, 234, NOW())", "(234, 345, NOW())", "(456, 123, NOW())").setDelimiter(", "), "").addExpectSQL(4, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(456, 456, NOW())").setDelimiter(", "), "").addExpectSQL(5, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(234, 123, NOW())", "(123, 234, NOW())").setDelimiter(", "), "").addExpectSQL(6, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(123, 123, NOW())").setDelimiter(", "), "");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from offer where (offer_id, group_id ) = (123,234)";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(128, rrs.getNodes().length);
    for (int i = 0; i < 128; i++) {
        Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[i].getReplicaIndex());
        Assert.assertEquals("offer_dn[" + i + "]", rrs.getNodes()[i].getName());
        Assert.assertEquals("select * from offer where (offer_id, group_id ) = (123,234)", rrs.getNodes()[i].getStatement());
    }
    sql = "select * from offer where offer_id=123 and group_id=234";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("select * from offer where offer_id=123 and group_id=234", rrs.getNodes()[0].getStatement());
    // WITHOUT SQL CHANGE unless schema is appeared
    sql = "select * from  cndb.offer where false" + " or offer_id=123 and group_id=123 or offer_id=123 and group_id=234 or offer_id=123 and group_id=345 or offer_id=123 and group_id=456  " + " or offer_id=234 and group_id=123 or offer_id=234 and group_id=234 or offer_id=234 and group_id=345 or offer_id=234 and group_id=456  " + " or offer_id=345 and group_id=123 or offer_id=345 and group_id=234 or offer_id=345 and group_id=345 or offer_id=345 and group_id=456  " + " or offer_id=456 and group_id=123 or offer_id=456 and group_id=234 or offer_id=456 and group_id=345 or offer_id=456 and group_id=456  ";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    String sqlTemp = "SELECT * FROM offer WHERE FALSE OR offer_id = 123 AND group_id = 123 OR offer_id = 123 AND group_id = 234 OR offer_id = 123 AND group_id = 345 OR offer_id = 123 AND group_id = 456 OR offer_id = 234 AND group_id = 123 OR offer_id = 234 AND group_id = 234 OR offer_id = 234 AND group_id = 345 OR offer_id = 234 AND group_id = 456 OR offer_id = 345 AND group_id = 123 OR offer_id = 345 AND group_id = 234 OR offer_id = 345 AND group_id = 345 OR offer_id = 345 AND group_id = 456 OR offer_id = 456 AND group_id = 123 OR offer_id = 456 AND group_id = 234 OR offer_id = 456 AND group_id = 345 OR offer_id = 456 AND group_id = 456";
    nodeMap = getNodeMap(rrs, 7);
    nameAsserter = new NodeNameAsserter("offer_dn[58]", "offer_dn[100]", "offer_dn[86]", "offer_dn[72]", "offer_dn[114]", "offer_dn[44]", "offer_dn[30]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, sqlTemp).addExpectSQL(1, sqlTemp).addExpectSQL(2, sqlTemp).addExpectSQL(3, sqlTemp).addExpectSQL(4, sqlTemp).addExpectSQL(5, sqlTemp).addExpectSQL(6, sqlTemp);
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from  offer where false" + " or offer_id=123 and group_id=123" + " or group_id=123 and offer_id=234" + " or offer_id=123 and group_id=345" + " or offer_id=123 and group_id=456  ";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    sqlTemp = "select * from  offer where false or offer_id=123 and group_id=123 or group_id=123 and offer_id=234 or offer_id=123 and group_id=345 or offer_id=123 and group_id=456  ";
    nodeMap = getNodeMap(rrs, 4);
    nameAsserter = new NodeNameAsserter("offer_dn[72]", "offer_dn[58]", "offer_dn[44]", "offer_dn[30]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, sqlTemp).addExpectSQL(1, sqlTemp).addExpectSQL(2, sqlTemp).addExpectSQL(3, sqlTemp);
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) PermutationGenerator(com.alibaba.cobar.route.util.PermutationUtil.PermutationGenerator)

Aggregations

SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)27 CobarConfig (com.alibaba.cobar.CobarConfig)6 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)5 MySQLDataSource (com.alibaba.cobar.mysql.MySQLDataSource)4 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)4 CobarCluster (com.alibaba.cobar.CobarCluster)3 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)3 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)3 ByteBuffer (java.nio.ByteBuffer)3 DataSourceConfig (com.alibaba.cobar.config.model.DataSourceConfig)2 QuarantineConfig (com.alibaba.cobar.config.model.QuarantineConfig)2 UserConfig (com.alibaba.cobar.config.model.UserConfig)2 ConfigException (com.alibaba.cobar.config.util.ConfigException)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 ArrayList (java.util.ArrayList)2 CobarNode (com.alibaba.cobar.CobarNode)1 ConfigInitializer (com.alibaba.cobar.ConfigInitializer)1 TableConfig (com.alibaba.cobar.config.model.TableConfig)1 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)1 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1