use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.
the class MultiNodeQueryHandler method rowEofResponse.
@Override
public void rowEofResponse(byte[] eof, MySQLConnection conn) {
conn.setRunning(false);
ServerConnection source = session.getSource();
RouteResultsetNode node = null;
Object attachment = conn.getAttachment();
if (attachment instanceof RouteResultsetNode) {
node = (RouteResultsetNode) attachment;
conn.recordSql(source.getHost(), source.getSchema(), node.getStatement());
} else {
LOGGER.warn(new StringBuilder().append("back-end conn: ").append(conn).append(" has wrong attachment: ").append(attachment).append(", for front-end conn: ").append(source));
}
if (source.isAutocommit()) {
if (node != null) {
conn = session.removeTarget(node);
if (conn != null) {
if (isFail.get() || session.closed()) {
conn.quit();
} else {
conn.release();
}
}
}
}
if (decrementCountBy(1)) {
if (isFail.get()) {
notifyError();
recycleResources();
return;
}
try {
if (source.isAutocommit()) {
session.releaseConnections();
}
eof[3] = ++packetId;
source.write(source.writeToBuffer(eof, buffer));
} catch (Exception e) {
LOGGER.warn("exception happens in success notification: " + session.getSource(), e);
}
}
}
use of com.alibaba.cobar.route.RouteResultsetNode 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.route.RouteResultsetNode in project cobar by alibaba.
the class ExplainHandler method handle.
public static void handle(String stmt, ServerConnection c, int offset) {
stmt = stmt.substring(offset);
RouteResultset rrs = getRouteResultset(c, stmt);
if (rrs == null)
return;
ByteBuffer buffer = c.allocate();
// write header
ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
byte packetId = header.packetId;
buffer = header.write(buffer, c);
// write fields
for (FieldPacket field : fields) {
field.packetId = ++packetId;
buffer = field.write(buffer, c);
}
// write eof
EOFPacket eof = new EOFPacket();
eof.packetId = ++packetId;
buffer = eof.write(buffer, c);
// write rows
RouteResultsetNode[] rrsn = (rrs != null) ? rrs.getNodes() : EMPTY_ARRAY;
for (RouteResultsetNode node : rrsn) {
RowDataPacket row = getRow(node, c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c);
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c);
// post write
c.write(buffer);
}
use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.
the class MultiNodeExecutor method execute.
/**
* 多数据节点执行
*
* @param nodes never null
*/
public void execute(RouteResultsetNode[] nodes, final boolean autocommit, final BlockingSession ss, final int flag) {
// 初始化
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.isFail.set(false);
this.unfinishedNodeCount = nodes.length;
this.errno = 0;
this.errMessage = null;
this.fieldEOF = false;
this.packetId = 0;
this.affectedRows = 0L;
this.insertId = 0L;
this.buffer = ss.getSource().allocate();
} finally {
lock.unlock();
}
if (ss.getSource().isClosed()) {
decrementCountToZero();
ss.getSource().recycle(this.buffer);
return;
}
// 多节点处理
ConcurrentMap<RouteResultsetNode, Channel> target = ss.getTarget();
for (RouteResultsetNode rrn : nodes) {
Channel c = target.get(rrn);
if (c != null) {
c.setRunning(true);
}
}
ThreadPoolExecutor exec = ss.getSource().getProcessor().getExecutor();
for (final RouteResultsetNode rrn : nodes) {
final Channel c = target.get(rrn);
if (c != null) {
exec.execute(new Runnable() {
@Override
public void run() {
execute0(rrn, c, autocommit, ss, flag);
}
});
} else {
newExecute(rrn, autocommit, ss, flag);
}
}
}
use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.
the class RollbackExecutor method rollback.
/**
* 事务回滚
*/
public void rollback(final BlockingSession session) {
final ServerConnection source = session.getSource();
final ConcurrentMap<RouteResultsetNode, Channel> target = session.getTarget();
final int initNodeCount = target.size();
if (initNodeCount <= 0) {
ByteBuffer buffer = source.allocate();
source.write(source.writeToBuffer(OkPacket.OK, buffer));
return;
}
// 初始化
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.isFail.set(false);
this.nodeCount = initNodeCount;
} finally {
lock.unlock();
}
if (source.isClosed()) {
decrementCountToZero();
return;
}
// 执行
Executor committer = source.getProcessor().getCommitter();
int started = 0;
for (RouteResultsetNode rrn : target.keySet()) {
final MySQLChannel mc = (MySQLChannel) target.get(rrn);
if (mc != null) {
mc.setRunning(true);
committer.execute(new Runnable() {
@Override
public void run() {
_rollback(mc, session);
}
});
++started;
}
}
if (started < initNodeCount) {
decrementCountBy(initNodeCount - started);
}
}
Aggregations