Skip to content

Commit 176dcaf

Browse files
jsvdmashhurs
andauthored
Backport #511: properly name netty threads (#512)
Co-authored-by: Mashhur <[email protected]>
1 parent 5941a0f commit 176dcaf

File tree

8 files changed

+56
-16
lines changed

8 files changed

+56
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 6.9.2
2+
- Name netty threads according to their purpose and the plugin id [#511](https://github.com/logstash-plugins/logstash-input-beats/pull/511)
3+
14
## 6.9.1
25
- Upgrade netty to 4.1.115 [#507](https://github.com/logstash-plugins/logstash-input-beats/pull/507)
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.9.1
1+
6.9.2

lib/logstash/inputs/beats.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def register
247247
end # def register
248248

249249
def create_server
250-
server = org.logstash.beats.Server.new(@host, @port, @client_inactivity_timeout, @event_loop_threads, @executor_threads)
250+
server = org.logstash.beats.Server.new(@id, @host, @port, @client_inactivity_timeout, @event_loop_threads, @executor_threads)
251251
server.setSslHandlerProvider(new_ssl_handshake_provider(new_ssl_context_builder)) if @ssl_enabled
252252
server
253253
end

spec/inputs/beats_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
let(:port) { 9001 }
3939

4040
it "sends the required options to the server" do
41-
expect(org.logstash.beats.Server).to receive(:new).with(host, port, client_inactivity_timeout, event_loop_threads, executor_threads)
41+
expect(org.logstash.beats.Server).to receive(:new).with(plugin.id, host, port, client_inactivity_timeout, event_loop_threads, executor_threads)
4242
subject.register
4343
end
4444
end
@@ -531,8 +531,8 @@
531531
subject(:plugin) { LogStash::Inputs::Beats.new(config) }
532532

533533
before do
534-
@server = org.logstash.beats.Server.new(host, port, client_inactivity_timeout, event_loop_threads, executor_threads)
535-
expect( org.logstash.beats.Server ).to receive(:new).with(host, port, client_inactivity_timeout, event_loop_threads, executor_threads).and_return @server
534+
@server = org.logstash.beats.Server.new(plugin.id, host, port, client_inactivity_timeout, event_loop_threads, executor_threads)
535+
expect( org.logstash.beats.Server ).to receive(:new).with(plugin.id, host, port, client_inactivity_timeout, event_loop_threads, executor_threads).and_return @server
536536
expect( @server ).to receive(:listen)
537537

538538
subject.register

src/main/java/org/logstash/beats/Runner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static public void main(String[] args) throws Exception {
1717
// Check for leaks.
1818
// ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
1919

20-
Server server = new Server("0.0.0.0", DEFAULT_PORT, 15, 0, Runtime.getRuntime().availableProcessors());
20+
Server server = new Server("test", "0.0.0.0", DEFAULT_PORT, 15, 0, Runtime.getRuntime().availableProcessors());
2121

2222
if(args.length > 0 && args[0].equals("ssl")) {
2323
logger.debug("Using SSL");

src/main/java/org/logstash/beats/Server.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import org.apache.logging.log4j.Logger;
1919
import org.logstash.netty.SslHandlerProvider;
2020

21+
import static org.logstash.beats.util.DaemonThreadFactory.daemonThreadFactory;
22+
2123
public class Server {
2224
private final static Logger logger = LogManager.getLogger(Server.class);
2325

2426
private final int port;
27+
28+
private final String id;
2529
private final String host;
2630
private final int eventLoopThreadCount;
2731
private final int executorThreadCount;
@@ -33,7 +37,8 @@ public class Server {
3337

3438
private final int clientInactivityTimeoutSeconds;
3539

36-
public Server(String host, int port, int clientInactivityTimeoutSeconds, int eventLoopThreadCount, int executorThreadCount) {
40+
public Server(String id, String host, int port, int clientInactivityTimeoutSeconds, int eventLoopThreadCount, int executorThreadCount) {
41+
this.id = id;
3742
this.host = host;
3843
this.port = port;
3944
this.clientInactivityTimeoutSeconds = clientInactivityTimeoutSeconds;
@@ -54,12 +59,12 @@ public Server listen() throws InterruptedException {
5459
logger.error("Could not shut down worker group before starting", e);
5560
}
5661
}
57-
bossGroup = new NioEventLoopGroup(eventLoopThreadCount); // TODO: add a config to make it adjustable, no need many threads
58-
workGroup = new NioEventLoopGroup(eventLoopThreadCount);
62+
bossGroup = new NioEventLoopGroup(eventLoopThreadCount, daemonThreadFactory(id + "-bossGroup")); // TODO: add a config to make it adjustable, no need many threads
63+
workGroup = new NioEventLoopGroup(eventLoopThreadCount, daemonThreadFactory(id + "-workGroup"));
5964
try {
6065
logger.info("Starting server on port: {}", this.port);
6166

62-
beatsInitializer = new BeatsInitializer(messageListener, clientInactivityTimeoutSeconds, executorThreadCount);
67+
beatsInitializer = new BeatsInitializer(id, messageListener, clientInactivityTimeoutSeconds, executorThreadCount);
6368

6469
ServerBootstrap server = new ServerBootstrap();
6570
server.group(bossGroup, workGroup)
@@ -143,12 +148,14 @@ private class BeatsInitializer extends ChannelInitializer<SocketChannel> {
143148
private final IMessageListener localMessageListener;
144149
private final int localClientInactivityTimeoutSeconds;
145150

146-
BeatsInitializer(IMessageListener messageListener, int clientInactivityTimeoutSeconds, int beatsHandlerThread) {
151+
BeatsInitializer(String pluginId, IMessageListener messageListener, int clientInactivityTimeoutSeconds, int beatsHandlerThreadCount) {
147152
// Keeps a local copy of Server settings, so they can't be modified once it starts listening
148153
this.localMessageListener = messageListener;
149154
this.localClientInactivityTimeoutSeconds = clientInactivityTimeoutSeconds;
150-
idleExecutorGroup = new DefaultEventExecutorGroup(DEFAULT_IDLESTATEHANDLER_THREAD);
151-
beatsHandlerExecutorGroup = new DefaultEventExecutorGroup(beatsHandlerThread);
155+
idleExecutorGroup = new DefaultEventExecutorGroup(DEFAULT_IDLESTATEHANDLER_THREAD,
156+
daemonThreadFactory(pluginId + "-idleStateHandler"));
157+
beatsHandlerExecutorGroup = new DefaultEventExecutorGroup(beatsHandlerThreadCount,
158+
daemonThreadFactory(pluginId + "-beatsHandler"));
152159
}
153160

154161
public void initChannel(SocketChannel socket) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.logstash.beats.util;
2+
3+
import java.util.concurrent.ThreadFactory;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
public class DaemonThreadFactory implements ThreadFactory {
7+
8+
final ThreadGroup group;
9+
final AtomicInteger threadNumber = new AtomicInteger(1);
10+
final String namePrefix;
11+
12+
DaemonThreadFactory(String namePrefix) {
13+
this.namePrefix = namePrefix;
14+
group = Thread.currentThread().getThreadGroup();
15+
}
16+
17+
@Override
18+
public Thread newThread(Runnable r) {
19+
Thread t = new Thread(group, r,
20+
namePrefix + "[T#" + threadNumber.getAndIncrement() + "]",
21+
0);
22+
t.setDaemon(true);
23+
return t;
24+
}
25+
26+
public static ThreadFactory daemonThreadFactory(String namePrefix) {
27+
return new DaemonThreadFactory(namePrefix);
28+
}
29+
30+
}

src/test/java/org/logstash/beats/ServerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testServerShouldTerminateConnectionWhenExceptionHappen() throws Inte
5252

5353
final CountDownLatch latch = new CountDownLatch(concurrentConnections);
5454

55-
final Server server = new Server(host, randomPort, inactivityTime, eventLoopThreadCount, executorThreadCount);
55+
final Server server = new Server("testServer", host, randomPort, inactivityTime, eventLoopThreadCount, executorThreadCount);
5656

5757
final AtomicBoolean otherCause = new AtomicBoolean(false);
5858

@@ -118,7 +118,7 @@ public void testServerShouldTerminateConnectionIdleForTooLong() throws Interrupt
118118

119119
final CountDownLatch latch = new CountDownLatch(concurrentConnections);
120120
final AtomicBoolean exceptionClose = new AtomicBoolean(false);
121-
final Server server = new Server(host, randomPort, inactivityTime, eventLoopThreadCount, executorThreadCount);
121+
final Server server = new Server("testServer", host, randomPort, inactivityTime, eventLoopThreadCount, executorThreadCount);
122122
server.setMessageListener(new MessageListener() {
123123
@Override
124124
public void onNewConnection(ChannelHandlerContext ctx) {
@@ -174,7 +174,7 @@ public void run() {
174174

175175
@Test
176176
public void testServerShouldAcceptConcurrentConnection() throws InterruptedException {
177-
final Server server = new Server(host, randomPort, 30, eventLoopThreadCount, executorThreadCount);
177+
final Server server = new Server("testServer", host, randomPort, 30, eventLoopThreadCount, executorThreadCount);
178178
SpyListener listener = new SpyListener();
179179
server.setMessageListener(listener);
180180
Runnable serverTask = new Runnable() {

0 commit comments

Comments
 (0)