Search in sources :

Example 1 with ProxyBufferImpl

use of io.mycat.proxy.buffer.ProxyBufferImpl in project Mycat2 by MyCATApache.

the class ResultSetHandler method requestEmptyPacket.

/**
 * loaddata empty packet
 *
 * @param nextPacketId content of file后的packetId
 */
default void requestEmptyPacket(MySQLClientSession mysql, byte nextPacketId, ResultSetCallBack<MySQLClientSession> callBack) {
    assert (mysql.currentProxyBuffer() == null);
    mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
    MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(4);
    request(mysql, mySQLPacket, nextPacketId, callBack);
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 2 with ProxyBufferImpl

use of io.mycat.proxy.buffer.ProxyBufferImpl in project Mycat2 by MyCATApache.

the class ResultSetHandler method request.

/**
 * COM_QUERY 命令请求报文调用此方法 head是payload的第一个字节 data根据实际报文构造 该方法自动构造请求报文,生成报文序列号以及长度,
 * 但是被限制整个报文长度不超过proxybuffer的chunk大小,大小也不应该超过mysql拆分报文大小 如需构造大的报文,可以自行替换proxbuffer即可
 */
default void request(MySQLClientSession mysql, int head, byte[] data, ResultSetCallBack<MySQLClientSession> callBack) {
    if (data == null) {
        data = EMPTY;
    }
    assert (mysql.currentProxyBuffer() == null);
    int chunkSize = mysql.getIOThread().getBufPool().chunkSize();
    if (data.length > (chunkSize - 5) || data.length > MySQLPacketSplitter.MAX_PACKET_SIZE) {
        throw new MycatException("ResultSetHandler unsupport request length more than 1024 bytes");
    }
    mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
    MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(data.length + 5);
    mySQLPacket.writeByte((byte) head);
    mySQLPacket.writeBytes(data);
    request(mysql, mySQLPacket, mysql.setPacketId(0), callBack);
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) MycatException(io.mycat.MycatException) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 3 with ProxyBufferImpl

use of io.mycat.proxy.buffer.ProxyBufferImpl in project Mycat2 by MyCATApache.

the class BackendMySQLPacketResolver method appendPayload.

/**
 * 保证这个函数不会更改buffer的下标
 * @param mySQLPacket
 * @param payloadStartIndex
 * @param payloadEndIndex
 */
@Override
public final void appendPayload(MySQLPacket mySQLPacket, int payloadStartIndex, int payloadEndIndex) {
    ByteBuffer byteBuffer = mySQLPacket.currentBuffer().currentByteBuffer();
    int position = byteBuffer.position();
    int limit = byteBuffer.limit();
    try {
        if (getPayloadLength() < MySQLPacketSplitter.MAX_PACKET_SIZE) {
            mySQLPacket.packetReadStartIndex(payloadStartIndex);
            mySQLPacket.packetReadEndIndex(payloadEndIndex);
            setPayload(mySQLPacket);
        } else {
            if (this.payload == null) {
                ProxyBufferImpl payload = new ProxyBufferImpl(mySQLPacket.currentBuffer().bufferPool());
                this.payload = payload;
                payload.newBuffer((int) (0xffffff * 1.5));
                ByteBuffer append = mySQLPacket.currentBuffer().currentByteBuffer().duplicate();
                append.position(payloadStartIndex);
                append.limit(payloadEndIndex);
                payload.currentByteBuffer().put(append);
            } else {
                ProxyBuffer payload = (ProxyBuffer) this.payload;
                ByteBuffer append = mySQLPacket.currentBuffer().currentByteBuffer();
                int length = payloadEndIndex - payloadStartIndex;
                if (payload.currentByteBuffer().remaining() < length) {
                    int newLength = (int) ((payload.capacity() + length) * 1.5);
                    payload.expendToLength(newLength);
                    payload.currentByteBuffer().limit(payload.capacity());
                }
                append.position(payloadStartIndex);
                append.limit(payloadEndIndex);
                payload.put(append);
            }
        }
    } finally {
        byteBuffer.position(position);
        byteBuffer.limit(limit);
    }
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) ProxyBuffer(io.mycat.beans.mysql.packet.ProxyBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ProxyBufferImpl

use of io.mycat.proxy.buffer.ProxyBufferImpl 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 5 with ProxyBufferImpl

use of io.mycat.proxy.buffer.ProxyBufferImpl in project Mycat2 by MyCATApache.

the class ResultSetHandler method request.

/**
 * @param packetData 包含报文头部的完整报文(不是payload)
 */
default void request(MySQLClientSession mysql, byte[] packetData, ResultSetCallBack<MySQLClientSession> callBack) {
    try {
        mysql.setCallBack(callBack);
        mysql.switchNioHandler(this);
        assert (mysql.currentProxyBuffer() == null);
        mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
        mysql.prepareReveiceResponse();
        mysql.writeProxyBufferToChannel(packetData);
    } catch (Exception e) {
        MycatMonitor.onResultSetWriteException(mysql, e);
        ResultSetCallBack callBackAndReset = mysql.getCallBack();
        onFinishedCollectException(mysql, e);
        onException(mysql, e);
        callBackAndReset.onFinishedException(e, this, null);
    }
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) ResultSetCallBack(io.mycat.proxy.callback.ResultSetCallBack) MycatException(io.mycat.MycatException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Aggregations

ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)6 MycatException (io.mycat.MycatException)3 MySQLPacket (io.mycat.beans.mysql.packet.MySQLPacket)3 ProxyBuffer (io.mycat.beans.mysql.packet.ProxyBuffer)2 ByteBuffer (java.nio.ByteBuffer)2 ResultSetCallBack (io.mycat.proxy.callback.ResultSetCallBack)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1