Search in sources :

Example 56 with SchemaConfig

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

the class DruidMysqlRouteStrategyTest method testTableMetaRead.

public void testTableMetaRead() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = " desc offer";
    RouteResultset rrs = routeStrategy.route(schema, ServerParse.DESCRIBE, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = "desc cndb.offer";
    rrs = routeStrategy.route(schema, ServerParse.DESCRIBE, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer", rrs.getNodes()[0].getStatement());
    sql = "desc cndb.offer col1";
    rrs = routeStrategy.route(schema, ServerParse.DESCRIBE, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc offer col1", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  offer  IN db_name WHERE true";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE true", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL COLUMNS FROM  db.offer  IN db_name WHERE true";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW FULL COLUMNS FROM offer WHERE true", rrs.getNodes()[0].getStatement());
    sql = "SHOW FULL TABLES FROM `TESTDB` WHERE Table_type != 'VIEW'";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("SHOW FULL TABLES WHERE Table_type != 'VIEW'", rrs.getNodes()[0].getStatement());
    sql = "SHOW INDEX  IN offer FROM  db_name";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    // random return one node
    // Assert.assertEquals("offer_dn[0]", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW INDEX  FROM offer", rrs.getNodes()[0].getStatement());
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 3);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn0", "offer_dn0", "independent_dn0");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW TABLES like 'solo'").addExpectSQL(1, "SHOW TABLES like 'solo'").addExpectSQL(2, "SHOW TABLES like 'solo'").addExpectSQL(3, "SHOW TABLES like 'solo'");
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "SHOW TABLES in db_name ";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("detail_dn0", "offer_dn0", "independent_dn0");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW TABLES").addExpectSQL(1, "SHOW TABLES").addExpectSQL(2, "SHOW TABLES").addExpectSQL(3, "SHOW TABLES");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    sql = "SHOW TABLeS ";
    rrs = routeStrategy.route(schema, ServerParse.SHOW, sql, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    nodeMap = getNodeMap(rrs, 3);
    nameAsserter = new NodeNameAsserter("offer_dn0", "detail_dn0", "independent_dn0");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sqlAsserter = new SimpleSQLAsserter();
    sqlAsserter.addExpectSQL(0, "SHOW TABLeS ").addExpectSQL(1, "SHOW TABLeS ").addExpectSQL(2, "SHOW TABLeS ");
    asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Example 57 with SchemaConfig

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

the class DruidMysqlRouteStrategyTest method testMoreGlobalTableroute.

public void testMoreGlobalTableroute() throws Exception {
    String sql = null;
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    // select of global table route to only one datanode defined
    sql = "select * from company,area where area.company_id=company.id ";
    schema = schemaMap.get("TESTDB");
    rrs = routeStrategy.route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(true, rrs.isCacheAble());
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig)

Example 58 with SchemaConfig

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

the class HintDBTypeTest method testHint.

/**
 * testHint
 *
 * @throws Exception
 */
@Test
public void testHint() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    // (new hint,/*!dble*/),runOnSlave=false force master
    String sql = "/*!dble:db_type=master*/select * from employee where sharding_id=1";
    CacheService cacheService = new CacheService(false);
    RouteService routerService = new RouteService(cacheService);
    RouteResultset rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    // (new hint,/*#dble*/),runOnSlave=false force master
    sql = "/*#dble:db_type=master*/select * from employee where sharding_id=1";
    rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    // (new hint,/*dble*/),runOnSlave=false force master
    sql = "/*dble:db_type=master*/select * from employee where sharding_id=1";
    rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    // no hint ,runOnSlave=null
    sql = "select * from employee where sharding_id=1";
    rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(rrs.getRunOnSlave() == null);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Example 59 with SchemaConfig

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

the class TestSelectBetweenSqlParser method testBetweenSqlRoute.

@Test
public void testBetweenSqlRoute() throws SQLException {
    String sql = "select * from offer_detail where offer_id between 1 and 33";
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(5, rrs.getNodes().length);
    sql = "select * from offer_detail where col_1 = 33 and offer_id between 1 and 33 and col_2 = 18";
    schema = schemaMap.get("cndb");
    rrs = RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(5, rrs.getNodes().length);
    // sql = "select b.* from offer_date b join  offer_detail a on a.id=b.id " +
    // "where b.col_date between '2014-02-02' and '2014-04-12' and col_1 = 3 and offer_id between 1 and 33";
    sql = "select b.* from offer_detail a  join  offer_date b on a.id=b.id " + "where b.col_date between '2014-02-02' and '2014-04-12' and col_1 = 3 and offer_id between 1 and 33";
    // sql = "select a.* from offer_detail a join offer_date b on a.id=b.id " +
    // "where b.col_date = '2014-04-02' and col_1 = 33 and offer_id =1";
    schema = schemaMap.get("cndb");
    rrs = RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    sql = "select b.* from  offer_date b " + "where b.col_date > '2014-02-02'";
    // sql = "select a.* from offer_detail a join offer_date b on a.id=b.id " +
    // "where b.col_date = '2014-04-02' and col_1 = 33 and offer_id =1";
    schema = schemaMap.get("cndb");
    rrs = RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(128, rrs.getNodes().length);
    sql = "select * from offer_date where col_1 = 33 and col_date between '2014-01-02' and '2014-01-12'";
    schema = schemaMap.get("cndb");
    rrs = RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    sql = "select * from offer_date a where col_1 = 33 and a.col_date between '2014-01-02' and '2014-01-12'";
    schema = schemaMap.get("cndb");
    rrs = RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Test(org.junit.Test)

Example 60 with SchemaConfig

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

the class ShardingDefaultSpace method testDefaultSpace.

public void testDefaultSpace() throws SQLException {
    SchemaConfig schema = this.getSchema();
    String sql = "insert into offer (member_id, gmt_create) values ('1','2001-09-13 20:20:33')";
    for (int i = 0; i < total; i++) {
        RouteStrategyFactory.getRouteStrategy().route(schema, -1, sql, null, cachePool);
    }
}
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