Skip to content

fix: race condition in workflow reconciler #2549

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
Oct 10, 2024
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
@@ -1,5 +1,6 @@
package io.javaoperatorsdk.operator.processing.dependent.workflow;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -36,7 +37,7 @@ protected AbstractWorkflowExecutor(DefaultWorkflow<P> workflow, P primary, Conte
this.context = context;
this.primaryID = ResourceID.fromResource(primary);
executorService = context.getWorkflowExecutorService();
results = new ConcurrentHashMap<>(workflow.getDependentResourcesByName().size());
results = new HashMap<>(workflow.getDependentResourcesByName().size());
}

protected abstract Logger logger();
Expand Down Expand Up @@ -84,13 +85,13 @@ protected boolean isMarkedForDelete(DependentResourceNode<?, P> drn) {
return getResultFlagFor(drn, WorkflowResult.DetailBuilder::isMarkedForDelete);
}

protected WorkflowResult.DetailBuilder createOrGetResultFor(
protected synchronized WorkflowResult.DetailBuilder createOrGetResultFor(
DependentResourceNode<?, P> dependentResourceNode) {
return results.computeIfAbsent(dependentResourceNode,
unused -> new WorkflowResult.DetailBuilder());
}

protected Optional<WorkflowResult.DetailBuilder<?>> getResultFor(
protected synchronized Optional<WorkflowResult.DetailBuilder<?>> getResultFor(
DependentResourceNode<?, P> dependentResourceNode) {
return Optional.ofNullable(results.get(dependentResourceNode));
}
Expand All @@ -115,8 +116,8 @@ protected synchronized void handleExceptionInExecutor(
createOrGetResultFor(dependentResourceNode).withError(e);
}

protected boolean isNotReady(DependentResourceNode<?, P> dependentResourceNode) {
return getResultFlagFor(dependentResourceNode, WorkflowResult.DetailBuilder::isNotReady);
protected boolean isReady(DependentResourceNode<?, P> dependentResourceNode) {
return getResultFlagFor(dependentResourceNode, WorkflowResult.DetailBuilder::isReady);
}

protected boolean isInError(DependentResourceNode<?, P> dependentResourceNode) {
Expand All @@ -132,15 +133,17 @@ protected synchronized void handleNodeExecutionFinish(
}
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "OptionalUsedAsFieldOrParameterType"})
protected <R> boolean isConditionMet(
Optional<ConditionWithType<R, P, ?>> condition,
DependentResourceNode<R, P> dependentResource) {
final var dr = dependentResource.getDependentResource();
return condition.map(c -> {
final DetailedCondition.Result<?> r = c.detailedIsMet(dr, primary, context);
results.computeIfAbsent(dependentResource, unused -> new WorkflowResult.DetailBuilder())
.withResultForCondition(c, r);
synchronized (this) {
results.computeIfAbsent(dependentResource, unused -> new WorkflowResult.DetailBuilder())
.withResultForCondition(c, r);
}
return r;
}).orElse(DetailedCondition.Result.metWithoutResult).isSuccess();
}
Expand Down Expand Up @@ -170,7 +173,7 @@ protected <R> void registerOrDeregisterEventSourceBasedOnActivation(
}
}

protected Map<DependentResource, WorkflowResult.Detail<?>> asDetails() {
protected synchronized Map<DependentResource, WorkflowResult.Detail<?>> asDetails() {
return results.entrySet().stream()
.collect(
Collectors.toMap(e -> e.getKey().getDependentResource(), e -> e.getValue().build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private synchronized void handleDelete(DependentResourceNode dependentResourceNo

private boolean allDependentsDeletedAlready(DependentResourceNode<?, P> dependentResourceNode) {
var dependents = dependentResourceNode.getParents();
return dependents.stream().allMatch(d -> alreadyVisited(d) && !isNotReady(d)
return dependents.stream().allMatch(d -> alreadyVisited(d) && isReady(d)
&& !isInError(d) && !postDeleteConditionNotMet(d));
}

Expand Down Expand Up @@ -231,7 +231,7 @@ private void markDependentsForDelete(DependentResourceNode<?, P> dependentResour
private boolean allParentsReconciledAndReady(DependentResourceNode<?, ?> dependentResourceNode) {
return dependentResourceNode.getDependsOn().isEmpty()
|| dependentResourceNode.getDependsOn().stream()
.allMatch(d -> alreadyVisited(d) && !isNotReady(d));
.allMatch(d -> alreadyVisited(d) && isReady(d));
}

private boolean hasErroredParent(DependentResourceNode<?, ?> dependentResourceNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public boolean hasPostDeleteConditionNotMet() {
return deletePostconditionResult != null && !deletePostconditionResult.isSuccess();
}

public boolean isNotReady() {
return readyPostconditionResult != null && !readyPostconditionResult.isSuccess();
public boolean isReady() {
return readyPostconditionResult == null || readyPostconditionResult.isSuccess();
}

DetailBuilder<R> markAsVisited() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ void readyConditionNotMetInOneParent() {
void diamondShareWithReadyCondition() {
var workflow = new WorkflowBuilder<TestCustomResource>()
.addDependentResource(dr1)
.addDependentResourceAndConfigure(dr2).toDependOn(dr1)
.addDependentResourceAndConfigure(dr2)
.toDependOn(dr1)
.withReadyPostcondition(notMetCondition)
.addDependentResourceAndConfigure(dr3).toDependOn(dr1)
.addDependentResourceAndConfigure(dr4).toDependOn(dr2, dr3)
Expand Down
Loading