diff --git a/CHANGELOG.md b/CHANGELOG.md index b330567376..83c47e17cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,8 +27,9 @@ to docs, or any other relevant information. ### Added - Experimental Event Groups associate related workflow commands so their history events can be - viewed together. Create a group with `Workflow.newEventGroup` and attach it by calling - `addEventGroups` on the command's options. + viewed together. Create a group with `Workflow.newEventGroup` and attach it either by running + commands inside `EventGroup.run` or by calling `addEventGroups` on the command's options. Signal + and update handlers receive an implicit group for the signal event or update that invoked them. ### Changed - Updated Cloud API bindings to v0.27.0, including opt-in user group memberships, projects, diff --git a/temporal-sdk/src/main/java/io/temporal/activity/ActivityOptions.java b/temporal-sdk/src/main/java/io/temporal/activity/ActivityOptions.java index 69114dedaf..055b882983 100644 --- a/temporal-sdk/src/main/java/io/temporal/activity/ActivityOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/activity/ActivityOptions.java @@ -250,7 +250,8 @@ public Builder setSummary(String summary) { } /** - * Event Groups to associate with this activity. Calls accumulate. + * Event Groups to associate with this activity, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/activity/LocalActivityOptions.java b/temporal-sdk/src/main/java/io/temporal/activity/LocalActivityOptions.java index 03d78ecf94..2a71ba96bc 100644 --- a/temporal-sdk/src/main/java/io/temporal/activity/LocalActivityOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/activity/LocalActivityOptions.java @@ -181,7 +181,8 @@ public Builder setSummary(String summary) { } /** - * Event Groups to associate with this local activity. Calls accumulate. + * Event Groups to associate with this local activity, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/DeterministicRunnerImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/DeterministicRunnerImpl.java index 0bf086f537..d9714a48ae 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/DeterministicRunnerImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/DeterministicRunnerImpl.java @@ -443,6 +443,7 @@ private WorkflowThread newRootThread(Runnable runnable) { ROOT_THREAD_PRIORITY, false, runnerCancellationScope, + EventGroupScope.EMPTY, runnable, cache, getContextPropagators(), @@ -472,6 +473,7 @@ public WorkflowThread newWorkflowThread( WORKFLOW_THREAD_PRIORITY + (addedThreads++), detached, CancellationScopeImpl.current(), + EventGroupScope.current(), runnable, cache, getContextPropagators(), @@ -496,6 +498,7 @@ public WorkflowThread newCallbackThread(Runnable runnable, @Nullable String name + (addedThreads++), // maintain the order in toExecuteInWorkflowThread false, runnerCancellationScope, + EventGroupScope.EMPTY, runnable, cache, getContextPropagators(), diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupImpl.java index dbf6a32189..6d1c3de2b9 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupImpl.java @@ -7,12 +7,8 @@ import io.temporal.common.converter.DefaultDataConverter; import io.temporal.common.converter.RawValue; import io.temporal.workflow.EventGroup; -import java.util.ArrayList; -import java.util.Collection; +import io.temporal.workflow.Functions; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -38,6 +34,27 @@ public String getLabel() { return label; } + @Override + public void run(Functions.Proc proc) { + run( + () -> { + proc.apply(); + return null; + }); + } + + @Override + public R run(Functions.Func func) { + return EventGroupScope.current().withExplicitGroups(Collections.singletonList(this)).run(func); + } + + /** + * Encoded per call. A group may be declared statically and shared by workflows whose data + * converters differ, for instance through a context-aware payload codec, so a cached marker could + * carry a label encoded for another workflow. + * + * @param dataConverter converter used to encode the label, including any payload codec + */ EventGroupMarker toMarker(DataConverter dataConverter) { EventGroupMarker.Label.Builder labelMarker = EventGroupMarker.Label.newBuilder().setId(id); if (label != null) { @@ -56,24 +73,4 @@ EventGroupMarker toMarker(DataConverter dataConverter) { public String toString() { return "EventGroup{" + "id='" + id + '\'' + ", label='" + label + '\'' + '}'; } - - static List toMarkers( - Collection groups, DataConverter dataConverter) { - if (groups.isEmpty()) { - return Collections.emptyList(); - } - Map byId = new LinkedHashMap<>(); - for (EventGroup group : groups) { - Preconditions.checkArgument( - group instanceof EventGroupImpl, - "Event groups must be created with Workflow.newEventGroup, got %s", - group); - byId.putIfAbsent(group.getId(), (EventGroupImpl) group); - } - List markers = new ArrayList<>(byId.size()); - for (EventGroupImpl group : byId.values()) { - markers.add(group.toMarker(dataConverter)); - } - return markers; - } } diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupScope.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupScope.java new file mode 100644 index 0000000000..5838960e2b --- /dev/null +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/EventGroupScope.java @@ -0,0 +1,107 @@ +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 currentScope = + ThreadLocal.withInitial(() -> EMPTY); + + static EventGroupScope current() { + return currentScope.get(); + } + + static R runInInboundEventScope(long eventId, Functions.Func 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 runInInboundUpdateScope(String updateId, Functions.Func 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 explicitGroups; + + private final @Nullable EventGroupMarker implicitGroup; + + private EventGroupScope( + Map explicitGroups, @Nullable EventGroupMarker implicitGroup) { + this.explicitGroups = explicitGroups; + this.implicitGroup = implicitGroup; + } + + EventGroupScope withExplicitGroups(Collection additionalGroups) { + if (additionalGroups.isEmpty()) { + return this; + } + Map 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 function with this scope as the current one. */ + R run(Functions.Func 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 toMarkers( + Collection directGroups, DataConverter dataConverter) { + EventGroupScope effective = withExplicitGroups(directGroups); + if (effective.explicitGroups.isEmpty() && effective.implicitGroup == null) { + return Collections.emptyList(); + } + List 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; + } +} diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/RootWorkflowThreadImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/RootWorkflowThreadImpl.java index 29d0f6401c..fa4c351397 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/RootWorkflowThreadImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/RootWorkflowThreadImpl.java @@ -20,6 +20,7 @@ public class RootWorkflowThreadImpl extends WorkflowThreadImpl { int priority, boolean detached, CancellationScopeImpl parentCancellationScope, + EventGroupScope eventGroupScope, Runnable runnable, WorkflowExecutorCache cache, List contextPropagators, @@ -32,6 +33,7 @@ public class RootWorkflowThreadImpl extends WorkflowThreadImpl { priority, detached, parentCancellationScope, + eventGroupScope, runnable, cache, contextPropagators, diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/SignalDispatcher.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/SignalDispatcher.java index 9bc6bdfa7b..0521696566 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/SignalDispatcher.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/SignalDispatcher.java @@ -96,8 +96,13 @@ public void handleSignal( boolean threadDestroyed = false; runningSignalHandlers.put(eventId, new SignalHandlerInfo(eventId, signalName, policy)); try { - inboundCallsInterceptor.handleSignal( - new WorkflowInboundCallsInterceptor.SignalInput(signalName, args, eventId, header)); + EventGroupScope.runInInboundEventScope( + eventId, + () -> { + inboundCallsInterceptor.handleSignal( + new WorkflowInboundCallsInterceptor.SignalInput(signalName, args, eventId, header)); + return null; + }); } catch (DestroyWorkflowThreadError e) { threadDestroyed = true; throw e; @@ -131,9 +136,14 @@ public void registerDynamicSignalHandler( WorkflowOutboundCallsInterceptor.RegisterDynamicSignalHandlerInput input) { dynamicSignalHandler = input.getHandler(); for (SignalData signalData : signalBuffer) { - dynamicSignalHandler.handle( - signalData.getSignalName(), - new EncodedValues(signalData.getPayload(), dataConverterWithWorkflowContext)); + EventGroupScope.runInInboundEventScope( + signalData.getEventId(), + () -> { + dynamicSignalHandler.handle( + signalData.getSignalName(), + new EncodedValues(signalData.getPayload(), dataConverterWithWorkflowContext)); + return null; + }); } } diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java index ada310fe18..71cceefc2a 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java @@ -465,6 +465,13 @@ public LocalActivityOutput executeLocalActivity(LocalActivityInput inp long originalScheduledTime = System.currentTimeMillis(); CompletablePromise localActivityResult = WorkflowInternal.newCompletablePromise(); + + // Retries and backoff timers are issued from callback threads, which don't inherit + // the active EG scope from this thread. Capture the current scope (merging in direct + // assignments passed to this LA), and reuse it for the backoff timers and later attempts. + EventGroupScope eventGroupScope = + EventGroupScope.current().withExplicitGroups(input.getOptions().getEventGroups()); + executeLocalActivityOverLocalRetryThreshold( input.getActivityName(), input.getActivityId(), @@ -475,6 +482,7 @@ public LocalActivityOutput executeLocalActivity(LocalActivityInput inp originalScheduledTime, 1, null, + eventGroupScope, localActivityResult); // Avoid passing the input to the output handle as it causes the input to be retained for the @@ -512,6 +520,7 @@ private void executeLocalActivityOverLocalRetryThreshold( long originalScheduledTime, int attempt, @Nullable Failure previousExecutionFailure, + EventGroupScope eventGroupScope, CompletablePromise result) { CompletablePromise localExecutionResult = executeLocalActivityLocally( @@ -523,7 +532,8 @@ private void executeLocalActivityOverLocalRetryThreshold( arguments, originalScheduledTime, attempt, - previousExecutionFailure); + previousExecutionFailure, + eventGroupScope); localExecutionResult.handle( (r, e) -> { @@ -535,11 +545,8 @@ private void executeLocalActivityOverLocalRetryThreshold( (LocalActivityCallback.LocalActivityFailedException) e; @Nullable Duration backoff = laException.getBackoff(); if (backoff != null) { - WorkflowInternal.newTimer( - backoff, - TimerOptions.newBuilder() - .addEventGroups(options.getEventGroups().toArray(new EventGroup[0])) - .build()) + eventGroupScope + .run(() -> WorkflowInternal.newTimer(backoff)) .thenApply( unused -> { executeLocalActivityOverLocalRetryThreshold( @@ -553,6 +560,7 @@ private void executeLocalActivityOverLocalRetryThreshold( laException.getLastAttempt() + 1, // Carry the attempt failure, not the local ActivityFailure wrapper. laException.getFailure().getCause(), + eventGroupScope, result); return null; }); @@ -583,7 +591,8 @@ private CompletablePromise executeLocalActivityLoc @Nullable LocalActivityArguments arguments, long originalScheduledTime, int attempt, - @Nullable Failure previousExecutionFailure) { + @Nullable Failure previousExecutionFailure, + EventGroupScope eventGroupScope) { LocalActivityCallbackImpl callback = new LocalActivityCallbackImpl(); ExecuteLocalActivityParameters params = @@ -596,7 +605,8 @@ private CompletablePromise executeLocalActivityLoc arguments, attempt, originalScheduledTime, - previousExecutionFailure); + previousExecutionFailure, + eventGroupScope); Functions.Proc cancellationCallback = replayContext.scheduleLocalActivityTask(params, callback); CancellationScope.current() .getCancellationRequest() @@ -700,7 +710,8 @@ private ExecuteLocalActivityParameters constructExecuteLocalActivityParameters( @Nullable LocalActivityArguments arguments, int attempt, long originalScheduledTime, - @Nullable Failure previousExecutionFailure) { + @Nullable Failure previousExecutionFailure, + EventGroupScope eventGroupScope) { options = LocalActivityOptions.newBuilder(options).validateAndBuildWithDefaults(); PollActivityTaskQueueResponse.Builder activityTask = @@ -758,7 +769,10 @@ private ExecuteLocalActivityParameters constructExecuteLocalActivityParameters( options.isDoNotIncludeArgumentsIntoMarker(), localRetryThreshold, userMetadata, - eventGroupMarkers(options.getEventGroups()), + eventGroupScope.toMarkers( + // Direct assignments have already been applied to the eventGroupScope, hence + // emptyList() here + Collections.emptyList(), dataConverterWithCurrentWorkflowContext), arguments); } @@ -1835,7 +1849,8 @@ public Optional getCurrentUpdateInfo() { } private List eventGroupMarkers(List directGroups) { - return EventGroupImpl.toMarkers(directGroups, dataConverterWithCurrentWorkflowContext); + return EventGroupScope.current() + .toMarkers(directGroups, dataConverterWithCurrentWorkflowContext); } /** Simple wrapper over a failure just to allow completing the CompletablePromise as a failure */ diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/UpdateDispatcher.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/UpdateDispatcher.java index 500b3eae34..7562c4f2ea 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/UpdateDispatcher.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/UpdateDispatcher.java @@ -104,10 +104,13 @@ public Optional handleExecuteUpdate( boolean threadDestroyed = false; try { Object result = - inboundCallsInterceptor - .executeUpdate( - new WorkflowInboundCallsInterceptor.UpdateInput(updateName, header, args)) - .getResult(); + EventGroupScope.runInInboundUpdateScope( + updateId, + () -> + inboundCallsInterceptor + .executeUpdate( + new WorkflowInboundCallsInterceptor.UpdateInput(updateName, header, args)) + .getResult()); return resultDataConverter.toPayloads(result); } catch (DestroyWorkflowThreadError e) { threadDestroyed = true; diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowThreadImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowThreadImpl.java index 3cf2bffd74..c8b346ce17 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowThreadImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowThreadImpl.java @@ -42,6 +42,7 @@ class RunnableWrapper implements Runnable { private String originalName; private String name; private final CancellationScopeImpl cancellationScope; + private final EventGroupScope eventGroupScope; private final List contextPropagators; private final Map propagatedContexts; @@ -51,6 +52,7 @@ class RunnableWrapper implements Runnable { String name, boolean detached, CancellationScopeImpl parent, + EventGroupScope eventGroupScope, Runnable runnable, List contextPropagators, Map propagatedContexts) { @@ -62,6 +64,7 @@ class RunnableWrapper implements Runnable { this.cancellationScope = new CancellationScopeImpl( detached, deterministicCancellationScopeOrder, runnable, parent); + this.eventGroupScope = eventGroupScope; Preconditions.checkState( context.getStatus() == Status.CREATED, "threadContext not in CREATED state"); this.contextPropagators = contextPropagators; @@ -91,7 +94,11 @@ public void run() { // initialYield blocks thread until the first runUntilBlocked is called. // Otherwise, r starts executing without control of the sync. threadContext.initialYield(); - cancellationScope.run(); + eventGroupScope.run( + () -> { + cancellationScope.run(); + return null; + }); } catch (DestroyWorkflowThreadError e) { if (!threadContext.isDestroyRequested()) { threadContext.setUnhandledException(e); @@ -157,6 +164,7 @@ public void setName(String name) { int priority, boolean detached, CancellationScopeImpl parentCancellationScope, + EventGroupScope eventGroupScope, Runnable runnable, WorkflowExecutorCache cache, List contextPropagators, @@ -174,6 +182,7 @@ public void setName(String name) { Preconditions.checkNotNull(name, "Thread name shouldn't be null"), detached, parentCancellationScope, + eventGroupScope, runnable, contextPropagators, propagatedContexts); diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/CancelExternalWorkflowOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/CancelExternalWorkflowOptions.java index 8b595a278c..1484cf5b78 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/CancelExternalWorkflowOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/CancelExternalWorkflowOptions.java @@ -53,7 +53,8 @@ public Builder setReason(@Nullable String reason) { } /** - * Event Groups to associate with the cancellation request command. Calls accumulate. + * Event Groups to associate with the cancellation request command, in addition to those in + * scope. Calls accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/ChildWorkflowOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/ChildWorkflowOptions.java index 2c74dc0704..b0d2098487 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/ChildWorkflowOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/ChildWorkflowOptions.java @@ -335,8 +335,8 @@ public Builder setStaticDetails(String staticDetails) { } /** - * Event Groups to associate with the commands that start and cancel this child workflow. Calls - * accumulate. + * Event Groups to associate with the commands that start and cancel this child workflow, in + * addition to those in scope. Calls accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/ContinueAsNewOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/ContinueAsNewOptions.java index 0e80e7250d..cc410decd3 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/ContinueAsNewOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/ContinueAsNewOptions.java @@ -128,7 +128,8 @@ public Builder setTypedSearchAttributes(SearchAttributes typedSearchAttributes) } /** - * Event Groups to associate with the continue-as-new command. Calls accumulate. + * Event Groups to associate with the continue-as-new command, in addition to those in scope. + * Calls accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/EventGroup.java b/temporal-sdk/src/main/java/io/temporal/workflow/EventGroup.java index 60382b5afa..d2991cabcf 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/EventGroup.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/EventGroup.java @@ -9,11 +9,14 @@ * be presented together in UI/CLI. * *

Event Groups are created with {@link Workflow#newEventGroup(String)} or {@link - * Workflow#newEventGroup(String, EventGroupOptions)}, then attached to commands through the {@code - * addEventGroups} method of the corresponding options builder. + * Workflow#newEventGroup(String, EventGroupOptions)}. A group can then be attached to commands + * either directly, through the {@code addEventGroups} method of the corresponding options builder + * (e.g. {@link io.temporal.activity.ActivityOptions.Builder#addEventGroups}), or through scope + * propagation, by issuing commands from within {@link #run(Functions.Proc)}. * - *

Groups are identified by their ID. When several groups with the same ID are attached to a - * single command, they are recorded only once, and the label of the first one is used. + *

Groups are identified by their ID. When several groups with the same ID apply to a single + * command, they are recorded only once, and the label of the outermost scope, or of the first group + * directly attached, is used. */ @Experimental public interface EventGroup { @@ -25,4 +28,20 @@ public interface EventGroup { /** Label of this group, as provided through {@link EventGroupOptions#getLabel()}. */ @Nullable String getLabel(); + + /** + * Runs the given procedure with this group in scope. All commands issued from within the + * procedure, including those issued from workflow threads started from within it, are associated + * with this group. Scopes may be nested, in which case commands are associated with all enclosing + * groups. + */ + void run(Functions.Proc proc); + + /** + * Runs the given function with this group in scope, and returns its result. All commands issued + * from within the function, including those issued from workflow threads started from within it, + * are associated with this group. Scopes may be nested, in which case commands are associated + * with all enclosing groups. + */ + R run(Functions.Func func); } diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/GetVersionOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/GetVersionOptions.java index 14912216df..afa56ad6c0 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/GetVersionOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/GetVersionOptions.java @@ -39,7 +39,8 @@ private Builder(GetVersionOptions options) { } /** - * Event Groups to associate with the version marker. Calls accumulate. + * Event Groups to associate with the version marker, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/MutableSideEffectOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/MutableSideEffectOptions.java index d5e60eb945..f0b791b98c 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/MutableSideEffectOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/MutableSideEffectOptions.java @@ -52,7 +52,8 @@ public MutableSideEffectOptions.Builder setSummary(String summary) { } /** - * Event Groups to associate with the marker recorded by this call. Calls accumulate. + * Event Groups to associate with the marker recorded by this call, in addition to those in + * scope. Calls accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/NexusOperationOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/NexusOperationOptions.java index c98acb1df8..79b47a33cf 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/NexusOperationOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/NexusOperationOptions.java @@ -113,7 +113,8 @@ public NexusOperationOptions.Builder setSummary(String summary) { } /** - * Event Groups to associate with this Nexus operation. Calls accumulate. + * Event Groups to associate with this Nexus operation, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/SideEffectOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/SideEffectOptions.java index 22782207b1..28699244a7 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/SideEffectOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/SideEffectOptions.java @@ -52,7 +52,8 @@ public SideEffectOptions.Builder setSummary(String summary) { } /** - * Event Groups to associate with this side effect. Calls accumulate. + * Event Groups to associate with this side effect, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/SignalChildWorkflowOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/SignalChildWorkflowOptions.java index 575307a988..9483b826dc 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/SignalChildWorkflowOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/SignalChildWorkflowOptions.java @@ -42,7 +42,8 @@ private Builder(SignalChildWorkflowOptions options) { } /** - * Event Groups to associate with the signal command. Calls accumulate. + * Event Groups to associate with the signal command, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/SignalExternalWorkflowOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/SignalExternalWorkflowOptions.java index 8c48aff351..8a505da47e 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/SignalExternalWorkflowOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/SignalExternalWorkflowOptions.java @@ -55,7 +55,8 @@ private Builder(SignalExternalWorkflowOptions options) { } /** - * Event Groups to associate with the signal command. Calls accumulate. + * Event Groups to associate with the signal command, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/TimerOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/TimerOptions.java index e5b67558ea..eb1734a7f2 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/TimerOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/TimerOptions.java @@ -52,7 +52,7 @@ public Builder setSummary(String summary) { } /** - * Event Groups to associate with this timer. Calls accumulate. + * Event Groups to associate with this timer, in addition to those in scope. Calls accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/UpsertMemoOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/UpsertMemoOptions.java index b3f98903d2..6fc25ab035 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/UpsertMemoOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/UpsertMemoOptions.java @@ -39,7 +39,8 @@ private Builder(UpsertMemoOptions options) { } /** - * Event Groups to associate with the memo update. Calls accumulate. + * Event Groups to associate with the memo update, in addition to those in scope. Calls + * accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/main/java/io/temporal/workflow/UpsertSearchAttributesOptions.java b/temporal-sdk/src/main/java/io/temporal/workflow/UpsertSearchAttributesOptions.java index f3534b1bb3..ce379ea3a9 100644 --- a/temporal-sdk/src/main/java/io/temporal/workflow/UpsertSearchAttributesOptions.java +++ b/temporal-sdk/src/main/java/io/temporal/workflow/UpsertSearchAttributesOptions.java @@ -44,7 +44,8 @@ private Builder(UpsertSearchAttributesOptions options) { } /** - * Event Groups to associate with the search attributes update. Calls accumulate. + * Event Groups to associate with the search attributes update, in addition to those in scope. + * Calls accumulate. * * @see EventGroup */ diff --git a/temporal-sdk/src/test/java/io/temporal/internal/sync/EventGroupImplTest.java b/temporal-sdk/src/test/java/io/temporal/internal/sync/EventGroupImplTest.java index f221cc653d..0a71b2ad22 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/sync/EventGroupImplTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/sync/EventGroupImplTest.java @@ -13,6 +13,7 @@ import io.temporal.common.converter.DefaultDataConverter; import io.temporal.workflow.EventGroup; import io.temporal.workflow.EventGroupOptions; +import io.temporal.workflow.Functions; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -33,7 +34,7 @@ private static EventGroup group(String id, String label) { @Test public void idIsUsedVerbatim() { List markers = - EventGroupImpl.toMarkers(Collections.singletonList(group("c-id", "ccc")), CONVERTER); + EventGroupScope.EMPTY.toMarkers(Collections.singletonList(group("c-id", "ccc")), CONVERTER); assertEquals(1, markers.size()); assertEquals("c-id", markers.get(0).getLabel().getId()); @@ -48,7 +49,8 @@ public void emptyIdIsRejected() { @Test public void labelConvertsToJsonString() { Payload payload = - EventGroupImpl.toMarkers(Collections.singletonList(group("bbb", "Label B")), CONVERTER) + EventGroupScope.EMPTY + .toMarkers(Collections.singletonList(group("bbb", "Label B")), CONVERTER) .get(0) .getLabel() .getLabel(); @@ -65,7 +67,8 @@ public void emptyLabelIsRejected() { @Test public void omittedLabelProducesNoPayload() { EventGroupMarker marker = - EventGroupImpl.toMarkers( + EventGroupScope.EMPTY + .toMarkers( Collections.singletonList(WorkflowInternal.newEventGroup("aaa", null)), CONVERTER) .get(0); @@ -79,7 +82,8 @@ public void labelIsCodecEncodedAndIdIsNot() { CONVERTER, Collections.singletonList(new CodecDataConverterTest.PrefixPayloadCodec())); EventGroupMarker marker = - EventGroupImpl.toMarkers(Collections.singletonList(group("bbb", "Label B")), codecConverter) + EventGroupScope.EMPTY + .toMarkers(Collections.singletonList(group("bbb", "Label B")), codecConverter) .get(0); assertEquals("bbb", marker.getLabel().getId()); @@ -91,7 +95,7 @@ public void labelIsCodecEncodedAndIdIsNot() { @Test public void groupsWithTheSameIdAreRecordedOnceWithTheFirstLabel() { List markers = - EventGroupImpl.toMarkers( + EventGroupScope.EMPTY.toMarkers( Arrays.asList(group("b-id", "bbb1"), group("aaa", null), group("b-id", "bbb2")), CONVERTER); @@ -118,10 +122,20 @@ public String getId() { public String getLabel() { return null; } + + @Override + public void run(Functions.Proc proc) { + throw new UnsupportedOperationException(); + } + + @Override + public R run(Functions.Func func) { + throw new UnsupportedOperationException(); + } }; assertThrows( IllegalArgumentException.class, - () -> EventGroupImpl.toMarkers(Collections.singletonList(foreign), CONVERTER)); + () -> EventGroupScope.EMPTY.toMarkers(Collections.singletonList(foreign), CONVERTER)); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/EventGroupsTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/EventGroupsTest.java index 75d74f2b70..2ebe15ab5b 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/EventGroupsTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/EventGroupsTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import io.temporal.activity.Activity; @@ -14,6 +15,7 @@ import io.temporal.api.enums.v1.EventType; import io.temporal.api.history.v1.HistoryEvent; import io.temporal.api.sdk.v1.EventGroupMarker; +import io.temporal.client.WorkflowClient; import io.temporal.client.WorkflowStub; import io.temporal.common.RetryOptions; import io.temporal.common.SearchAttributeKey; @@ -41,12 +43,7 @@ import org.junit.Test; import org.junit.experimental.categories.Category; -/** - * Tests for Event Groups. - * - *

Event Group scopes (`EG-SCOPE`), implicit Event Groups (`EG-IMPLICIT`), and aggregation cases - * involving scopes are not covered: Java only supports attaching Event Groups directly to commands. - */ +/** Tests for Event Groups. */ public class EventGroupsTest { private final Activities activities = new Activities(); @@ -62,6 +59,20 @@ public class EventGroupsTest { .build(), LabelIdWorkflowImpl.class, LabelPayloadWorkflowImpl.class, + ScopeWorkflowImpl.class, + NestedScopesWorkflowImpl.class, + ReenteredScopeWorkflowImpl.class, + ConcurrentScopesWorkflowImpl.class, + ScopeKeptAfterExitWorkflowImpl.class, + ScopeNotInheritedWorkflowImpl.class, + ScopeUnwindsWorkflowImpl.class, + StaticSignalWorkflowImpl.class, + RuntimeSignalWorkflowImpl.class, + BufferedSignalWorkflowImpl.class, + CatchAllSignalWorkflowImpl.class, + StaticUpdateWorkflowImpl.class, + RuntimeUpdateWorkflowImpl.class, + CatchAllUpdateWorkflowImpl.class, AggregationWorkflowImpl.class, TimerCommandWorkflowImpl.class, AwaitWorkflowImpl.class, @@ -79,13 +90,15 @@ public class EventGroupsTest { VersionCommandWorkflowImpl.class, SideEffectCommandWorkflowImpl.class, MutableSideEffectCommandWorkflowImpl.class, - ContinueAsNewCommandWorkflowImpl.class) + ContinueAsNewCommandWorkflowImpl.class, + ContinueAsNewShortFormCommandWorkflowImpl.class) .setActivityImplementations(activities) .build(); @Before public void resetActivities() { activities.hold = new CountDownLatch(1); + activities.handlerRegistered = new CountDownLatch(1); } // //////////////////////////////////////////////////////////////////////////////////////////////// @@ -202,26 +215,768 @@ public void execute() { // 3. Explicit Event Group Scopes (`EG-SCOPE`) // //////////////////////////////////////////////////////////////////////////////////////////////// - // FIXME: Event Group Scopes are not yet supported in Java. + @Test + public void commandsInAnEventGroupScopeCarryItsMarker() { + List events = scopeHistory(); + + // EG-SCOPE-00: Commands in an Event Group scope carry its marker + assertLabelGroups( + require(events, EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED, "activity"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "timer"), "aaa"); + assertLabelGroups(requireChild(events, "child-workflow"), "aaa"); + } + + private List scopeHistory() { + ScopeWorkflow workflow = testWorkflowRule.newWorkflowStubTimeoutOptions(ScopeWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface ScopeWorkflow { + @WorkflowMethod + void execute(); + } + + public static class ScopeWorkflowImpl implements ScopeWorkflow { + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + a.run( + () -> { + Workflow.newActivityStub(EventGroupActivities.class, activity(null, "activity")).run(); + Workflow.sleep(Duration.ofMillis(1), timer(null, "timer")); + Workflow.newChildWorkflowStub(ChildWorkflow.class, child(null, "child-workflow")) + .execute(); + }); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void nestingEventGroupScopesComposesCorrectly() { + List events = nestedScopesHistory(); + + // EG-SCOPE-01: Nesting Event Group Scopes composes correctly + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-before"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-b"), "aaa", "bbb"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-after"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "outside")); + } + + private List nestedScopesHistory() { + NestedScopesWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(NestedScopesWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface NestedScopesWorkflow { + @WorkflowMethod + void execute(); + } + + public static class NestedScopesWorkflowImpl implements NestedScopesWorkflow { + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + EventGroup b = Workflow.newEventGroup("bbb"); + a.run( + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "a-before")); + b.run(() -> Workflow.sleep(Duration.ofMillis(1), timer(null, "a-b"))); + Workflow.sleep(Duration.ofMillis(1), timer(null, "a-after")); + }); + Workflow.sleep(Duration.ofMillis(1), timer(null, "outside")); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void reenteringAnEventGroupInstanceNestsCorrectly() { + List events = reenteredScopeHistory(); + + // EG-SCOPE-02: Re-entering an Event Group instance nests correctly + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-before"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-inner"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-after"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "outside")); + } + + private List reenteredScopeHistory() { + ReenteredScopeWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(ReenteredScopeWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface ReenteredScopeWorkflow { + @WorkflowMethod + void execute(); + } + + public static class ReenteredScopeWorkflowImpl implements ReenteredScopeWorkflow { + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + a.run( + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "a-before")); + a.run(() -> Workflow.sleep(Duration.ofMillis(1), timer(null, "a-inner"))); + Workflow.sleep(Duration.ofMillis(1), timer(null, "a-after")); + }); + Workflow.sleep(Duration.ofMillis(1), timer(null, "outside")); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void anEventGroupInstanceCanBeScopedConcurrentlyFromTwoBranches() { + List events = concurrentScopesHistory(); + + // EG-SCOPE-03: An Event Group instance can be scoped concurrently from two branches + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "b-a-c"), "bbb", "aaa", "ccc"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "b-a"), "bbb", "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "b-after-a"), "bbb"); + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "d-a-e"), "ddd", "aaa", "eee"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "d-a"), "ddd", "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "d-after-a"), "ddd"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "outside")); + } + + private List concurrentScopesHistory() { + ConcurrentScopesWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(ConcurrentScopesWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface ConcurrentScopesWorkflow { + @WorkflowMethod + void execute(); + } + + public static class ConcurrentScopesWorkflowImpl implements ConcurrentScopesWorkflow { + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + EventGroup b = Workflow.newEventGroup("bbb"); + EventGroup c = Workflow.newEventGroup("ccc"); + EventGroup d = Workflow.newEventGroup("ddd"); + EventGroup e = Workflow.newEventGroup("eee"); + + Promise left = + Async.procedure( + () -> + b.run( + () -> { + a.run( + () -> { + c.run( + () -> Workflow.sleep(Duration.ofMillis(1), timer(null, "b-a-c"))); + Workflow.sleep(Duration.ofMillis(1), timer(null, "b-a")); + }); + Workflow.sleep(Duration.ofMillis(1), timer(null, "b-after-a")); + })); + Promise right = + Async.procedure( + () -> + d.run( + () -> { + a.run( + () -> { + e.run( + () -> Workflow.sleep(Duration.ofMillis(1), timer(null, "d-a-e"))); + Workflow.sleep(Duration.ofMillis(1), timer(null, "d-a")); + }); + Workflow.sleep(Duration.ofMillis(1), timer(null, "d-after-a")); + })); + Promise.allOf(left, right).get(); + + Workflow.sleep(Duration.ofMillis(1), timer(null, "outside")); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void aTaskStartedInsideAScopeKeepsItAfterTheScopeExits() { + List events = scopeKeptAfterExitHistory(); + + // EG-SCOPE-04: A task started inside a scope keeps it after the scope exits + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "inside-before"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "inside-after"), "aaa"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "outside")); + } + + private List scopeKeptAfterExitHistory() { + ScopeKeptAfterExitWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(ScopeKeptAfterExitWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface ScopeKeptAfterExitWorkflow { + @WorkflowMethod + void execute(); + } + + public static class ScopeKeptAfterExitWorkflowImpl implements ScopeKeptAfterExitWorkflow { + private boolean scopeExited; + + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + Promise task = + a.run( + () -> + Async.procedure( + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "inside-before")); + Workflow.await(() -> scopeExited); + Workflow.sleep(Duration.ofMillis(1), timer(null, "inside-after")); + })); + scopeExited = true; + task.get(); + + Workflow.sleep(Duration.ofMillis(1), timer(null, "outside")); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void aTaskCreatedOutsideAScopeDoesNotInheritItWhenResumedInside() { + List events = scopeNotInheritedHistory(); + + // EG-SCOPE-05: A task created outside a scope does not inherit it when resumed inside + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "outside-task")); + } + + private List scopeNotInheritedHistory() { + ScopeNotInheritedWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(ScopeNotInheritedWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface ScopeNotInheritedWorkflow { + @WorkflowMethod + void execute(); + } + + public static class ScopeNotInheritedWorkflowImpl implements ScopeNotInheritedWorkflow { + private boolean scopeEntered; + + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + Promise task = + Async.procedure( + () -> { + Workflow.await(() -> scopeEntered); + Workflow.sleep(Duration.ofMillis(1), timer(null, "outside-task")); + }); + a.run( + () -> { + scopeEntered = true; + task.get(); + }); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void anEventGroupScopeUnwindsCleanlyWhenItsBodyThrows() { + List events = scopeUnwindsHistory(); + + // EG-SCOPE-06: An Event Group scope unwinds cleanly when its body throws + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-b"), "aaa", "bbb"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "a-after"), "aaa"); + } + + private List scopeUnwindsHistory() { + ScopeUnwindsWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(ScopeUnwindsWorkflow.class); + workflow.execute(); + return history(workflow); + } + + @WorkflowInterface + public interface ScopeUnwindsWorkflow { + @WorkflowMethod + void execute(); + } + + public static class ScopeUnwindsWorkflowImpl implements ScopeUnwindsWorkflow { + @Override + public void execute() { + EventGroup a = Workflow.newEventGroup("aaa"); + EventGroup b = Workflow.newEventGroup("bbb"); + a.run( + () -> { + try { + b.run( + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "a-b")); + throw new IllegalStateException("boom"); + }); + } catch (IllegalStateException expected) { + // The point of the test is what the scope looks like once this unwinds. + } + Workflow.sleep(Duration.ofMillis(1), timer(null, "a-after")); + }); + } + } // //////////////////////////////////////////////////////////////////////////////////////////////// // 4. Implicit Event Groups (`EG-IMPLICIT`) // //////////////////////////////////////////////////////////////////////////////////////////////// - // FIXME: Event Group Scopes are not yet supported in Java. + @Test + public void implicitScopeOnStaticallyDeclaredSignalHandlerExistsAndComposesAppropriately() { + List events = staticSignalHistory(); + long signalEventId = signaledEventId(events, "mySignal"); + + // EG-IMPLICIT-00: A statically declared signal handler carries the signaled event's marker + HistoryEvent fromSignal = require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-signal"); + assertEquals(signalEventId, inboundEventId(fromSignal)); + assertLabelGroups(fromSignal); + + // EG-IMPLICIT-01: An explicit scope composes with a statically declared signal handler's + // implicit scope + HistoryEvent inScope = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-signal-in-scope"); + assertEquals(signalEventId, inboundEventId(inScope)); + assertLabelGroups(inScope, "aaa"); + + // EG-IMPLICIT-30: Signal implicit scope does not leak to commands in the Workflow main function + assertNoMarkers(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-main-before-signal")); + assertNoMarkers(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-main-after-signal")); + } + + private List staticSignalHistory() { + StaticSignalWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(StaticSignalWorkflow.class); + WorkflowClient.start(workflow::execute); + workflow.mySignal(); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface StaticSignalWorkflow { + @WorkflowMethod + void execute(); + + @SignalMethod(name = "mySignal") + void mySignal(); + } + + public static class StaticSignalWorkflowImpl implements StaticSignalWorkflow { + private boolean handled; + + @Override + public void execute() { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-main-before-signal")); + Workflow.await(() -> handled); + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-main-after-signal")); + } + + @Override + public void mySignal() { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-signal")); + Workflow.newEventGroup("aaa") + .run(() -> Workflow.sleep(Duration.ofMillis(1), timer(null, "from-signal-in-scope"))); + handled = true; + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void implicitScopeOnRuntimeRegisteredSignalHandlerExistsAndComposesAppropriately() + throws InterruptedException { + List events = runtimeSignalHistory(); + long signalEventId = signaledEventId(events, "mySignal"); + + // EG-IMPLICIT-10: A runtime-registered signal handler does not inherit its registration scope + HistoryEvent fromSignal = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-runtime-signal"); + assertEquals(signalEventId, inboundEventId(fromSignal)); + assertLabelGroups(fromSignal); + + // EG-IMPLICIT-11: An explicit scope composes with a runtime-registered signal handler's + // implicit scope + HistoryEvent inScope = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-runtime-signal-in-scope"); + assertEquals(signalEventId, inboundEventId(inScope)); + assertLabelGroups(inScope, "inside"); + } + + private List runtimeSignalHistory() throws InterruptedException { + RuntimeSignalWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(RuntimeSignalWorkflow.class); + WorkflowClient.start(workflow::execute); + // A signal sent before registration would be buffered, which is EG-IMPLICIT-12's case. + assertTrue(activities.handlerRegistered.await(10, TimeUnit.SECONDS)); + WorkflowStub.fromTyped(workflow).signal("mySignal"); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface RuntimeSignalWorkflow { + @WorkflowMethod + void execute(); + } + + public static class RuntimeSignalWorkflowImpl implements RuntimeSignalWorkflow { + private boolean handled; + + @Override + public void execute() { + EventGroup outside = Workflow.newEventGroup("outside"); + EventGroup inside = Workflow.newEventGroup("inside"); + outside.run( + () -> + Workflow.registerListener( + (MySignalListener) + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-runtime-signal")); + inside.run( + () -> + Workflow.sleep( + Duration.ofMillis(1), + timer(null, "from-runtime-signal-in-scope"))); + handled = true; + })); + Workflow.newActivityStub( + EventGroupActivities.class, activity(null, "activity-handler-registered")) + .handlerRegistered(); + Workflow.await(() -> handled); + } + } + + @WorkflowInterface + public interface MySignalListener { + @SignalMethod(name = "mySignal") + void mySignal(); + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void aSignalBufferedBeforeRuntimeRegistrationKeepsItsOriginalImplicitMarker() { + List events = bufferedSignalHistory(); + + // EG-IMPLICIT-12: A signal buffered before runtime registration keeps its original implicit + // marker + HistoryEvent fromSignal = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-runtime-signal"); + assertEquals(signaledEventId(events, "mySignal"), inboundEventId(fromSignal)); + assertLabelGroups(fromSignal); + } + + private List bufferedSignalHistory() { + BufferedSignalWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(BufferedSignalWorkflow.class); + WorkflowClient.start(workflow::execute); + WorkflowStub.fromTyped(workflow).signal("mySignal"); + workflow.unblock(); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface BufferedSignalWorkflow { + @WorkflowMethod + void execute(); + + @SignalMethod(name = "unblock") + void unblock(); + } + + public static class BufferedSignalWorkflowImpl implements BufferedSignalWorkflow { + private boolean unblocked; + private boolean handled; + + @Override + public void execute() { + Workflow.await(() -> unblocked); + Workflow.registerListener( + (MySignalListener) + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-runtime-signal")); + handled = true; + }); + Workflow.await(() -> handled); + } + + @Override + public void unblock() { + unblocked = true; + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void aCatchAllSignalHandlerCarriesTheSignaledEventsMarker() { + List events = catchAllSignalHistory(); + + // EG-IMPLICIT-20: A catch-all signal handler carries the signaled event's marker + HistoryEvent fromSignal = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-catch-all-signal"); + assertEquals(signaledEventId(events, "non-existent-signal"), inboundEventId(fromSignal)); + assertLabelGroups(fromSignal); + } + + private List catchAllSignalHistory() { + CatchAllSignalWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(CatchAllSignalWorkflow.class); + WorkflowClient.start(workflow::execute); + WorkflowStub.fromTyped(workflow).signal("non-existent-signal"); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface CatchAllSignalWorkflow { + @WorkflowMethod + void execute(); + } + + public static class CatchAllSignalWorkflowImpl implements CatchAllSignalWorkflow { + private boolean handled; + + @Override + public void execute() { + Workflow.registerListener( + (DynamicSignalHandler) + (name, args) -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-catch-all-signal")); + handled = true; + }); + Workflow.await(() -> handled); + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void implicitScopeOnStaticallyDeclaredUpdateHandlerExistsAndComposesAppropriately() { + List events = staticUpdateHistory(); + String updateId = acceptedUpdateId(events, "myUpdate"); + + // EG-IMPLICIT-50: A statically declared update handler carries the update ID + HistoryEvent fromUpdate = require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-update"); + assertEquals(updateId, inboundUpdateId(fromUpdate)); + assertLabelGroups(fromUpdate); + + // EG-IMPLICIT-51: An explicit scope composes with a statically declared update handler's + // implicit scope + HistoryEvent inScope = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-update-in-scope"); + assertEquals(updateId, inboundUpdateId(inScope)); + assertLabelGroups(inScope, "inside"); + + // EG-IMPLICIT-80: Update implicit scope does not leak to commands in the Workflow main function + assertNoMarkers(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-main-before-update")); + assertNoMarkers(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-main-after-update")); + } + + private List staticUpdateHistory() { + StaticUpdateWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(StaticUpdateWorkflow.class); + WorkflowClient.start(workflow::execute); + workflow.myUpdate(); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface StaticUpdateWorkflow { + @WorkflowMethod + void execute(); + + @UpdateMethod(name = "myUpdate") + void myUpdate(); + } + + public static class StaticUpdateWorkflowImpl implements StaticUpdateWorkflow { + private boolean handled; + + @Override + public void execute() { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-main-before-update")); + Workflow.await(() -> handled); + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-main-after-update")); + } + + @Override + public void myUpdate() { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-update")); + Workflow.newEventGroup("inside") + .run(() -> Workflow.sleep(Duration.ofMillis(1), timer(null, "from-update-in-scope"))); + handled = true; + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void implicitScopeOnRuntimeRegisteredUpdateHandlerExistsAndComposesAppropriately() + throws InterruptedException { + List events = runtimeUpdateHistory(); + String updateId = acceptedUpdateId(events, "myUpdate"); + + // EG-IMPLICIT-60: A runtime-registered update handler does not inherit its registration scope + HistoryEvent fromUpdate = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-runtime-update"); + assertEquals(updateId, inboundUpdateId(fromUpdate)); + assertLabelGroups(fromUpdate); + + // EG-IMPLICIT-61: An explicit scope composes with a runtime-registered update handler's + // implicit scope + HistoryEvent inScope = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-runtime-update-in-scope"); + assertEquals(updateId, inboundUpdateId(inScope)); + assertLabelGroups(inScope, "inside"); + } + + private List runtimeUpdateHistory() throws InterruptedException { + RuntimeUpdateWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(RuntimeUpdateWorkflow.class); + WorkflowClient.start(workflow::execute); + // Updates are rejected until a handler is registered. + assertTrue(activities.handlerRegistered.await(10, TimeUnit.SECONDS)); + WorkflowStub.fromTyped(workflow).update("myUpdate", Void.class); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface RuntimeUpdateWorkflow { + @WorkflowMethod + void execute(); + } + + public static class RuntimeUpdateWorkflowImpl implements RuntimeUpdateWorkflow { + private boolean handled; + + @Override + public void execute() { + EventGroup outside = Workflow.newEventGroup("outside"); + EventGroup inside = Workflow.newEventGroup("inside"); + outside.run( + () -> + Workflow.registerListener( + (MyUpdateListener) + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-runtime-update")); + inside.run( + () -> + Workflow.sleep( + Duration.ofMillis(1), + timer(null, "from-runtime-update-in-scope"))); + handled = true; + })); + Workflow.newActivityStub( + EventGroupActivities.class, activity(null, "activity-handler-registered")) + .handlerRegistered(); + Workflow.await(() -> handled); + } + } + + @WorkflowInterface + public interface MyUpdateListener { + @UpdateMethod(name = "myUpdate") + void myUpdate(); + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void aCatchAllUpdateHandlerCarriesTheUpdateId() throws InterruptedException { + List events = catchAllUpdateHistory(); + + // EG-IMPLICIT-70: A catch-all update handler carries the update ID + HistoryEvent fromUpdate = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "from-catch-all-update"); + assertEquals(acceptedUpdateId(events, "non-existent-update"), inboundUpdateId(fromUpdate)); + assertLabelGroups(fromUpdate); + } + + private List catchAllUpdateHistory() throws InterruptedException { + CatchAllUpdateWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(CatchAllUpdateWorkflow.class); + WorkflowClient.start(workflow::execute); + // Updates are rejected until a handler is registered. + assertTrue(activities.handlerRegistered.await(10, TimeUnit.SECONDS)); + WorkflowStub.fromTyped(workflow).update("non-existent-update", Void.class); + WorkflowStub.fromTyped(workflow).getResult(Void.class); + return history(workflow); + } + + @WorkflowInterface + public interface CatchAllUpdateWorkflow { + @WorkflowMethod + void execute(); + } + + public static class CatchAllUpdateWorkflowImpl implements CatchAllUpdateWorkflow { + private boolean handled; + + @Override + public void execute() { + Workflow.registerListener( + (DynamicUpdateHandler) + (name, args) -> { + Workflow.sleep(Duration.ofMillis(1), timer(null, "from-catch-all-update")); + handled = true; + return null; + }); + Workflow.newActivityStub( + EventGroupActivities.class, activity(null, "activity-handler-registered")) + .handlerRegistered(); + Workflow.await(() -> handled); + } + } // //////////////////////////////////////////////////////////////////////////////////////////////// // 5. Event Group Marker Aggregation (`EG-AGGREGATION`) // //////////////////////////////////////////////////////////////////////////////////////////////// @Test - public void directlyAttachedMarkersDedupeById() { + public void markersDedupeByIdWhetherScopedOrDirectlyAttached() { List events = aggregationHistory(); // EG-AGGREGATION-00: Duplicate markers directly attached to one command collapse into a set assertLabelGroups( require(events, EventType.EVENT_TYPE_TIMER_STARTED, "direct-duplicates"), "aaa", "b-id"); + // EG-AGGREGATION-01: Nested scopes of duplicate groups collapse into a set + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "nested-scopes"), "aaa", "b-id"); + + // EG-AGGREGATION-02: Scoped and directly attached markers collapse into one set + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "scope-and-direct-b"), "aaa", "b-id"); + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "scope-and-direct-a-b"), "aaa", "b-id"); + // EG-AGGREGATION-03: The same group instance listed twice contributes one marker assertLabelGroups( require(events, EventType.EVENT_TYPE_TIMER_STARTED, "same-instance-twice"), "aaa"); @@ -230,6 +985,10 @@ public void directlyAttachedMarkersDedupeById() { HistoryEvent sameId = require(events, EventType.EVENT_TYPE_TIMER_STARTED, "same-id-direct"); assertLabelGroups(sameId, "b-id"); assertEquals("bbb1", labelText(onlyLabel(sameId))); + HistoryEvent sameIdScoped = + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "same-id-scope-and-direct"); + assertLabelGroups(sameIdScoped, "b-id"); + assertEquals("bbb1", labelText(onlyLabel(sameIdScoped))); } private List aggregationHistory() { @@ -264,6 +1023,25 @@ public void execute() { .setSummary("direct-duplicates") .addEventGroups(a2, b1, a1, b1, a2, a1) .build()); + a1.run( + () -> + a2.run( + () -> + b1.run( + () -> + Workflow.sleep(Duration.ofMillis(1), timer(null, "nested-scopes"))))); + a1.run( + () -> + b1.run( + () -> { + Workflow.sleep(Duration.ofMillis(1), timer(b1, "scope-and-direct-b")); + Workflow.sleep( + Duration.ofMillis(1), + TimerOptions.newBuilder() + .setSummary("scope-and-direct-a-b") + .addEventGroups(b1, a1) + .build()); + })); Workflow.sleep( Duration.ofMillis(1), TimerOptions.newBuilder() @@ -273,6 +1051,7 @@ public void execute() { Workflow.sleep( Duration.ofMillis(1), TimerOptions.newBuilder().setSummary("same-id-direct").addEventGroups(b1, b2).build()); + b1.run(() -> Workflow.sleep(Duration.ofMillis(1), timer(b2, "same-id-scope-and-direct"))); } } @@ -287,7 +1066,8 @@ public void timerCommandsCarryMarkers() { List events = timerCommandHistory(); // EG-COMMANDS-00: Timer commands carry markers - assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "60s"), "direct"); + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "60s"), "direct", "scope"); } @Test @@ -295,9 +1075,11 @@ public void timerCancellationCommandsCarryTheTimersMarkers() { List events = timerCommandHistory(); // EG-COMMANDS-00-CANCEL: Timer Cancellation commands carry Timer's markers - assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "60s"), "direct"); - assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_CANCELED, null), "direct"); - assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "1ms")); + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "60s"), "direct", "scope"); + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_CANCELED, null), "direct", "scope"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "1ms"), "scope"); } private List timerCommandHistory() { @@ -317,13 +1099,17 @@ public static class TimerCommandWorkflowImpl implements TimerCommandWorkflow { @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - CancellationScope timer60s = - Workflow.newCancellationScope( - () -> Workflow.newTimer(Duration.ofSeconds(60), timer(direct, "60s")).get()); - Promise running = Async.procedure(timer60s::run); - Workflow.sleep(Duration.ofMillis(1), timer(null, "1ms")); - timer60s.cancel(); - swallowCancellation(running); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> { + CancellationScope timer60s = + Workflow.newCancellationScope( + () -> Workflow.newTimer(Duration.ofSeconds(60), timer(direct, "60s")).get()); + Promise running = Async.procedure(timer60s::run); + Workflow.sleep(Duration.ofMillis(1), timer(null, "1ms")); + timer60s.cancel(); + swallowCancellation(running); + }); } } @@ -334,7 +1120,8 @@ public void waitConditionsWithTimeoutsCarryMarkersToTheirTimers() { List events = awaitHistory(); // EG-COMMANDS-01: Wait conditions with timeouts carry markers to their timers - assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, "await"), "direct"); + assertLabelGroups( + require(events, EventType.EVENT_TYPE_TIMER_STARTED, "await"), "direct", "scope"); } private List awaitHistory() { @@ -353,7 +1140,8 @@ public static class AwaitWorkflowImpl implements AwaitWorkflow { @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.await(Duration.ofMillis(1), timer(direct, "await"), () -> false); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run(() -> Workflow.await(Duration.ofMillis(1), timer(direct, "await"), () -> false)); } } @@ -365,7 +1153,9 @@ public void activityCommandsCarryMarkers() { // EG-COMMANDS-02: Activity commands carry markers assertLabelGroups( - require(events, EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED, "activity"), "direct"); + require(events, EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED, "activity"), + "direct", + "scope"); } @Test @@ -376,9 +1166,12 @@ public void activityCancellationCommandsCarryTheActivitysMarkers() { assertLabelGroups( require( events, EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED, "activity-cancelled-sleep-5s"), - "direct"); + "direct", + "scope"); assertLabelGroups( - require(events, EventType.EVENT_TYPE_ACTIVITY_TASK_CANCEL_REQUESTED, null), "direct"); + require(events, EventType.EVENT_TYPE_ACTIVITY_TASK_CANCEL_REQUESTED, null), + "direct", + "scope"); } private List activityCommandHistory() { @@ -402,22 +1195,28 @@ public static class ActivityCommandWorkflowImpl implements ActivityCommandWorkfl @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.newActivityStub(EventGroupActivities.class, activity(direct, "activity")).run(); - - CancellationScope cancellation = - Workflow.newCancellationScope( - () -> - Workflow.newActivityStub( - EventGroupActivities.class, - activity(direct, "activity-cancelled-sleep-5s").toBuilder() - .setCancellationType(ActivityCancellationType.TRY_CANCEL) - .build()) - .sleepUntilCancelled()); - Promise running = Async.procedure(cancellation::run); - // A Workflow Task boundary so the schedule command reaches the server before cancellation. - Workflow.sleep(Duration.ofMillis(1)); - cancellation.cancel(); - swallowCancellation(running); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> { + Workflow.newActivityStub(EventGroupActivities.class, activity(direct, "activity")) + .run(); + + CancellationScope cancellation = + Workflow.newCancellationScope( + () -> + Workflow.newActivityStub( + EventGroupActivities.class, + activity(direct, "activity-cancelled-sleep-5s").toBuilder() + .setCancellationType(ActivityCancellationType.TRY_CANCEL) + .build()) + .sleepUntilCancelled()); + Promise running = Async.procedure(cancellation::run); + // A Workflow Task boundary so the schedule command reaches the server before + // cancellation. + Workflow.sleep(Duration.ofMillis(1)); + cancellation.cancel(); + swallowCancellation(running); + }); } } @@ -429,12 +1228,12 @@ public void localActivityCommandsCarryMarkers() { // EG-COMMANDS-03: Local Activity commands carry markers assertLabelGroups( - require(events, EventType.EVENT_TYPE_MARKER_RECORDED, "local-activity"), "direct"); + require(events, EventType.EVENT_TYPE_MARKER_RECORDED, "local-activity"), "direct", "scope"); // EG-COMMANDS-03-CANCEL is skipped: LocalActivityStateMachine.cancel is a no-op, // so a cancellation request produces no cancellation marker. // assertLabelGroups(require(events, EventType.EVENT_TYPE_ACTIVITY_TASK_CANCEL_REQUESTED, null), - // "direct"); + // "direct", "scope"); } private List localActivityCommandHistory() { @@ -454,9 +1253,12 @@ public static class LocalActivityCommandWorkflowImpl implements LocalActivityCom @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.newLocalActivityStub( - EventGroupActivities.class, localActivity(direct, "local-activity")) - .localActivity(); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.newLocalActivityStub( + EventGroupActivities.class, localActivity(direct, "local-activity")) + .localActivity()); } } @@ -476,9 +1278,9 @@ public void localActivityRetryBackoffTimerCarriesTheLasMarkers() { assertEquals(2, markers.size()); // EG-COMMANDS-03-BACKOFF: Local Activity Retry Backoff Timer carries LA's markers - assertLabelGroups(markers.get(0), "direct"); - assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, null), "direct"); - assertLabelGroups(markers.get(1), "direct"); + assertLabelGroups(markers.get(0), "direct", "scope"); + assertLabelGroups(require(events, EventType.EVENT_TYPE_TIMER_STARTED, null), "direct", "scope"); + assertLabelGroups(markers.get(1), "direct", "scope"); } private List localActivityBackoffHistory() { @@ -515,7 +1317,11 @@ public void execute() { .setSummary("backoff-local-activity-fail-first-attempt") .addEventGroups(direct) .build(); - Workflow.newLocalActivityStub(EventGroupActivities.class, options).failFirstAttempt(); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.newLocalActivityStub(EventGroupActivities.class, options) + .failFirstAttempt()); } } @@ -526,7 +1332,7 @@ public void childWorkflowCommandsCarryMarkers() { List events = childCommandHistory(); // EG-COMMANDS-04: Child Workflow commands carry markers - assertLabelGroups(requireChild(events, "child-workflow"), "direct"); + assertLabelGroups(requireChild(events, "child-workflow"), "direct", "scope"); } @Test @@ -535,13 +1341,14 @@ public void childWorkflowCancellationCommandsCarryTheChildWorkflowsMarkers() { // EG-COMMANDS-04-CANCEL: Child Workflow Cancellation commands carry the Child Workflow's // markers - assertLabelGroups(requireChild(events, "child-workflow-sleep-5s"), "direct"); + assertLabelGroups(requireChild(events, "child-workflow-sleep-5s"), "direct", "scope"); assertLabelGroups( require( events, EventType.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED, null), - "direct"); + "direct", + "scope"); } private List childCommandHistory() { @@ -561,24 +1368,29 @@ public static class ChildCommandWorkflowImpl implements ChildCommandWorkflow { @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.newChildWorkflowStub(ChildWorkflow.class, child(direct, "child-workflow")).execute(); - - // The plan uses WAIT_CANCELLATION_REQUESTED, but the test server delivers the cancel request - // without holding the time-skipping lock, so time can skip to the parent's run timeout while - // the parent waits for the request to be acknowledged. - ChildWorkflowSleep5s sleeping = - Workflow.newChildWorkflowStub( - ChildWorkflowSleep5s.class, - child(direct, "child-workflow-sleep-5s").toBuilder() - .setCancellationType(ChildWorkflowCancellationType.TRY_CANCEL) - .build()); - CancellationScope cancellation = Workflow.newCancellationScope(sleeping::execute); - Promise running = Async.procedure(cancellation::run); - // Cancelling while the child's start is still in flight makes the test server reject the - // workflow task with UnhandledCommand, and the workflow then times out. - Workflow.getWorkflowExecution(sleeping).get(); - cancellation.cancel(); - swallowCancellation(running); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> { + Workflow.newChildWorkflowStub(ChildWorkflow.class, child(direct, "child-workflow")) + .execute(); + + // The plan uses WAIT_CANCELLATION_REQUESTED, but the test server delivers the cancel + // request without holding the time-skipping lock, so time can skip to the parent's run + // timeout while the parent waits for the request to be acknowledged. + ChildWorkflowSleep5s sleeping = + Workflow.newChildWorkflowStub( + ChildWorkflowSleep5s.class, + child(direct, "child-workflow-sleep-5s").toBuilder() + .setCancellationType(ChildWorkflowCancellationType.TRY_CANCEL) + .build()); + CancellationScope cancellation = Workflow.newCancellationScope(sleeping::execute); + Promise running = Async.procedure(cancellation::run); + // Cancelling while the child's start is still in flight makes the test server reject + // the workflow task with UnhandledCommand, and the workflow then times out. + Workflow.getWorkflowExecution(sleeping).get(); + cancellation.cancel(); + swallowCancellation(running); + }); } } @@ -614,7 +1426,7 @@ public void signalExternalWorkflowCommandsCarryMarkers() { List events = signalCommandHistory(); // EG-COMMANDS-06: Signal External Workflow commands carry markers - assertLabelGroups(requireSignal(events, "other-workflow"), "direct"); + assertLabelGroups(requireSignal(events, "other-workflow"), "direct", "scope"); } @Test @@ -622,7 +1434,7 @@ public void signalChildWorkflowCommandsCarryMarkers() { List events = signalCommandHistory(); // EG-COMMANDS-06-CHILD: Signal Child Workflow commands carry markers - assertLabelGroups(requireSignal(events, "child-workflow-wait-for-signals"), "direct"); + assertLabelGroups(requireSignal(events, "child-workflow-wait-for-signals"), "direct", "scope"); } private List signalCommandHistory() { @@ -642,23 +1454,31 @@ public static class SignalCommandWorkflowImpl implements SignalCommandWorkflow { @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - try { - Workflow.newUntypedExternalWorkflowStub("other-workflow") - .signalWithOptions( - "signal", - SignalExternalWorkflowOptions.newBuilder().addEventGroups(direct).build()); - } catch (ApplicationFailure expected) { - // A missing workflow still records the initiated event before the request fails. - } - + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> { + try { + Workflow.newUntypedExternalWorkflowStub("other-workflow") + .signalWithOptions( + "signal", + SignalExternalWorkflowOptions.newBuilder().addEventGroups(direct).build()); + } catch (ApplicationFailure expected) { + // A missing workflow still records the initiated event before the request fails. + } + }); + + // Started outside the scope, so only the signal can contribute the scope's marker. ChildWorkflowWaitForSignals child = Workflow.newChildWorkflowStub( ChildWorkflowWaitForSignals.class, child(null, "child-workflow-wait-for-signals")); Promise done = Async.procedure(child::execute); Workflow.getWorkflowExecution(child).get(); - ChildWorkflowStub.fromTyped(child) - .signalWithOptions( - "signal", SignalChildWorkflowOptions.newBuilder().addEventGroups(direct).build()); + scope.run( + () -> + ChildWorkflowStub.fromTyped(child) + .signalWithOptions( + "signal", + SignalChildWorkflowOptions.newBuilder().addEventGroups(direct).build())); done.get(); } } @@ -698,7 +1518,8 @@ public void cancelExternalWorkflowCommandsCarryMarkers() { events, EventType.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED, null), - "direct"); + "direct", + "scope"); } private List cancelExternalCommandHistory() { @@ -718,13 +1539,18 @@ public static class CancelExternalCommandWorkflowImpl implements CancelExternalC @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); + EventGroup scope = Workflow.newEventGroup("scope"); // The test server never resolves a cancel of a missing workflow, so waiting on it runs the // workflow out to its start-to-close timeout. The initiated event is recorded either way. - Async.procedure( + scope.run( () -> - Workflow.newUntypedExternalWorkflowStub("other-workflow") - .cancelWithOptions( - CancelExternalWorkflowOptions.newBuilder().addEventGroups(direct).build())); + Async.procedure( + () -> + Workflow.newUntypedExternalWorkflowStub("other-workflow") + .cancelWithOptions( + CancelExternalWorkflowOptions.newBuilder() + .addEventGroups(direct) + .build()))); } } @@ -736,7 +1562,9 @@ public void modifyWorkflowPropertiesCommandsCarryMarkers() { // EG-COMMANDS-20: Modify Workflow Properties commands carry markers assertLabelGroups( - require(events, EventType.EVENT_TYPE_WORKFLOW_PROPERTIES_MODIFIED, null), "direct"); + require(events, EventType.EVENT_TYPE_WORKFLOW_PROPERTIES_MODIFIED, null), + "direct", + "scope"); } private List upsertMemoCommandHistory() { @@ -756,9 +1584,12 @@ public static class UpsertMemoCommandWorkflowImpl implements UpsertMemoCommandWo @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.upsertMemo( - Collections.singletonMap("some-key", "some-value"), - UpsertMemoOptions.newBuilder().addEventGroups(direct).build()); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.upsertMemo( + Collections.singletonMap("some-key", "some-value"), + UpsertMemoOptions.newBuilder().addEventGroups(direct).build())); } } @@ -773,7 +1604,9 @@ public void upsertSearchAttributeCommandsCarryMarkers() { // EG-COMMANDS-21: Upsert Search Attribute commands carry markers assertLabelGroups( - require(events, EventType.EVENT_TYPE_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES, null), "direct"); + require(events, EventType.EVENT_TYPE_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES, null), + "direct", + "scope"); } private List upsertSearchAttributesCommandHistory() { @@ -794,22 +1627,28 @@ public static class UpsertSearchAttributesCommandWorkflowImpl @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.upsertTypedSearchAttributes( - UpsertSearchAttributesOptions.newBuilder().addEventGroups(direct).build(), - SearchAttributeKey.forKeyword("CustomKeywordField").valueSet("some-value")); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.upsertTypedSearchAttributes( + UpsertSearchAttributesOptions.newBuilder().addEventGroups(direct).build(), + SearchAttributeKey.forKeyword("CustomKeywordField").valueSet("some-value"))); } } // //////////////////////////////////////////////////////////////////////////////////////////////// @Test - public void versionCommandsCarryMarkers() { + public void versionCommandsCarryTheAmbientScope() { List events = versionCommandHistory(); - // EG-COMMANDS-23: Version commands carry markers - assertLabelGroups(require(events, EventType.EVENT_TYPE_MARKER_RECORDED, null), "direct"); + // EG-COMMANDS-23: Version commands carry the ambient scope + assertLabelGroups( + require(events, EventType.EVENT_TYPE_MARKER_RECORDED, null), "direct", "scope"); assertLabelGroups( - require(events, EventType.EVENT_TYPE_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES, null), "direct"); + require(events, EventType.EVENT_TYPE_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES, null), + "direct", + "scope"); } private List versionCommandHistory() { @@ -829,22 +1668,26 @@ public static class VersionCommandWorkflowImpl implements VersionCommandWorkflow @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.getVersion( - "my-change", - Workflow.DEFAULT_VERSION, - 1, - GetVersionOptions.newBuilder().addEventGroups(direct).build()); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.getVersion( + "my-change", + Workflow.DEFAULT_VERSION, + 1, + GetVersionOptions.newBuilder().addEventGroups(direct).build())); } } // //////////////////////////////////////////////////////////////////////////////////////////////// @Test - public void sideEffectCommandsCarryMarkers() { + public void sideEffectCommandsCarryTheAmbientScope() { List events = sideEffectCommandHistory(); - // EG-COMMANDS-24: Side Effect commands carry markers - assertLabelGroups(require(events, EventType.EVENT_TYPE_MARKER_RECORDED, null), "direct"); + // EG-COMMANDS-24: Side Effect commands carry the ambient scope + assertLabelGroups( + require(events, EventType.EVENT_TYPE_MARKER_RECORDED, null), "direct", "scope"); } private List sideEffectCommandHistory() { @@ -864,19 +1707,25 @@ public static class SideEffectCommandWorkflowImpl implements SideEffectCommandWo @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.sideEffect( - Integer.class, () -> 42, SideEffectOptions.newBuilder().addEventGroups(direct).build()); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.sideEffect( + Integer.class, + () -> 42, + SideEffectOptions.newBuilder().addEventGroups(direct).build())); } } // //////////////////////////////////////////////////////////////////////////////////////////////// @Test - public void mutableSideEffectCommandsCarryMarkers() { + public void mutableSideEffectCommandsCarryTheAmbientScope() { List events = mutableSideEffectCommandHistory(); - // EG-COMMANDS-25: Mutable Side Effect commands carry markers - assertLabelGroups(require(events, EventType.EVENT_TYPE_MARKER_RECORDED, null), "direct"); + // EG-COMMANDS-25: Mutable Side Effect commands carry the ambient scope + assertLabelGroups( + require(events, EventType.EVENT_TYPE_MARKER_RECORDED, null), "direct", "scope"); } private List mutableSideEffectCommandHistory() { @@ -897,24 +1746,29 @@ public static class MutableSideEffectCommandWorkflowImpl @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.mutableSideEffect( - "my-id", - Integer.class, - (previous, next) -> !previous.equals(next), - () -> 42, - MutableSideEffectOptions.newBuilder().addEventGroups(direct).build()); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.mutableSideEffect( + "my-id", + Integer.class, + (previous, next) -> !previous.equals(next), + () -> 42, + MutableSideEffectOptions.newBuilder().addEventGroups(direct).build())); } } // //////////////////////////////////////////////////////////////////////////////////////////////// @Test - public void continueAsNewCommandsCarryMarkers() { + public void continueAsNewCarriesBothDirectlyAttachedAndAmbientMarkers() { List events = continueAsNewCommandHistory(); - // EG-COMMANDS-40: Continue-As-New carries directly attached markers + // EG-COMMANDS-40: Continue-As-New carries directly attached and ambient markers assertLabelGroups( - require(events, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CONTINUED_AS_NEW, null), "direct"); + require(events, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CONTINUED_AS_NEW, null), + "direct", + "scope"); } private List continueAsNewCommandHistory() { @@ -939,7 +1793,51 @@ public String execute() { return firstRunId.get(); } EventGroup direct = Workflow.newEventGroup("direct"); - Workflow.continueAsNew(ContinueAsNewOptions.newBuilder().addEventGroups(direct).build()); + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run( + () -> + Workflow.continueAsNew( + ContinueAsNewOptions.newBuilder().addEventGroups(direct).build())); + return "unreachable"; + } + } + + // //////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + public void shortFormContinueAsNewCarriesAmbientMarkersOnly() { + List events = continueAsNewShortFormCommandHistory(); + + // EG-COMMANDS-40: Continue-As-New's short form, which takes no options, carries ambient + // markers only + assertLabelGroups( + require(events, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CONTINUED_AS_NEW, null), "scope"); + } + + private List continueAsNewShortFormCommandHistory() { + ContinueAsNewShortFormCommandWorkflow workflow = + testWorkflowRule.newWorkflowStubTimeoutOptions(ContinueAsNewShortFormCommandWorkflow.class); + return history(workflow, workflow.execute()); + } + + @WorkflowInterface + public interface ContinueAsNewShortFormCommandWorkflow { + @WorkflowMethod + String execute(); + } + + public static class ContinueAsNewShortFormCommandWorkflowImpl + implements ContinueAsNewShortFormCommandWorkflow { + @Override + public String execute() { + // The markers stay on the first run, which the client stub no longer points at once the + // workflow has continued as new. + Optional firstRunId = Workflow.getInfo().getContinuedExecutionRunId(); + if (firstRunId.isPresent()) { + return firstRunId.get(); + } + EventGroup scope = Workflow.newEventGroup("scope"); + scope.run(() -> Workflow.continueAsNew()); return "unreachable"; } } @@ -961,12 +1859,16 @@ public interface EventGroupActivities { @ActivityMethod(name = "backoff-local-activity-fail-first-attempt") void failFirstAttempt(); + + @ActivityMethod(name = "activity-handler-registered") + void handlerRegistered(); } public static class Activities implements EventGroupActivities { // Released after the workflow returns, so the cancelled activity does not complete into a // workflow task that is still running. private CountDownLatch hold = new CountDownLatch(1); + private CountDownLatch handlerRegistered = new CountDownLatch(1); @Override public void run() {} @@ -992,6 +1894,11 @@ public void failFirstAttempt() { throw ApplicationFailure.newFailure("flaky activity failing on purpose", "flaky"); } } + + @Override + public void handlerRegistered() { + handlerRegistered.countDown(); + } } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1063,6 +1970,60 @@ private static String labelText(EventGroupMarker marker) { marker.getLabel().getLabel(), String.class, String.class); } + private static void assertNoMarkers(HistoryEvent event) { + assertEquals(Collections.emptyList(), event.getEventGroupMarkersList()); + } + + private static long inboundEventId(HistoryEvent event) { + List ids = new ArrayList<>(); + for (EventGroupMarker marker : event.getEventGroupMarkersList()) { + if (marker.hasInboundEvent()) { + ids.add(marker.getInboundEvent().getInboundEventId()); + } + } + assertEquals(1, ids.size()); + return ids.get(0); + } + + private static String inboundUpdateId(HistoryEvent event) { + List ids = new ArrayList<>(); + for (EventGroupMarker marker : event.getEventGroupMarkersList()) { + if (marker.hasInboundUpdate()) { + ids.add(marker.getInboundUpdate().getInboundUpdateId()); + } + } + assertEquals(1, ids.size()); + return ids.get(0); + } + + private static long signaledEventId(List events, String signalName) { + for (HistoryEvent event : events) { + if (event.getEventType() == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED + && signalName.equals( + event.getWorkflowExecutionSignaledEventAttributes().getSignalName())) { + return event.getEventId(); + } + } + fail("no signal named " + signalName); + return 0; + } + + private static String acceptedUpdateId(List events, String updateName) { + for (HistoryEvent event : events) { + if (event.getEventType() == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_UPDATE_ACCEPTED + && updateName.equals( + event + .getWorkflowExecutionUpdateAcceptedEventAttributes() + .getAcceptedRequest() + .getInput() + .getName())) { + return event.getWorkflowExecutionUpdateAcceptedEventAttributes().getProtocolInstanceId(); + } + } + fail("no accepted update named " + updateName); + return null; + } + private static String summaryOf(HistoryEvent event) { if (!event.hasUserMetadata() || !event.getUserMetadata().hasSummary()) { return ""; @@ -1131,12 +2092,15 @@ private static LocalActivityOptions localActivity(EventGroup direct, String summ } private static ActivityOptions activity(EventGroup direct, String summary) { - return ActivityOptions.newBuilder() - .setScheduleToStartTimeout(Duration.ofSeconds(10)) - .setStartToCloseTimeout(Duration.ofSeconds(20)) - .setSummary(summary) - .addEventGroups(direct) - .build(); + ActivityOptions.Builder builder = + ActivityOptions.newBuilder() + .setScheduleToStartTimeout(Duration.ofSeconds(10)) + .setStartToCloseTimeout(Duration.ofSeconds(20)) + .setSummary(summary); + if (direct != null) { + builder.addEventGroups(direct); + } + return builder.build(); } private static TimerOptions timer(EventGroup direct, String summary) { diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/EventGroupsNexusTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/EventGroupsNexusTest.java index 0453dda22d..32a9ee2bf2 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/EventGroupsNexusTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/EventGroupsNexusTest.java @@ -64,7 +64,8 @@ public void nexusOperationCommandsCarryMarkers() { // EG-COMMANDS-05: Nexus Operation commands carry markers assertLabelGroups( require(events, EventType.EVENT_TYPE_NEXUS_OPERATION_SCHEDULED, "nexus-operation"), - "direct"); + "direct", + "scope"); } @Test @@ -75,9 +76,12 @@ public void nexusOperationCancellationCommandsCarryTheNexusOperationsMarkers() { // markers assertLabelGroups( require(events, EventType.EVENT_TYPE_NEXUS_OPERATION_SCHEDULED, "nexus-operation-sleep-5s"), - "direct"); + "direct", + "scope"); assertLabelGroups( - require(events, EventType.EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED, null), "direct"); + require(events, EventType.EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED, null), + "direct", + "scope"); } private List nexusCommandHistory() { @@ -100,6 +104,7 @@ public static class NexusCommandWorkflowImpl implements NexusCommandWorkflow { @Override public void execute() { EventGroup direct = Workflow.newEventGroup("direct"); + EventGroup scope = Workflow.newEventGroup("scope"); NexusServiceOptions serviceOptions = NexusServiceOptions.newBuilder() .setEndpoint(getEndpointName()) @@ -111,24 +116,28 @@ public void execute() { .build(); TestNexusService service = Workflow.newNexusServiceStub(TestNexusService.class, serviceOptions); - Workflow.startNexusOperation(service::nexusOperation).getResult().get(); - - try { - Workflow.newCancellationScope( - () -> { - NexusOperationHandle handle = - Workflow.startNexusOperation(service::nexusOperationSleep5s); - // Cancelling before the operation is scheduled records no cancel-requested event. - handle.getExecution().get(); - CancellationScope.current().cancel(); - handle.getResult().get(); - }) - .run(); - } catch (NexusOperationFailure expected) { - if (!(expected.getCause() instanceof CanceledFailure)) { - throw expected; - } - } + scope.run( + () -> { + Workflow.startNexusOperation(service::nexusOperation).getResult().get(); + + try { + Workflow.newCancellationScope( + () -> { + NexusOperationHandle handle = + Workflow.startNexusOperation(service::nexusOperationSleep5s); + // Cancelling before the operation is scheduled records no + // cancel-requested event. + handle.getExecution().get(); + CancellationScope.current().cancel(); + handle.getResult().get(); + }) + .run(); + } catch (NexusOperationFailure expected) { + if (!(expected.getCause() instanceof CanceledFailure)) { + throw expected; + } + } + }); } }