Skip to content

fix: use synchronized instead ReentrantLock in event processor #1291

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 1 commit into from
Jun 17, 2022
Merged
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 @@ -7,7 +7,6 @@
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -40,7 +39,6 @@ class EventProcessor<R extends HasMetadata> implements EventHandler, LifecycleAw
private final Map<ResourceID, RetryExecution> retryState = new HashMap<>();
private final ExecutorService executor;
private final String controllerName;
private final ReentrantLock lock = new ReentrantLock();
private final Metrics metrics;
private volatile boolean running;
private final Cache<R> cache;
Expand Down Expand Up @@ -97,8 +95,7 @@ private EventProcessor(
}

@Override
public void handleEvent(Event event) {
lock.lock();
public synchronized void handleEvent(Event event) {
try {
log.debug("Received event: {}", event);

Expand All @@ -113,7 +110,6 @@ public void handleEvent(Event event) {
}
handleMarkedEventForResource(resourceID);
} finally {
lock.unlock();
MDCUtils.removeResourceIDInfo();
}
}
Expand Down Expand Up @@ -201,57 +197,53 @@ private RetryInfo retryInfo(ResourceID resourceID) {
return retryState.get(resourceID);
}

void eventProcessingFinished(
synchronized void eventProcessingFinished(
ExecutionScope<R> executionScope, PostExecutionControl<R> postExecutionControl) {
lock.lock();
try {
if (!running) {
return;
}
ResourceID resourceID = executionScope.getResourceID();
log.debug(
"Event processing finished. Scope: {}, PostExecutionControl: {}",
executionScope,
postExecutionControl);
unsetUnderExecution(resourceID);

// 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 (isRetryConfigured()
&& postExecutionControl.exceptionDuringExecution()
&& !eventMarker.deleteEventPresent(resourceID)) {
handleRetryOnException(
executionScope, postExecutionControl.getRuntimeException().orElseThrow());
return;
}
cleanupOnSuccessfulExecution(executionScope);
metrics.finishedReconciliation(resourceID);
if (eventMarker.deleteEventPresent(resourceID)) {
cleanupForDeletedEvent(executionScope.getResourceID());
} else if (postExecutionControl.isFinalizerRemoved()) {
eventMarker.markProcessedMarkForDeletion(resourceID);
if (!running) {
return;
}
ResourceID resourceID = executionScope.getResourceID();
log.debug(
"Event processing finished. Scope: {}, PostExecutionControl: {}",
executionScope,
postExecutionControl);
unsetUnderExecution(resourceID);

// 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 (isRetryConfigured()
&& postExecutionControl.exceptionDuringExecution()
&& !eventMarker.deleteEventPresent(resourceID)) {
handleRetryOnException(
executionScope, postExecutionControl.getRuntimeException().orElseThrow());
return;
}
cleanupOnSuccessfulExecution(executionScope);
metrics.finishedReconciliation(resourceID);
if (eventMarker.deleteEventPresent(resourceID)) {
cleanupForDeletedEvent(executionScope.getResourceID());
} else if (postExecutionControl.isFinalizerRemoved()) {
eventMarker.markProcessedMarkForDeletion(resourceID);
} else {
postExecutionControl
.getUpdatedCustomResource()
.ifPresent(
r -> {
if (!postExecutionControl.updateIsStatusPatch()) {
eventSourceManager
.getControllerResourceEventSource()
.handleRecentResourceUpdate(
ResourceID.fromResource(r), r, executionScope.getResource());
}
});
if (eventMarker.eventPresent(resourceID)) {
submitReconciliationExecution(resourceID);
} else {
postExecutionControl
.getUpdatedCustomResource()
.ifPresent(
r -> {
if (!postExecutionControl.updateIsStatusPatch()) {
eventSourceManager
.getControllerResourceEventSource()
.handleRecentResourceUpdate(
ResourceID.fromResource(r), r, executionScope.getResource());
}
});
if (eventMarker.eventPresent(resourceID)) {
submitReconciliationExecution(resourceID);
} else {
reScheduleExecutionIfInstructed(postExecutionControl, executionScope.getResource());
}
reScheduleExecutionIfInstructed(postExecutionControl, executionScope.getResource());
}
} finally {
lock.unlock();
}

}

private void reScheduleExecutionIfInstructed(
Expand Down Expand Up @@ -343,24 +335,14 @@ private boolean isRetryConfigured() {
}

@Override
public void stop() {
lock.lock();
try {
this.running = false;
} finally {
lock.unlock();
}
public synchronized void stop() {
this.running = false;
}

@Override
public void start() throws OperatorException {
lock.lock();
try {
this.running = true;
handleAlreadyMarkedEvents();
} finally {
lock.unlock();
}
this.running = true;
handleAlreadyMarkedEvents();
}

private void handleAlreadyMarkedEvents() {
Expand Down