Search in sources :

Example 1 with MySQLConnection

use of io.mycat.backend.mysql.nio.MySQLConnection in project Mycat_plus by coderczp.

the class ConMap method clearConnections.

public void clearConnections(String reason, PhysicalDatasource dataSouce) {
    for (NIOProcessor processor : MycatServer.getInstance().getProcessors()) {
        ConcurrentMap<Long, BackendConnection> map = processor.getBackends();
        Iterator<Entry<Long, BackendConnection>> itor = map.entrySet().iterator();
        while (itor.hasNext()) {
            Entry<Long, BackendConnection> entry = itor.next();
            BackendConnection con = entry.getValue();
            if (con instanceof MySQLConnection) {
                if (((MySQLConnection) con).getPool() == dataSouce) {
                    con.close(reason);
                    itor.remove();
                }
            } else if ((con instanceof JDBCConnection) && (((JDBCConnection) con).getPool() == dataSouce)) {
                con.close(reason);
                itor.remove();
            }
        }
    }
    items.clear();
}
Also used : Entry(java.util.Map.Entry) NIOProcessor(io.mycat.net.NIOProcessor) JDBCConnection(io.mycat.backend.jdbc.JDBCConnection) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Example 2 with MySQLConnection

use of io.mycat.backend.mysql.nio.MySQLConnection in project Mycat-Server by MyCATApache.

the class KillConnectionHandler method connectionAcquired.

@Override
public void connectionAcquired(BackendConnection conn) {
    MySQLConnection mysqlCon = (MySQLConnection) conn;
    conn.setResponseHandler(this);
    CommandPacket packet = new CommandPacket();
    packet.packetId = 0;
    packet.command = MySQLPacket.COM_QUERY;
    packet.arg = new StringBuilder("KILL ").append(killee.getThreadId()).toString().getBytes();
    packet.write(mysqlCon);
}
Also used : CommandPacket(io.mycat.net.mysql.CommandPacket) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Example 3 with MySQLConnection

use of io.mycat.backend.mysql.nio.MySQLConnection 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
            if (conn instanceof MySQLConnection) {
                MySQLConnection mysqlCon = (MySQLConnection) conn;
                String xaTxId = null;
                if (session.getXaTXID() != null) {
                    xaTxId = session.getXaTXID() + ",'" + mysqlCon.getSchema() + "'";
                }
                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);
                }
            } else {
                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 MySQLConnection

use of io.mycat.backend.mysql.nio.MySQLConnection in project Mycat-Server by MyCATApache.

the class MultiNodeCoordinator method errorResponse.

@Override
public void errorResponse(byte[] err, BackendConnection conn) {
    faileCount.incrementAndGet();
    // replayCommit
    if (conn instanceof MySQLConnection) {
        MySQLConnection mysqlCon = (MySQLConnection) conn;
        String xaTxId = session.getXaTXID();
        if (xaTxId != null) {
            xaTxId += ",'" + mysqlCon.getSchema() + "'";
            String cmd = "XA COMMIT " + xaTxId;
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Replay Commit execute the cmd :" + cmd + ",current host:" + mysqlCon.getHost() + ":" + mysqlCon.getPort());
            }
            mysqlCon.execCmd(cmd);
        }
    }
    // release connection
    if (this.cmdHandler.releaseConOnErr()) {
        session.releaseConnection(conn);
    } else {
        session.releaseConnectionIfSafe(conn, LOGGER.isDebugEnabled(), false);
    }
    if (this.finished()) {
        cmdHandler.errorResponse(session, err, this.nodeCount, this.faileCount.get());
        if (cmdHandler.isAutoClearSessionCons()) {
            session.clearResources(session.getSource().isTxInterrupted());
        }
    }
}
Also used : MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Example 5 with MySQLConnection

use of io.mycat.backend.mysql.nio.MySQLConnection in project Mycat-Server by MyCATApache.

the class ConMap method clearConnections.

public void clearConnections(String reason, PhysicalDatasource dataSouce) {
    for (NIOProcessor processor : MycatServer.getInstance().getProcessors()) {
        ConcurrentMap<Long, BackendConnection> map = processor.getBackends();
        Iterator<Entry<Long, BackendConnection>> itor = map.entrySet().iterator();
        while (itor.hasNext()) {
            Entry<Long, BackendConnection> entry = itor.next();
            BackendConnection con = entry.getValue();
            if (con instanceof MySQLConnection) {
                if (((MySQLConnection) con).getPool() == dataSouce) {
                    con.close(reason);
                    itor.remove();
                }
            } else if ((con instanceof JDBCConnection) && (((JDBCConnection) con).getPool() == dataSouce)) {
                con.close(reason);
                itor.remove();
            }
        }
    }
    items.clear();
}
Also used : Entry(java.util.Map.Entry) NIOProcessor(io.mycat.net.NIOProcessor) JDBCConnection(io.mycat.backend.jdbc.JDBCConnection) MySQLConnection(io.mycat.backend.mysql.nio.MySQLConnection)

Aggregations

MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)22 BackendConnection (io.mycat.backend.BackendConnection)6 JDBCConnection (io.mycat.backend.jdbc.JDBCConnection)6 CoordinatorLogEntry (io.mycat.backend.mysql.xa.CoordinatorLogEntry)4 NIOProcessor (io.mycat.net.NIOProcessor)4 RowDataPacket (io.mycat.net.mysql.RowDataPacket)4 RouteResultsetNode (io.mycat.route.RouteResultsetNode)4 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)2 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)2 ParticipantLogEntry (io.mycat.backend.mysql.xa.ParticipantLogEntry)2 ConfigInitializer (io.mycat.config.ConfigInitializer)2 MycatCluster (io.mycat.config.MycatCluster)2 MycatConfig (io.mycat.config.MycatConfig)2 FirewallConfig (io.mycat.config.model.FirewallConfig)2 SchemaConfig (io.mycat.config.model.SchemaConfig)2 UserConfig (io.mycat.config.model.UserConfig)2 BackendAIOConnection (io.mycat.net.BackendAIOConnection)2 CommandPacket (io.mycat.net.mysql.CommandPacket)2 ServerConnection (io.mycat.server.ServerConnection)2