Skip to content

Commit 520e615

Browse files
authored
feature: improvements on reschedule (#587)
The the execution is rescheduled only if there are no buffered events.
1 parent abc689b commit 520e615

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void close() {
152152
}
153153
}
154154

155-
private void executeBufferedEvents(String customResourceUid) {
155+
private boolean executeBufferedEvents(String customResourceUid) {
156156
boolean newEventForResourceId = eventBuffer.containsEvents(customResourceUid);
157157
boolean controllerUnderExecution = isControllerUnderExecution(customResourceUid);
158158
Optional<CustomResource> latestCustomResource =
@@ -167,6 +167,7 @@ private void executeBufferedEvents(String customResourceUid) {
167167
retryInfo(customResourceUid));
168168
log.debug("Executing events for custom resource. Scope: {}", executionScope);
169169
executor.execute(new ControllerExecution(executionScope));
170+
return true;
170171
} else {
171172
log.debug(
172173
"Skipping executing controller for resource id: {}. Events in queue: {}."
@@ -175,6 +176,7 @@ private void executeBufferedEvents(String customResourceUid) {
175176
newEventForResourceId,
176177
controllerUnderExecution,
177178
latestCustomResource.isPresent());
179+
return false;
178180
}
179181
}
180182

@@ -211,8 +213,10 @@ void eventProcessingFinished(
211213
cleanupAfterDeletedEvent(executionScope.getCustomResourceUid());
212214
} else {
213215
cacheUpdatedResourceIfChanged(executionScope, postExecutionControl);
214-
reScheduleExecutionIfInstructed(postExecutionControl, executionScope.getCustomResource());
215-
executeBufferedEvents(executionScope.getCustomResourceUid());
216+
var executed = executeBufferedEvents(executionScope.getCustomResourceUid());
217+
if (!executed) {
218+
reScheduleExecutionIfInstructed(postExecutionControl, executionScope.getCustomResource());
219+
}
216220
}
217221
} finally {
218222
lock.unlock();

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/DefaultEventHandlerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ public void scheduleTimedEventIfInstructedByPostExecutionControl() {
232232
.scheduleOnce(any(), eq(testDelay));
233233
}
234234

235+
@Test
236+
public void reScheduleOnlyIfNotExecutedBufferedEvents() {
237+
var testDelay = 10000l;
238+
when(eventDispatcherMock.handleExecution(any()))
239+
.thenReturn(PostExecutionControl.defaultDispatch().withReSchedule(testDelay));
240+
241+
defaultEventHandler.handleEvent(prepareCREvent());
242+
defaultEventHandler.handleEvent(prepareCREvent());
243+
244+
verify(retryTimerEventSourceMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(0))
245+
.scheduleOnce(any(), eq(testDelay));
246+
}
247+
235248
@Test
236249
public void doNotFireEventsIfClosing() {
237250
defaultEventHandler.close();

samples/common/crd/test_object.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ metadata:
44
name: custom-service1
55
spec:
66
name: testservice1
7-
label: testlabel
7+
label: testlabel

0 commit comments

Comments
 (0)