Search in sources :

Example 1 with NettyBufferPool

use of io.mycat.buffer.NettyBufferPool 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 2 with NettyBufferPool

use of io.mycat.buffer.NettyBufferPool in project Mycat_plus by coderczp.

the class MycatServer method startup.

public void startup() throws IOException {
    SystemConfig system = config.getSystem();
    int processorCount = system.getProcessors();
    // init RouteStrategyFactory first
    RouteStrategyFactory.init();
    // server startup
    LOGGER.info(NAME + " is ready to startup ...");
    String inf = "Startup processors ...,total processors:" + system.getProcessors() + ",aio thread pool size:" + system.getProcessorExecutor() + "    \r\n each process allocated socket buffer pool " + " bytes ,a page size:" + system.getBufferPoolPageSize() + "  a page's chunk number(PageSize/ChunkSize) is:" + (system.getBufferPoolPageSize() / system.getBufferPoolChunkSize()) + "  buffer page's number is:" + system.getBufferPoolPageNumber();
    LOGGER.info(inf);
    LOGGER.info("sysconfig params:" + system.toString());
    // startup manager
    ManagerConnectionFactory mf = new ManagerConnectionFactory();
    ServerConnectionFactory sf = new ServerConnectionFactory();
    SocketAcceptor manager = null;
    SocketAcceptor server = null;
    aio = (system.getUsingAIO() == 1);
    // startup processors
    int threadPoolSize = system.getProcessorExecutor();
    processors = new NIOProcessor[processorCount];
    // a page size
    int bufferPoolPageSize = system.getBufferPoolPageSize();
    // total page number
    short bufferPoolPageNumber = system.getBufferPoolPageNumber();
    // minimum allocation unit
    short bufferPoolChunkSize = system.getBufferPoolChunkSize();
    int socketBufferLocalPercent = system.getProcessorBufferLocalPercent();
    int bufferPoolType = system.getProcessorBufferPoolType();
    switch(bufferPoolType) {
        case 0:
            bufferPool = new DirectByteBufferPool(bufferPoolPageSize, bufferPoolChunkSize, bufferPoolPageNumber, system.getFrontSocketSoRcvbuf());
            totalNetWorkBufferSize = bufferPoolPageSize * bufferPoolPageNumber;
            break;
        case 1:
            /**
             * todo 对应权威指南修改:
             *
             * bytebufferarena由6个bytebufferlist组成,这六个list有减少内存碎片的机制
             * 每个bytebufferlist由多个bytebufferchunk组成,每个list也有减少内存碎片的机制
             * 每个bytebufferchunk由多个page组成,平衡二叉树管理内存使用状态,计算灵活
             * 设置的pagesize对应bytebufferarena里面的每个bytebufferlist的每个bytebufferchunk的buffer长度
             * bufferPoolChunkSize对应每个bytebufferchunk的每个page的长度
             * bufferPoolPageNumber对应每个bytebufferlist有多少个bytebufferchunk
             */
            totalNetWorkBufferSize = 6 * bufferPoolPageSize * bufferPoolPageNumber;
            break;
        case 2:
            bufferPool = new NettyBufferPool(bufferPoolChunkSize);
            LOGGER.info("Use Netty Buffer Pool");
            break;
        default:
            bufferPool = new DirectByteBufferPool(bufferPoolPageSize, bufferPoolChunkSize, bufferPoolPageNumber, system.getFrontSocketSoRcvbuf());
            ;
            totalNetWorkBufferSize = bufferPoolPageSize * bufferPoolPageNumber;
    }
    /**
     * Off Heap For Merge/Order/Group/Limit 初始化
     */
    if (system.getUseOffHeapForMerge() == 1) {
        try {
            myCatMemory = new MyCatMemory(system, totalNetWorkBufferSize);
        } catch (NoSuchFieldException e) {
            LOGGER.error("NoSuchFieldException", e);
        } catch (IllegalAccessException e) {
            LOGGER.error("Error", e);
        }
    }
    businessExecutor = ExecutorUtil.create("BusinessExecutor", threadPoolSize);
    sequenceExecutor = ExecutorUtil.create("SequenceExecutor", threadPoolSize);
    timerExecutor = ExecutorUtil.create("Timer", system.getTimerExecutor());
    listeningExecutorService = MoreExecutors.listeningDecorator(businessExecutor);
    for (int i = 0; i < processors.length; i++) {
        processors[i] = new NIOProcessor("Processor" + i, bufferPool, businessExecutor);
    }
    if (aio) {
        LOGGER.info("using aio network handler ");
        asyncChannelGroups = new AsynchronousChannelGroup[processorCount];
        // startup connector
        connector = new AIOConnector();
        for (int i = 0; i < processors.length; i++) {
            asyncChannelGroups[i] = AsynchronousChannelGroup.withFixedThreadPool(processorCount, new ThreadFactory() {

                private int inx = 1;

                @Override
                public Thread newThread(Runnable r) {
                    Thread th = new Thread(r);
                    // TODO
                    th.setName(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "AIO" + (inx++));
                    LOGGER.info("created new AIO thread " + th.getName());
                    return th;
                }
            });
        }
        manager = new AIOAcceptor(NAME + "Manager", system.getBindIp(), system.getManagerPort(), mf, this.asyncChannelGroups[0]);
        // startup server
        server = new AIOAcceptor(NAME + "Server", system.getBindIp(), system.getServerPort(), sf, this.asyncChannelGroups[0]);
    } else {
        LOGGER.info("using nio network handler ");
        NIOReactorPool reactorPool = new NIOReactorPool(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "NIOREACTOR", processors.length);
        connector = new NIOConnector(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "NIOConnector", reactorPool);
        ((NIOConnector) connector).start();
        manager = new NIOAcceptor(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + NAME + "Manager", system.getBindIp(), system.getManagerPort(), mf, reactorPool);
        server = new NIOAcceptor(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + NAME + "Server", system.getBindIp(), system.getServerPort(), sf, reactorPool);
    }
    // manager start
    manager.start();
    LOGGER.info(manager.getName() + " is started and listening on " + manager.getPort());
    server.start();
    // server started
    LOGGER.info(server.getName() + " is started and listening on " + server.getPort());
    LOGGER.info("===============================================");
    // init datahost
    Map<String, PhysicalDBPool> dataHosts = config.getDataHosts();
    LOGGER.info("Initialize dataHost ...");
    for (PhysicalDBPool node : dataHosts.values()) {
        String index = dnIndexProperties.getProperty(node.getHostName(), "0");
        if (!"0".equals(index)) {
            LOGGER.info("init datahost: " + node.getHostName() + "  to use datasource index:" + index);
        }
        node.init(Integer.parseInt(index));
        node.startHeartbeat();
    }
    long dataNodeIldeCheckPeriod = system.getDataNodeIdleCheckPeriod();
    heartbeatScheduler.scheduleAtFixedRate(updateTime(), 0L, TIME_UPDATE_PERIOD, TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(processorCheck(), 0L, system.getProcessorCheckPeriod(), TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(dataNodeConHeartBeatCheck(dataNodeIldeCheckPeriod), 0L, dataNodeIldeCheckPeriod, TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(dataNodeHeartbeat(), 0L, system.getDataNodeHeartbeatPeriod(), TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(dataSourceOldConsClear(), 0L, DEFAULT_OLD_CONNECTION_CLEAR_PERIOD, TimeUnit.MILLISECONDS);
    scheduler.schedule(catletClassClear(), 30000, TimeUnit.MILLISECONDS);
    if (system.getCheckTableConsistency() == 1) {
        scheduler.scheduleAtFixedRate(tableStructureCheck(), 0L, system.getCheckTableConsistencyPeriod(), TimeUnit.MILLISECONDS);
    }
    if (system.getUseSqlStat() == 1) {
        scheduler.scheduleAtFixedRate(recycleSqlStat(), 0L, DEFAULT_SQL_STAT_RECYCLE_PERIOD, TimeUnit.MILLISECONDS);
    }
    if (system.getUseGlobleTableCheck() == 1) {
        // 全局表一致性检测是否开启
        scheduler.scheduleAtFixedRate(glableTableConsistencyCheck(), 0L, system.getGlableTableCheckPeriod(), TimeUnit.MILLISECONDS);
    }
    // 定期清理结果集排行榜,控制拒绝策略
    scheduler.scheduleAtFixedRate(resultSetMapClear(), 0L, system.getClearBigSqLResultSetMapMs(), TimeUnit.MILLISECONDS);
    // new Thread(tableStructureCheck()).start();
    // XA Init recovery Log
    LOGGER.info("===============================================");
    LOGGER.info("Perform XA recovery log ...");
    performXARecoveryLog();
    if (isUseZkSwitch()) {
        // 首次启动如果发现zk上dnindex为空,则将本地初始化上zk
        initZkDnindex();
    }
    initRuleData();
    startup.set(true);
}
Also used : AIOConnector(io.mycat.net.AIOConnector) ThreadFactory(java.util.concurrent.ThreadFactory) SystemConfig(io.mycat.config.model.SystemConfig) NIOReactorPool(io.mycat.net.NIOReactorPool) ManagerConnectionFactory(io.mycat.manager.ManagerConnectionFactory) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) NIOProcessor(io.mycat.net.NIOProcessor) ServerConnectionFactory(io.mycat.server.ServerConnectionFactory) NIOConnector(io.mycat.net.NIOConnector) SocketAcceptor(io.mycat.net.SocketAcceptor) DirectByteBufferPool(io.mycat.buffer.DirectByteBufferPool) NIOAcceptor(io.mycat.net.NIOAcceptor) AIOAcceptor(io.mycat.net.AIOAcceptor) NettyBufferPool(io.mycat.buffer.NettyBufferPool) MyCatMemory(io.mycat.memory.MyCatMemory)

Example 3 with NettyBufferPool

use of io.mycat.buffer.NettyBufferPool in project Mycat-Server by MyCATApache.

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 4 with NettyBufferPool

use of io.mycat.buffer.NettyBufferPool in project Mycat-Server by MyCATApache.

the class MycatServer method startup.

public void startup() throws IOException {
    SystemConfig system = config.getSystem();
    int processorCount = system.getProcessors();
    // init RouteStrategyFactory first
    RouteStrategyFactory.init();
    // server startup
    LOGGER.info(NAME + " is ready to startup ...");
    String inf = "Startup processors ...,total processors:" + system.getProcessors() + ",aio thread pool size:" + system.getProcessorExecutor() + "    \r\n each process allocated socket buffer pool " + " bytes ,a page size:" + system.getBufferPoolPageSize() + "  a page's chunk number(PageSize/ChunkSize) is:" + (system.getBufferPoolPageSize() / system.getBufferPoolChunkSize()) + "  buffer page's number is:" + system.getBufferPoolPageNumber();
    LOGGER.info(inf);
    LOGGER.info("sysconfig params:" + system.toString());
    // startup manager
    ManagerConnectionFactory mf = new ManagerConnectionFactory();
    ServerConnectionFactory sf = new ServerConnectionFactory();
    SocketAcceptor manager = null;
    SocketAcceptor server = null;
    aio = (system.getUsingAIO() == 1);
    // startup processors
    int threadPoolSize = system.getProcessorExecutor();
    processors = new NIOProcessor[processorCount];
    // a page size
    int bufferPoolPageSize = system.getBufferPoolPageSize();
    // total page number
    short bufferPoolPageNumber = system.getBufferPoolPageNumber();
    // minimum allocation unit
    short bufferPoolChunkSize = system.getBufferPoolChunkSize();
    int socketBufferLocalPercent = system.getProcessorBufferLocalPercent();
    int bufferPoolType = system.getProcessorBufferPoolType();
    switch(bufferPoolType) {
        case 0:
            bufferPool = new DirectByteBufferPool(bufferPoolPageSize, bufferPoolChunkSize, bufferPoolPageNumber, system.getFrontSocketSoRcvbuf());
            totalNetWorkBufferSize = bufferPoolPageSize * bufferPoolPageNumber;
            break;
        case 1:
            /**
             * todo 对应权威指南修改:
             *
             * bytebufferarena由6个bytebufferlist组成,这六个list有减少内存碎片的机制
             * 每个bytebufferlist由多个bytebufferchunk组成,每个list也有减少内存碎片的机制
             * 每个bytebufferchunk由多个page组成,平衡二叉树管理内存使用状态,计算灵活
             * 设置的pagesize对应bytebufferarena里面的每个bytebufferlist的每个bytebufferchunk的buffer长度
             * bufferPoolChunkSize对应每个bytebufferchunk的每个page的长度
             * bufferPoolPageNumber对应每个bytebufferlist有多少个bytebufferchunk
             */
            totalNetWorkBufferSize = 6 * bufferPoolPageSize * bufferPoolPageNumber;
            break;
        case 2:
            bufferPool = new NettyBufferPool(bufferPoolChunkSize);
            LOGGER.info("Use Netty Buffer Pool");
            break;
        default:
            bufferPool = new DirectByteBufferPool(bufferPoolPageSize, bufferPoolChunkSize, bufferPoolPageNumber, system.getFrontSocketSoRcvbuf());
            ;
            totalNetWorkBufferSize = bufferPoolPageSize * bufferPoolPageNumber;
    }
    /**
     * Off Heap For Merge/Order/Group/Limit 初始化
     */
    if (system.getUseOffHeapForMerge() == 1) {
        try {
            myCatMemory = new MyCatMemory(system, totalNetWorkBufferSize);
        } catch (NoSuchFieldException e) {
            LOGGER.error("NoSuchFieldException", e);
        } catch (IllegalAccessException e) {
            LOGGER.error("Error", e);
        }
    }
    businessExecutor = ExecutorUtil.create("BusinessExecutor", threadPoolSize);
    sequenceExecutor = ExecutorUtil.create("SequenceExecutor", threadPoolSize);
    timerExecutor = ExecutorUtil.create("Timer", system.getTimerExecutor());
    listeningExecutorService = MoreExecutors.listeningDecorator(businessExecutor);
    for (int i = 0; i < processors.length; i++) {
        processors[i] = new NIOProcessor("Processor" + i, bufferPool, businessExecutor);
    }
    if (aio) {
        LOGGER.info("using aio network handler ");
        asyncChannelGroups = new AsynchronousChannelGroup[processorCount];
        // startup connector
        connector = new AIOConnector();
        for (int i = 0; i < processors.length; i++) {
            asyncChannelGroups[i] = AsynchronousChannelGroup.withFixedThreadPool(processorCount, new ThreadFactory() {

                private int inx = 1;

                @Override
                public Thread newThread(Runnable r) {
                    Thread th = new Thread(r);
                    // TODO
                    th.setName(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "AIO" + (inx++));
                    LOGGER.info("created new AIO thread " + th.getName());
                    return th;
                }
            });
        }
        manager = new AIOAcceptor(NAME + "Manager", system.getBindIp(), system.getManagerPort(), mf, this.asyncChannelGroups[0]);
        // startup server
        server = new AIOAcceptor(NAME + "Server", system.getBindIp(), system.getServerPort(), sf, this.asyncChannelGroups[0]);
    } else {
        LOGGER.info("using nio network handler ");
        NIOReactorPool reactorPool = new NIOReactorPool(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "NIOREACTOR", processors.length);
        connector = new NIOConnector(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "NIOConnector", reactorPool);
        ((NIOConnector) connector).start();
        manager = new NIOAcceptor(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + NAME + "Manager", system.getBindIp(), system.getManagerPort(), mf, reactorPool);
        server = new NIOAcceptor(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + NAME + "Server", system.getBindIp(), system.getServerPort(), sf, reactorPool);
    }
    // manager start
    manager.start();
    LOGGER.info(manager.getName() + " is started and listening on " + manager.getPort());
    server.start();
    // server started
    LOGGER.info(server.getName() + " is started and listening on " + server.getPort());
    LOGGER.info("===============================================");
    // init datahost
    Map<String, PhysicalDBPool> dataHosts = config.getDataHosts();
    LOGGER.info("Initialize dataHost ...");
    for (PhysicalDBPool node : dataHosts.values()) {
        String index = dnIndexProperties.getProperty(node.getHostName(), "0");
        if (!"0".equals(index)) {
            LOGGER.info("init datahost: " + node.getHostName() + "  to use datasource index:" + index);
        }
        node.init(Integer.parseInt(index));
        node.startHeartbeat();
    }
    long dataNodeIldeCheckPeriod = system.getDataNodeIdleCheckPeriod();
    heartbeatScheduler.scheduleAtFixedRate(updateTime(), 0L, TIME_UPDATE_PERIOD, TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(processorCheck(), 0L, system.getProcessorCheckPeriod(), TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(dataNodeConHeartBeatCheck(dataNodeIldeCheckPeriod), 0L, dataNodeIldeCheckPeriod, TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(dataNodeHeartbeat(), 0L, system.getDataNodeHeartbeatPeriod(), TimeUnit.MILLISECONDS);
    heartbeatScheduler.scheduleAtFixedRate(dataSourceOldConsClear(), 0L, DEFAULT_OLD_CONNECTION_CLEAR_PERIOD, TimeUnit.MILLISECONDS);
    scheduler.schedule(catletClassClear(), 30000, TimeUnit.MILLISECONDS);
    if (system.getCheckTableConsistency() == 1) {
        scheduler.scheduleAtFixedRate(tableStructureCheck(), 0L, system.getCheckTableConsistencyPeriod(), TimeUnit.MILLISECONDS);
    }
    if (system.getUseSqlStat() == 1) {
        scheduler.scheduleAtFixedRate(recycleSqlStat(), 0L, DEFAULT_SQL_STAT_RECYCLE_PERIOD, TimeUnit.MILLISECONDS);
    }
    if (system.getUseGlobleTableCheck() == 1) {
        // 全局表一致性检测是否开启
        scheduler.scheduleAtFixedRate(glableTableConsistencyCheck(), 0L, system.getGlableTableCheckPeriod(), TimeUnit.MILLISECONDS);
    }
    // 定期清理结果集排行榜,控制拒绝策略
    scheduler.scheduleAtFixedRate(resultSetMapClear(), 0L, system.getClearBigSqLResultSetMapMs(), TimeUnit.MILLISECONDS);
    // new Thread(tableStructureCheck()).start();
    // XA Init recovery Log
    LOGGER.info("===============================================");
    LOGGER.info("Perform XA recovery log ...");
    performXARecoveryLog();
    if (isUseZkSwitch()) {
        // 首次启动如果发现zk上dnindex为空,则将本地初始化上zk
        initZkDnindex();
    }
    initRuleData();
    startup.set(true);
}
Also used : AIOConnector(io.mycat.net.AIOConnector) ThreadFactory(java.util.concurrent.ThreadFactory) SystemConfig(io.mycat.config.model.SystemConfig) NIOReactorPool(io.mycat.net.NIOReactorPool) ManagerConnectionFactory(io.mycat.manager.ManagerConnectionFactory) PhysicalDBPool(io.mycat.backend.datasource.PhysicalDBPool) NIOProcessor(io.mycat.net.NIOProcessor) ServerConnectionFactory(io.mycat.server.ServerConnectionFactory) NIOConnector(io.mycat.net.NIOConnector) SocketAcceptor(io.mycat.net.SocketAcceptor) DirectByteBufferPool(io.mycat.buffer.DirectByteBufferPool) NIOAcceptor(io.mycat.net.NIOAcceptor) AIOAcceptor(io.mycat.net.AIOAcceptor) NettyBufferPool(io.mycat.buffer.NettyBufferPool) MyCatMemory(io.mycat.memory.MyCatMemory)

Aggregations

NettyBufferPool (io.mycat.buffer.NettyBufferPool)4 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)2 DirectByteBufferPool (io.mycat.buffer.DirectByteBufferPool)2 SystemConfig (io.mycat.config.model.SystemConfig)2 ManagerConnectionFactory (io.mycat.manager.ManagerConnectionFactory)2 MyCatMemory (io.mycat.memory.MyCatMemory)2 AIOAcceptor (io.mycat.net.AIOAcceptor)2 AIOConnector (io.mycat.net.AIOConnector)2 NIOAcceptor (io.mycat.net.NIOAcceptor)2 NIOConnector (io.mycat.net.NIOConnector)2 NIOProcessor (io.mycat.net.NIOProcessor)2 NIOReactorPool (io.mycat.net.NIOReactorPool)2 SocketAcceptor (io.mycat.net.SocketAcceptor)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 FieldPacket (io.mycat.net.mysql.FieldPacket)2 RowDataPacket (io.mycat.net.mysql.RowDataPacket)2 ServerConnectionFactory (io.mycat.server.ServerConnectionFactory)2 PoolArenaMetric (io.netty.buffer.PoolArenaMetric)2 PoolChunkListMetric (io.netty.buffer.PoolChunkListMetric)2 PoolChunkMetric (io.netty.buffer.PoolChunkMetric)2