Search in sources :

Example 11 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class AbstractRouteStrategy method route.

@Override
public RouteResultset route(SchemaConfig schema, int sqlType, String origSQL, ServerConnection sc, LayerCachePool cachePool) throws SQLException {
    RouteResultset rrs = new RouteResultset(origSQL, sqlType);
    /*
         * debug mode and load data ,no cache
         */
    if (LOGGER.isDebugEnabled() && origSQL.startsWith(LoadData.LOAD_DATA_HINT)) {
        rrs.setCacheAble(false);
    }
    if (sqlType == ServerParse.CALL) {
        rrs.setCallStatement(true);
        Procedure procedure = parseProcedure(origSQL);
        rrs.setProcedure(procedure);
    }
    if (schema == null) {
        rrs = routeNormalSqlWithAST(null, origSQL, rrs, cachePool, sc);
    } else {
        if (sqlType == ServerParse.SHOW) {
            rrs.setStatement(origSQL);
            rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode());
        } else {
            rrs = routeNormalSqlWithAST(schema, origSQL, rrs, cachePool, sc);
        }
    }
    return rrs;
}
Also used : Procedure(com.actiontech.dble.route.Procedure) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 12 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class DruidUpdateParserTest method throwExceptionParse.

public void throwExceptionParse(String sql, boolean throwException) throws NoSuchMethodException {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement sqlStatement = statementList.get(0);
    MySqlUpdateStatement update = (MySqlUpdateStatement) sqlStatement;
    SchemaConfig schemaConfig = mock(SchemaConfig.class);
    Map<String, TableConfig> tables = mock(Map.class);
    TableConfig tableConfig = mock(TableConfig.class);
    String tableName = "hotnews";
    when((schemaConfig).getTables()).thenReturn(tables);
    when(tables.get(tableName)).thenReturn(tableConfig);
    when(tableConfig.getParentTC()).thenReturn(null);
    RouteResultset routeResultset = new RouteResultset(sql, 11);
    Class c = DruidUpdateParser.class;
    Method method = c.getDeclaredMethod("confirmShardColumnNotUpdated", new Class[] { SQLUpdateStatement.class, SchemaConfig.class, String.class, String.class, String.class, RouteResultset.class });
    method.setAccessible(true);
    try {
        method.invoke(c.newInstance(), update, schemaConfig, tableName, "ID", "", routeResultset);
        if (throwException) {
            System.out.println("Not passed without exception is not correct");
            Assert.assertTrue(false);
        } else {
            System.out.println("Passed without exception. Maybe the partition key exists in update statement,but not update in fact");
            Assert.assertTrue(true);
        }
    } catch (Exception e) {
        if (throwException) {
            System.out.println(e.getCause().getClass());
            Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
            System.out.println("SQLNonTransientException is expected");
        } else {
            System.out.println("need checked");
            Assert.assertTrue(false);
        }
    }
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Method(java.lang.reflect.Method) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SQLNonTransientException(java.sql.SQLNonTransientException) TableConfig(com.actiontech.dble.config.model.TableConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) DruidUpdateParser(com.actiontech.dble.route.parser.druid.impl.DruidUpdateParser) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 13 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class RouteResultCopy method rrCopy.

public static RouteResultset rrCopy(RouteResultset rrs, int sqlType, String stmt) {
    RouteResultset rr = new RouteResultset(stmt, sqlType);
    rr.setRunOnSlave(rrs.getRunOnSlave());
    rr.setFinishedRoute(rrs.isFinishedRoute());
    rr.setGlobalTable(rrs.isGlobalTable());
    rr.setCanRunInReadDB(false);
    RouteResultsetNode[] ns = rrs.getNodes();
    RouteResultsetNode[] nodes = new RouteResultsetNode[ns.length];
    for (int i = 0; i < ns.length; i++) {
        nodes[i] = rrnCopy(ns[i], sqlType, stmt);
    }
    rr.setNodes(nodes);
    return rr;
}
Also used : RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 14 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class RouterUtil method routeFromParser.

public static RouteResultset routeFromParser(DruidParser druidParser, SchemaConfig schema, RouteResultset rrs, SQLStatement statement, String originSql, LayerCachePool cachePool, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    schema = druidParser.parser(schema, rrs, statement, originSql, cachePool, visitor, sc);
    if (rrs.isFinishedExecute()) {
        return null;
    }
    if (rrs.isFinishedRoute()) {
        return rrs;
    }
    /**
     * no name table or others
     */
    DruidShardingParseInfo ctx = druidParser.getCtx();
    if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
        if (schema == null) {
            schema = DbleServer.getInstance().getConfig().getSchemas().get(SchemaUtil.getRandomDb());
        }
        return RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode());
    }
    /* multi-tables*/
    if (druidParser.getCtx().getRouteCalculateUnits().size() == 0) {
        RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
        druidParser.getCtx().addRouteCalculateUnit(routeCalculateUnit);
    }
    SortedSet<RouteResultsetNode> nodeSet = new TreeSet<>();
    for (RouteCalculateUnit unit : druidParser.getCtx().getRouteCalculateUnits()) {
        RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, druidParser.getCtx(), unit, rrs, isSelect(statement), cachePool);
        if (rrsTmp != null && rrsTmp.getNodes() != null) {
            Collections.addAll(nodeSet, rrsTmp.getNodes());
            if (rrsTmp.isGlobalTable()) {
                break;
            }
        }
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
    int i = 0;
    for (RouteResultsetNode aNodeSet : nodeSet) {
        nodes[i] = aNodeSet;
        i++;
    }
    rrs.setNodes(nodes);
    return rrs;
}
Also used : DruidShardingParseInfo(com.actiontech.dble.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 15 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class Explain2Handler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    try {
        stmt = stmt.substring(offset);
        if (!stmt.toLowerCase().contains("datanode=") || !stmt.toLowerCase().contains("sql=")) {
            showError(stmt, c, "explain2 datanode=? sql=?");
            return;
        }
        String dataNode = stmt.substring(stmt.indexOf("=") + 1, stmt.indexOf("sql=")).trim();
        String sql = "explain " + stmt.substring(stmt.indexOf("sql=") + 4, stmt.length()).trim();
        if (dataNode == null || dataNode.isEmpty() || sql == null || sql.isEmpty()) {
            showError(stmt, c, "dataNode or sql is null or empty");
            return;
        }
        RouteResultsetNode node = new RouteResultsetNode(dataNode, ServerParse.SELECT, sql);
        RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
        EMPTY_ARRAY[0] = node;
        rrs.setNodes(EMPTY_ARRAY);
        SingleNodeHandler singleNodeHandler = new SingleNodeHandler(rrs, c.getSession2());
        singleNodeHandler.execute();
    } catch (Exception e) {
        LOGGER.info(e.getMessage(), e.getCause());
        showError(stmt, c, e.getMessage());
    }
}
Also used : SingleNodeHandler(com.actiontech.dble.backend.mysql.nio.handler.SingleNodeHandler) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Aggregations

RouteResultset (com.actiontech.dble.route.RouteResultset)22 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)8 SQLNonTransientException (java.sql.SQLNonTransientException)8 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)6 TableConfig (com.actiontech.dble.config.model.TableConfig)4 SQLException (java.sql.SQLException)4 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)3 IOException (java.io.IOException)3 FetchStoreNodeOfChildTableHandler (com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler)2 DruidShardingParseInfo (com.actiontech.dble.route.parser.druid.DruidShardingParseInfo)2 LoadData (com.actiontech.dble.sqlengine.mpp.LoadData)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 ByteBuffer (java.nio.ByteBuffer)2 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)1 ShowTablesHandler (com.actiontech.dble.backend.mysql.nio.handler.ShowTablesHandler)1 ShowVariablesHandler (com.actiontech.dble.backend.mysql.nio.handler.ShowVariablesHandler)1 SingleNodeHandler (com.actiontech.dble.backend.mysql.nio.handler.SingleNodeHandler)1 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)1 ServerConfig (com.actiontech.dble.config.ServerConfig)1