Skip to content

Commit 5514f18

Browse files
committed
feat: make debugging thread pool configurable
1 parent 9a5b395 commit 5514f18

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ExecutorServiceProducer.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.javaoperatorsdk.operator.api.config;
22

3-
import java.util.concurrent.BlockingQueue;
43
import java.util.concurrent.ExecutorService;
54
import java.util.concurrent.LinkedBlockingQueue;
6-
import java.util.concurrent.RejectedExecutionHandler;
7-
import java.util.concurrent.ThreadFactory;
85
import java.util.concurrent.ThreadPoolExecutor;
96
import java.util.concurrent.TimeUnit;
107
import java.util.concurrent.atomic.AtomicReference;
@@ -16,8 +13,7 @@ class ExecutorServiceProducer {
1613

1714
static ExecutorService getExecutor(int threadPoolSize) {
1815
final var gotSet =
19-
executor.compareAndSet(null, new DebugThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L,
20-
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()));
16+
executor.compareAndSet(null, new InstrumentedExecutorService(threadPoolSize));
2117
final var result = executor.get();
2218
if (!gotSet) {
2319
// check that we didn't try to change the pool size
@@ -30,35 +26,19 @@ static ExecutorService getExecutor(int threadPoolSize) {
3026
return result;
3127
}
3228

33-
private static class DebugThreadPoolExecutor extends ThreadPoolExecutor {
29+
private static class InstrumentedExecutorService extends ThreadPoolExecutor {
30+
private final boolean debug;
3431

35-
public DebugThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
36-
TimeUnit unit,
37-
BlockingQueue<Runnable> workQueue) {
38-
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
39-
}
40-
41-
public DebugThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
42-
TimeUnit unit, BlockingQueue<Runnable> workQueue,
43-
ThreadFactory threadFactory) {
44-
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
45-
}
46-
47-
public DebugThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
48-
TimeUnit unit, BlockingQueue<Runnable> workQueue,
49-
RejectedExecutionHandler handler) {
50-
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
51-
}
52-
53-
public DebugThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
54-
TimeUnit unit, BlockingQueue<Runnable> workQueue,
55-
ThreadFactory threadFactory, RejectedExecutionHandler handler) {
56-
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
32+
public InstrumentedExecutorService(int corePoolSize) {
33+
super(corePoolSize, corePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
34+
debug = Utils.debugThreadPool();
5735
}
5836

5937
@Override
6038
public void shutdown() {
61-
Thread.dumpStack();
39+
if (debug) {
40+
Thread.dumpStack();
41+
}
6242
super.shutdown();
6343
}
6444
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/Utils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Utils {
1313

1414
private static final Logger log = LoggerFactory.getLogger(Utils.class);
1515
public static final String CHECK_CRD_ENV_KEY = "JAVA_OPERATOR_SDK_CHECK_CRD";
16+
public static final String DEBUG_THREAD_POOL_ENV_KEY = "JAVA_OPERATOR_SDK_DEBUG_THREAD_POOL";
1617

1718
/**
1819
* Attempts to load version information from a properties file produced at build time, currently
@@ -60,7 +61,15 @@ public static boolean isValidateCustomResourcesEnvVarSet() {
6061
}
6162

6263
public static boolean shouldCheckCRDAndValidateLocalModel() {
63-
final var value = System.getProperty(CHECK_CRD_ENV_KEY);
64+
return getBooleanEnvProperty(CHECK_CRD_ENV_KEY);
65+
}
66+
67+
private static boolean getBooleanEnvProperty(String envKey) {
68+
final var value = System.getProperty(envKey);
6469
return value == null || Boolean.getBoolean(value);
6570
}
71+
72+
public static boolean debugThreadPool() {
73+
return getBooleanEnvProperty(DEBUG_THREAD_POOL_ENV_KEY);
74+
}
6675
}

0 commit comments

Comments
 (0)