Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate netty boss and worker groups to improve the graceful shutdown. #178

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.9.0
- Netty boss and worker groups are separated [#178](https://github.com/logstash-plugins/logstash-input-http/pull/178)
As a result, when shutdown requested incoming connections are closed first and improved graceful shutdown

## 3.8.1
- bump netty to 4.1.109 [#173](https://github.com/logstash-plugins/logstash-input-http/pull/173)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.8.1
3.9.0
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class NettyHttpServer implements Runnable, Closeable {
private final int port;
private final int connectionBacklog = 128;

private final EventLoopGroup bossGroup;
private final EventLoopGroup processorGroup;
private final ThreadPoolExecutor executorGroup;
private final HttpResponseStatus responseStatus;
Expand All @@ -37,8 +38,14 @@ public NettyHttpServer(String host, int port, IMessageHandler messageHandler,
this.host = host;
this.port = port;
this.responseStatus = HttpResponseStatus.valueOf(responseCode);

// boss group is responsible for accepting incoming connections and sending to worker loop
// process group is channel handler, see the https://github.com/netty/netty/discussions/13305
// see the https://github.com/netty/netty/discussions/11808#discussioncomment-1610918 for why separation is good
bossGroup = new NioEventLoopGroup(1, daemonThreadFactory("http-input-connector"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review note: I think single thread for 128 backlog should be okay.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we took offline discussion, I have kept current thread config but added TODO to follow up:

  • default socket backlog is 1024, so which default acceptor thread count is better suite?
  • do we need to make it configurable or not? - sometimes providing internal config also makes user environment hard

processorGroup = new NioEventLoopGroup(threads, daemonThreadFactory("http-input-processor"));

// event handler group
executorGroup = new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(maxPendingRequests), daemonThreadFactory("http-input-handler-executor"),
new CustomRejectedExecutionHandler());
Expand All @@ -51,7 +58,7 @@ public NettyHttpServer(String host, int port, IMessageHandler messageHandler,
}

serverBootstrap = new ServerBootstrap()
.group(processorGroup)
.group(bossGroup, processorGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, connectionBacklog)
.childOption(ChannelOption.SO_KEEPALIVE, true)
Expand All @@ -73,7 +80,9 @@ public void run() {
public void close() {
try {
// stop accepting new connections first
processorGroup.shutdownGracefully(0, 10, TimeUnit.SECONDS).sync();
bossGroup.shutdownGracefully().sync();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review note: As my understanding, ServerBootstrap internally uses SingleThreadEventExecutor and with 0 quiet period, netty may directly terminate the tasks, so let's align with default to get better user experience.

// stop the worker group
processorGroup.shutdownGracefully().sync();
// then shutdown the message handler executor
executorGroup.shutdown();
try {
Expand Down