Search in sources :

Example 11 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection 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 12 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.

the class SingleNodeExecutor method handleError.

/**
 * 执行异常处理
 */
private void handleError(int errno, String message, BlockingSession ss) {
    endRunning();
    // 清理
    ss.clear();
    ServerConnection sc = ss.getSource();
    sc.setTxInterrupt();
    // 通知
    ErrorPacket err = new ErrorPacket();
    // ERROR_PACKET
    err.packetId = ++packetId;
    err.errno = errno;
    err.message = StringUtil.encode(message, sc.getCharset());
    err.write(sc);
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ServerConnection(com.alibaba.cobar.server.ServerConnection)

Example 13 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection 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 14 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection in project cobar by alibaba.

the class MultiNodeQueryHandler method rowEofResponse.

@Override
public void rowEofResponse(byte[] eof, MySQLConnection conn) {
    conn.setRunning(false);
    ServerConnection source = session.getSource();
    RouteResultsetNode node = null;
    Object attachment = conn.getAttachment();
    if (attachment instanceof RouteResultsetNode) {
        node = (RouteResultsetNode) attachment;
        conn.recordSql(source.getHost(), source.getSchema(), node.getStatement());
    } else {
        LOGGER.warn(new StringBuilder().append("back-end conn: ").append(conn).append(" has wrong attachment: ").append(attachment).append(", for front-end conn: ").append(source));
    }
    if (source.isAutocommit()) {
        if (node != null) {
            conn = session.removeTarget(node);
            if (conn != null) {
                if (isFail.get() || session.closed()) {
                    conn.quit();
                } else {
                    conn.release();
                }
            }
        }
    }
    if (decrementCountBy(1)) {
        if (isFail.get()) {
            notifyError();
            recycleResources();
            return;
        }
        try {
            if (source.isAutocommit()) {
                session.releaseConnections();
            }
            eof[3] = ++packetId;
            source.write(source.writeToBuffer(eof, buffer));
        } catch (Exception e) {
            LOGGER.warn("exception happens in success notification: " + session.getSource(), e);
        }
    }
}
Also used : RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) ServerConnection(com.alibaba.cobar.server.ServerConnection) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 15 with ServerConnection

use of com.alibaba.cobar.server.ServerConnection 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)

Aggregations

ServerConnection (com.alibaba.cobar.server.ServerConnection)29 IOException (java.io.IOException)16 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)6 UnknownDataNodeException (com.alibaba.cobar.exception.UnknownDataNodeException)5 MySQLChannel (com.alibaba.cobar.mysql.bio.MySQLChannel)5 ByteBuffer (java.nio.ByteBuffer)5 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)4 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)4 Channel (com.alibaba.cobar.mysql.bio.Channel)3 OkPacket (com.alibaba.cobar.net.mysql.OkPacket)3 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 CobarConfig (com.alibaba.cobar.CobarConfig)2 MySQLDataNode (com.alibaba.cobar.mysql.MySQLDataNode)2 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)2 MySQLPacket (com.alibaba.cobar.net.mysql.MySQLPacket)2 LinkedList (java.util.LinkedList)2 FrontendConnection (com.alibaba.cobar.net.FrontendConnection)1 NIOProcessor (com.alibaba.cobar.net.NIOProcessor)1