sofastack / sofa-jraft

A production-grade java implementation of RAFT consensus algorithm.
https://www.sofastack.tech/projects/sofa-jraft/
Apache License 2.0
3.53k stars 1.12k forks source link

RpcProcessor接口中executor线程池导致堆内存运行时暴涨 #1026

Closed googlefan closed 9 months ago

googlefan commented 10 months ago

Your question

我在抽象 提交任务到leader 的processor 流程中, 尝试使用线程池来提高并发执行效率,但是在复写了 executor 方法后,实际运行时发现,堆内存涨的很快,但是整体服务的处理反而发生 阻塞,处理效率降低了.我是按照接口定义来使用这个方法的,我这么使用这个方法没问题的吧?为什么会这样呢?

Your scenes

我的代码如下:

public abstract class BaseAsyncProcessor<T extends Command> implements RpcProcessor<T> {
    protected RaftGroupService raftGroupService;

    protected BaseAsyncProcessor(RaftGroupService raftGroupService) {
        super();
        this.raftGroupService = raftGroupService;
    }
    //  这个线程池貌似没什么卵用,还可能增加Follower堆内存使用量可能是使用姿势不对,先不使用吧
    //    @Override
    //    public Executor executor() {
    //        return ThreadPoolUtil.newBuilder()
    //                             .poolName("JRaft-RPC-BZ-Processor")
    //                             .enableMetric(true)
    //                             .coreThreads(Utils.cpus())
    //                             .maximumThreads(Utils.cpus() * 3)
    //                             .keepAliveSeconds(60L)
    //                             .workQueue(new ArrayBlockingQueue<>(100))
    //                             .rejectedHandler(new ThreadPoolExecutor.AbortPolicy())
    //                             .threadFactory(new NamedThreadFactory("JRaft-RPC-BZ-Processor-", true))
    //                             .build();
    //    }

监控visualvm 发现堆内存比不使用这段代码 要大很多,该线程池线程的运行时执行态并没有(貌似线程池线程并没有工作).

Your advice

Describe the advice or solution you'd like

Environment

killme2008 commented 9 months ago

很简单,因为你的 executor 实现每次都在创建一个线程池,你应该在构造函数里创建,然后这里只是返回。