Search in sources :

Example 11 with SchemaConfig

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

the class ServerRouteTest method testGroupLimit.

public void testGroupLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = "select count(*) from wp_image where member_id = 'pavarotti17'";
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(0, rrs.getFlag());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[123]", rrs.getNodes()[0].getName());
    Assert.assertEquals("select count(*) from wp_image where member_id = 'pavarotti17'", rrs.getNodes()[0].getStatement());
    sql = "select count(*) from wp_image where member_id in ('pavarotti17','qaa')";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT COUNT(*) FROM wp_image WHERE member_id IN ('pavarotti17')").addExpectSQL(1, "SELECT COUNT(*) FROM wp_image WHERE member_id IN ('qaa')");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select min(id) from wp_image where member_id in ('pavarotti17','qaa') limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(RouteResultset.MIN_FLAG, rrs.getFlag());
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(99L, 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, "SELECT MIN(id) FROM wp_image WHERE member_id IN ('pavarotti17') LIMIT 0, 99").addExpectSQL(1, "SELECT MIN(id) FROM wp_image WHERE member_id IN ('qaa') LIMIT 0, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select max(offer_id) 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' limit 99 offset 1";
    rrs = ServerRouter.route(schema, sql, null, null);
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(RouteResultset.MAX_FLAG, rrs.getFlag());
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(99L, rrs.getLimitSize());
    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 MAX(offer_id) FROM wp_image WHERE member_id IN ('pavarotti17', 'pavarotti17') OR member_id IN ('pavarotti17', 'pavarotti17') OR wp_image.member_id = 'pavarotti17' LIMIT 1, 99").addExpectSQL(1, "SELECT MAX(offer_id) FROM wp_image WHERE member_id IN ('qaa') OR FALSE OR FALSE LIMIT 1, 99").addExpectSQL(2, "SELECT MAX(offer_id) FROM wp_image WHERE FALSE OR member_id IN ('1qq') OR FALSE LIMIT 1, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select count(*) from (select * from wp_image) w, (select * from offer) o " + " where o.member_id=w.member_id and o.member_id='pavarotti17' limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(99L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 128);
    nameAsserter = new IndexedNodeNameAsserter("offer_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; ++i) {
        sqlAsserter.addExpectSQL(i, "select count(*) from (select * from wp_image) w, (select * from offer) o  where o.member_id=w.member_id and o.member_id='pavarotti17' limit 99");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select count(*) from (select * from wp_image) w, (select * from offer limit 99) o " + " where o.member_id=w.member_id and o.member_id='pavarotti17' ";
    rrs = ServerRouter.route(schema, sql, null, null);
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    if (rrs.getNodes().length > 1)
        Assert.assertEquals(99L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 128);
    nameAsserter = new IndexedNodeNameAsserter("offer_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; ++i) {
        sqlAsserter.addExpectSQL(i, "select count(*) from (select * from wp_image) w, (select * from offer limit 99) o  where o.member_id=w.member_id and o.member_id='pavarotti17' ");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select count(*) from (select * from wp_image where member_id='abc' or member_id='pavarotti17' limit 100) w, (select * from offer_detail where offer_id='123') o " + " where o.member_id=w.member_id and o.member_id='pavarotti17' limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Assert.assertEquals(100L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[12]", "offer_dn[123]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT COUNT(*) FROM (SELECT * FROM wp_image WHERE member_id = 'abc' OR FALSE LIMIT 0, 100) AS " + aliasConvert("w") + ", (SELECT * FROM offer_detail WHERE offer_id = '123') AS " + aliasConvert("o") + " WHERE o.member_id = w.member_id AND o.member_id = 'pavarotti17' LIMIT 0, 99").addExpectSQL(1, "SELECT COUNT(*) FROM (SELECT * FROM wp_image WHERE FALSE OR member_id = 'pavarotti17' LIMIT 0, 100) AS " + aliasConvert("w") + ", (SELECT * FROM offer_detail WHERE offer_id = '123') AS " + aliasConvert("o") + " WHERE o.member_id = w.member_id AND o.member_id = 'pavarotti17' LIMIT 0, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select count(*) from (select * from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(88L, rrs.getLimitSize());
    Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn[29]", "detail_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT COUNT(*) FROM (SELECT * FROM (SELECT * FROM offer_detail WHERE FALSE OR offer_id = '234' LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99").addExpectSQL(1, "SELECT COUNT(*) FROM (SELECT * FROM (SELECT * FROM offer_detail WHERE offer_id = '123' OR FALSE LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select count(*) from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(88L, rrs.getLimitSize());
    Assert.assertEquals(0, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn[29]", "detail_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT COUNT(*) FROM (SELECT * FROM (SELECT MAX(id) FROM offer_detail WHERE FALSE OR offer_id = '234' LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99").addExpectSQL(1, "SELECT COUNT(*) FROM (SELECT * FROM (SELECT MAX(id) FROM offer_detail WHERE offer_id = '123' OR FALSE LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(88L, rrs.getLimitSize());
    Assert.assertEquals(RouteResultset.MAX_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn[29]", "detail_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM (SELECT * FROM (SELECT MAX(id) FROM offer_detail WHERE FALSE OR offer_id = '234' LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99").addExpectSQL(1, "SELECT * FROM (SELECT * FROM (SELECT MAX(id) FROM offer_detail WHERE offer_id = '123' OR FALSE LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "select * from (select count(*) from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(88L, rrs.getLimitSize());
    Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn[29]", "detail_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SELECT * FROM (SELECT COUNT(*) FROM (SELECT * FROM offer_detail WHERE FALSE OR offer_id = '234' LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99").addExpectSQL(1, "SELECT * FROM (SELECT COUNT(*) FROM (SELECT * FROM offer_detail WHERE offer_id = '123' OR FALSE LIMIT 0, 88) AS " + aliasConvert("offer") + " WHERE offer.member_id = 'abc' LIMIT 0, 60) AS " + aliasConvert("w") + " WHERE w.member_id = 'pavarotti17' LIMIT 0, 99");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 12 with SchemaConfig

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

the class ServerRouteTest method testRouteInsertShort.

public void testRouteInsertShort() throws Exception {
    String sql = "inSErt into offer_detail (`offer_id`, gmt) values (123,now())";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("detail_dn[15]", rrs.getNodes()[0].getName());
    Assert.assertEquals("inSErt into offer_detail (`offer_id`, gmt) values (123,now())", rrs.getNodes()[0].getStatement());
    sql = "inSErt into offer_detail ( gmt) values (now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(128, rrs.getNodes().length);
    sql = "inSErt into offer_detail (offer_id, gmt) values (123,now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("detail_dn[15]", rrs.getNodes()[0].getName());
    Assert.assertEquals("inSErt into offer_detail (offer_id, gmt) values (123,now())", rrs.getNodes()[0].getStatement());
    sql = "insert into offer(group_id,offer_id,member_id)values(234,123,'abc')";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[12]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into offer(group_id,offer_id,member_id)values(234,123,'abc')", rrs.getNodes()[0].getStatement());
    sql = "insert into offer (group_id, offer_id, gmt) values (234,123,now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into offer (group_id, offer_id, gmt) values (234,123,now())", rrs.getNodes()[0].getStatement());
    sql = "insert into offer (offer_id, group_id, gmt) values (123,234,now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into offer (offer_id, group_id, gmt) values (123,234,now())", rrs.getNodes()[0].getStatement());
    sql = "insert into offer (offer_id, group_id, gmt) values (234,123,now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into offer (offer_id, group_id, gmt) values (234,123,now())", rrs.getNodes()[0].getStatement());
    sql = "insert into wp_image (member_id,gmt) values ('pavarotti17',now())";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[123]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into wp_image (member_id,gmt) values ('pavarotti17',now())", rrs.getNodes()[0].getStatement());
    sql = "insert low_priority into offer set offer_id=123,  group_id=234,gmt=now() on duplicate key update `dual`=1";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert low_priority into offer set offer_id=123,  group_id=234,gmt=now() on duplicate key update `dual`=1", rrs.getNodes()[0].getStatement());
    sql = "update ignore wp_image set name='abc',gmt=now()where `select`='abc'";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[12]", rrs.getNodes()[0].getName());
    Assert.assertEquals("update ignore wp_image set name='abc',gmt=now()where `select`='abc'", rrs.getNodes()[0].getStatement());
    sql = "delete from offer.*,wp_image.* using offer a,wp_image b where a.member_id=b.member_id and a.member_id='abc' ";
    schema = schemaMap.get("cndb");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[12]", rrs.getNodes()[0].getName());
    Assert.assertEquals("delete from offer.*,wp_image.* using offer a,wp_image b where a.member_id=b.member_id and a.member_id='abc' ", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 13 with SchemaConfig

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

the class ServerRouteTest method testCobarHint.

public void testCobarHint() throws Exception {
    SchemaConfig schema = schemaMap.get("cndb");
    String sql = "  /*!cobar: $dataNodeId=2.1, $table='offer'*/ select * from `dual`";
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(1, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[2]", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    sql = "/*!cobar: $dataNodeId=2.1, $table='offer', $replica =2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(1, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[2]", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    sql = "/*!cobar: $dataNodeId   = [ 1,2,5.2]  , $table =  'offer'   */ select * from `dual`";
    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[1]", "offer_dn[2]", "offer_dn[5]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`").addExpectSQL(1, " select * from `dual`").addExpectSQL(2, " select * from `dual`");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            if (nodeIndex.equals(2))
                Assert.assertEquals(2, replica.intValue());
            else
                Assert.assertEquals(RouteResultsetNode.DEFAULT_REPLICA_INDEX, replica);
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar: $dataNodeId   = [ 1,2,5.2]  , $table =  'offer'  , $replica =1 */ select * from `dual`";
    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[1]", "offer_dn[2]", "offer_dn[5]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`").addExpectSQL(1, " select * from `dual`").addExpectSQL(2, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            if (nodeIndex.equals(2))
                Assert.assertEquals(2, replica.intValue());
            else
                Assert.assertEquals(1, replica.intValue());
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar: $partitionOperand=( 'member_id' = 'pavarotti17'), $table='offer'*/ select * from `dual`";
    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[123]", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    sql = "/*!cobar:$partitionOperand =   ( 'member_id' = ['pavarotti17'  ,   'qaa' ]  ), $table='offer'  , $replica =  2*/  select * from `dual`";
    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, "  select * from `dual`").addExpectSQL(1, "  select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            Assert.assertEquals(2, replica.intValue());
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:$partitionOperand = ( ['group_id','offer_id'] = [234,4]), $table='offer'*/ select * from `dual`";
    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[29]", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    sql = "/*!cobar:$partitionOperand=(['offer_id','group_id']=[[123,3],[234,4]]), $table='offer'  , $replica =2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[29]", "offer_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`").addExpectSQL(1, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            Assert.assertEquals(2, replica.intValue());
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:$partitionOperand=(['group_id','offer_id']=[[123,3], [ 234,4 ] ]), $table='offer'  */ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[29]", "offer_dn[15]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`").addExpectSQL(1, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            Assert.assertEquals(RouteResultsetNode.DEFAULT_REPLICA_INDEX, replica);
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:$partitionOperand=(['offer_id','NON_EXistence']=[[123,3],[234,4]]), $table='offer'  , $replica =2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 128);
    nameAsserter = new IndexedNodeNameAsserter("offer_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; i++) {
        sqlAsserter.addExpectSQL(i, " select * from `dual`");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            Assert.assertEquals(2, replica.intValue());
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:  $dataNodeId   = 1  ,$table =  'wp_image'*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 1);
    nameAsserter = new NodeNameAsserter("offer_dn[1]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:  $dataNodeId   = [0,3]  ,$table =  'wp_image'*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("offer_dn[0]", "offer_dn[3]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`").addExpectSQL(1, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:  $table =  'wp_image'*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 128);
    nameAsserter = new IndexedNodeNameAsserter("offer_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; i++) {
        sqlAsserter.addExpectSQL(i, " select * from `dual`");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:  $dataNodeId   = 0  ,$table =  'independent'*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 1);
    nameAsserter = new NodeNameAsserter("independent_dn[0]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:  $dataNodeId   = [ 1,2,5]  ,$table =  'independent'*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("independent_dn[1]", "independent_dn[2]", "independent_dn[5]");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, " select * from `dual`").addExpectSQL(1, " select * from `dual`").addExpectSQL(2, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:  $table =  'independent'*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 128);
    nameAsserter = new IndexedNodeNameAsserter("independent_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; i++) {
        sqlAsserter.addExpectSQL(i, " select * from `dual`");
    }
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:$partitionOperand=(['member_id','NON_EXistence']=[['pavarotti17'],['qaa',4]]), $table='offer'  , $replica=2*/ select * from `dual`";
    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, " select * from `dual`").addExpectSQL(1, " select * from `dual`");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter, new ReplicaAsserter() {

        @Override
        public void assertReplica(Integer nodeIndex, Integer replica) {
            Assert.assertEquals(2, replica.intValue());
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "/*!cobar:$partitionOperand=(['offer_id','NON_EXistence']=[[123,3],[234,4]]), $table='non_existence'  , $replica=2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(2, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    sql = "/*!cobar:$partitionOperand=(['offer_id','group_id']=[[123,3],[234,4]]), $table='non_existence'  , $replica=2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(2, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("dubbo");
    sql = "/*!cobar: $replica=2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(2, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("dubbo");
    sql = "/*!cobar: $dataNodeId = [ 0.1],$replica=2*/ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(1, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("dubbo");
    sql = "/*!cobar: */ select * from `dual`";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(" select * from `dual`", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

Example 14 with SchemaConfig

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

the class ServerRouteTest method testConfigSchema.

public void testConfigSchema() throws Exception {
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer where offer_id=1";
        ServerRouter.route(schema, sql, null, null);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer where col11111=1";
        ServerRouter.route(schema, sql, null, null);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer ";
        ServerRouter.route(schema, sql, null, null);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ConfigException(com.alibaba.cobar.config.util.ConfigException) NoSuchElementException(java.util.NoSuchElementException)

Example 15 with SchemaConfig

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

the class ServerRouteTest method testIgnoreSchema.

public void testIgnoreSchema() throws Exception {
    SchemaConfig schema = schemaMap.get("ignoreSchemaTest");
    String sql = "select * from offer where offer_id=1";
    RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest.offer where ignoreSchemaTest.offer.offer_id=1";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("SELECT * FROM offer WHERE offer.offer_id = 1", rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest2.offer where ignoreSchemaTest2.offer.offer_id=1";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest2.offer a,ignoreSchemaTest.offer b  where ignoreSchemaTest2.offer.offer_id=1";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("SELECT * FROM ignoreSchemaTest2.offer AS " + aliasConvert("a") + ", offer AS " + aliasConvert("b") + " WHERE ignoreSchemaTest2.offer.offer_id = 1", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("ignoreSchemaTest0");
    sql = "select * from offer where offer_id=1";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
    sql = "select * from ignoreSchemaTest0.offer where ignoreSchemaTest.offer.offer_id=1";
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("SELECT * FROM offer WHERE ignoreSchemaTest.offer.offer_id = 1", rrs.getNodes()[0].getStatement());
    sql = "insert into offer (group_id, offer_id, gmt) values (234,123,now())";
    schema = schemaMap.get("ignoreSchemaTest0");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("insert into offer (group_id, offer_id, gmt) values (234,123,now())", rrs.getNodes()[0].getStatement());
    sql = "insert into ignoreSchemaTest0.offer (group_id, offer_id, gmt) values (234,123,now())";
    schema = schemaMap.get("ignoreSchemaTest0");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
    Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
    Assert.assertEquals("INSERT INTO offer (group_id, offer_id, gmt) VALUES (234, 123, NOW())", rrs.getNodes()[0].getStatement());
    sql = "insert into ignoreSchemaTest2.offer (group_id, offer_id, gmt) values (234,123,now())";
    schema = schemaMap.get("ignoreSchemaTest0");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
    sql = "insert into ignoreSchemaTest2.offer (ignoreSchemaTest0.offer.group_id, offer_id, gmt) values (234,123,now())";
    schema = schemaMap.get("ignoreSchemaTest0");
    rrs = ServerRouter.route(schema, sql, null, null);
    Assert.assertEquals("cndb_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("INSERT INTO ignoreSchemaTest2.offer (offer.group_id, offer_id, gmt) VALUES (234, 123, NOW())", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig)

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