Skip to content

Commit dc0b4ee

Browse files
csvirimetacosm
andauthored
improve: lazy init workflow executor (#2266)
Signed-off-by: Attila Mészáros <csviri@gmail.com> Signed-off-by: Chris Laprun <claprun@redhat.com> Co-authored-by: Chris Laprun <claprun@redhat.com>
1 parent b880791 commit dc0b4ee

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ExecutorServiceManager {
2929
private ExecutorService workflowExecutor;
3030
private ExecutorService cachingExecutorService;
3131
private boolean started;
32+
private ConfigurationService configurationService;
3233

3334
ExecutorServiceManager(ConfigurationService configurationService) {
3435
start(configurationService);
@@ -95,30 +96,38 @@ public ExecutorService reconcileExecutorService() {
9596
}
9697

9798
public ExecutorService workflowExecutorService() {
99+
lazyInitWorkflowExecutorService();
98100
return workflowExecutor;
99101
}
100102

103+
private synchronized void lazyInitWorkflowExecutorService() {
104+
if (workflowExecutor == null) {
105+
workflowExecutor =
106+
new InstrumentedExecutorService(configurationService.getWorkflowExecutorService());
107+
}
108+
}
109+
101110
public ExecutorService cachingExecutorService() {
102111
return cachingExecutorService;
103112
}
104113

105114
public void start(ConfigurationService configurationService) {
106115
if (!started) {
116+
this.configurationService = configurationService; // used to lazy init workflow executor
107117
this.cachingExecutorService = Executors.newCachedThreadPool();
108118
this.executor = new InstrumentedExecutorService(configurationService.getExecutorService());
109-
this.workflowExecutor =
110-
new InstrumentedExecutorService(configurationService.getWorkflowExecutorService());
111119
started = true;
112120
}
113121
}
114122

115123
public void stop(Duration gracefulShutdownTimeout) {
116124
try {
117-
var parallelExec = Executors.newFixedThreadPool(3);
118125
log.debug("Closing executor");
126+
var parallelExec = Executors.newFixedThreadPool(3);
119127
parallelExec.invokeAll(List.of(shutdown(executor, gracefulShutdownTimeout),
120128
shutdown(workflowExecutor, gracefulShutdownTimeout),
121129
shutdown(cachingExecutorService, gracefulShutdownTimeout)));
130+
workflowExecutor = null;
122131
parallelExec.shutdownNow();
123132
started = false;
124133
} catch (InterruptedException e) {
@@ -130,6 +139,10 @@ public void stop(Duration gracefulShutdownTimeout) {
130139
private static Callable<Void> shutdown(ExecutorService executorService,
131140
Duration gracefulShutdownTimeout) {
132141
return () -> {
142+
// workflow executor can be null
143+
if (executorService == null) {
144+
return null;
145+
}
133146
executorService.shutdown();
134147
if (!executorService.awaitTermination(gracefulShutdownTimeout.toMillis(),
135148
TimeUnit.MILLISECONDS)) {

0 commit comments

Comments
 (0)