Search in sources :

Example 21 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testModifySQLLimit.

public void testModifySQLLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = null;
    RouteResultset rrs = null;
    // SQL span multi datanode
    sql = "select * from orders limit 2,3";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("dn2", "dn1");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    Assert.assertEquals(3, rrs.getLimitSize());
    MySqlStatementParser parser = new MySqlStatementParser("SELECT * FROM orders LIMIT 0,5");
    SQLStatement statement = parser.parseStatement();
    Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
    // SQL  not span multi datanode
    sql = "select * from customer where id=10000 limit 2,3";
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 1);
    nameAsserter = new NodeNameAsserter("dn1");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    Assert.assertEquals(3, rrs.getLimitSize());
    Assert.assertEquals("select * from customer where id=10000 limit 2,3", rrs.getNodes()[0].getStatement());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 22 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testConfigSchema.

public void testConfigSchema() throws Exception {
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer where offer_id=1";
        routeStrategy.route(schema, 1, sql, null, cachePool);
        Assert.assertFalse(true);
    } catch (Exception e) {
        Assert.assertEquals("route rule for table OFFER is required: select * from offer where offer_id=1", e.getMessage());
    }
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer where col11111=1";
        routeStrategy.route(schema, 1, sql, null, cachePool);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
    try {
        SchemaConfig schema = schemaMap.get("config");
        String sql = "select * from offer ";
        routeStrategy.route(schema, 1, sql, null, cachePool);
        Assert.assertFalse(true);
    } catch (Exception e) {
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException)

Example 23 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testERroute.

public void testERroute() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "insert into orders (id,name,customer_id) values(1,'testonly',1)";
    RouteResultset rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    sql = "insert into orders (id,name,customer_id) values(1,'testonly',2000001)";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // can't update join key
    sql = "update orders set id=1 ,name='aaa' , customer_id=2000001";
    String err = null;
    try {
        rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    } catch (SQLNonTransientException e) {
        err = e.getMessage();
    }
    Assert.assertEquals(true, err.startsWith("Parent relevant column can't be updated ORDERS->CUSTOMER_ID"));
    // route by parent rule ,update sql
    sql = "update orders set id=1 ,name='aaa' where customer_id=2000001";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // route by parent rule but can't find datanode
    sql = "update orders set id=1 ,name='aaa' where customer_id=-1";
    try {
        rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    } catch (Exception e) {
        err = e.getMessage();
    }
    Assert.assertEquals(true, err.startsWith("can't find datanode for sharding column:"));
    // route by parent rule ,select sql
    sql = "select * from orders  where customer_id=2000001";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // route by parent rule ,delete sql
    sql = "delete from orders  where customer_id=2000001";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // test alias in column
    sql = "select name as order_name from  orders order by order_name limit 10,5";
    rrs = routeStrategy.route(schema, 1, sql, null, cachePool);
    MySqlStatementParser parser = new MySqlStatementParser("SELECT name AS order_name FROM orders ORDER BY order_name LIMIT 0,15");
    SQLStatement statement = parser.parseStatement();
// Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException)

Example 24 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testGroupLimit.

public void testGroupLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    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 = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    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 = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    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 = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    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 = routeStrategy.route(schema, 1, sql, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Example 25 with SchemaConfig

use of com.actiontech.dble.config.model.SchemaConfig in project dble by actiontech.

the class DruidMysqlRouteStrategyTest method testGlobalTableSingleNodeLimit.

public void testGlobalTableSingleNodeLimit() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from globalsn";
    RouteResultset rrs = null;
    rrs = routeStrategy.route(schema, ServerParse.SELECT, sql, null, cachePool);
    Assert.assertEquals(100L, rrs.getLimitSize());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Aggregations

SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)70 TableConfig (com.actiontech.dble.config.model.TableConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 Test (org.junit.Test)15 RouteResultset (com.actiontech.dble.route.RouteResultset)6 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)5 ServerConfig (com.actiontech.dble.config.ServerConfig)5 UserConfig (com.actiontech.dble.config.model.UserConfig)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 CacheService (com.actiontech.dble.cache.CacheService)4 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)4 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)4 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)4 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)3