Search in sources :

Example 1 with RouteResultset

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

the class DruidInsertParser method parserChildTable.

private void parserChildTable(SchemaInfo schemaInfo, final RouteResultset rrs, MySqlInsertStatement insertStmt, final ServerConnection sc) throws SQLNonTransientException {
    final SchemaConfig schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    final TableConfig tc = schema.getTables().get(tableName);
    if (isMultiInsert(insertStmt)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    String joinKey = tc.getJoinKey();
    int joinKeyIndex = getJoinKeyIndex(schemaInfo, insertStmt, joinKey);
    final String joinKeyVal = insertStmt.getValues().getValues().get(joinKeyIndex).toString();
    String realVal = StringUtil.removeApostrophe(joinKeyVal);
    final String sql = RouterUtil.removeSchema(insertStmt.toString(), schemaInfo.getSchema());
    rrs.setStatement(sql);
    // try to route by ER parent partion key
    RouteResultset theRrs = routeByERParentKey(rrs, tc, realVal);
    if (theRrs != null) {
        rrs.setFinishedRoute(true);
    } else {
        rrs.setFinishedExecute(true);
        DbleServer.getInstance().getComplexQueryExecutor().execute(new Runnable() {

            // get child result will be blocked, so use ComplexQueryExecutor
            @Override
            public void run() {
                // route by sql query root parent's data node
                String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("to find root parent's node sql :" + findRootTBSql);
                }
                FetchStoreNodeOfChildTableHandler fetchHandler = new FetchStoreNodeOfChildTableHandler(findRootTBSql, sc.getSession2());
                String dn = fetchHandler.execute(schema.getName(), tc.getRootParent().getDataNodes());
                if (dn == null) {
                    sc.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "can't find (root) parent sharding node for sql:" + sql);
                    return;
                }
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("found partition node for child table to insert " + dn + " sql :" + sql);
                }
                RouterUtil.routeToSingleNode(rrs, dn);
                sc.getSession2().execute(rrs);
            }
        });
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) FetchStoreNodeOfChildTableHandler(com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler) TableConfig(com.actiontech.dble.config.model.TableConfig) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 2 with RouteResultset

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

the class DruidReplaceParser method parserChildTable.

private void parserChildTable(SchemaInfo schemaInfo, final RouteResultset rrs, MySqlReplaceStatement replace, final ServerConnection sc) throws SQLNonTransientException {
    final SchemaConfig schema = schemaInfo.getSchemaConfig();
    String tableName = schemaInfo.getTable();
    final TableConfig tc = schema.getTables().get(tableName);
    // check if the childtable replace with the multi
    if (isMultiReplace(replace)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    // find the value of child table join key
    String joinKey = tc.getJoinKey();
    int joinKeyIndex = getJoinKeyIndex(schemaInfo, replace, joinKey);
    final String joinKeyVal = replace.getValuesList().get(0).getValues().get(joinKeyIndex).toString();
    String realVal = StringUtil.removeApostrophe(joinKeyVal);
    final String sql = RouterUtil.removeSchema(replace.toString(), schemaInfo.getSchema());
    rrs.setStatement(sql);
    // try to route by ER parent partition key
    RouteResultset theRrs = routeByERParentKey(rrs, tc, realVal);
    if (theRrs != null) {
        rrs.setFinishedRoute(true);
    } else {
        rrs.setFinishedExecute(true);
        DbleServer.getInstance().getComplexQueryExecutor().execute(new Runnable() {

            // get child result will be blocked, so use ComplexQueryExecutor
            @Override
            public void run() {
                // route by sql query root parent's data node
                String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("to find root parent's node sql :" + findRootTBSql);
                }
                FetchStoreNodeOfChildTableHandler fetchHandler = new FetchStoreNodeOfChildTableHandler(findRootTBSql, sc.getSession2());
                String dn = fetchHandler.execute(schema.getName(), tc.getRootParent().getDataNodes());
                if (dn == null) {
                    sc.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "can't find (root) parent sharding node for sql:" + sql);
                    return;
                }
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("found partition node for child table to insert " + dn + " sql :" + sql);
                }
                RouterUtil.routeToSingleNode(rrs, dn);
                sc.getSession2().execute(rrs);
            }
        });
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) FetchStoreNodeOfChildTableHandler(com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler) TableConfig(com.actiontech.dble.config.model.TableConfig) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 3 with RouteResultset

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

the class ExplainHandler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    stmt = stmt.substring(offset).trim();
    RouteResultset rrs = getRouteResultset(c, stmt);
    if (rrs == null) {
        return;
    }
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.getPacketId();
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : FIELDS) {
        field.setPacketId(++packetId);
        buffer = field.write(buffer, c, true);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.setPacketId(++packetId);
    buffer = eof.write(buffer, c, true);
    if (!rrs.isNeedOptimizer()) {
        // write rows
        for (RouteResultsetNode node : rrs.getNodes()) {
            RowDataPacket row = getRow(node, c.getCharset().getResults());
            row.setPacketId(++packetId);
            buffer = row.write(buffer, c, true);
        }
    } else {
        List<String[]> results = getComplexQueryResult(rrs, c);
        for (String[] result : results) {
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(result[0], c.getCharset().getResults()));
            row.add(StringUtil.encode(result[1], c.getCharset().getResults()));
            row.add(StringUtil.encode(result[2].replaceAll("[\\t\\n\\r]", " "), c.getCharset().getResults()));
            row.setPacketId(++packetId);
            buffer = row.write(buffer, c, true);
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(com.actiontech.dble.net.mysql.ResultSetHeaderPacket) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 4 with RouteResultset

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

the class ServerLoadDataInfileHandler method start.

@Override
public void start(String strSql) {
    clear();
    this.sql = strSql;
    if (this.checkPartition(strSql)) {
        serverConnection.writeErrMessage(ErrorCode.ER_UNSUPPORTED_PS, " unsupported load data with Partition");
        clear();
        return;
    }
    SQLStatementParser parser = new MySqlStatementParser(strSql);
    statement = (MySqlLoadDataInFileStatement) parser.parseStatement();
    fileName = parseFileName(strSql);
    if (fileName == null) {
        serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, " file name is null !");
        clear();
        return;
    }
    schema = DbleServer.getInstance().getConfig().getSchemas().get(serverConnection.getSchema());
    tableId2DataNodeCache = (LayerCachePool) DbleServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
    tableName = statement.getTableName().getSimpleName();
    if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
        tableName = tableName.toLowerCase();
    }
    tableConfig = schema.getTables().get(tableName);
    tempPath = SystemConfig.getHomePath() + File.separator + "temp" + File.separator + serverConnection.getId() + File.separator;
    tempFile = tempPath + "clientTemp.txt";
    tempByteBuffer = new ByteArrayOutputStream();
    List<SQLExpr> columns = statement.getColumns();
    if (tableConfig != null) {
        String pColumn = getPartitionColumn();
        if (pColumn != null && columns != null && columns.size() > 0) {
            for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
                String column = StringUtil.removeBackQuote(columns.get(i).toString());
                if (pColumn.equalsIgnoreCase(column)) {
                    partitionColumnIndex = i;
                    break;
                }
            }
        }
    }
    parseLoadDataPram();
    if (statement.isLocal()) {
        isStartLoadData = true;
        // request file from client
        ByteBuffer buffer = serverConnection.allocate();
        RequestFilePacket filePacket = new RequestFilePacket();
        filePacket.setFileName(fileName.getBytes());
        filePacket.setPacketId(1);
        filePacket.write(buffer, serverConnection, true);
    } else {
        if (!new File(fileName).exists()) {
            serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, fileName + " is not found!");
            clear();
        } else {
            parseFileByLine(fileName, loadData.getCharset(), loadData.getLineTerminatedBy());
            RouteResultset rrs = buildResultSet(routeResultMap);
            if (rrs != null) {
                flushDataToFile();
                isStartLoadData = false;
                serverConnection.getSession2().execute(rrs);
            }
        }
    }
}
Also used : RequestFilePacket(com.actiontech.dble.net.mysql.RequestFilePacket) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) ByteBuffer(java.nio.ByteBuffer) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 5 with RouteResultset

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

the class ServerLoadDataInfileHandler method tryDirectRoute.

private RouteResultset tryDirectRoute(String strSql, String[] lineList) {
    RouteResultset rrs = new RouteResultset(strSql, ServerParse.INSERT);
    rrs.setLoadData(true);
    if (tableConfig == null && schema.getDataNode() != null) {
        // default node
        RouteResultsetNode rrNode = new RouteResultsetNode(schema.getDataNode(), ServerParse.INSERT, strSql);
        rrs.setNodes(new RouteResultsetNode[] { rrNode });
        return rrs;
    } else if (tableConfig != null && tableConfig.isGlobalTable()) {
        ArrayList<String> dataNodes = tableConfig.getDataNodes();
        RouteResultsetNode[] rrsNodes = new RouteResultsetNode[dataNodes.size()];
        for (int i = 0, dataNodesSize = dataNodes.size(); i < dataNodesSize; i++) {
            String dataNode = dataNodes.get(i);
            RouteResultsetNode rrNode = new RouteResultsetNode(dataNode, ServerParse.INSERT, strSql);
            rrsNodes[i] = rrNode;
        }
        rrs.setNodes(rrsNodes);
        return rrs;
    } else if (tableConfig != null) {
        DruidShardingParseInfo ctx = new DruidShardingParseInfo();
        ctx.addTable(tableName);
        if (partitionColumnIndex == -1 || partitionColumnIndex >= lineList.length) {
            return null;
        } else {
            String value = lineList[partitionColumnIndex];
            RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
            routeCalculateUnit.addShardingExpr(tableName, getPartitionColumn(), parseFieldString(value, loadData.getEnclose(), loadData.getEscape()));
            ctx.addRouteCalculateUnit(routeCalculateUnit);
            try {
                SortedSet<RouteResultsetNode> nodeSet = new TreeSet<>();
                for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
                    RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, false, tableId2DataNodeCache);
                    if (rrsTmp != null) {
                        Collections.addAll(nodeSet, rrsTmp.getNodes());
                    }
                }
                RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
                int i = 0;
                for (RouteResultsetNode aNodeSet : nodeSet) {
                    nodes[i] = aNodeSet;
                    i++;
                }
                rrs.setNodes(nodes);
                return rrs;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return null;
}
Also used : DruidShardingParseInfo(com.actiontech.dble.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) SQLException(java.sql.SQLException) 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