use of com.alibaba.cobar.server.ServerConnection 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.server.ServerConnection in project cobar by alibaba.
the class MultiNodeQueryHandler method fieldEofResponse.
@Override
public void fieldEofResponse(byte[] header, List<byte[]> fields, byte[] eof, MySQLConnection conn) {
lock.lock();
try {
if (fieldsReturned) {
return;
}
fieldsReturned = true;
header[3] = ++packetId;
ServerConnection source = session.getSource();
buffer = source.writeToBuffer(header, buffer);
for (int i = 0, len = fields.size(); i < len; ++i) {
byte[] field = fields.get(i);
field[3] = ++packetId;
buffer = source.writeToBuffer(field, buffer);
}
eof[3] = ++packetId;
buffer = source.writeToBuffer(eof, buffer);
} finally {
lock.unlock();
}
}
use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.
the class RollbackExecutor method handleException.
private void handleException(Channel mc, BlockingSession session, Exception e) {
isFail.set(true);
if (decrementCountBy(1)) {
try {
session.clear();
ServerConnection source = session.getSource();
LOGGER.warn(new StringBuilder().append(source).append(mc).append("rollback").toString(), e);
String msg = e.getMessage();
source.writeErrMessage(ErrorCode.ER_YES, msg == null ? e.getClass().getSimpleName() : msg);
} catch (Exception e2) {
LOGGER.warn("exception happens in failure notification: " + session.getSource(), e2);
}
}
}
use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.
the class KillQueryHandler method handle.
public static void handle(String stmt, int offset, final ServerConnection c) {
String id = stmt.substring(offset).trim();
if (StringUtil.isEmpty(id)) {
c.writeErrMessage(ErrorCode.ER_NO_SUCH_THREAD, "NULL connection id");
} else {
// get value
long value = 0;
try {
value = Long.parseLong(id);
} catch (NumberFormatException e) {
c.writeErrMessage(ErrorCode.ER_NO_SUCH_THREAD, "Invalid connection id:" + id);
return;
}
// kill query itself
if (value == c.getId()) {
c.cancel(null);
return;
}
// get the connection and kill query
FrontendConnection fc = null;
NIOProcessor[] processors = CobarServer.getInstance().getProcessors();
for (NIOProcessor p : processors) {
if ((fc = p.getFrontends().get(value)) != null) {
break;
}
}
if (fc != null) {
if (fc instanceof ServerConnection) {
((ServerConnection) fc).cancel(c);
} else {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Unknown command");
}
} else {
c.writeErrMessage(ErrorCode.ER_NO_SUCH_THREAD, "Unknown connection id:" + id);
}
}
}
Aggregations