use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.
the class SingleNodeHandler method execute.
public void execute() throws Exception {
lock.lock();
try {
this.isRunning = true;
this.packetId = 0;
this.buffer = session.getSource().allocate();
} finally {
lock.unlock();
}
final MySQLConnection conn = session.getTarget(route);
if (conn == null) {
CobarConfig conf = CobarServer.getInstance().getConfig();
MySQLDataNode dn = conf.getDataNodes().get(route.getName());
dn.getConnection(this, null);
} else {
conn.setRunning(true);
session.getSource().getProcessor().getExecutor().execute(new Runnable() {
@Override
public void run() {
_execute(conn);
}
});
}
}
use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.
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;
}
// 执行
Executor executor = session.getSource().getProcessor().getExecutor();
int started = 0;
for (final RouteResultsetNode node : session.getTargetKeys()) {
if (node == null) {
try {
logger.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
} catch (Exception e) {
}
continue;
}
final MySQLConnection conn = session.getTarget(node);
if (conn != null) {
conn.setRunning(true);
executor.execute(new Runnable() {
@Override
public void run() {
if (isFail.get() || session.closed()) {
backendConnError(conn, "cancelled by other thread");
return;
}
conn.setResponseHandler(RollbackNodeHandler.this);
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.clearConnections();
}
}
use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.
the class NonBlockingSession method clearConnections.
private void clearConnections(boolean pessimisticRelease) {
for (RouteResultsetNode node : target.keySet()) {
MySQLConnection c = target.remove(node);
if (c == null || c.isClosedOrQuit()) {
continue;
}
// 如果通道正在运行中,则关闭当前通道。
if (c.isRunning() || (pessimisticRelease && source.isClosed())) {
c.close();
continue;
}
// 非事务中的通道,直接释放资源。
if (c.isAutocommit()) {
c.release();
continue;
}
c.setResponseHandler(new RollbackReleaseHandler());
c.rollback();
}
}
use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.
the class CommitNodeHandler method commit.
public void commit(OkPacket packet) {
final int initCount = session.getTargetCount();
lock.lock();
try {
reset(initCount);
okPacket = packet;
} finally {
lock.unlock();
}
if (session.closed()) {
decrementCountToZero();
return;
}
// 执行
Executor executor = session.getSource().getProcessor().getExecutor();
int started = 0;
for (RouteResultsetNode rrn : session.getTargetKeys()) {
if (rrn == null) {
try {
logger.error("null is contained in RoutResultsetNodes, source = " + session.getSource());
} catch (Exception e) {
}
continue;
}
final MySQLConnection conn = session.getTarget(rrn);
if (conn != null) {
conn.setRunning(true);
executor.execute(new Runnable() {
@Override
public void run() {
if (isFail.get() || session.closed()) {
backendConnError(conn, "cancelled by other thread");
return;
}
conn.setResponseHandler(CommitNodeHandler.this);
conn.commit();
}
});
++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.clearConnections();
}
}
use of com.alibaba.cobar.mysql.nio.MySQLConnection in project cobar by alibaba.
the class MultiNodeQueryHandler method execute.
public void execute() throws Exception {
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.reset(route.length);
this.fieldsReturned = false;
this.affectedRows = 0L;
this.insertId = 0L;
this.buffer = session.getSource().allocate();
} finally {
lock.unlock();
}
if (session.closed()) {
decrementCountToZero();
recycleResources();
return;
}
session.setConnectionRunning(route);
ThreadPoolExecutor executor = session.getSource().getProcessor().getExecutor();
for (final RouteResultsetNode node : route) {
final MySQLConnection conn = session.getTarget(node);
if (conn != null) {
conn.setAttachment(node);
executor.execute(new Runnable() {
@Override
public void run() {
_execute(conn, node);
}
});
} else {
CobarConfig conf = CobarServer.getInstance().getConfig();
MySQLDataNode dn = conf.getDataNodes().get(node.getName());
dn.getConnection(this, node);
}
}
}
Aggregations