Search in sources :

Example 16 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class DataNodeMergeManager method run.

@Override
public void run() {
    if (!running.compareAndSet(false, true)) {
        return;
    }
    boolean nulpack = false;
    try {
        for (; ; ) {
            final PackWraper pack = packs.poll();
            if (pack == null) {
                nulpack = true;
                break;
            }
            if (pack == END_FLAG_PACK) {
                /**
                     * 最后一个节点datenode发送了row eof packet说明了整个
                     * 分片数据全部接收完成,进而将结果集全部发给你Mycat 客户端
                     */
                final int warningCount = 0;
                final EOFPacket eofp = new EOFPacket();
                final ByteBuffer eof = ByteBuffer.allocate(9);
                BufferUtil.writeUB3(eof, eofp.calcPacketSize());
                eof.put(eofp.packetId);
                eof.put(eofp.fieldCount);
                BufferUtil.writeUB2(eof, warningCount);
                BufferUtil.writeUB2(eof, eofp.status);
                final ServerConnection source = multiQueryHandler.getSession().getSource();
                final byte[] array = eof.array();
                Iterator<UnsafeRow> iters = null;
                if (unsafeRowGrouper != null) {
                    /**
                         * group by里面需要排序情况
                         */
                    if (globalSorter != null) {
                        iters = unsafeRowGrouper.getResult(globalSorter);
                    } else {
                        iters = unsafeRowGrouper.getResult(globalMergeResult);
                    }
                } else if (globalSorter != null) {
                    iters = globalSorter.sort();
                } else if (!isStreamOutputResult) {
                    iters = globalMergeResult.sort();
                }
                if (iters != null)
                    multiQueryHandler.outputMergeResult(source, array, iters);
                if (unsafeRowGrouper != null) {
                    unsafeRowGrouper.free();
                    unsafeRowGrouper = null;
                }
                if (globalSorter != null) {
                    globalSorter.cleanupResources();
                    globalSorter = null;
                }
                if (globalMergeResult != null) {
                    globalMergeResult.cleanupResources();
                    globalMergeResult = null;
                }
                break;
            }
            unsafeRow = new UnsafeRow(fieldCount);
            bufferHolder = new BufferHolder(unsafeRow, 0);
            unsafeRowWriter = new UnsafeRowWriter(bufferHolder, fieldCount);
            bufferHolder.reset();
            /**
                 *构造一行row,将对应的col填充.
                 */
            MySQLMessage mm = new MySQLMessage(pack.rowData);
            mm.readUB3();
            mm.read();
            for (int i = 0; i < fieldCount; i++) {
                byte[] colValue = mm.readBytesWithLength();
                if (colValue != null)
                    unsafeRowWriter.write(i, colValue);
                else
                    unsafeRow.setNullAt(i);
            }
            unsafeRow.setTotalSize(bufferHolder.totalSize());
            if (unsafeRowGrouper != null) {
                unsafeRowGrouper.addRow(unsafeRow);
            } else if (globalSorter != null) {
                globalSorter.insertRow(unsafeRow);
            } else {
                globalMergeResult.insertRow(unsafeRow);
            }
            unsafeRow = null;
            bufferHolder = null;
            unsafeRowWriter = null;
        }
    } catch (final Exception e) {
        multiQueryHandler.handleDataProcessException(e);
    } finally {
        running.set(false);
        if (nulpack && !packs.isEmpty()) {
            this.run();
        }
    }
}
Also used : EOFPacket(io.mycat.net.mysql.EOFPacket) ServerConnection(io.mycat.server.ServerConnection) UnsafeRowWriter(io.mycat.memory.unsafe.row.UnsafeRowWriter) ByteBuffer(java.nio.ByteBuffer) UnsafeRow(io.mycat.memory.unsafe.row.UnsafeRow) BufferHolder(io.mycat.memory.unsafe.row.BufferHolder) IOException(java.io.IOException) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 17 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat-Server by MyCATApache.

the class EngineCtx method writeRow.

public void writeRow(RowDataPacket rowDataPkg) {
    ServerConnection sc = session.getSource();
    try {
        writeLock.lock();
        rowDataPkg.packetId = incPackageId();
        // 输出完整的 记录到客户端
        ByteBuffer buf = rowDataPkg.write(sc.allocate(), sc, true);
        sc.write(buf);
    //LOGGER.info("write  row ,packgId:" + rowDataPkg.packetId);
    } finally {
        writeLock.unlock();
    }
}
Also used : ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ServerConnection (io.mycat.server.ServerConnection)17 ByteBuffer (java.nio.ByteBuffer)8 EOFPacket (io.mycat.net.mysql.EOFPacket)5 RowDataPacket (io.mycat.net.mysql.RowDataPacket)4 QueryResult (io.mycat.statistic.stat.QueryResult)4 FieldPacket (io.mycat.net.mysql.FieldPacket)2 BackendConnection (io.mycat.backend.BackendConnection)1 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)1 MySQLMessage (io.mycat.backend.mysql.MySQLMessage)1 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)1 MycatConfig (io.mycat.config.MycatConfig)1 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)1 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)1 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)1 FrontendConnection (io.mycat.net.FrontendConnection)1 NIOProcessor (io.mycat.net.NIOProcessor)1 BinaryRowDataPacket (io.mycat.net.mysql.BinaryRowDataPacket)1 ErrorPacket (io.mycat.net.mysql.ErrorPacket)1 OkPacket (io.mycat.net.mysql.OkPacket)1 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)1