Repository navigation
Add experimental support for Event Groups (implicit and scope propagation) #3198
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
+1,407
−226
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package io.temporal.internal.sync; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import io.temporal.api.sdk.v1.EventGroupMarker; | ||
| import io.temporal.common.converter.DataConverter; | ||
| import io.temporal.workflow.EventGroup; | ||
| import io.temporal.workflow.Functions; | ||
| import java.util.*; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * The Event Groups that apply to commands issued from the current workflow thread, either because | ||
| * they were entered with {@link EventGroup#run}, or implicitly because the thread runs a signal or | ||
| * update handler. | ||
| */ | ||
| final class EventGroupScope { | ||
| static final EventGroupScope EMPTY = new EventGroupScope(Collections.emptyMap(), null); | ||
|
|
||
| private static final ThreadLocal<EventGroupScope> currentScope = | ||
| ThreadLocal.withInitial(() -> EMPTY); | ||
|
|
||
| static EventGroupScope current() { | ||
| return currentScope.get(); | ||
| } | ||
|
|
||
| static <R> R runInInboundEventScope(long eventId, Functions.Func<R> func) { | ||
| EventGroupMarker marker = | ||
| EventGroupMarker.newBuilder() | ||
| .setInboundEvent(EventGroupMarker.InboundEvent.newBuilder().setInboundEventId(eventId)) | ||
| .build(); | ||
|
|
||
| // Runtime-registered handlers do not inherit scoped EGs from the thread that registered them | ||
| return new EventGroupScope(Collections.emptyMap(), marker).run(func); | ||
| } | ||
|
|
||
| static <R> R runInInboundUpdateScope(String updateId, Functions.Func<R> func) { | ||
| EventGroupMarker marker = | ||
| EventGroupMarker.newBuilder() | ||
| .setInboundUpdate( | ||
| EventGroupMarker.InboundUpdate.newBuilder().setInboundUpdateId(updateId)) | ||
| .build(); | ||
|
|
||
| // Runtime-registered handlers do not inherit scoped EGs from the thread that registered them | ||
| return new EventGroupScope(Collections.emptyMap(), marker).run(func); | ||
| } | ||
|
|
||
| /** Keyed by group ID, to make merging scopes easier. */ | ||
| private final Map<String, EventGroupImpl> explicitGroups; | ||
|
|
||
| private final @Nullable EventGroupMarker implicitGroup; | ||
|
|
||
| private EventGroupScope( | ||
| Map<String, EventGroupImpl> explicitGroups, @Nullable EventGroupMarker implicitGroup) { | ||
| this.explicitGroups = explicitGroups; | ||
| this.implicitGroup = implicitGroup; | ||
| } | ||
|
|
||
| EventGroupScope withExplicitGroups(Collection<EventGroup> additionalGroups) { | ||
| if (additionalGroups.isEmpty()) { | ||
| return this; | ||
| } | ||
| Map<String, EventGroupImpl> merged = new LinkedHashMap<>(explicitGroups); | ||
| for (EventGroup group : additionalGroups) { | ||
| Preconditions.checkArgument( | ||
| group instanceof EventGroupImpl, | ||
| "Event groups must be created with Workflow.newEventGroup, got %s", | ||
| group); | ||
| merged.putIfAbsent(group.getId(), (EventGroupImpl) group); | ||
| } | ||
| return new EventGroupScope(merged, implicitGroup); | ||
| } | ||
|
|
||
| /** Run the procedure with this scope as the current one. */ | ||
| void run(Functions.Proc proc) { | ||
| run( | ||
| () -> { | ||
| proc.apply(); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| /** Run the function with this scope as the current one. */ | ||
| <R> R run(Functions.Func<R> func) { | ||
| EventGroupScope previous = current(); | ||
| try { | ||
| currentScope.set(this); | ||
| return func.apply(); | ||
| } finally { | ||
| EventGroupScope after = currentScope.get(); | ||
| currentScope.set(previous); | ||
| if (after != this) { | ||
| throw new Error("Unexpected Event Group Scope on exit"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param directGroups groups attached directly to the command, through its options | ||
| * @param dataConverter converter used to encode labels, including any payload codec | ||
| */ | ||
| List<EventGroupMarker> toMarkers( | ||
| Collection<EventGroup> directGroups, DataConverter dataConverter) { | ||
| EventGroupScope effective = withExplicitGroups(directGroups); | ||
| if (effective.explicitGroups.isEmpty() && effective.implicitGroup == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| List<EventGroupMarker> markers = new ArrayList<>(effective.explicitGroups.size() + 1); | ||
| for (EventGroupImpl group : effective.explicitGroups.values()) { | ||
| markers.add(group.toMarker(dataConverter)); | ||
| } | ||
| if (effective.implicitGroup != null) { | ||
| markers.add(effective.implicitGroup); | ||
| } | ||
| return markers; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we could add a check that we are in a workflow here