dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

Threadpool timeout control on provider.

Open jieyisoft opened this issue 8 years ago • 5 comments

基于dubbo 现有线程池方式,在consumer端能进行超时控制。但是在provider端,比如60s内,线程必须完成,若没有完成必须要强制终止任务。基于这个业务场景,重新实现了一个新的Dispatcher。但担心在高并发场景下,是否会影响原始线程性能。或则是否有更好的实现方式? 部分代码如下: public class AlltimeoutDispatcher implements Dispatcher {

public static final String NAME = "alltimeout";

@Override
public ChannelHandler dispatch(ChannelHandler handler, URL url) {
    return new AlltimeoutChannelHandler(handler, url);
}

}

public void received(Channel channel, Object message) throws RemotingException { ExecutorService cexecutor = getExecutorService(); try {

    ChannelEventRunnable channelEventRunnable = new ChannelEventRunnable(channel, handler,
            ChannelState.RECEIVED, message);
    final int threadtimeout = url.getParameter("threadtimeout", DEFAULT_THREAD_TIME_OUT_MILLISECONDS);
    //线程需要超时时间控制
    final Future future = cexecutor.submit(channelEventRunnable);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //通过future控制每个线程的超时时间
                future.get(threadtimeout, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                logger.error("future time wait InterruptedException...");
                future.cancel(true);
            } catch (java.util.concurrent.ExecutionException e) {
                logger.error("future time wait concurrent.ExecutionException...");
                future.cancel(true);
            } catch (TimeoutException e) {
                logger.error("future time wait TimeoutException...");
                future.cancel(true);
            }
        }
    });

    t.start();

} catch (Throwable t) {
    throw new ExecutionException(message, channel, getClass() + " error when process received event .", t);
}

}

jieyisoft avatar Jan 12 '18 08:01 jieyisoft

I think your code may have some problems

chickenlj avatar Jan 15 '18 05:01 chickenlj

我目前也遇到这种问题,需要按照客户端超时时间,做服务端的终止线程操作,如果不终止会导致dubbo的业务处理线程池不够用

z-z-liu avatar Feb 08 '18 03:02 z-z-liu

@chickenlj 能具体说下什么问题吗

z-z-liu avatar Feb 08 '18 03:02 z-z-liu

通过日志可以看出因数据库性能波动,导致查询时间不稳定,现在不打算从数据库解决这个问题,想直接从服务端超时终止线程去优化

z-z-liu avatar Feb 08 '18 04:02 z-z-liu

我也遇到过,如果处理线程池不够用,会抛出异常,导致很多请求进不来了

luyuanwan avatar Oct 24 '19 15:10 luyuanwan