use of com.alibaba.cobar.net.mysql.BinaryPacket 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.net.mysql.BinaryPacket 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.net.mysql.BinaryPacket in project cobar by alibaba.
the class MySQLChannel method sendSqlMode.
/**
* 发送SQL_MODE设置
*/
private void sendSqlMode() throws IOException {
CommandPacket cmd = getSqlModeCommand();
cmd.write(out);
out.flush();
BinaryPacket bin = receive();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(bin);
throw new ErrorPacketException(new String(err.message, charset));
default:
throw new UnknownPacketException(bin.toString());
}
}
use of com.alibaba.cobar.net.mysql.BinaryPacket in project cobar by alibaba.
the class MySQLChannel method receive.
public BinaryPacket receive() throws IOException {
BinaryPacket bin = new BinaryPacket();
bin.read(in);
return bin;
}
use of com.alibaba.cobar.net.mysql.BinaryPacket in project cobar by alibaba.
the class MySQLChannel method execute.
public BinaryPacket execute(RouteResultsetNode rrn, ServerConnection sc, boolean autocommit) throws IOException {
// 状态一致性检查
if (this.charsetIndex != sc.getCharsetIndex()) {
/*如果后端MySQL服务器配置了character_set_server=utf8mb4,
并且数据库客户端要求使用uft8,
我们将不发送客户端的字符集要求。
这种情况在我们使用mysql官方的Connector/J编程的时候遇见,
详细见:http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-troubleshooting.html#qandaitem-15-1-15
https://dev.mysql.com/doc/relnotes/connector-j/en/news-5-1-13.html
*/
if (this.charsetIndex != 45 && sc.getCharsetIndex() != 33)
sendCharset(sc.getCharsetIndex());
}
if (this.txIsolation != sc.getTxIsolation()) {
sendTxIsolation(sc.getTxIsolation());
}
if (this.autocommit != autocommit) {
sendAutocommit(autocommit);
}
// 生成执行数据包
CommandPacket packet = new CommandPacket();
packet.packetId = 0;
packet.command = MySQLPacket.COM_QUERY;
packet.arg = rrn.getStatement().getBytes(charset);
// 记录执行开始时间
lastActiveTime = TimeUtil.currentTimeMillis();
// 递交执行数据包并等待执行返回
packet.write(out);
out.flush();
BinaryPacket bin = receive();
// SQL执行时间统计
long now = TimeUtil.currentTimeMillis();
if (now > lastActiveTime) {
recordSql(sc.getHost(), sc.getSchema(), rrn.getStatement());
}
// 记录执行结束时间
lastActiveTime = now;
return bin;
}
Aggregations