-
Notifications
You must be signed in to change notification settings - Fork 80
Graceful shutdown improvements and undefined method 'accept'
fix.
#500
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
Merged
mashhurs
merged 8 commits into
logstash-plugins:main
from
mashhurs:fix-crash-with-no-method-accept
Aug 28, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa58877
Fix the abnormal case where codec is not available to accept the even…
mashhurs 01be0be
Improving gracefully shutdown process in a number of better ways: clo…
mashhurs 5a77a31
Version up and add a descriptive changelog.
mashhurs ca69fab
Apply suggestions from code review
mashhurs 577efd7
Wait for worker group graceful shutdown before event executors.
mashhurs a29da32
Update src/main/java/org/logstash/beats/Server.java
mashhurs be59fb1
Separate event executors termination with pending tasks into new method.
mashhurs 54323de
Changelog revised.
mashhurs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6.8.4 | ||
6.9.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ | |
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.handler.timeout.IdleStateHandler; | ||
import io.netty.util.concurrent.DefaultEventExecutorGroup; | ||
import io.netty.util.concurrent.EventExecutor; | ||
import io.netty.util.concurrent.EventExecutorGroup; | ||
import io.netty.util.concurrent.SingleThreadEventExecutor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.logstash.netty.SslHandlerProvider; | ||
|
@@ -23,6 +25,7 @@ public class Server { | |
private final String host; | ||
private final int eventLoopThreadCount; | ||
private final int executorThreadCount; | ||
private NioEventLoopGroup bossGroup; | ||
private NioEventLoopGroup workGroup; | ||
private IMessageListener messageListener = new MessageListener(); | ||
private SslHandlerProvider sslHandlerProvider; | ||
|
@@ -51,14 +54,15 @@ public Server listen() throws InterruptedException { | |
logger.error("Could not shut down worker group before starting", e); | ||
} | ||
} | ||
bossGroup = new NioEventLoopGroup(eventLoopThreadCount); // TODO: add a config to make it adjustable, no need many threads | ||
workGroup = new NioEventLoopGroup(eventLoopThreadCount); | ||
try { | ||
logger.info("Starting server on port: {}", this.port); | ||
|
||
beatsInitializer = new BeatsInitializer(messageListener, clientInactivityTimeoutSeconds, executorThreadCount); | ||
|
||
ServerBootstrap server = new ServerBootstrap(); | ||
server.group(workGroup) | ||
server.group(bossGroup, workGroup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Till now, we were using workgroup for handling incoming socket connections and there was no guarantee socket handlers got terminated first. With this separation we will have two major improvements:
References: |
||
.channel(NioServerSocketChannel.class) | ||
.childOption(ChannelOption.SO_LINGER, 0) // Since the protocol doesn't support yet a remote close from the server and we don't want to have 'unclosed' socket lying around we have to use `SO_LINGER` to force the close of the socket. | ||
.childHandler(beatsInitializer); | ||
|
@@ -83,10 +87,23 @@ public void stop() { | |
} | ||
|
||
private void shutdown() { | ||
// as much as possible we try to gracefully shut down | ||
// no longer accept incoming socket connections | ||
// with event loop group (workGroup) declares quite period with `shutdownGracefully` which queued tasks will not receive work for the best cases | ||
// executor group threads will be asked and waited to gracefully shutdown if they have pending tasks. This helps each individual handler process the event/exception, especially when multichannel use case. | ||
// there is no guarantee that executor threads will terminate during the shutdown because of many factors such as ack processing | ||
// so any pending tasks which send batches to BeatsHandler will be ignored | ||
try { | ||
// boss group shuts down socket connections | ||
// shutting down bossGroup separately gives us faster channel closure | ||
if (bossGroup != null) { | ||
bossGroup.shutdownGracefully().sync(); | ||
} | ||
|
||
if (workGroup != null) { | ||
workGroup.shutdownGracefully().sync(); | ||
} | ||
|
||
if (beatsInitializer != null) { | ||
beatsInitializer.shutdownEventExecutor(); | ||
} | ||
|
@@ -160,10 +177,31 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E | |
public void shutdownEventExecutor() { | ||
try { | ||
idleExecutorGroup.shutdownGracefully().sync(); | ||
|
||
shutdownEventExecutorsWithPendingTasks(); | ||
|
||
// make sure non-pending tasked executors get terminated | ||
beatsHandlerExecutorGroup.shutdownGracefully().sync(); | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private void shutdownEventExecutorsWithPendingTasks() { | ||
try { | ||
// DefaultEventExecutorGroup internally executes numbers of SingleThreadEventExecutor | ||
// try to gracefully shut down every thread if they have unacked pending batches (pending tasks) | ||
for (final EventExecutor eventExecutor : beatsHandlerExecutorGroup) { | ||
if (eventExecutor instanceof SingleThreadEventExecutor) { | ||
final SingleThreadEventExecutor singleExecutor = (SingleThreadEventExecutor) eventExecutor; | ||
if (singleExecutor.pendingTasks() > 0) { | ||
singleExecutor.shutdownGracefully().sync(); | ||
} | ||
} | ||
} | ||
mashhurs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.