首页 > 解决方案 > Netty WriteAndFlush 间歇性卡住

问题描述

Netty 服务器引导:

EventLoopGroup bg = new NioEventLoopGroup(1, new CamelThreadFactory("Thread#", "NettyServerTCPBoss", false));
EventLoopGroup wg = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new CamelThreadFactory("Thread#", "NettyServerTCPWorker", false));
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);

Netty 管道配置:

    ch.pipeline().addLast("encoder", new StringEncoder());
    ch.pipeline().addLast("decoder", new RequestPayloadDecoder())); //This extends ByteToMessageDecoder
    ch.pipeline().addLast("idlehandler", new IdleStateHandler(..));
    ch.pipeline().addLast(new DefaultEventExecutorGroup(100),"handler", new 
org.apache.camel.component.netty4.handlers.ServerChannelHandler()); 

将 AutoRead 设置为 false

public void channelActive(ChannelHandlerContext ctx) throws Exception {
InetSocketAddress clientAddress = ((InetSocketAddress) ctx.channel().remoteAddress());
ctx.channel().config().setAutoRead(false);
ctx.channel().read();
}

channelRead0() 上的处理程序逻辑

 public void process(ChannelHandlerContext nettyChannelContext) throws Exception {
    if(validate() && channel.isWritable()){
    outboundMessages = //get from database
    for(List<Outbound> outbound : outboundMessages){
    if(channel().isWritable()){
    ChannelFuture writeFuture = nettyChannelContext.channel().writeAndFlush(outbound.getString()); //Send the message
    writeFuture.sync(); // wait for the IO to complete
    logger.info("Updating message status for event row id {}",outbound.getRowId);
    updateMessageStatusInDb(outbound);
    } } }
    ctx.channel.read();//Call read() after writes are done to process next inbound message 
    }

问题 一切似乎都运行良好,但间歇性地在 writeFuture.sync() 上卡住了 10 多分钟。我尝试使用侦听器而不是同步()。但是接收 operationComplete() 回调需要类似的时间。
示例日志:
2019-06-18 14:16:39.263 [Camel Thread #20 - NettyEventExecutorGroup] INFO 更新事件行 id::: 40257 的消息状态
2019-06-18 14:16:39.265 [Camel Thread #20 - NettyEventExecutorGroup ] INFO 更新事件行 id::: 40263 的消息状态
2019-06-18 14:16:39.266 [Camel Thread #20 - NettyEventExecutorGroup] INFO 更新事件行 id::: 40269 的消息状态
2019-06-18 14: 30:09.982[Camel 线程 #20 - NettyEventExecutorGroup] INFO 更新事件行 id::: 40275 的消息状态
2019-06-18 14:30:09.987 [Camel 线程 #20 - NettyEventExecutorGroup] INFO 更新事件行 id::: 42691 的消息状态

为什么 IO 需要这么长时间才能完成,对于每发送 30k 条消息,这发生在 40-50 条消息上。你能提供一些指示吗?

标签: netty

解决方案


推荐阅读