Search in sources :

Example 6 with SchemaConfig

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

the class ShowDatabase method execute.

public static void execute(ManagerConnection c) {
    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;
    Map<String, SchemaConfig> schemas = CobarServer.getInstance().getConfig().getSchemas();
    for (String name : new TreeSet<String>(schemas.keySet())) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(name, c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c);
    }
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // write buffer
    c.write(buffer);
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) TreeSet(java.util.TreeSet) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Example 7 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 8 with SchemaConfig

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

the class ShowCobarCluster method getRows.

private static List<RowDataPacket> getRows(ServerConnection c) {
    List<RowDataPacket> rows = new LinkedList<RowDataPacket>();
    CobarConfig config = CobarServer.getInstance().getConfig();
    CobarCluster cluster = config.getCluster();
    Map<String, SchemaConfig> schemas = config.getSchemas();
    SchemaConfig schema = (c.getSchema() == null) ? null : schemas.get(c.getSchema());
    // 如果没有指定schema或者schema为null,则使用全部集群。
    if (schema == null) {
        Map<String, CobarNode> nodes = cluster.getNodes();
        for (CobarNode n : nodes.values()) {
            if (n != null && n.isOnline()) {
                rows.add(getRow(n, c.getCharset()));
            }
        }
    } else {
        String group = (schema.getGroup() == null) ? "default" : schema.getGroup();
        List<String> nodeList = cluster.getGroups().get(group);
        if (nodeList != null && nodeList.size() > 0) {
            Map<String, CobarNode> nodes = cluster.getNodes();
            for (String id : nodeList) {
                CobarNode n = nodes.get(id);
                if (n != null && n.isOnline()) {
                    rows.add(getRow(n, c.getCharset()));
                }
            }
        }
        // 如果schema对应的group或者默认group都没有有效的节点,则使用全部集群。
        if (rows.size() == 0) {
            Map<String, CobarNode> nodes = cluster.getNodes();
            for (CobarNode n : nodes.values()) {
                if (n != null && n.isOnline()) {
                    rows.add(getRow(n, c.getCharset()));
                }
            }
        }
    }
    if (rows.size() == 0) {
        alarm.error(Alarms.CLUSTER_EMPTY + c.toString());
    }
    return rows;
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) CobarNode(com.alibaba.cobar.CobarNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) CobarConfig(com.alibaba.cobar.CobarConfig) LinkedList(java.util.LinkedList) CobarCluster(com.alibaba.cobar.CobarCluster)

Example 9 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 10 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