1414import io .javaoperatorsdk .operator .OperatorException ;
1515import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
1616import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
17+ import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
1718import io .javaoperatorsdk .operator .api .config .ExecutorServiceManager ;
1819import io .javaoperatorsdk .operator .api .monitoring .Metrics ;
1920import io .javaoperatorsdk .operator .api .reconciler .Constants ;
@@ -37,71 +38,64 @@ public class EventProcessor<R extends HasMetadata> implements EventHandler, Life
3738 private static final long MINIMAL_RATE_LIMIT_RESCHEDULE_DURATION = 50 ;
3839
3940 private volatile boolean running ;
41+ private final ControllerConfiguration <?> controllerConfiguration ;
4042 private final ReconciliationDispatcher <R > reconciliationDispatcher ;
4143 private final Retry retry ;
4244 private final ExecutorService executor ;
43- private final String controllerName ;
4445 private final Metrics metrics ;
4546 private final Cache <R > cache ;
4647 private final EventSourceManager <R > eventSourceManager ;
4748 private final RateLimiter <? extends RateLimitState > rateLimiter ;
4849 private final ResourceStateManager resourceStateManager = new ResourceStateManager ();
4950 private final Map <String , Object > metricsMetadata ;
5051
52+
5153 public EventProcessor (EventSourceManager <R > eventSourceManager ) {
5254 this (
55+ eventSourceManager .getController ().getConfiguration (),
5356 eventSourceManager .getControllerResourceEventSource (),
5457 ExecutorServiceManager .instance ().executorService (),
55- eventSourceManager .getController ().getConfiguration ().getName (),
5658 new ReconciliationDispatcher <>(eventSourceManager .getController ()),
57- eventSourceManager .getController ().getConfiguration ().getRetry (),
5859 ConfigurationServiceProvider .instance ().getMetrics (),
59- eventSourceManager .getController ().getConfiguration ().getRateLimiter (),
6060 eventSourceManager );
6161 }
6262
6363 @ SuppressWarnings ("rawtypes" )
6464 EventProcessor (
65+ ControllerConfiguration controllerConfiguration ,
6566 ReconciliationDispatcher <R > reconciliationDispatcher ,
6667 EventSourceManager <R > eventSourceManager ,
67- String relatedControllerName ,
68- Retry retry ,
69- RateLimiter rateLimiter ,
7068 Metrics metrics ) {
7169 this (
70+ controllerConfiguration ,
7271 eventSourceManager .getControllerResourceEventSource (),
7372 null ,
74- relatedControllerName ,
7573 reconciliationDispatcher ,
76- retry ,
7774 metrics ,
78- rateLimiter ,
7975 eventSourceManager );
8076 }
8177
8278 @ SuppressWarnings ({"rawtypes" , "unchecked" })
8379 private EventProcessor (
80+ ControllerConfiguration controllerConfiguration ,
8481 Cache <R > cache ,
8582 ExecutorService executor ,
86- String relatedControllerName ,
8783 ReconciliationDispatcher <R > reconciliationDispatcher ,
88- Retry retry ,
8984 Metrics metrics ,
90- RateLimiter rateLimiter ,
9185 EventSourceManager <R > eventSourceManager ) {
86+ this .controllerConfiguration = controllerConfiguration ;
9287 this .running = false ;
9388 this .executor =
9489 executor == null
9590 ? new ScheduledThreadPoolExecutor (
9691 ConfigurationService .DEFAULT_RECONCILIATION_THREADS_NUMBER )
9792 : executor ;
98- this .controllerName = relatedControllerName ;
9993 this .reconciliationDispatcher = reconciliationDispatcher ;
100- this .retry = retry ;
94+ this .retry = controllerConfiguration . getRetry () ;
10195 this .cache = cache ;
10296 this .metrics = metrics != null ? metrics : Metrics .NOOP ;
10397 this .eventSourceManager = eventSourceManager ;
104- this .rateLimiter = rateLimiter ;
98+ this .rateLimiter = controllerConfiguration . getRateLimiter () ;
10599
106100 metricsMetadata = Optional .ofNullable (eventSourceManager .getController ())
107101 .map (Controller ::getAssociatedGroupVersionKind )
@@ -272,18 +266,31 @@ synchronized void eventProcessingFinished(
272266 reScheduleExecutionIfInstructed (postExecutionControl , executionScope .getResource ());
273267 }
274268 }
275-
276269 }
277270
278271 private void reScheduleExecutionIfInstructed (
279272 PostExecutionControl <R > postExecutionControl , R customResource ) {
273+
280274 postExecutionControl
281275 .getReScheduleDelay ()
282- .ifPresent (delay -> {
276+ .ifPresentOrElse (delay -> {
283277 var resourceID = ResourceID .fromResource (customResource );
284278 log .debug ("ReScheduling event for resource: {} with delay: {}" ,
285279 resourceID , delay );
286280 retryEventSource ().scheduleOnce (resourceID , delay );
281+ }, () -> scheduleExecutionForMaxReconciliationInterval (customResource ));
282+ }
283+
284+ private void scheduleExecutionForMaxReconciliationInterval (R customResource ) {
285+ this .controllerConfiguration
286+ .maxReconciliationInterval ()
287+ .ifPresent (m -> {
288+ var resourceID = ResourceID .fromResource (customResource );
289+ var delay = m .toMillis ();
290+ log .debug ("ReScheduling event for resource because for max reconciliation interval: " +
291+ "{} with delay: {}" ,
292+ resourceID , delay );
293+ retryEventSource ().scheduleOnce (resourceID , delay );
287294 });
288295 }
289296
@@ -319,7 +326,10 @@ private void handleRetryOnException(
319326 metrics .failedReconciliation (resourceID , exception , metricsMetadata );
320327 retryEventSource ().scheduleOnce (resourceID , delay );
321328 },
322- () -> log .error ("Exhausted retries for {}" , executionScope ));
329+ () -> {
330+ log .error ("Exhausted retries for {}" , executionScope );
331+ scheduleExecutionForMaxReconciliationInterval (executionScope .getResource ());
332+ });
323333 }
324334
325335 private void cleanupOnSuccessfulExecution (ExecutionScope <R > executionScope ) {
@@ -390,7 +400,7 @@ public void run() {
390400 final var name = thread .getName ();
391401 try {
392402 MDCUtils .addResourceInfo (executionScope .getResource ());
393- thread .setName ("ReconcilerExecutor-" + controllerName + "-" + thread .getId ());
403+ thread .setName ("ReconcilerExecutor-" + controllerName () + "-" + thread .getId ());
394404 PostExecutionControl <R > postExecutionControl =
395405 reconciliationDispatcher .handleExecution (executionScope );
396406 eventProcessingFinished (executionScope , postExecutionControl );
@@ -403,10 +413,14 @@ public void run() {
403413
404414 @ Override
405415 public String toString () {
406- return controllerName + " -> " + executionScope ;
416+ return controllerName () + " -> " + executionScope ;
407417 }
408418 }
409419
420+ private String controllerName () {
421+ return controllerConfiguration .getName ();
422+ }
423+
410424 public synchronized boolean isUnderProcessing (ResourceID resourceID ) {
411425 return isControllerUnderExecution (resourceStateManager .getOrCreate (resourceID ));
412426 }
0 commit comments