use of com.alibaba.cobar.exception.UnknownPacketException 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.exception.UnknownPacketException in project cobar by alibaba.
the class MySQLChannel method sendTxIsolation.
/**
* 发送事务级别设置
*/
private void sendTxIsolation(int txIsolation) throws IOException {
CommandPacket cmd = getTxIsolationCommand(txIsolation);
cmd.write(out);
out.flush();
BinaryPacket bin = receive();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
this.txIsolation = txIsolation;
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.exception.UnknownPacketException in project cobar by alibaba.
the class RollbackExecutor method _rollback.
private void _rollback(MySQLChannel mc, BlockingSession session) {
final ServerConnection source = session.getSource();
if (isFail.get() || source.isClosed()) {
mc.setRunning(false);
try {
throw new Exception("other task fails, rollback failed channel");
} catch (Exception e) {
handleException(mc, session, e);
}
return;
}
try {
BinaryPacket bin = mc.rollback();
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, "rollback error!");
} else {
// all tasks are successful
session.release();
ByteBuffer buffer = source.allocate();
source.write(bin.write(buffer, source));
}
} catch (Exception e) {
LOGGER.warn("exception happens in success notification: " + source, e);
}
}
break;
case ErrorPacket.FIELD_COUNT:
isFail.set(true);
if (decrementCountBy(1)) {
try {
session.clear();
LOGGER.warn(mc.getErrLog("rollback", mc.getErrMessage(bin), source));
ByteBuffer buffer = source.allocate();
source.write(bin.write(buffer, source));
} catch (Exception e) {
LOGGER.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);
}
}
use of com.alibaba.cobar.exception.UnknownPacketException in project cobar by alibaba.
the class BlockingSession method clear.
/**
* MUST be called at the end of {@link NodeExecutor}
*
* @param pessimisticRelease true if this method might be invoked
* concurrently with {@link #kill()}
*/
private void clear(boolean pessimisticRelease) {
for (RouteResultsetNode rrn : target.keySet()) {
Channel c = target.remove(rrn);
// 通道不存在或者已被关闭
if (c == null || c.isClosed()) {
continue;
}
// 如果通道正在运行中,则关闭当前通道。
if (c.isRunning() || (pessimisticRelease && source.isClosed())) {
c.close();
continue;
}
// 非事务中的通道,直接释放资源。
if (c.isAutocommit()) {
c.release();
continue;
}
// 事务中的通道,需要先回滚后再释放资源。
MySQLChannel mc = (MySQLChannel) c;
try {
BinaryPacket bin = mc.rollback();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
mc.release();
break;
case ErrorPacket.FIELD_COUNT:
mc.close();
break;
default:
throw new UnknownPacketException(bin.toString());
}
} catch (IOException e) {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(mc).append("rollback").toString(), e);
mc.close();
} catch (RuntimeException e) {
StringBuilder s = new StringBuilder();
LOGGER.warn(s.append(mc).append("rollback").toString(), e);
mc.close();
}
}
}
use of com.alibaba.cobar.exception.UnknownPacketException in project cobar by alibaba.
the class MySQLChannel method handshake.
private MySQLChannel handshake() throws IOException {
// 读取握手数据包
BinaryPacket initPacket = new BinaryPacket();
initPacket.read(in);
HandshakePacket hsp = new HandshakePacket();
hsp.read(initPacket);
// 设置通道参数
this.threadId = hsp.threadId;
int ci = hsp.serverCharsetIndex & 0xff;
if ((dbCharset = CharsetUtil.getDbCharset(ci)) != null) {
this.charsetIndex = ci;
this.charset = CharsetUtil.getCharset(ci);
} else {
throw new UnknownCharsetException("charset:" + ci);
}
// 发送认证数据包
BinaryPacket bin = null;
try {
bin = sendAuth411(hsp);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e.getMessage());
}
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
afterSuccess();
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(bin);
throw new ErrorPacketException(new String(err.message, charset));
case EOFPacket.FIELD_COUNT:
auth323(bin.packetId, hsp.seed);
break;
default:
throw new UnknownPacketException(bin.toString());
}
return this;
}
Aggregations