Search in sources :

Example 26 with RouteResultsetNode

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

the class LoadDataUtil method requestFileDataResponse.

public static void requestFileDataResponse(byte[] data, BackendConnection conn) {
    byte packId = data[3];
    BackendAIOConnection backendAIOConnection = (BackendAIOConnection) conn;
    RouteResultsetNode rrn = (RouteResultsetNode) conn.getAttachment();
    LoadData loadData = rrn.getLoadData();
    List<String> loadDataData = loadData.getData();
    try {
        if (loadDataData != null && loadDataData.size() > 0) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            for (int i = 0, loadDataDataSize = loadDataData.size(); i < loadDataDataSize; i++) {
                String line = loadDataData.get(i);
                String s = (i == loadDataDataSize - 1) ? line : line + loadData.getLineTerminatedBy();
                byte[] bytes = s.getBytes(loadData.getCharset());
                bos.write(bytes);
            }
            packId = writeToBackConnection(packId, new ByteArrayInputStream(bos.toByteArray()), backendAIOConnection);
        } else {
            //从文件读取
            packId = writeToBackConnection(packId, new BufferedInputStream(new FileInputStream(loadData.getFileName())), backendAIOConnection);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        //结束必须发空包
        byte[] empty = new byte[] { 0, 0, 0, 3 };
        empty[3] = ++packId;
        backendAIOConnection.write(empty);
    }
}
Also used : LoadData(io.mycat.sqlengine.mpp.LoadData) BackendAIOConnection(io.mycat.net.BackendAIOConnection) RouteResultsetNode(io.mycat.route.RouteResultsetNode)

Example 27 with RouteResultsetNode

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

the class MultiNodeQueryHandler method connectionAcquired.

@Override
public void connectionAcquired(final BackendConnection conn) {
    final RouteResultsetNode node = (RouteResultsetNode) conn.getAttachment();
    session.bindConnection(node, conn);
    _execute(conn, node);
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode)

Example 28 with RouteResultsetNode

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

the class RollbackNodeHandler method rollback.

public void rollback() {
    final int initCount = session.getTargetCount();
    lock.lock();
    try {
        reset(initCount);
    } finally {
        lock.unlock();
    }
    if (session.closed()) {
        decrementCountToZero();
        return;
    }
    // 执行
    int started = 0;
    for (final RouteResultsetNode node : session.getTargetKeys()) {
        if (node == null) {
            LOGGER.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
            continue;
        }
        final BackendConnection conn = session.getTarget(node);
        if (conn != null) {
            boolean isClosed = conn.isClosedOrQuit();
            if (isClosed) {
                session.getSource().writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "receive rollback,but find backend con is closed or quit");
                LOGGER.error(conn + "receive rollback,but fond backend con is closed or quit");
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("rollback job run for " + conn);
            }
            if (clearIfSessionClosed(session)) {
                return;
            }
            conn.setResponseHandler(RollbackNodeHandler.this);
            //support the XA rollback
            if (session.getXaTXID() != null && conn instanceof MySQLConnection) {
                MySQLConnection mysqlCon = (MySQLConnection) conn;
                String xaTxId = session.getXaTXID();
                //exeBatch cmd issue : the 2nd package can not receive the response
                mysqlCon.execCmd("XA END " + xaTxId + ";");
                mysqlCon.execCmd("XA ROLLBACK " + xaTxId + ";");
            } else {
                conn.rollback();
            }
            ++started;
        }
    }
    if (started < initCount && decrementCountBy(initCount - started)) {
        /**
			 * assumption: only caused by front-end connection close. <br/>
			 * Otherwise, packet must be returned to front-end
			 */
        session.clearResources(true);
    }
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Example 29 with RouteResultsetNode

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

the class PostgreSQLBackendConnection method query.

/**********
	 * 此查询用于心跳检查和获取连接后的健康检查
	 */
@Override
public void query(String query) throws UnsupportedEncodingException {
    RouteResultsetNode rrn = new RouteResultsetNode("default", ServerParse.SELECT, query);
    synAndDoExecute(null, rrn, this.charsetIndex, this.txIsolation, true);
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode)

Example 30 with RouteResultsetNode

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

the class DruidInsertParser method parserBatchInsert.

/**
	 * insert into .... select .... 或insert into table() values (),(),....
	 * @param schema
	 * @param rrs
	 * @param insertStmt
	 * @throws SQLNonTransientException
	 */
private void parserBatchInsert(SchemaConfig schema, RouteResultset rrs, String partitionColumn, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    //insert into table() values (),(),....
    if (insertStmt.getValuesList().size() > 1) {
        //字段列数
        int columnNum = insertStmt.getColumns().size();
        int shardingColIndex = getShardingColIndex(insertStmt, partitionColumn);
        if (shardingColIndex == -1) {
            String msg = "bad insert sql (sharding column:" + partitionColumn + " not provided," + insertStmt;
            LOGGER.warn(msg);
            throw new SQLNonTransientException(msg);
        } else {
            List<ValuesClause> valueClauseList = insertStmt.getValuesList();
            Map<Integer, List<ValuesClause>> nodeValuesMap = new HashMap<Integer, List<ValuesClause>>();
            Map<Integer, Integer> slotsMap = new HashMap<>();
            TableConfig tableConfig = schema.getTables().get(tableName);
            AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
            for (ValuesClause valueClause : valueClauseList) {
                if (valueClause.getValues().size() != columnNum) {
                    String msg = "bad insert sql columnSize != valueSize:" + columnNum + " != " + valueClause.getValues().size() + "values:" + valueClause;
                    LOGGER.warn(msg);
                    throw new SQLNonTransientException(msg);
                }
                SQLExpr expr = valueClause.getValues().get(shardingColIndex);
                String shardingValue = null;
                if (expr instanceof SQLIntegerExpr) {
                    SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
                    shardingValue = intExpr.getNumber() + "";
                } else if (expr instanceof SQLCharExpr) {
                    SQLCharExpr charExpr = (SQLCharExpr) expr;
                    shardingValue = charExpr.getText();
                }
                Integer nodeIndex = algorithm.calculate(shardingValue);
                if (algorithm instanceof SlotFunction) {
                    slotsMap.put(nodeIndex, ((SlotFunction) algorithm).slotValue());
                }
                //没找到插入的分片
                if (nodeIndex == null) {
                    String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
                    LOGGER.warn(msg);
                    throw new SQLNonTransientException(msg);
                }
                if (nodeValuesMap.get(nodeIndex) == null) {
                    nodeValuesMap.put(nodeIndex, new ArrayList<ValuesClause>());
                }
                nodeValuesMap.get(nodeIndex).add(valueClause);
            }
            RouteResultsetNode[] nodes = new RouteResultsetNode[nodeValuesMap.size()];
            int count = 0;
            for (Map.Entry<Integer, List<ValuesClause>> node : nodeValuesMap.entrySet()) {
                Integer nodeIndex = node.getKey();
                List<ValuesClause> valuesList = node.getValue();
                insertStmt.setValuesList(valuesList);
                nodes[count] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), insertStmt.toString());
                if (algorithm instanceof SlotFunction) {
                    nodes[count].setSlot(slotsMap.get(nodeIndex));
                    nodes[count].setStatement(ParseUtil.changeInsertAddSlot(nodes[count].getStatement(), nodes[count].getSlot()));
                }
                nodes[count++].setSource(rrs);
            }
            rrs.setNodes(nodes);
            rrs.setFinishedRoute(true);
        }
    } else if (insertStmt.getQuery() != null) {
        // insert into .... select ....
        String msg = "TODO:insert into .... select .... not supported!";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) HashMap(java.util.HashMap) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SlotFunction(io.mycat.route.function.SlotFunction) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) TableConfig(io.mycat.config.model.TableConfig) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

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