Search in sources :

Example 21 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class ShowConnection method getRow.

private static RowDataPacket getRow(FrontendConnection c, String charset) {
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    row.add(c.getProcessor().getName().getBytes());
    row.add(LongUtil.toBytes(c.getId()));
    row.add(StringUtil.encode(c.getHost(), charset));
    row.add(IntegerUtil.toBytes(c.getPort()));
    row.add(IntegerUtil.toBytes(c.getLocalPort()));
    row.add(StringUtil.encode(c.getUser(), charset));
    row.add(StringUtil.encode(c.getSchema(), charset));
    row.add(StringUtil.encode(c.getCharset() + ":" + c.getCharsetIndex(), charset));
    row.add(LongUtil.toBytes(c.getNetInBytes()));
    row.add(LongUtil.toBytes(c.getNetOutBytes()));
    row.add(LongUtil.toBytes((TimeUtil.currentTimeMillis() - c.getStartupTime()) / 1000L));
    ByteBuffer bb = c.getReadBuffer();
    row.add(IntegerUtil.toBytes(bb == null ? 0 : bb.capacity()));
    row.add(IntegerUtil.toBytes(c.getWriteQueue().size()));
    String txLevel = "";
    String txAutommit = "";
    if (c instanceof ServerConnection) {
        ServerConnection mysqlC = (ServerConnection) c;
        txLevel = mysqlC.getTxIsolation() + "";
        txAutommit = mysqlC.isAutocommit() + "";
    }
    row.add(txLevel.getBytes());
    row.add(txAutommit.getBytes());
    return row;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Example 22 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class DataMergeService method run.

@Override
public void run() {
    // @since 2016-03-23
    if (!running.compareAndSet(false, true)) {
        return;
    }
    // eof handler has been placed to "if (pack == END_FLAG_PACK){}" in for-statement
    // @author Uncle-pan
    // @since 2016-03-23
    boolean nulpack = false;
    try {
        // loop-on-packs
        for (; ; ) {
            final PackWraper pack = packs.poll();
            // @since 2016-03-23
            if (pack == null) {
                nulpack = true;
                break;
            }
            // eof: handling eof pack and exit
            if (pack == END_FLAG_PACK) {
                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();
                multiQueryHandler.outputMergeResult(source, array, getResults(array));
                break;
            }
            // merge: sort-or-group, or simple add
            final RowDataPacket row = new RowDataPacket(fieldCount);
            row.read(pack.rowData);
            if (grouper != null) {
                grouper.addRow(row);
            } else if (sorter != null) {
                if (!sorter.addRow(row)) {
                    canDiscard.put(pack.dataNode, true);
                }
            } else {
                result.get(pack.dataNode).add(row);
            }
        }
    // rof
    } catch (final Exception e) {
        multiQueryHandler.handleDataProcessException(e);
    } finally {
        running.set(false);
    }
    // @since 2016-03-23
    if (nulpack && !packs.isEmpty()) {
        this.run();
    }
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Example 23 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

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) {
                hasEndFlag = true;
                if (packs.peek() != null) {
                    packs.add(pack);
                    continue;
                }
                /**
                 * 最后一个节点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, isMiddleResultDone);
                }
                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();
            int nullnum = 0;
            for (int i = 0; i < fieldCount; i++) {
                byte[] colValue = mm.readBytesWithLength();
                if (colValue != null)
                    unsafeRowWriter.write(i, colValue);
                else {
                    if (mergeColsIndex != null && mergeColsIndex.length > 0) {
                        if (Arrays.binarySearch(mergeColsIndex, i) < 0) {
                            nullnum++;
                        }
                    }
                    unsafeRow.setNullAt(i);
                }
            }
            if (mergeColsIndex != null && mergeColsIndex.length > 0) {
                if (nullnum == (fieldCount - mergeColsIndex.length)) {
                    if (!hasEndFlag) {
                        packs.add(pack);
                        continue;
                    }
                }
            }
            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) {
        e.printStackTrace();
        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 24 with ServerConnection

use of io.mycat.server.ServerConnection in project Mycat_plus by coderczp.

the class EngineCtx method writeHeader.

public void writeHeader(List<byte[]> afields, List<byte[]> bfields) {
    if (headerWrited.compareAndSet(false, true)) {
        try {
            writeLock.lock();
            // write new header
            ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
            headerPkg.fieldCount = afields.size() + bfields.size() - 1;
            headerPkg.packetId = incPackageId();
            LOGGER.debug("packge id " + headerPkg.packetId);
            ServerConnection sc = session.getSource();
            ByteBuffer buf = headerPkg.write(sc.allocate(), sc, true);
            // wirte a fields
            for (byte[] field : afields) {
                field[3] = incPackageId();
                buf = sc.writeToBuffer(field, buf);
            }
            // write b field
            for (int i = 1; i < bfields.size(); i++) {
                byte[] bfield = bfields.get(i);
                bfield[3] = incPackageId();
                buf = sc.writeToBuffer(bfield, buf);
            }
            // write field eof
            EOFPacket eofPckg = new EOFPacket();
            eofPckg.packetId = incPackageId();
            buf = eofPckg.write(buf, sc, true);
            sc.write(buf);
        // LOGGER.info("header outputed ,packgId:" + eofPckg.packetId);
        } finally {
            writeLock.unlock();
        }
    }
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Example 25 with ServerConnection

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

the class ShowConnection method getRow.

private static RowDataPacket getRow(FrontendConnection c, String charset) {
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    row.add(c.getProcessor().getName().getBytes());
    row.add(LongUtil.toBytes(c.getId()));
    row.add(StringUtil.encode(c.getHost(), charset));
    row.add(IntegerUtil.toBytes(c.getPort()));
    row.add(IntegerUtil.toBytes(c.getLocalPort()));
    row.add(StringUtil.encode(c.getUser(), charset));
    row.add(StringUtil.encode(c.getSchema(), charset));
    row.add(StringUtil.encode(c.getCharset() + ":" + c.getCharsetIndex(), charset));
    row.add(LongUtil.toBytes(c.getNetInBytes()));
    row.add(LongUtil.toBytes(c.getNetOutBytes()));
    row.add(LongUtil.toBytes((TimeUtil.currentTimeMillis() - c.getStartupTime()) / 1000L));
    ByteBuffer bb = c.getReadBuffer();
    row.add(IntegerUtil.toBytes(bb == null ? 0 : bb.capacity()));
    row.add(IntegerUtil.toBytes(c.getWriteQueue().size()));
    String txLevel = "";
    String txAutommit = "";
    if (c instanceof ServerConnection) {
        ServerConnection mysqlC = (ServerConnection) c;
        txLevel = mysqlC.getTxIsolation() + "";
        txAutommit = mysqlC.isAutocommit() + "";
    }
    row.add(txLevel.getBytes());
    row.add(txAutommit.getBytes());
    return row;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) ServerConnection(io.mycat.server.ServerConnection) ByteBuffer(java.nio.ByteBuffer)

Aggregations

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