use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.
the class SingleNodeExecutor method handleRowData.
/**
* 处理RowData数据
*/
private void handleRowData(RouteResultsetNode rrn, BlockingSession ss, MySQLChannel mc, ByteBuffer bb, byte id) throws IOException {
final ServerConnection sc = ss.getSource();
this.packetId = id;
BinaryPacket bin = null;
int size = 0;
try {
for (; ; ) {
bin = mc.receive();
switch(bin.data[0]) {
case ErrorPacket.FIELD_COUNT:
LOGGER.warn(mc.getErrLog(rrn.getStatement(), mc.getErrMessage(bin), sc));
mc.setRunning(false);
if (mc.isAutocommit()) {
ss.clear();
}
endRunning();
// ERROR_PACKET
bin.packetId = ++packetId;
bb = bin.write(bb, sc);
sc.write(bb);
return;
case EOFPacket.FIELD_COUNT:
mc.setRunning(false);
if (mc.isAutocommit()) {
ss.clear();
}
endRunning();
// LAST_EOF
bin.packetId = ++packetId;
bb = bin.write(bb, sc);
sc.write(bb);
return;
default:
// ROWS
bin.packetId = ++packetId;
bb = bin.write(bb, sc);
size += bin.packetLength;
if (size > RECEIVE_CHUNK_SIZE) {
handleNext(rrn, ss, mc, bb, packetId);
return;
}
}
}
} catch (IOException e) {
sc.recycle(bb);
throw e;
}
}
use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.
the class SingleNodeExecutor method handleError.
/**
* 执行异常处理
*/
private void handleError(int errno, String message, BlockingSession ss) {
endRunning();
// 清理
ss.clear();
ServerConnection sc = ss.getSource();
sc.setTxInterrupt();
// 通知
ErrorPacket err = new ErrorPacket();
// ERROR_PACKET
err.packetId = ++packetId;
err.errno = errno;
err.message = StringUtil.encode(message, sc.getCharset());
err.write(sc);
}
use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.
the class SingleNodeExecutor method execute0.
/**
* 数据通道执行
*/
private void execute0(RouteResultsetNode rrn, BlockingSession ss, Channel c, int flag) {
final ServerConnection sc = ss.getSource();
// 检查连接是否已关闭
if (sc.isClosed()) {
c.setRunning(false);
endRunning();
ss.clear();
return;
}
try {
// 执行并等待返回
MySQLChannel mc = (MySQLChannel) c;
BinaryPacket bin = mc.execute(rrn, sc, sc.isAutocommit());
// 接收和处理数据
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
{
mc.setRunning(false);
if (mc.isAutocommit()) {
ss.clear();
}
endRunning();
// OK_PACKET
bin.packetId = ++packetId;
// set lastInsertId
setLastInsertId(bin, sc);
sc.write(bin.write(sc.allocate(), sc));
break;
}
case ErrorPacket.FIELD_COUNT:
{
LOGGER.warn(mc.getErrLog(rrn.getStatement(), mc.getErrMessage(bin), sc));
mc.setRunning(false);
if (mc.isAutocommit()) {
ss.clear();
}
endRunning();
// ERROR_PACKET
bin.packetId = ++packetId;
sc.write(bin.write(sc.allocate(), sc));
break;
}
default:
// HEADER|FIELDS|FIELD_EOF|ROWS|LAST_EOF
handleResultSet(rrn, ss, mc, bin, flag);
}
} catch (IOException e) {
LOGGER.warn(new StringBuilder().append(sc).append(rrn).toString(), e);
c.close();
String msg = e.getMessage();
handleError(ErrorCode.ER_YES, msg == null ? e.getClass().getSimpleName() : msg, ss);
} catch (RuntimeException e) {
LOGGER.warn(new StringBuilder().append(sc).append(rrn).toString(), e);
c.close();
String msg = e.getMessage();
handleError(ErrorCode.ER_YES, msg == null ? e.getClass().getSimpleName() : msg, ss);
}
}
use of com.alibaba.cobar.server.ServerConnection 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.server.ServerConnection in project cobar by alibaba.
the class DefaultCommitExecutor method _commit.
private void _commit(MySQLChannel mc, BlockingSession session) {
ServerConnection source = session.getSource();
if (isFail.get() || source.isClosed()) {
mc.setRunning(false);
try {
throw new Exception("other task fails, commit cancel");
} catch (Exception e) {
handleException(mc, session, e);
}
return;
}
try {
BinaryPacket bin = mc.commit();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
mc.setRunning(false);
if (decrementCountBy(1)) {
try {
if (isFail.get()) {
// some other tasks failed
session.clear();
source.writeErrMessage(ErrorCode.ER_YES, getErrorMessage() + " error!");
} else {
// all tasks are successful
session.release();
if (indicatedOK != null) {
indicatedOK.write(source);
} else {
ByteBuffer buffer = source.allocate();
source.write(bin.write(buffer, source));
}
}
} catch (Exception e) {
getLogger().warn("exception happens in success notification: " + source, e);
}
}
break;
case ErrorPacket.FIELD_COUNT:
mc.setRunning(false);
isFail.set(true);
if (decrementCountBy(1)) {
try {
session.clear();
getLogger().warn(mc.getErrLog(getErrorMessage(), mc.getErrMessage(bin), source));
ByteBuffer buffer = source.allocate();
source.write(bin.write(buffer, source));
} catch (Exception e) {
getLogger().warn("exception happens in failure notification: " + source, e);
}
}
break;
default:
throw new UnknownPacketException(bin.toString());
}
} catch (IOException e) {
mc.close();
handleException(mc, session, e);
} catch (RuntimeException e) {
mc.close();
handleException(mc, session, e);
}
}
Aggregations