Search in sources :

Example 6 with UnknownPacketException

use of com.alibaba.cobar.exception.UnknownPacketException in project cobar by alibaba.

the class MySQLChannel method sendCharset.

/**
     * 发送字符集设置
     */
private void sendCharset(int ci) throws IOException {
    // 发送命令: 直接写入到out中即可
    CommandPacket cmd = getCharsetCommand(ci);
    cmd.write(out);
    out.flush();
    BinaryPacket bin = receive();
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            this.charsetIndex = ci;
            this.charset = CharsetUtil.getCharset(ci);
            this.dbCharset = CharsetUtil.getCharset(ci);
            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());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 7 with UnknownPacketException

use of com.alibaba.cobar.exception.UnknownPacketException in project cobar by alibaba.

the class MySQLChannel method sendAutocommit.

/**
     * 发送事务递交模式设置
     */
private void sendAutocommit(boolean autocommit) throws IOException {
    CommandPacket cmd = getAutocommitCommand(autocommit);
    cmd.write(out);
    out.flush();
    BinaryPacket bin = receive();
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            this.autocommit = autocommit;
            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());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 8 with UnknownPacketException

use of com.alibaba.cobar.exception.UnknownPacketException 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);
    }
}
Also used : UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 9 with UnknownPacketException

use of com.alibaba.cobar.exception.UnknownPacketException in project cobar by alibaba.

the class MySQLChannel method auth323.

/**
     * 323协议认证
     */
private void auth323(byte packetId, byte[] seed) throws IOException {
    Reply323Packet r323 = new Reply323Packet();
    r323.packetId = ++packetId;
    String passwd = dsc.getPassword();
    if (passwd != null && passwd.length() > 0) {
        r323.seed = SecurityUtil.scramble323(passwd, new String(seed)).getBytes();
    }
    r323.write(out);
    out.flush();
    BinaryPacket bin = receive();
    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));
        default:
            throw new UnknownPacketException(bin.toString());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) Reply323Packet(com.alibaba.cobar.net.mysql.Reply323Packet) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Aggregations

UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)9 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)9 ErrorPacketException (com.alibaba.cobar.exception.ErrorPacketException)6 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)6 CommandPacket (com.alibaba.cobar.net.mysql.CommandPacket)4 IOException (java.io.IOException)3 ServerConnection (com.alibaba.cobar.server.ServerConnection)2 ByteBuffer (java.nio.ByteBuffer)2 UnknownCharsetException (com.alibaba.cobar.exception.UnknownCharsetException)1 Channel (com.alibaba.cobar.mysql.bio.Channel)1 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)1 HandshakePacket (com.alibaba.cobar.net.mysql.HandshakePacket)1 Reply323Packet (com.alibaba.cobar.net.mysql.Reply323Packet)1 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1