netty - 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 条消息上。你能提供一些指示吗?
解决方案
推荐阅读
- assembly - 我应该编写一个汇编级程序,使用 LDBA 指令打印我的名字,并在 Pep 9 中直接寻址
- python - Python比较图像中哪个RGB值具有最多像素
- html - 无法让我的导航栏下拉菜单“滑动”
- javascript - 将 onChange 逻辑添加到第三方 react-day-picker 元素
- helix-3d-toolkit - Helix Toolkit wpf 文档
- linux-kernel - 如何在内核空间中分配用户空间内存?
- javascript - 拒绝从 bundle.js 执行脚本,因为它的 MIME 类型
- javascript - 创建一个 html 按钮以启动例程并使用此函数将结果写入 HTML 页面
- python - 搜索结果中的 django get_absolute_url
- mysql - 自制,MySQL 8 支持