Search in sources :

Example 71 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.

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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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(new SystemConfig(), schema, 1, sql, null, 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 : SystemConfig(io.mycat.config.model.SystemConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(io.mycat.config.model.SchemaConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) NoSuchElementException(java.util.NoSuchElementException) RouteResultset(io.mycat.route.RouteResultset)

Example 72 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.

the class DruidMysqlRouteStrategyTest method testSelectNoTable.

/**
 * select 1
 * select 1 union all select 2
 *
 * @throws Exception
 */
public void testSelectNoTable() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select 1";
    RouteResultset rrs = null;
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    sql = "select 1 union select 2";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 73 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.

the class DruidMysqlRouteStrategyTest method testTableMetaRead.

public void testTableMetaRead() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = " desc offer";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, 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(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, 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(new SystemConfig(), schema, ServerParse.DESCRIBE, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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(new SystemConfig(), schema, ServerParse.SHOW, sql, null, 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 : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 74 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.

the class DruidMysqlRouteStrategyTest method testRouteCache.

public void testRouteCache() throws Exception {
    // select cache ID
    this.cachePool.putIfAbsent("TESTDB_EMPLOYEE", "88", "dn2");
    SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = "select * from employee where id=88";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    // 已经缓存了,不必再缓存了
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(null, rrs.getPrimaryKey());
    Assert.assertEquals(-1, rrs.getLimitSize());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // select cache ID not found ,return all node and rrst not cached
    sql = "select * from employee where id=89";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("TESTDB_EMPLOYEE.ID", rrs.getPrimaryKey());
    Assert.assertEquals(-1, rrs.getLimitSize());
    // update cache ID found
    sql = "update employee  set name='aaa' where id=88";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(null, rrs.getPrimaryKey());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
    // delete cache ID found
    sql = "delete from  employee  where id=88";
    rrs = routeStrategy.route(new SystemConfig(), schema, -1, sql, null, null, cachePool);
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultset(io.mycat.route.RouteResultset)

Example 75 with SystemConfig

use of io.mycat.config.model.SystemConfig in project Mycat_plus by coderczp.

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(new SystemConfig(), schema, ServerParse.SELECT, sql, null, 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(new SystemConfig(), schema, ServerParse.SELECT, sql, null, 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 : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

SystemConfig (io.mycat.config.model.SystemConfig)120 SchemaConfig (io.mycat.config.model.SchemaConfig)110 RouteResultset (io.mycat.route.RouteResultset)64 Test (org.junit.Test)60 RouteResultsetNode (io.mycat.route.RouteResultsetNode)14 CacheService (io.mycat.cache.CacheService)10 SQLNonTransientException (java.sql.SQLNonTransientException)10 NoSuchElementException (java.util.NoSuchElementException)10 RouteService (io.mycat.route.RouteService)8 ArrayList (java.util.ArrayList)8 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 TableConfig (io.mycat.config.model.TableConfig)6 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)2 NettyBufferPool (io.mycat.buffer.NettyBufferPool)2 MycatConfig (io.mycat.config.MycatConfig)2 ManagerConnectionFactory (io.mycat.manager.ManagerConnectionFactory)2 MyCatMemory (io.mycat.memory.MyCatMemory)2 AIOAcceptor (io.mycat.net.AIOAcceptor)2