Skip to content

Only one scheduled event (re-schedul / retry) at one time #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,15 @@ void eventProcessingFinished(
// If a delete event present at this phase, it was received during reconciliation.
// So we either removed the finalizer during reconciliation or we don't use finalizers.
// Either way we don't want to retry.
if (retry != null && postExecutionControl.exceptionDuringExecution() &&
if (isRetryConfigured() && postExecutionControl.exceptionDuringExecution() &&
!eventMarker.deleteEventPresent(customResourceID)) {
handleRetryOnException(executionScope);
// todo revisit monitoring since events are not present anymore
// final var monitor = monitor(); executionScope.getEvents().forEach(e ->
// monitor.failedEvent(executionScope.getCustomResourceID(), e));
return;
}

if (retry != null) {
handleSuccessfulExecutionRegardingRetry(executionScope);
}
cleanupOnSuccessfulExecution(executionScope);
if (eventMarker.deleteEventPresent(customResourceID)) {
cleanupForDeletedEvent(executionScope.getCustomResourceID());
} else {
Expand Down Expand Up @@ -265,7 +262,7 @@ private boolean isCacheReadyForInstantReconciliation(ExecutionScope<R> execution
private void reScheduleExecutionIfInstructed(PostExecutionControl<R> postExecutionControl,
R customResource) {
postExecutionControl.getReScheduleDelay().ifPresent(delay -> eventSourceManager
.getRetryTimerEventSource()
.getRetryAndRescheduleTimerEventSource()
.scheduleOnce(customResource, delay));
}

Expand Down Expand Up @@ -295,19 +292,21 @@ private void handleRetryOnException(ExecutionScope<R> executionScope) {
delay,
customResourceID);
eventSourceManager
.getRetryTimerEventSource()
.getRetryAndRescheduleTimerEventSource()
.scheduleOnce(executionScope.getCustomResource(), delay);
},
() -> log.error("Exhausted retries for {}", executionScope));
}

private void handleSuccessfulExecutionRegardingRetry(ExecutionScope<R> executionScope) {
private void cleanupOnSuccessfulExecution(ExecutionScope<R> executionScope) {
log.debug(
"Marking successful execution for resource: {}",
"Cleanup for successful execution for resource: {}",
getName(executionScope.getCustomResource()));
retryState.remove(executionScope.getCustomResourceID());
if (isRetryConfigured()) {
retryState.remove(executionScope.getCustomResourceID());
}
eventSourceManager
.getRetryTimerEventSource()
.getRetryAndRescheduleTimerEventSource()
.cancelOnceSchedule(executionScope.getCustomResourceID());
}

Expand Down Expand Up @@ -337,6 +336,10 @@ private void unsetUnderExecution(CustomResourceID customResourceUid) {
underProcessing.remove(customResourceUid);
}

private boolean isRetryConfigured() {
return retry != null;
}

@Override
public void close() {
lock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DefaultEventSourceManager<R extends CustomResource<?, ?>>
private final ReentrantLock lock = new ReentrantLock();
private final Set<EventSource> eventSources = Collections.synchronizedSet(new HashSet<>());
private DefaultEventHandler<R> defaultEventHandler;
private TimerEventSource<R> retryTimerEventSource;
private TimerEventSource<R> retryAndRescheduleTimerEventSource;
private CustomResourceEventSource customResourceEventSource;

DefaultEventSourceManager(DefaultEventHandler<R> defaultEventHandler) {
Expand All @@ -39,8 +39,8 @@ private void init(DefaultEventHandler<R> defaultEventHandler) {
this.defaultEventHandler = defaultEventHandler;
defaultEventHandler.setEventSourceManager(this);

this.retryTimerEventSource = new TimerEventSource<>();
registerEventSource(retryTimerEventSource);
this.retryAndRescheduleTimerEventSource = new TimerEventSource<>();
registerEventSource(retryAndRescheduleTimerEventSource);
}

@Override
Expand Down Expand Up @@ -98,8 +98,8 @@ public void cleanupForCustomResource(CustomResourceID customResourceUid) {
}
}

public TimerEventSource getRetryTimerEventSource() {
return retryTimerEventSource;
public TimerEventSource getRetryAndRescheduleTimerEventSource() {
return retryAndRescheduleTimerEventSource;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private T clone(T customResource) {
/**
* This will ensure that the next event received after this method is called will not be filtered
* out.
*
*
* @param customResourceID - to which the event is related
*/
public void whitelistNextEvent(CustomResourceID customResourceID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DefaultEventHandlerTest {

@BeforeEach
public void setup() {
when(defaultEventSourceManagerMock.getRetryTimerEventSource())
when(defaultEventSourceManagerMock.getRetryAndRescheduleTimerEventSource())
.thenReturn(retryTimerEventSourceMock);
defaultEventHandler.setEventSourceManager(defaultEventSourceManagerMock);
defaultEventHandlerWithRetry.setEventSourceManager(defaultEventSourceManagerMock);
Expand Down Expand Up @@ -280,6 +280,17 @@ public void dontWhitelistsEventIfUpdatedEventInCache() {
verify(mockCREventSource, times(0)).whitelistNextEvent(eq(crID));
}

@Test
public void cancelScheduleOnceEventsOnSuccessfulExecution() {
var crID = new CustomResourceID("test-cr", TEST_NAMESPACE);
var cr = testCustomResource(crID);

defaultEventHandler.eventProcessingFinished(new ExecutionScope(cr, null),
PostExecutionControl.defaultDispatch());

verify(retryTimerEventSourceMock, times(1)).cancelOnceSchedule(eq(crID));
}

private CustomResourceID eventAlreadyUnderProcessing() {
when(eventDispatcherMock.handleExecution(any()))
.then(
Expand Down