use of com.alibaba.cobar.route.RouteResultsetNode 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);
}
}
}
use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.
the class MultiNodeQueryHandler method okResponse.
@Override
public void okResponse(byte[] data, MySQLConnection conn) {
boolean executeResponse = false;
try {
executeResponse = conn.syncAndExcute();
} catch (UnsupportedEncodingException e) {
connectionError(e, conn);
}
if (executeResponse) {
ServerConnection source = session.getSource();
conn.setRunning(false);
Object attachment = conn.getAttachment();
if (attachment instanceof RouteResultsetNode) {
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));
}
OkPacket ok = new OkPacket();
ok.read(data);
lock.lock();
try {
affectedRows += ok.affectedRows;
if (ok.insertId > 0) {
insertId = (insertId == 0) ? ok.insertId : Math.min(insertId, ok.insertId);
}
} finally {
lock.unlock();
}
if (decrementCountBy(1)) {
if (isFail.get()) {
notifyError();
return;
}
try {
recycleResources();
// OK_PACKET
ok.packetId = ++packetId;
ok.affectedRows = affectedRows;
if (insertId > 0) {
ok.insertId = insertId;
source.setLastInsertId(insertId);
}
if (source.isAutocommit()) {
if (!autocommit) {
// 前端非事务模式,后端事务模式,则需要自动递交后端事务。
icHandler.commit();
} else {
session.releaseConnections();
ok.write(source);
}
} else {
ok.write(source);
}
} 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 NonBlockingSession method execute.
@Override
public void execute(RouteResultset rrs, int type) {
if (LOGGER.isDebugEnabled()) {
StringBuilder s = new StringBuilder();
LOGGER.debug(s.append(source).append(rrs).toString());
}
// 检查路由结果是否为空
RouteResultsetNode[] nodes = rrs.getNodes();
if (nodes == null || nodes.length == 0) {
source.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No dataNode selected");
return;
}
if (nodes.length == 1) {
singleNodeHandler = new SingleNodeHandler(nodes[0], this);
// singleNodeHandler.execute();
} else {
boolean autocommit = source.isAutocommit();
if (autocommit && isModifySQL(type)) {
autocommit = false;
}
multiNodeHandler = new MultiNodeQueryHandler(nodes, autocommit, this);
// multiNodeHandler.execute();
}
}
use of com.alibaba.cobar.route.RouteResultsetNode in project cobar by alibaba.
the class NonBlockingSession method kill.
private void kill(Runnable run) {
boolean hooked = false;
AtomicInteger count = null;
Map<RouteResultsetNode, MySQLConnection> killees = null;
for (RouteResultsetNode node : target.keySet()) {
MySQLConnection c = target.get(node);
if (c != null && c.isRunning()) {
if (!hooked) {
hooked = true;
killees = new HashMap<RouteResultsetNode, MySQLConnection>();
count = new AtomicInteger(0);
}
killees.put(node, c);
count.incrementAndGet();
}
}
if (hooked) {
for (Entry<RouteResultsetNode, MySQLConnection> en : killees.entrySet()) {
KillConnectionHandler kill = new KillConnectionHandler(en.getValue(), this, run, count);
CobarConfig conf = CobarServer.getInstance().getConfig();
MySQLDataNode dn = conf.getDataNodes().get(en.getKey().getName());
try {
dn.getConnection(kill, en.getKey());
} catch (Exception e) {
LOGGER.error("get killer connection failed for " + en.getKey(), e);
kill.connectionError(e, null);
}
}
} else {
run.run();
}
}
Aggregations