Search in sources :

Example 6 with MySQLPacket

use of io.mycat.beans.mysql.packet.MySQLPacket in project Mycat2 by MyCATApache.

the class BackendMySQLPacketResolver method currentPayload.

@Override
public final MySQLPacket currentPayload() {
    MySQLPacket mySQLPacket = currentProxybuffer();
    int i = mySQLPacket.packetReadStartIndex();
    if (mySQLPacket == payload) {
        return mySQLPacket;
    } else {
        payload.packetReadStartIndex(0);
        payload.packetReadEndIndex(payload.currentBuffer().currentByteBuffer().position());
    }
    return payload;
}
Also used : MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 7 with MySQLPacket

use of io.mycat.beans.mysql.packet.MySQLPacket in project Mycat2 by MyCATApache.

the class BackendMySQLPacketResolver method resetPayload.

@Override
public final void resetPayload() {
    MySQLPacket mySQLPacket = payload;
    payload = null;
    if (mySQLPacket != null && mySQLPacket != currentProxybuffer()) {
        mySQLPacket.reset();
    }
}
Also used : MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 8 with MySQLPacket

use of io.mycat.beans.mysql.packet.MySQLPacket in project Mycat2 by MyCATApache.

the class ResultSetHandler method request.

/**
 * 满足payload byte + long格式的请求
 */
default void request(MySQLClientSession mysql, int head, long data, ResultSetCallBack<MySQLClientSession> callBack) {
    assert (mysql.currentProxyBuffer() == null);
    mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
    MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(12);
    mySQLPacket.writeByte((byte) head);
    mySQLPacket.writeFixInt(4, data);
    request(mysql, mySQLPacket, 0, callBack);
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 9 with MySQLPacket

use of io.mycat.beans.mysql.packet.MySQLPacket in project Mycat2 by MyCATApache.

the class ResultSetHandler method onSocketRead.

// /**
// * 该方法可能会被重写
// */
// default void onFinishedCollect(MySQLClientSession mysql, boolean success, String errorMessage) {
// ResultSetCallBack callBack = mysql.getCallBackAndReset();
// assert callBack != null;
// if (success) {
// callBack.onFinishedCollect(mysql, this, true, getResult(), null);
// } else {
// callBack.onFinishedCollect(mysql, this, false, errorMessage, null);
// }
// }
/**
 * 读事件处理
 */
@Override
default void onSocketRead(MySQLClientSession mysql) {
    assert mysql.getCurNIOHandler() == this;
    if (!mysql.checkOpen()) {
        ResultSetCallBack callBackAndReset = mysql.getCallBack();
        ClosedChannelException closedChannelException = new ClosedChannelException();
        onException(mysql, closedChannelException);
        callBackAndReset.onFinishedException(closedChannelException, this, null);
        return;
    }
    try {
        MySQLPacketResolver resolver = mysql.getBackendPacketResolver();
        ProxyBuffer proxyBuffer = mysql.currentProxyBuffer();
        proxyBuffer.newBufferIfNeed();
        if (!mysql.readFromChannel()) {
            return;
        }
        mysql.setRequestSuccess(true);
        int totalPacketEndIndex = proxyBuffer.channelReadEndIndex();
        MySQLPacket mySQLPacket = (MySQLPacket) proxyBuffer;
        boolean isResponseFinished = false;
        ErrorPacketImpl errorPacket = null;
        while (mysql.readProxyPayloadFully()) {
            MySQLPayloadType type = mysql.getBackendPacketResolver().getMySQLPayloadType();
            isResponseFinished = mysql.isResponseFinished();
            MySQLPacket payload = mysql.currentProxyPayload();
            int startPos = payload.packetReadStartIndex();
            int endPos = payload.packetReadEndIndex();
            switch(type) {
                case REQUEST:
                    this.onRequest(mySQLPacket, startPos, endPos);
                    break;
                case LOAD_DATA_REQUEST:
                    this.onLoadDataRequest(mySQLPacket, startPos, endPos);
                    break;
                case REQUEST_COM_QUERY:
                    this.onRequestComQuery(mySQLPacket, startPos, endPos);
                    break;
                case REQUEST_SEND_LONG_DATA:
                    this.onPrepareLongData(mySQLPacket, startPos, endPos);
                    break;
                case REQUEST_PREPARE:
                    this.onReqeustPrepareStatement(mySQLPacket, startPos, endPos);
                    break;
                case REQUEST_COM_STMT_CLOSE:
                    this.onRequestComStmtClose(mySQLPacket, startPos, endPos);
                    break;
                case FIRST_ERROR:
                    {
                        ErrorPacketImpl packet = new ErrorPacketImpl();
                        errorPacket = packet;
                        packet.readPayload(mySQLPacket);
                        this.onFirstError(packet);
                        break;
                    }
                case FIRST_OK:
                    MycatMonitor.onResultSetEnd(mysql);
                    this.onOk(mySQLPacket, startPos, endPos);
                    break;
                case FIRST_EOF:
                    this.onEof(mySQLPacket, startPos, endPos);
                    break;
                case COLUMN_COUNT:
                    this.onColumnCount(resolver.getColumnCount());
                    break;
                case COLUMN_DEF:
                    this.onColumnDef(mySQLPacket, startPos, endPos);
                    break;
                case COLUMN_EOF:
                    this.onColumnDefEof(mySQLPacket, startPos, endPos);
                    break;
                case TEXT_ROW:
                    this.onTextRow(mySQLPacket, startPos, endPos);
                    break;
                case BINARY_ROW:
                    this.onBinaryRow(mySQLPacket, startPos, endPos);
                    break;
                case ROW_EOF:
                case ROW_OK:
                    MycatMonitor.onResultSetEnd(mysql);
                    this.onRowOk(mySQLPacket, startPos, endPos);
                    break;
                case ROW_ERROR:
                    ErrorPacketImpl packet = new ErrorPacketImpl();
                    errorPacket = packet;
                    packet.readPayload(mySQLPacket);
                    this.onRowError(packet, startPos, endPos);
                    break;
                case PREPARE_OK:
                    this.onPrepareOk(resolver);
                    break;
                case PREPARE_OK_PARAMER_DEF:
                    this.onPrepareOkParameterDef(mySQLPacket, startPos, endPos);
                    break;
                case PREPARE_OK_COLUMN_DEF:
                    this.onPrepareOkColumnDef(mySQLPacket, startPos, endPos);
                    break;
                case PREPARE_OK_COLUMN_DEF_EOF:
                    this.onPrepareOkColumnDefEof(resolver);
                    break;
                case PREPARE_OK_PARAMER_DEF_EOF:
                    this.onPrepareOkParameterDefEof(resolver);
                    break;
            }
            mysql.resetCurrentProxyPayload();
            proxyBuffer.channelReadEndIndex(totalPacketEndIndex);
            if (isResponseFinished) {
                break;
            }
            assert mysql.getCurNIOHandler() == this;
            MySQLPacketResolver packetResolver = mysql.getBackendPacketResolver();
            mySQLPacket.packetReadStartIndex(packetResolver.getEndPos());
        }
        if (isResponseFinished) {
            ByteBuffer allocate = ByteBuffer.allocate(8192);
            if (mysql.channel().read(allocate) > 0) {
                throw new IllegalArgumentException();
            }
            boolean responseFinished = mysql.isResponseFinished();
            mysql.getBackendPacketResolver().setState(MySQLPacketResolver.ComQueryState.QUERY_PACKET);
            ResultSetCallBack callBackAndReset = mysql.getCallBack();
            mysql.setCallBack(null);
            onFinishedCollect(mysql);
            onClear(mysql);
            if (errorPacket == null) {
                callBackAndReset.onFinished(mysql.isMonopolized(), mysql, this, null);
            } else {
                callBackAndReset.onErrorPacket(errorPacket, mysql.isMonopolized(), mysql, this, null);
            }
            return;
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        ResultSetCallBack callBackAndReset = mysql.getCallBack();
        Objects.requireNonNull(callBackAndReset);
        mysql.setCallBack(null);
        if (mysql.isRequestSuccess()) {
            MycatMonitor.onResultSetReadException(mysql, e);
            onFinishedCollectException(mysql, e);
            onException(mysql, e);
            callBackAndReset.onFinishedException(e, this, null);
            return;
        } else {
            MycatMonitor.onResultSetWriteException(mysql, e);
            onFinishedCollectException(mysql, e);
            onException(mysql, e);
            callBackAndReset.onFinishedSendException(e, this, null);
            return;
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MySQLPayloadType(io.mycat.proxy.packet.MySQLPayloadType) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket) ResultSetCallBack(io.mycat.proxy.callback.ResultSetCallBack) ProxyBuffer(io.mycat.beans.mysql.packet.ProxyBuffer) MySQLPacketResolver(io.mycat.proxy.packet.MySQLPacketResolver) ByteBuffer(java.nio.ByteBuffer) ErrorPacketImpl(io.mycat.beans.mysql.packet.ErrorPacketImpl) MycatException(io.mycat.MycatException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Aggregations

MySQLPacket (io.mycat.beans.mysql.packet.MySQLPacket)9 ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)3 MycatException (io.mycat.MycatException)2 ProxyBuffer (io.mycat.beans.mysql.packet.ProxyBuffer)2 Authenticator (io.mycat.Authenticator)1 MycatUser (io.mycat.MycatUser)1 MySQLPayloadWriter (io.mycat.beans.mysql.MySQLPayloadWriter)1 AuthPacket (io.mycat.beans.mysql.packet.AuthPacket)1 AuthSwitchRequestPacket (io.mycat.beans.mysql.packet.AuthSwitchRequestPacket)1 ErrorPacketImpl (io.mycat.beans.mysql.packet.ErrorPacketImpl)1 UserConfig (io.mycat.config.UserConfig)1 ResultSetCallBack (io.mycat.proxy.callback.ResultSetCallBack)1 MySQLPacketResolver (io.mycat.proxy.packet.MySQLPacketResolver)1 MySQLPayloadType (io.mycat.proxy.packet.MySQLPayloadType)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1