use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.
the class BlockingSession method clear.
/**
* MUST be called at the end of {@link NodeExecutor}
*
* @param pessimisticRelease true if this method might be invoked
* concurrently with {@link #kill()}
*/
private void clear(boolean pessimisticRelease) {
for (RouteResultsetNode rrn : target.keySet()) {
Channel c = target.remove(rrn);
// 通道不存在或者已被关闭
if (c == null || c.isClosed()) {
continue;
}
// 如果通道正在运行中,则关闭当前通道。
if (c.isRunning() || (pessimisticRelease && source.isClosed())) {
c.close();
continue;
}
// 非事务中的通道,直接释放资源。
if (c.isAutocommit()) {
c.release();
continue;
}
// 事务中的通道,需要先回滚后再释放资源。
MySQLChannel mc = (MySQLChannel) c;
try {
BinaryPacket bin = mc.rollback();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
mc.release();
break;
case ErrorPacket.FIELD_COUNT:
mc.close();
break;
default:
throw new UnknownPacketException(bin.toString());
}
} catch (IOException e) {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(mc).append("rollback").toString(), e);
mc.close();
} catch (RuntimeException e) {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(mc).append("rollback").toString(), e);
mc.close();
}
}
}
use of com.alibaba.cobar.route.RouteResultsetNode 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.route.RouteResultsetNode in project cobar by alibaba.
the class DefaultCommitExecutor method commit.
/**
* 提交事务
*/
public void commit(final OkPacket packet, final BlockingSession session, final int initCount) {
// 初始化
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.isFail.set(false);
this.nodeCount = initCount;
this.indicatedOK = packet;
} finally {
lock.unlock();
}
if (session.getSource().isClosed()) {
decrementCountToZero();
return;
}
// 执行
final ConcurrentMap<RouteResultsetNode, Channel> target = session.getTarget();
Executor committer = session.getSource().getProcessor().getCommitter();
int started = 0;
for (RouteResultsetNode rrn : target.keySet()) {
if (rrn == null) {
try {
getLogger().error("null is contained in RoutResultsetNodes, source = " + session.getSource() + ", bindChannel = " + target);
} catch (Exception e) {
}
continue;
}
final MySQLChannel mc = (MySQLChannel) target.get(rrn);
if (mc != null) {
mc.setRunning(true);
committer.execute(new Runnable() {
@Override
public void run() {
_commit(mc, session);
}
});
++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.clear();
}
}
use of com.alibaba.cobar.route.RouteResultsetNode 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.route.RouteResultsetNode in project cobar by alibaba.
the class MultiNodeQueryHandler method connectionAcquired.
@Override
public void connectionAcquired(final MySQLConnection conn) {
Object attachment = conn.getAttachment();
if (!(attachment instanceof RouteResultsetNode)) {
backendConnError(conn, new StringBuilder().append("wrong attachement from connection build: ").append(conn).append(" bound by ").append(session.getSource()).toString());
conn.release();
return;
}
final RouteResultsetNode node = (RouteResultsetNode) attachment;
conn.setRunning(true);
session.bindConnection(node, conn);
session.getSource().getProcessor().getExecutor().execute(new Runnable() {
@Override
public void run() {
_execute(conn, node);
}
});
}
Aggregations