-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Implement correlation on event filter #1386
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,12 +16,17 @@ | |||||||
| package io.serverlessworkflow.impl.events; | ||||||||
|
|
||||||||
| import io.cloudevents.CloudEvent; | ||||||||
| import io.serverlessworkflow.api.types.CorrelateProperty; | ||||||||
| import io.serverlessworkflow.api.types.EventFilter; | ||||||||
| import io.serverlessworkflow.api.types.EventFilterCorrelate; | ||||||||
| import io.serverlessworkflow.api.types.EventProperties; | ||||||||
| import io.serverlessworkflow.impl.TaskContext; | ||||||||
| import io.serverlessworkflow.impl.WorkflowApplication; | ||||||||
| import io.serverlessworkflow.impl.WorkflowContext; | ||||||||
| import io.serverlessworkflow.impl.WorkflowModel; | ||||||||
| import io.serverlessworkflow.impl.WorkflowModelFactory; | ||||||||
| import java.util.AbstractCollection; | ||||||||
| import java.util.ArrayList; | ||||||||
| import java.util.Collection; | ||||||||
| import java.util.Iterator; | ||||||||
| import java.util.List; | ||||||||
|
|
@@ -52,8 +57,24 @@ public TypeEventRegistrationBuilder listen( | |||||||
| EventFilter register, WorkflowApplication application) { | ||||||||
| EventProperties properties = register.getWith(); | ||||||||
| String type = properties.getType(); | ||||||||
| return new TypeEventRegistrationBuilder( | ||||||||
| type, application.cloudEventPredicateFactory().build(application, properties)); | ||||||||
| CloudEventPredicate cePredicate = | ||||||||
| application.cloudEventPredicateFactory().build(application, properties); | ||||||||
| Collection<CloudEventPredicate> correlationPredicates = | ||||||||
| buildCorrelationPredicates(register.getCorrelate(), application); | ||||||||
| return new TypeEventRegistrationBuilder(type, cePredicate, correlationPredicates); | ||||||||
| } | ||||||||
|
|
||||||||
| private Collection<CloudEventPredicate> buildCorrelationPredicates( | ||||||||
| EventFilterCorrelate correlate, WorkflowApplication application) { | ||||||||
| if (correlate == null || correlate.getAdditionalProperties().isEmpty()) { | ||||||||
| return List.of(); | ||||||||
| } | ||||||||
| Collection<CloudEventPredicate> predicates = new ArrayList<>(); | ||||||||
| for (Map.Entry<String, CorrelateProperty> entry : | ||||||||
| correlate.getAdditionalProperties().entrySet()) { | ||||||||
| predicates.add(CorrelationPredicate.from(entry.getValue(), application)); | ||||||||
| } | ||||||||
| return predicates; | ||||||||
| } | ||||||||
|
|
||||||||
| @Override | ||||||||
|
|
@@ -63,18 +84,49 @@ public Collection<TypeEventRegistrationBuilder> listenToAll(WorkflowApplication | |||||||
|
|
||||||||
| private static class CloudEventConsumer extends AbstractCollection<TypeEventRegistration> | ||||||||
| implements Consumer<CloudEvent> { | ||||||||
| private final WorkflowModelFactory modelFactory; | ||||||||
| private Collection<TypeEventRegistration> registrations = new CopyOnWriteArrayList<>(); | ||||||||
|
|
||||||||
| CloudEventConsumer(WorkflowModelFactory modelFactory) { | ||||||||
| this.modelFactory = modelFactory; | ||||||||
| } | ||||||||
|
|
||||||||
| @Override | ||||||||
| public void accept(CloudEvent ce) { | ||||||||
| logger.debug("Received cloud event {}", ce); | ||||||||
| for (TypeEventRegistration registration : registrations) { | ||||||||
| if (registration.predicate().test(ce, registration.workflow(), registration.task())) { | ||||||||
| if (!testCorrelation(ce, registration)) { | ||||||||
| continue; | ||||||||
| } | ||||||||
| registration.consumer().accept(ce); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
matheusandre1 marked this conversation as resolved.
|
||||||||
|
|
||||||||
| private boolean testCorrelation(CloudEvent ce, TypeEventRegistration registration) { | ||||||||
| Collection<CloudEventPredicate> predicates = registration.correlationPredicates(); | ||||||||
| if (predicates.isEmpty()) { | ||||||||
| return true; | ||||||||
| } | ||||||||
| WorkflowModel eventModel = null; | ||||||||
| for (CloudEventPredicate pred : predicates) { | ||||||||
| if (pred instanceof ModelAwareCloudEventPredicate ma) { | ||||||||
| if (eventModel == null) { | ||||||||
| eventModel = modelFactory.from(ce); | ||||||||
| } | ||||||||
| if (!ma.test(eventModel, registration.workflow(), registration.task())) { | ||||||||
| return false; | ||||||||
|
Comment on lines
+118
to
+119
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop needs to return false when any predicate fails and true when all pass; this recommendation would only return the first predicate, ignoring the others. |
||||||||
| } | ||||||||
| } else { | ||||||||
| if (!pred.test(ce, registration.workflow(), registration.task())) { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
matheusandre1 marked this conversation as resolved.
|
||||||||
| } | ||||||||
| } | ||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| @Override | ||||||||
| public boolean add(TypeEventRegistration registration) { | ||||||||
| return registrations.add(registration); | ||||||||
|
|
@@ -107,12 +159,20 @@ public TypeEventRegistration register( | |||||||
| return new TypeEventRegistration(null, ce, null, workflow, task); | ||||||||
| } else { | ||||||||
| TypeEventRegistration registration = | ||||||||
| new TypeEventRegistration(builder.type(), ce, builder.cePredicate(), workflow, task); | ||||||||
| new TypeEventRegistration( | ||||||||
| builder.type(), | ||||||||
| ce, | ||||||||
| builder.cePredicate(), | ||||||||
| builder.correlationPredicates(), | ||||||||
| workflow, | ||||||||
| task); | ||||||||
| registrations | ||||||||
| .computeIfAbsent( | ||||||||
| registration.type(), | ||||||||
| k -> { | ||||||||
| CloudEventConsumer consumer = new CloudEventConsumer(); | ||||||||
| WorkflowModelFactory factory = | ||||||||
| workflow != null ? workflow.definition().application().modelFactory() : null; | ||||||||
| CloudEventConsumer consumer = new CloudEventConsumer(factory); | ||||||||
| register(k, consumer); | ||||||||
| return consumer; | ||||||||
| }) | ||||||||
|
|
||||||||
|
matheusandre1 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.impl.events; | ||
|
|
||
| import io.cloudevents.CloudEvent; | ||
| import io.serverlessworkflow.api.types.CorrelateProperty; | ||
| import io.serverlessworkflow.impl.TaskContext; | ||
| import io.serverlessworkflow.impl.WorkflowApplication; | ||
| import io.serverlessworkflow.impl.WorkflowContext; | ||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
| import io.serverlessworkflow.impl.WorkflowValueResolver; | ||
| import io.serverlessworkflow.impl.expressions.ExpressionDescriptor; | ||
| import java.util.Objects; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| class CorrelationPredicate implements ModelAwareCloudEventPredicate { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(CorrelationPredicate.class); | ||
|
|
||
| private final WorkflowValueResolver<Object> fromResolver; | ||
| private final WorkflowValueResolver<Object> expectResolver; | ||
|
|
||
| private CorrelationPredicate( | ||
| WorkflowValueResolver<Object> fromResolver, WorkflowValueResolver<Object> expectResolver) { | ||
| this.fromResolver = fromResolver; | ||
| this.expectResolver = expectResolver; | ||
| } | ||
|
|
||
| public static CorrelationPredicate from(CorrelateProperty prop, WorkflowApplication app) { | ||
| WorkflowValueResolver<Object> fromResolver = | ||
| app.expressionFactory().resolveValue(ExpressionDescriptor.from(prop.getFrom())); | ||
| WorkflowValueResolver<Object> expectResolver = | ||
| prop.getExpect() != null | ||
| ? app.expressionFactory().resolveValue(ExpressionDescriptor.from(prop.getExpect())) | ||
| : null; | ||
| return new CorrelationPredicate(fromResolver, expectResolver); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean test(CloudEvent cloudEvent, WorkflowContext workflow, TaskContext task) { | ||
| WorkflowModel eventModel = workflow.definition().application().modelFactory().from(cloudEvent); | ||
| return test(eventModel, workflow, task); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean test(WorkflowModel eventModel, WorkflowContext workflow, TaskContext task) { | ||
| Object eventValue = fromResolver.apply(workflow, task, eventModel); | ||
|
matheusandre1 marked this conversation as resolved.
|
||
| if (eventValue == null) { | ||
| logger.debug("Correlation from expression returned null"); | ||
| return false; | ||
| } | ||
|
matheusandre1 marked this conversation as resolved.
|
||
|
|
||
| if (expectResolver == null) { | ||
| logger.debug("Correlation no expect expression, accepting event value '{}'", eventValue); | ||
| return true; | ||
| } | ||
|
matheusandre1 marked this conversation as resolved.
|
||
|
|
||
| Object expectedValue = expectResolver.apply(workflow, task, task.input()); | ||
| boolean result = Objects.equals(eventValue, expectedValue); | ||
| logger.debug( | ||
| "Correlation eventValue='{}', expectedValue='{}', match={}", | ||
| eventValue, | ||
| expectedValue, | ||
| result); | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.impl.events; | ||
|
|
||
| import io.serverlessworkflow.impl.TaskContext; | ||
| import io.serverlessworkflow.impl.WorkflowContext; | ||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
|
|
||
| public interface ModelAwareCloudEventPredicate extends CloudEventPredicate { | ||
|
|
||
| boolean test(WorkflowModel eventModel, WorkflowContext workflow, TaskContext task); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.