Search in sources :

Example 1 with BinaryPacket

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);
    }
}
Also used : ServerConnection(com.alibaba.cobar.server.ServerConnection) MySQLChannel(com.alibaba.cobar.mysql.bio.MySQLChannel) IOException(java.io.IOException) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 2 with BinaryPacket

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

Example 3 with BinaryPacket

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

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;
}
Also used : BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 5 with BinaryPacket

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;
}
Also used : CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Aggregations

BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)16 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)10 ErrorPacketException (com.alibaba.cobar.exception.ErrorPacketException)7 IOException (java.io.IOException)7 CommandPacket (com.alibaba.cobar.net.mysql.CommandPacket)6 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)6 ServerConnection (com.alibaba.cobar.server.ServerConnection)6 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)3 UnknownCharsetException (com.alibaba.cobar.exception.UnknownCharsetException)2 ByteBuffer (java.nio.ByteBuffer)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 UnknownTxIsolationException (com.alibaba.cobar.exception.UnknownTxIsolationException)1 Channel (com.alibaba.cobar.mysql.bio.Channel)1 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)1 HandshakePacket (com.alibaba.cobar.net.mysql.HandshakePacket)1 MySQLPacket (com.alibaba.cobar.net.mysql.MySQLPacket)1 OkPacket (com.alibaba.cobar.net.mysql.OkPacket)1 Reply323Packet (com.alibaba.cobar.net.mysql.Reply323Packet)1 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)1 LinkedList (java.util.LinkedList)1