Search in sources :

Example 1 with RouteResultsetNode

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

the class ShareRowOutPutDataHandler method processSQL.

public void processSQL(String sql, EngineCtx ctx) {
    String ssql = joinParser.getSql();
    getRoute(ssql);
    RouteResultsetNode[] nodes = rrs.getNodes();
    if (nodes == null || nodes.length == 0 || nodes[0].getName() == null || nodes[0].getName().equals("")) {
        ctx.getSession().getSource().writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No dataNode found ,please check tables defined in schema:" + ctx.getSession().getSource().getSchema());
        return;
    }
    this.ctx = ctx;
    String[] dataNodes = getDataNodes();
    maxjob = dataNodes.length;
    ShareDBJoinHandler joinHandler = new ShareDBJoinHandler(this, joinParser.getJoinLkey());
    ctx.executeNativeSQLSequnceJob(dataNodes, ssql, joinHandler);
    EngineCtx.LOGGER.info("Catlet exec:" + getDataNode(getDataNodes()) + " sql:" + ssql);
    ctx.setAllJobFinishedListener(new AllJobFinishedListener() {

        @Override
        public void onAllJobFinished(EngineCtx ctx) {
            if (!jointTableIsData) {
                ctx.writeHeader(fields);
            }
            ctx.writeEof();
            EngineCtx.LOGGER.info("发送数据OK");
        }
    });
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode) AllJobFinishedListener(io.mycat.sqlengine.AllJobFinishedListener) EngineCtx(io.mycat.sqlengine.EngineCtx)

Example 2 with RouteResultsetNode

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

the class LockTablesHandler method connectionAcquired.

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

Example 3 with RouteResultsetNode

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

the class MultiNodeCoordinator method executeBatchNodeCmd.

/** Multi-nodes 1pc Commit Handle **/
public void executeBatchNodeCmd(SQLCtrlCommand cmdHandler) {
    this.cmdHandler = cmdHandler;
    final int initCount = session.getTargetCount();
    runningCount.set(initCount);
    nodeCount = initCount;
    failed.set(false);
    faileCount.set(0);
    //recovery nodes log
    ParticipantLogEntry[] participantLogEntry = new ParticipantLogEntry[initCount];
    // 执行
    int started = 0;
    for (RouteResultsetNode rrn : session.getTargetKeys()) {
        if (rrn == null) {
            LOGGER.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
            continue;
        }
        final BackendConnection conn = session.getTarget(rrn);
        if (conn != null) {
            conn.setResponseHandler(this);
            //process the XA_END XA_PREPARE Command
            MySQLConnection mysqlCon = (MySQLConnection) conn;
            String xaTxId = session.getXaTXID();
            if (mysqlCon.getXaStatus() == TxState.TX_STARTED_STATE) {
                //recovery Log
                participantLogEntry[started] = new ParticipantLogEntry(xaTxId, conn.getHost(), 0, conn.getSchema(), ((MySQLConnection) conn).getXaStatus());
                String[] cmds = new String[] { "XA END " + xaTxId, "XA PREPARE " + xaTxId };
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Start execute the batch cmd : " + cmds[0] + ";" + cmds[1] + "," + "current connection:" + conn.getHost() + ":" + conn.getPort());
                }
                mysqlCon.execBatchCmd(cmds);
            } else {
                //recovery Log
                participantLogEntry[started] = new ParticipantLogEntry(xaTxId, conn.getHost(), 0, conn.getSchema(), ((MySQLConnection) conn).getXaStatus());
                cmdHandler.sendCommand(session, conn);
            }
            ++started;
        }
    }
    //xa recovery log
    if (session.getXaTXID() != null) {
        CoordinatorLogEntry coordinatorLogEntry = new CoordinatorLogEntry(session.getXaTXID(), false, participantLogEntry);
        inMemoryRepository.put(session.getXaTXID(), coordinatorLogEntry);
        fileRepository.writeCheckpoint(inMemoryRepository.getAllCoordinatorLogEntries());
    }
    if (started < nodeCount) {
        runningCount.set(started);
        LOGGER.warn("some connection failed to execute " + (nodeCount - started));
        /**
			 * assumption: only caused by front-end connection close. <br/>
			 * Otherwise, packet must be returned to front-end
			 */
        failed.set(true);
    }
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) ParticipantLogEntry(io.mycat.backend.mysql.xa.ParticipantLogEntry) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection) CoordinatorLogEntry(io.mycat.backend.mysql.xa.CoordinatorLogEntry)

Example 4 with RouteResultsetNode

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

the class MultiNodeQueryHandler method execute.

public void execute() throws Exception {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        this.reset(rrs.getNodes().length);
        this.fieldsReturned = false;
        this.affectedRows = 0L;
        this.insertId = 0L;
    } finally {
        lock.unlock();
    }
    MycatConfig conf = MycatServer.getInstance().getConfig();
    startTime = System.currentTimeMillis();
    LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
    for (final RouteResultsetNode node : rrs.getNodes()) {
        BackendConnection conn = session.getTarget(node);
        if (session.tryExistsCon(conn, node)) {
            LOGGER.debug("node.getRunOnSlave()-" + node.getRunOnSlave());
            // 实现 master/slave注解
            node.setRunOnSlave(rrs.getRunOnSlave());
            LOGGER.debug("node.getRunOnSlave()-" + node.getRunOnSlave());
            _execute(conn, node);
        } else {
            // create new connection
            LOGGER.debug("node.getRunOnSlave()1-" + node.getRunOnSlave());
            // 实现 master/slave注解
            node.setRunOnSlave(rrs.getRunOnSlave());
            LOGGER.debug("node.getRunOnSlave()2-" + node.getRunOnSlave());
            PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
            dn.getConnection(dn.getDatabase(), autocommit, node, this, node);
        // 注意该方法不仅仅是获取连接,获取新连接成功之后,会通过层层回调,最后回调到本类 的connectionAcquired
        // 这是通过 上面方法的 this 参数的层层传递完成的。
        // connectionAcquired 进行执行操作:
        // session.bindConnection(node, conn);
        // _execute(conn, node);
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig)

Example 5 with RouteResultsetNode

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

the class MultiNodeQueryHandler method rowResponse.

@Override
public void rowResponse(final byte[] row, final BackendConnection conn) {
    if (errorRepsponsed.get()) {
        //conn.close(error);
        return;
    }
    lock.lock();
    try {
        this.selectRows++;
        RouteResultsetNode rNode = (RouteResultsetNode) conn.getAttachment();
        String dataNode = rNode.getName();
        if (dataMergeSvr != null) {
            // even through discarding the all rest data, we can't
            //close the connection for tx control such as rollback or commit.
            // So the "isClosedByDiscard" variable is unnecessary.
            // @author Uncle-pan
            // @since 2016-03-25
            dataMergeSvr.onNewRecord(dataNode, row);
        } else {
            RowDataPacket rowDataPkg = null;
            // cache primaryKey-> dataNode
            if (primaryKeyIndex != -1) {
                rowDataPkg = new RowDataPacket(fieldCount);
                rowDataPkg.read(row);
                String primaryKey = new String(rowDataPkg.fieldValues.get(primaryKeyIndex));
                LayerCachePool pool = MycatServer.getInstance().getRouterservice().getTableId2DataNodeCache();
                pool.putIfAbsent(priamaryKeyTable, primaryKey, dataNode);
            }
            row[3] = ++packetId;
            if (prepared) {
                if (rowDataPkg == null) {
                    rowDataPkg = new RowDataPacket(fieldCount);
                    rowDataPkg.read(row);
                }
                BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
                binRowDataPk.read(fieldPackets, rowDataPkg);
                binRowDataPk.write(session.getSource());
            } else {
                session.getSource().write(row);
            }
        }
    } catch (Exception e) {
        handleDataProcessException(e);
    } finally {
        lock.unlock();
    }
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode) LayerCachePool(io.mycat.cache.LayerCachePool)

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