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

Fix ThreadPool creation #261

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Changes from 1 commit
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
26 changes: 18 additions & 8 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.security.auth.login.FailedLoginException;
Expand All @@ -68,6 +69,8 @@ public class App {
private static final int AD_MAX_MAG_INSTANCES =
4; // 1000 instances ought to be enough for anyone
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final String COLLECTION_POOL_NAME = "collectionPool";
private static final String RECOVERY_POOL_NAME = "recoveryPool";
tylerbenson marked this conversation as resolved.
Show resolved Hide resolved

private static int loopCounter;
private int lastJsonConfigTs;
Expand All @@ -90,12 +93,12 @@ public App(AppConfig appConfig) {
this.appConfig = appConfig;

ExecutorService collectionThreadPool =
buildExecutorService(appConfig.getThreadPoolSize());
buildExecutorService(appConfig.getThreadPoolSize(), COLLECTION_POOL_NAME);
collectionProcessor =
new TaskProcessor(collectionThreadPool, appConfig.getReporter());

ExecutorService recoveryThreadPool =
buildExecutorService(appConfig.getReconnectionThreadPoolSize());
buildExecutorService(appConfig.getReconnectionThreadPoolSize(), RECOVERY_POOL_NAME);
recoveryProcessor = new TaskProcessor(recoveryThreadPool, appConfig.getReporter());

// setup client
Expand Down Expand Up @@ -269,7 +272,8 @@ private void clearInstances(Collection<Instance> instances) {
+ "previous one hogging threads");
recoveryProcessor.stop();
recoveryProcessor.setThreadPoolExecutor(
buildExecutorService(appConfig.getReconnectionThreadPoolSize()));
buildExecutorService(appConfig.getReconnectionThreadPoolSize(),
RECOVERY_POOL_NAME));
}

List<TaskStatusHandler> statuses =
Expand Down Expand Up @@ -303,11 +307,15 @@ public TaskStatusHandler invoke(
* @param size The thread pool size
* @return The create executor
*/
private ExecutorService buildExecutorService(int size) {
private ExecutorService buildExecutorService(int size, final String poolName) {
return Executors.newFixedThreadPool(size, new ThreadFactory() {

private final AtomicInteger counter = new AtomicInteger(0);

@Override
public Thread newThread(Runnable runnable) {
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
String threadName = poolName + "-" + counter.incrementAndGet();
Thread thread = new Thread(runnable, threadName);
thread.setDaemon(appConfig.isDaemon());
return thread;
}
Expand Down Expand Up @@ -499,7 +507,7 @@ public void doIteration() {
+ "previous one hogging threads");
collectionProcessor.stop();
collectionProcessor.setThreadPoolExecutor(
buildExecutorService(appConfig.getThreadPoolSize()));
buildExecutorService(appConfig.getThreadPoolSize(), COLLECTION_POOL_NAME));
}

List<TaskStatusHandler> statuses =
Expand Down Expand Up @@ -585,7 +593,8 @@ private void fixBrokenInstances(Reporter reporter) {
+ "previous one hogging threads");
recoveryProcessor.stop();
recoveryProcessor.setThreadPoolExecutor(
buildExecutorService(appConfig.getReconnectionThreadPoolSize()));
buildExecutorService(appConfig.getReconnectionThreadPoolSize(),
RECOVERY_POOL_NAME));
}

Collections.shuffle(fixInstanceTasks);
Expand Down Expand Up @@ -914,7 +923,8 @@ public void init(boolean forceNewConnection) {
+ "previous one hogging threads");
recoveryProcessor.stop();
recoveryProcessor.setThreadPoolExecutor(
buildExecutorService(appConfig.getReconnectionThreadPoolSize()));
buildExecutorService(appConfig.getReconnectionThreadPoolSize(),
RECOVERY_POOL_NAME));
}

List<TaskStatusHandler> statuses =
Expand Down