Skip to content

Commit 9facd8f

Browse files
committed
refactor: unify executor services management
1 parent bf26c88 commit 9facd8f

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,35 @@
1515
public class ExecutorServiceManager {
1616
private static final Logger log = LoggerFactory.getLogger(ExecutorServiceManager.class);
1717
private static ExecutorServiceManager instance;
18-
1918
private final ExecutorService executor;
20-
private ExecutorService workflowExecutor;
19+
private final ExecutorService workflowExecutor;
2120
private final int terminationTimeoutSeconds;
2221

23-
private ExecutorServiceManager(InstrumentedExecutorService executor,
22+
private ExecutorServiceManager(ExecutorService executor, ExecutorService workflowExecutor,
2423
int terminationTimeoutSeconds) {
25-
this.executor = executor;
24+
this.executor = new InstrumentedExecutorService(executor);
25+
this.workflowExecutor = new InstrumentedExecutorService(workflowExecutor);
2626
this.terminationTimeoutSeconds = terminationTimeoutSeconds;
2727
}
2828

2929
public static void init() {
3030
if (instance == null) {
3131
final var configuration = ConfigurationServiceProvider.instance();
32-
instance = new ExecutorServiceManager(
33-
new InstrumentedExecutorService(configuration.getExecutorService()),
32+
final var executorService = configuration.getExecutorService();
33+
final var workflowExecutorService = configuration.getWorkflowExecutorService();
34+
instance = new ExecutorServiceManager(executorService, workflowExecutorService,
3435
configuration.getTerminationTimeoutSeconds());
35-
log.debug("Initialized ExecutorServiceManager executor: {}, timeout: {}",
36-
configuration.getExecutorService().getClass(),
36+
log.debug(
37+
"Initialized ExecutorServiceManager executor: {}, workflow executor: {}, timeout: {}",
38+
executorService.getClass(),
39+
workflowExecutorService.getClass(),
3740
configuration.getTerminationTimeoutSeconds());
3841
} else {
3942
log.debug("Already started, reusing already setup instance!");
4043
}
4144
}
4245

43-
public static void stop() {
46+
public synchronized static void stop() {
4447
if (instance != null) {
4548
instance.doStop();
4649
}
@@ -49,7 +52,7 @@ public static void stop() {
4952
instance = null;
5053
}
5154

52-
public static ExecutorServiceManager instance() {
55+
public synchronized static ExecutorServiceManager instance() {
5356
if (instance == null) {
5457
// provide a default configuration if none has been provided by init
5558
init();
@@ -61,26 +64,17 @@ public ExecutorService executorService() {
6164
return executor;
6265
}
6366

64-
// needs to be synchronized since only called when a workflow is initialized, but that happens in
65-
// controllers which are started in parallel
66-
public synchronized ExecutorService workflowExecutorService() {
67-
if (workflowExecutor == null) {
68-
workflowExecutor =
69-
new InstrumentedExecutorService(
70-
ConfigurationServiceProvider.instance().getWorkflowExecutorService());
71-
}
67+
public ExecutorService workflowExecutorService() {
7268
return workflowExecutor;
7369
}
7470

7571
private void doStop() {
7672
try {
7773
log.debug("Closing executor");
7874
executor.shutdown();
79-
if (workflowExecutor != null) {
80-
workflowExecutor.shutdown();
81-
if (!workflowExecutor.awaitTermination(terminationTimeoutSeconds, TimeUnit.SECONDS)) {
82-
workflowExecutor.shutdownNow(); // if we timed out, waiting, cancel everything
83-
}
75+
workflowExecutor.shutdown();
76+
if (!workflowExecutor.awaitTermination(terminationTimeoutSeconds, TimeUnit.SECONDS)) {
77+
workflowExecutor.shutdownNow(); // if we timed out, waiting, cancel everything
8478
}
8579
if (!executor.awaitTermination(terminationTimeoutSeconds, TimeUnit.SECONDS)) {
8680
executor.shutdownNow(); // if we timed out, waiting, cancel everything

0 commit comments

Comments
 (0)