Search in sources :

Example 41 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ConfFileHandler method handle.

public static void handle(String stmt, ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    String theStmt = stmt.toUpperCase().trim();
    PackageBufINf bufInf = null;
    if (theStmt.equals("FILE @@LIST")) {
        bufInf = listConfigFiles(c, buffer, packetId);
    } else if (theStmt.startsWith("FILE @@SHOW")) {
        int index = stmt.lastIndexOf(' ');
        String fileName = stmt.substring(index + 1);
        bufInf = showConfigFile(c, buffer, packetId, fileName);
    } else if (theStmt.startsWith(UPLOAD_CMD)) {
        int index = stmt.indexOf(' ', UPLOAD_CMD.length());
        int index2 = stmt.indexOf(' ', index + 1);
        if (index <= 0 || index2 <= 0 || index + 1 > stmt.length() || index2 + 1 > stmt.length()) {
            bufInf = showInfo(c, buffer, packetId, "Invald param ,usage  ");
        }
        String fileName = stmt.substring(index + 1, index2);
        String content = stmt.substring(index2 + 1).trim();
        bufInf = upLoadConfigFile(c, buffer, packetId, fileName, content);
    } else {
        bufInf = showInfo(c, buffer, packetId, "Invald command ");
    }
    packetId = bufInf.packetId;
    buffer = bufInf.buffer;
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 42 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowDatasourceSyn method response.

public static void response(ManagerConnection c, String stmt) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    for (RowDataPacket row : getRows(c.getCharset())) {
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 43 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowDirectMemory method showDirectMemoryDetail.

public static void showDirectMemoryDetail(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = detailHeader.write(buffer, c, true);
    // write fields
    for (FieldPacket field : detailFields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = detailEof.write(buffer, c, true);
    // write rows
    byte packetId = detailEof.packetId;
    ConcurrentHashMap<Long, Long> bufferpoolUsageMap = bufferPool.getNetDirectMemoryUsage();
    try {
        if (useOffHeapForMerge == 1) {
            ConcurrentHashMap<Long, Long> concurrentHashMap = MycatServer.getInstance().getMyCatMemory().getResultMergeMemoryManager().getDirectMemorUsage();
            for (Long key : concurrentHashMap.keySet()) {
                RowDataPacket row = new RowDataPacket(DETAILl_FIELD_COUNT);
                Long value = concurrentHashMap.get(key);
                row.add(String.valueOf(key).getBytes(c.getCharset()));
                /**
                 * 该DIRECTMEMORY内存被结果集处理使用了
                 */
                row.add("MergeMemoryPool".getBytes(c.getCharset()));
                row.add(value > 0 ? JavaUtils.bytesToString2(value).getBytes(c.getCharset()) : "0".getBytes(c.getCharset()));
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        }
        if (processorBufferPoolType == 2) {
        } else {
            for (Long key : bufferpoolUsageMap.keySet()) {
                RowDataPacket row = new RowDataPacket(DETAILl_FIELD_COUNT);
                Long value = bufferpoolUsageMap.get(key);
                row.add(String.valueOf(key).getBytes(c.getCharset()));
                /**
                 * 该DIRECTMEMORY内存属于Buffer Pool管理的!
                 */
                row.add("NetWorkBufferPool".getBytes(c.getCharset()));
                row.add(value > 0 ? JavaUtils.bytesToString2(value).getBytes(c.getCharset()) : "0".getBytes(c.getCharset()));
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 44 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowDirectMemory method showDirectMemoryTotal.

public static void showDirectMemoryTotal(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = totalHeader.write(buffer, c, true);
    // write fields
    for (FieldPacket field : totalFields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = totalEof.write(buffer, c, true);
    // write rows
    byte packetId = totalEof.packetId;
    long usedforMerge = 0;
    long usedforNetwork = 0;
    long chunkSizeBytes = 0;
    int chunkCount = 0;
    if (processorBufferPoolType == 2 && bufferPool instanceof NettyBufferPool) {
        /**
         *计算逻辑就是,1.先计算PoolChunk分配的页,表示已经消耗的内存,
         *             2.然后计算小于一页情况,记录小于一页内存使用情况,
         *             上面二者合起来就是整个netty 使用的内存,
         *             已经分配了,但是没有使用的内存的情况
         */
        List<PoolArenaMetric> list = ((NettyBufferPool) bufferPool).getAllocator().getAlloc().directArenas();
        chunkSizeBytes = ((NettyBufferPool) bufferPool).getAllocator().getChunkSize();
        long pageSize = ((NettyBufferPool) bufferPool).getAllocator().getPageSize();
        long chunksUsedBytes = 0;
        /**
         *PoolArenas
         */
        for (PoolArenaMetric pool : list) {
            List<PoolChunkListMetric> pcks = pool.chunkLists();
            /**
             *针对PoolChunkList
             */
            for (PoolChunkListMetric pck : pcks) {
                Iterator<PoolChunkMetric> it = pck.iterator();
                while (it.hasNext()) {
                    chunkCount++;
                    PoolChunkMetric p = it.next();
                    chunksUsedBytes += (chunkSizeBytes - p.freeBytes());
                }
            }
            List<PoolSubpageMetric> tinySubpages = pool.tinySubpages();
            for (PoolSubpageMetric tiny : tinySubpages) {
                chunksUsedBytes -= (pageSize - (tiny.maxNumElements() - tiny.numAvailable()) * tiny.elementSize());
            }
            List<PoolSubpageMetric> smallSubpages = pool.smallSubpages();
            for (PoolSubpageMetric small : smallSubpages) {
                chunksUsedBytes -= (pageSize - (small.maxNumElements() - small.numAvailable()) * small.elementSize());
            }
        }
        usedforNetwork = chunkCount * chunkSizeBytes;
    }
    ConcurrentHashMap<Long, Long> bufferpoolUsageMap = bufferPool.getNetDirectMemoryUsage();
    RowDataPacket row = new RowDataPacket(TOTAL_FIELD_COUNT);
    try {
        /**
         * 通过-XX:MaxDirectMemorySize=2048m设置的值
         */
        row.add(JavaUtils.bytesToString2(Platform.getMaxDirectMemory()).getBytes(c.getCharset()));
        if (useOffHeapForMerge == 1) {
            /**
             * 结果集合并时,总共消耗的DirectMemory内存
             */
            ConcurrentHashMap<Long, Long> concurrentHashMap = MycatServer.getInstance().getMyCatMemory().getResultMergeMemoryManager().getDirectMemorUsage();
            for (Map.Entry<Long, Long> entry : concurrentHashMap.entrySet()) {
                usedforMerge += entry.getValue();
            }
        }
        /**
         * 网络packet处理,在buffer pool 已经使用DirectMemory内存
         */
        if (processorBufferPoolType == 2) {
            usedforNetwork = chunkSizeBytes * chunkCount;
        } else {
            for (Map.Entry<Long, Long> entry : bufferpoolUsageMap.entrySet()) {
                usedforNetwork += entry.getValue();
            }
        }
        row.add(JavaUtils.bytesToString2(usedforMerge + usedforNetwork).getBytes(c.getCharset()));
        long totalAvailable = 0;
        if (useOffHeapForMerge == 1) {
            /**
             * 设置使用off-heap内存处理结果集时,防止客户把MaxDirectMemorySize设置到物理内存的极限。
             * Mycat能使用的DirectMemory是MaxDirectMemorySize*DIRECT_SAFETY_FRACTION大小,
             * DIRECT_SAFETY_FRACTION为安全系数,为OS,Heap预留空间,避免因大结果集造成系统物理内存被耗尽!
             */
            totalAvailable = (long) (Platform.getMaxDirectMemory() * MyCatMemory.DIRECT_SAFETY_FRACTION);
        } else {
            totalAvailable = Platform.getMaxDirectMemory();
        }
        row.add(JavaUtils.bytesToString2(totalAvailable - usedforMerge - usedforNetwork).getBytes(c.getCharset()));
        if (useOffHeapForMerge == 1) {
            /**
             * 输出安全系统DIRECT_SAFETY_FRACTION
             */
            row.add(("" + MyCatMemory.DIRECT_SAFETY_FRACTION).getBytes(c.getCharset()));
        } else {
            row.add(("1.0").getBytes(c.getCharset()));
        }
        long resevedForOs = 0;
        if (useOffHeapForMerge == 1) {
            /**
             * 预留OS系统部分内存!!!
             */
            resevedForOs = (long) ((1 - MyCatMemory.DIRECT_SAFETY_FRACTION) * (Platform.getMaxDirectMemory() - 2 * MycatServer.getInstance().getTotalNetWorkBufferSize()));
        }
        row.add(resevedForOs > 0 ? JavaUtils.bytesToString2(resevedForOs).getBytes(c.getCharset()) : "0".getBytes(c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PoolSubpageMetric(io.netty.buffer.PoolSubpageMetric) ByteBuffer(java.nio.ByteBuffer) PoolArenaMetric(io.netty.buffer.PoolArenaMetric) PoolChunkListMetric(io.netty.buffer.PoolChunkListMetric) PoolChunkMetric(io.netty.buffer.PoolChunkMetric) FieldPacket(io.mycat.net.mysql.FieldPacket) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NettyBufferPool(io.mycat.buffer.NettyBufferPool)

Example 45 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowHeartbeatDetail method response.

public static void response(ManagerConnection c, String stmt) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    Pair<String, String> pair = ManagerParseHeartbeat.getPair(stmt);
    String name = pair.getValue();
    for (RowDataPacket row : getRows(name, c.getCharset())) {
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Aggregations

EOFPacket (io.mycat.net.mysql.EOFPacket)154 ByteBuffer (java.nio.ByteBuffer)153 FieldPacket (io.mycat.net.mysql.FieldPacket)146 RowDataPacket (io.mycat.net.mysql.RowDataPacket)137 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)18 NIOProcessor (io.mycat.net.NIOProcessor)14 UserStat (io.mycat.statistic.stat.UserStat)12 ServerConnection (io.mycat.server.ServerConnection)11 Map (java.util.Map)10 SchemaConfig (io.mycat.config.model.SchemaConfig)8 ArrayList (java.util.ArrayList)7 MycatConfig (io.mycat.config.MycatConfig)6 FrontendConnection (io.mycat.net.FrontendConnection)6 BackendConnection (io.mycat.backend.BackendConnection)4 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)4 UserConfig (io.mycat.config.model.UserConfig)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 TreeSet (java.util.TreeSet)3