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();
}
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);
}
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);
}
}
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());
}
}
}
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();
}
Aggregations