Search in sources :

Example 21 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testBatchInsert.

/**
     * 支持insert into ... values (),()...
     * 不支持insert into ... select...
     *
     * @throws Exception
     */
public void testBatchInsert() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    RouteResultset rrs = null;
    //不支持childtable 批量插入
    String sql = "insert into orders (id,name,customer_id) values(1,'testonly',1),(2,'testonly',2000001)";
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("ChildTable multi insert not provided", e.getMessage());
    }
    sql = "insert into employee (id,name,customer_id) select id,name,customer_id from customer";
    try {
        rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    } catch (Exception e) {
        Assert.assertEquals("TODO:insert into .... select .... not supported!", e.getMessage());
    }
    //分片表批量插入正常 employee
    sql = "insert into employee (id,name,sharding_id) values(1,'testonly',10000),(2,'testonly',10010)";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(2, rrs.getNodes().length);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("dn2", rrs.getNodes()[1].getName());
    String node1Sql = formatSql("insert into employee (id,name,sharding_id) values(1,'testonly',10000)");
    String node2Sql = formatSql("insert into employee (id,name,sharding_id) values(2,'testonly',10010)");
    RouteResultsetNode[] nodes = rrs.getNodes();
    Assert.assertEquals(node1Sql, nodes[0].getStatement());
    Assert.assertEquals(node2Sql, nodes[1].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) SQLNonTransientException(java.sql.SQLNonTransientException) NoSuchElementException(java.util.NoSuchElementException) RouteResultset(io.mycat.route.RouteResultset)

Example 22 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

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)

Example 23 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testroute.

public void testroute() throws Exception {
    SchemaConfig schema = schemaMap.get("cndb");
    String sql = "select * from independent where member='abc'";
    RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 128);
    IndexedNodeNameAsserter nameAsserter = new IndexedNodeNameAsserter("independent_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
    for (int i = 0; i < 128; ++i) {
        sqlAsserter.addExpectSQL(i, "select * from independent where member='abc'");
    }
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
    // include database schema ,should remove
    sql = "select * from cndb.independent A  where a.member='abc'";
    schema = schemaMap.get("cndb");
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    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 independent A  where a.member='abc'");
    }
    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 24 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

the class DruidMysqlRouteStrategyTest method testNonPartitionSQL.

public void testNonPartitionSQL() throws Exception {
    SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    schema = schemaMap.get("dubbo2");
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(new SystemConfig(), schema, 9, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW TABLES like 'solo'", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("dubbo");
    sql = "SHOW TABLES from db_name like 'solo'";
    rrs = routeStrategy.route(new SystemConfig(), schema, 9, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("SHOW TABLES like 'solo'", rrs.getNodes()[0].getStatement());
    sql = "desc cndb.offer";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    Assert.assertEquals(-1L, rrs.getLimitSize());
    Assert.assertEquals(1, rrs.getNodes().length);
    Assert.assertEquals("dubbo_dn", rrs.getNodes()[0].getName());
    Assert.assertEquals("desc cndb.offer", rrs.getNodes()[0].getStatement());
    schema = schemaMap.get("cndb");
    sql = "SHOW fulL TaBLES from db_name like 'solo'";
    rrs = routeStrategy.route(new SystemConfig(), schema, 9, sql, null, null, cachePool);
    Assert.assertEquals(false, rrs.isCacheAble());
    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 FULL TABLES like 'solo'").addExpectSQL(1, "SHOW FULL TABLES like 'solo'").addExpectSQL(2, "SHOW FULL TABLES like 'solo'").addExpectSQL(3, "SHOW FULL TABLES like 'solo'");
    RouteNodeAsserter 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 25 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

the class FetchStoreNodeOfChildTableHandler method execute.

public String execute(String schema, String sql, ArrayList<String> dataNodes) {
    String key = schema + ":" + sql;
    CachePool cache = MycatServer.getInstance().getCacheService().getCachePool("ER_SQL2PARENTID");
    String result = (String) cache.get(key);
    if (result != null) {
        return result;
    }
    this.sql = sql;
    int totalCount = dataNodes.size();
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 5 * 60 * 1000L;
    MycatConfig conf = MycatServer.getInstance().getConfig();
    LOGGER.debug("find child node with sql:" + sql);
    for (String dn : dataNodes) {
        if (dataNode != null) {
            return dataNode;
        }
        PhysicalDBNode mysqlDN = conf.getDataNodes().get(dn);
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("execute in datanode " + dn);
            }
            RouteResultsetNode node = new RouteResultsetNode(dn, ServerParse.SELECT, sql);
            // 获取 子表节点,最好走master为好
            node.setRunOnSlave(false);
            mysqlDN.getConnection(mysqlDN.getDatabase(), true, node, this, node);
        //				mysqlDN.getConnection(mysqlDN.getDatabase(), true,
        //						new RouteResultsetNode(dn, ServerParse.SELECT, sql),
        //						this, dn);
        } catch (Exception e) {
            LOGGER.warn("get connection err " + e);
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
        }
    }
    while (dataNode == null && System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
        if (dataNode != null || finished.get() >= totalCount) {
            break;
        }
    }
    if (dataNode != null) {
        cache.putIfAbsent(key, dataNode);
    }
    return dataNode;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig) IOException(java.io.IOException) CachePool(io.mycat.cache.CachePool)

Aggregations

RouteResultsetNode (io.mycat.route.RouteResultsetNode)43 RouteResultset (io.mycat.route.RouteResultset)15 SQLNonTransientException (java.sql.SQLNonTransientException)8 BackendConnection (io.mycat.backend.BackendConnection)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)7 MycatConfig (io.mycat.config.MycatConfig)7 SchemaConfig (io.mycat.config.model.SchemaConfig)7 SystemConfig (io.mycat.config.model.SystemConfig)7 TableConfig (io.mycat.config.model.TableConfig)4 SlotFunction (io.mycat.route.function.SlotFunction)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)2 CachePool (io.mycat.cache.CachePool)2