Skip to content

Commit c62d624

Browse files
committed
feat: add possibility to configure termination timeout
Fixes #421
1 parent d91f1f7 commit c62d624

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ on [event sources](https://csviri.medium.com/java-operator-sdk-introduction-to-e
5454
- Updated Fabric8 client to version 5.4.0
5555
- It is now possible to configure the controllers to not automatically add finalizers to resources.
5656
See the `Controller` annotation documentation for more details.
57+
- Added the possibility to configure how many seconds the SDK will wait before terminating reconciliation threads when a shut down is requested
5758

5859
#### Overview of the 1.8.0 changes
5960

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

+12
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ default int concurrentReconciliationThreads() {
7979
default ObjectMapper getObjectMapper() {
8080
return new ObjectMapper();
8181
}
82+
83+
int DEFAULT_TERMINATION_TIMEOUT_SECONDS = 10;
84+
85+
/**
86+
* Retrieves the number of seconds the SDK waits for reconciliation threads to terminate before
87+
* shutting down.
88+
*
89+
* @return the number of seconds to wait before terminating reconciliation threads
90+
*/
91+
default int getTerminationTimeoutSeconds() {
92+
return DEFAULT_TERMINATION_TIMEOUT_SECONDS;
93+
}
8294
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/DefaultEventHandler.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.fabric8.kubernetes.client.dsl.MixedOperation;
99
import io.javaoperatorsdk.operator.api.ResourceController;
1010
import io.javaoperatorsdk.operator.api.RetryInfo;
11+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1112
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1213
import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager;
1314
import io.javaoperatorsdk.operator.processing.event.Event;
@@ -41,6 +42,7 @@ public class DefaultEventHandler implements EventHandler {
4142
private final Retry retry;
4243
private final Map<String, RetryExecution> retryState = new HashMap<>();
4344
private final String controllerName;
45+
private final int terminationTimeout;
4446
private DefaultEventSourceManager eventSourceManager;
4547

4648
private final ReentrantLock lock = new ReentrantLock();
@@ -51,18 +53,34 @@ public DefaultEventHandler(
5153
new EventDispatcher(controller, configuration, client),
5254
configuration.getName(),
5355
GenericRetry.fromConfiguration(configuration.getRetryConfiguration()),
54-
configuration.getConfigurationService().concurrentReconciliationThreads());
56+
configuration.getConfigurationService().concurrentReconciliationThreads(),
57+
configuration.getConfigurationService().getTerminationTimeoutSeconds());
5558
}
5659

5760
DefaultEventHandler(
5861
EventDispatcher eventDispatcher,
5962
String relatedControllerName,
6063
Retry retry,
6164
int concurrentReconciliationThreads) {
65+
this(
66+
eventDispatcher,
67+
relatedControllerName,
68+
retry,
69+
concurrentReconciliationThreads,
70+
ConfigurationService.DEFAULT_TERMINATION_TIMEOUT_SECONDS);
71+
}
72+
73+
private DefaultEventHandler(
74+
EventDispatcher eventDispatcher,
75+
String relatedControllerName,
76+
Retry retry,
77+
int concurrentReconciliationThreads,
78+
int terminationTimeout) {
6279
this.eventDispatcher = eventDispatcher;
6380
this.retry = retry;
6481
this.controllerName = relatedControllerName;
6582
eventBuffer = new EventBuffer();
83+
this.terminationTimeout = terminationTimeout;
6684
executor =
6785
new ScheduledThreadPoolExecutor(
6886
concurrentReconciliationThreads,
@@ -73,7 +91,7 @@ public DefaultEventHandler(
7391
public void close() {
7492
try {
7593
log.debug("Closing handler for {}", controllerName);
76-
executor.awaitTermination(10, TimeUnit.SECONDS);
94+
executor.awaitTermination(terminationTimeout, TimeUnit.SECONDS);
7795
} catch (InterruptedException e) {
7896
log.debug("Exception closing handler for {}: {}", controllerName, e.getLocalizedMessage());
7997
}

0 commit comments

Comments
 (0)