Search in sources :

Example 1 with ServerConnection

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

the class SingleNodeHandler method okResponse.

@Override
public void okResponse(byte[] data, MySQLConnection conn) {
    boolean executeResponse = false;
    try {
        executeResponse = conn.syncAndExcute();
    } catch (UnsupportedEncodingException e) {
        executeException(conn);
    }
    if (executeResponse) {
        conn.setRunning(false);
        ServerConnection source = session.getSource();
        if (source.isAutocommit()) {
            session.clearConnections();
        }
        endRunning();
        OkPacket ok = new OkPacket();
        ok.read(data);
        source.setLastInsertId(ok.insertId);
        buffer = source.writeToBuffer(data, buffer);
        source.write(buffer);
    }
}
Also used : OkPacket(com.alibaba.cobar.net.mysql.OkPacket) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServerConnection(com.alibaba.cobar.server.ServerConnection)

Example 2 with ServerConnection

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

the class SingleNodeHandler method connectionError.

@Override
public void connectionError(Throwable e, MySQLConnection conn) {
    if (!session.closeConnection(route)) {
        conn.close();
    }
    endRunning();
    ErrorPacket err = new ErrorPacket();
    err.packetId = ++packetId;
    err.errno = ErrorCode.ER_YES;
    err.message = StringUtil.encode(e.getMessage(), session.getSource().getCharset());
    ServerConnection source = session.getSource();
    source.write(err.write(buffer, source));
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ServerConnection(com.alibaba.cobar.server.ServerConnection)

Example 3 with ServerConnection

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

the class SingleNodeHandler method fieldEofResponse.

@Override
public void fieldEofResponse(byte[] header, List<byte[]> fields, byte[] eof, MySQLConnection conn) {
    ServerConnection source = session.getSource();
    buffer = session.getSource().allocate();
    ++packetId;
    buffer = source.writeToBuffer(header, buffer);
    for (int i = 0, len = fields.size(); i < len; ++i) {
        ++packetId;
        buffer = source.writeToBuffer(fields.get(i), buffer);
    }
    ++packetId;
    buffer = source.writeToBuffer(eof, buffer);
    source.write(buffer);
}
Also used : ServerConnection(com.alibaba.cobar.server.ServerConnection)

Example 4 with ServerConnection

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

the class MultiNodeExecutor method handleSuccessOK.

/**
 * @throws nothing never throws any exception
 */
private void handleSuccessOK(BlockingSession ss, RouteResultsetNode rrn, boolean autocommit, OkPacket ok) {
    if (decrementCountAndIsZero()) {
        if (isFail.get()) {
            notifyFailure(ss);
            return;
        }
        try {
            ServerConnection source = ss.getSource();
            // OK_PACKET
            ok.packetId = ++packetId;
            ok.affectedRows = affectedRows;
            if (insertId > 0) {
                ok.insertId = insertId;
                source.setLastInsertId(insertId);
            }
            if (source.isAutocommit()) {
                if (!autocommit) {
                    // 前端非事务模式,后端事务模式,则需要自动递交后端事务。
                    icExecutor.commit(ok, ss, ss.getTarget().size());
                } else {
                    ss.release();
                    ok.write(source);
                }
            } else {
                ok.write(source);
            }
            source.recycle(buffer);
        } catch (Exception e) {
            LOGGER.warn("exception happens in success notification: " + ss.getSource(), e);
        }
    }
}
Also used : ServerConnection(com.alibaba.cobar.server.ServerConnection) UnknownDataNodeException(com.alibaba.cobar.exception.UnknownDataNodeException) IOException(java.io.IOException)

Example 5 with ServerConnection

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

the class MultiNodeExecutor method handleRowData.

/**
 * 处理RowData数据
 */
private void handleRowData(final RouteResultsetNode rrn, Channel c, BlockingSession ss) throws IOException {
    final ServerConnection source = ss.getSource();
    BinaryPacket bin = null;
    int size = 0;
    for (; ; ) {
        bin = ((MySQLChannel) c).receive();
        switch(bin.data[0]) {
            case ErrorPacket.FIELD_COUNT:
                c.setRunning(false);
                handleFailure(ss, rrn, new BinaryErrInfo(((MySQLChannel) c), bin, source, rrn));
                return;
            case EOFPacket.FIELD_COUNT:
                c.setRunning(false);
                if (source.isAutocommit()) {
                    c = ss.getTarget().remove(rrn);
                    if (c != null) {
                        if (isFail.get() || source.isClosed()) {
                            /**
                             * this {@link Channel} might be closed by other
                             * thread in this condition, so that do not release
                             * this channel
                             */
                            c.close();
                        } else {
                            c.release();
                        }
                    }
                }
                handleSuccessEOF(ss, bin);
                return;
            default:
                // ROWS
                bin.packetId = ++packetId;
                buffer = bin.write(buffer, source);
                size += bin.packetLength;
                if (size > RECEIVE_CHUNK_SIZE) {
                    handleNext(rrn, c, ss);
                    return;
                }
        }
    }
}
Also used : ServerConnection(com.alibaba.cobar.server.ServerConnection) 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