Search in sources :

Example 31 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode 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 32 with RouteResultsetNode

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

the class NonBlockingSession method kill.

protected void kill() {
    AtomicInteger count = new AtomicInteger(0);
    Map<RouteResultsetNode, BackendConnection> toKilled = new HashMap<>();
    for (Map.Entry<RouteResultsetNode, BackendConnection> entry : target.entrySet()) {
        BackendConnection c = entry.getValue();
        if (c != null && !c.isDDL()) {
            toKilled.put(entry.getKey(), c);
            count.incrementAndGet();
        } else if (c != null && c.isDDL()) {
            // if the sql executing is a ddl,do not kill the query,just close the connection
            this.terminate();
            return;
        }
    }
    for (Entry<RouteResultsetNode, BackendConnection> en : toKilled.entrySet()) {
        KillConnectionHandler kill = new KillConnectionHandler(en.getValue(), this);
        ServerConfig conf = DbleServer.getInstance().getConfig();
        PhysicalDBNode dn = conf.getDataNodes().get(en.getKey().getName());
        try {
            dn.getConnectionFromSameSource(en.getValue().getSchema(), true, en.getValue(), kill, en.getKey());
        } catch (Exception e) {
            LOGGER.info("get killer connection failed for " + en.getKey(), e);
            kill.connectionError(e, null);
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) ServerConfig(com.actiontech.dble.config.ServerConfig) BackendConnection(com.actiontech.dble.backend.BackendConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 33 with RouteResultsetNode

use of com.actiontech.dble.route.RouteResultsetNode 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)

Example 34 with RouteResultsetNode

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

the class ExplainHandler method buildHandlerTree.

private static String buildHandlerTree(DMLResponseHandler endHandler, Map<String, RefHandlerInfo> refMap, Map<DMLResponseHandler, RefHandlerInfo> handlerMap, Map<String, Integer> nameMap, Set<String> dependencies) {
    String rootName = null;
    int mergeNodeSize = endHandler.getMerges().size();
    for (int i = 0; i < mergeNodeSize; i++) {
        DMLResponseHandler startHandler = endHandler.getMerges().get(i);
        MultiNodeMergeHandler mergeHandler = (MultiNodeMergeHandler) startHandler;
        List<BaseSelectHandler> mergeList = new ArrayList<>();
        mergeList.addAll(((MultiNodeMergeHandler) startHandler).getExeHandlers());
        String mergeNode = genHandlerName("MERGE", nameMap);
        RefHandlerInfo refInfo = new RefHandlerInfo(mergeNode, "MERGE");
        handlerMap.put(mergeHandler, refInfo);
        refMap.put(mergeNode, refInfo);
        for (BaseSelectHandler exeHandler : mergeList) {
            RouteResultsetNode rrss = exeHandler.getRrss();
            String dateNode = rrss.getName() + "." + rrss.getMultiplexNum();
            refInfo.addChild(dateNode);
            String type = "BASE SQL";
            if (dependencies != null && dependencies.size() > 0) {
                type += "(May No Need)";
            }
            RefHandlerInfo baseSQLInfo = new RefHandlerInfo(dateNode, type, rrss.getStatement());
            refMap.put(dateNode, baseSQLInfo);
            if (dependencies != null && dependencies.size() > 0) {
                baseSQLInfo.addAllStepChildren(dependencies);
            }
        }
        String mergeRootName = getAllNodesFromLeaf(mergeHandler, refMap, handlerMap, nameMap);
        if (rootName == null) {
            if (mergeRootName == null) {
                rootName = mergeNode;
            } else {
                rootName = mergeRootName;
            }
        }
    }
    return rootName;
}
Also used : DMLResponseHandler(com.actiontech.dble.backend.mysql.nio.handler.query.DMLResponseHandler) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode)

Example 35 with RouteResultsetNode

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

the class ServerLoadDataInfileHandler method buildResultSet.

private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
    statement.setLocal(true);
    // druid will filter path, reset it now
    SQLLiteralExpr fn = new SQLCharExpr(fileName);
    statement.setFileName(fn);
    // replace IGNORE X LINES in SQL to avoid  IGNORING X LINE in every node.
    String srcStatement = this.ignoreLinesDelete(statement.toString());
    RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
    rrs.setLoadData(true);
    rrs.setStatement(srcStatement);
    rrs.setFinishedRoute(true);
    int size = routeMap.size();
    RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
    int index = 0;
    for (Map.Entry<String, LoadData> entry : routeMap.entrySet()) {
        RouteResultsetNode rrNode = new RouteResultsetNode(entry.getKey(), ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
        rrNode.setStatement(srcStatement);
        LoadData newLoadData = new LoadData();
        ObjectUtil.copyProperties(loadData, newLoadData);
        newLoadData.setLocal(true);
        LoadData loadData1 = entry.getValue();
        if (loadData1.getFileName() != null) {
            newLoadData.setFileName(loadData1.getFileName());
        } else {
            newLoadData.setData(loadData1.getData());
        }
        rrNode.setLoadData(newLoadData);
        routeResultsetNodes[index] = rrNode;
        index++;
    }
    rrs.setNodes(routeResultsetNodes);
    return rrs;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) LoadData(com.actiontech.dble.sqlengine.mpp.LoadData) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Aggregations

RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)48 BackendConnection (com.actiontech.dble.backend.BackendConnection)12 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)9 RouteResultset (com.actiontech.dble.route.RouteResultset)8 TableConfig (com.actiontech.dble.config.model.TableConfig)6 SQLNonTransientException (java.sql.SQLNonTransientException)6 MySQLConnection (com.actiontech.dble.backend.mysql.nio.MySQLConnection)5 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)5 PushDownVisitor (com.actiontech.dble.backend.mysql.nio.handler.builder.sqlvisitor.PushDownVisitor)4 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 ServerConfig (com.actiontech.dble.config.ServerConfig)3 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)3 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)3 LoadData (com.actiontech.dble.sqlengine.mpp.LoadData)3 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)2 DruidShardingParseInfo (com.actiontech.dble.route.parser.druid.DruidShardingParseInfo)2 SQLException (java.sql.SQLException)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2