Search in sources :

Example 1 with UnknownPacketException

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());
    }
}
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 2 with UnknownPacketException

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());
    }
}
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 3 with UnknownPacketException

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);
    }
}
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 4 with UnknownPacketException

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();
        }
    }
}
Also used : UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) Channel(com.alibaba.cobar.mysql.bio.Channel) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) IOException(java.io.IOException) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 5 with UnknownPacketException

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;
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) UnknownCharsetException(com.alibaba.cobar.exception.UnknownCharsetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) 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