Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ private WorkflowThread newRootThread(Runnable runnable) {
ROOT_THREAD_PRIORITY,
false,
runnerCancellationScope,
EventGroupScope.EMPTY,
runnable,
cache,
getContextPropagators(),
Expand Down Expand Up @@ -472,6 +473,7 @@ public WorkflowThread newWorkflowThread(
WORKFLOW_THREAD_PRIORITY + (addedThreads++),
detached,
CancellationScopeImpl.current(),
EventGroupScope.current(),
runnable,
cache,
getContextPropagators(),
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,6 +34,27 @@ public String getLabel() {
return label;
}

@Override
public void run(Functions.Proc proc) {
run(
() -> {
proc.apply();
return null;
});
}

@Override
public <R> R run(Functions.Func<R> 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) {
Expand All @@ -56,24 +73,4 @@ EventGroupMarker toMarker(DataConverter dataConverter) {
public String toString() {
return "EventGroup{" + "id='" + id + '\'' + ", label='" + label + '\'' + '}';
}

static List<EventGroupMarker> toMarkers(
Collection<EventGroup> groups, DataConverter dataConverter) {
if (groups.isEmpty()) {
return Collections.emptyList();
}
Map<String, EventGroupImpl> 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<EventGroupMarker> markers = new ArrayList<>(byId.size());
for (EventGroupImpl group : byId.values()) {
markers.add(group.toMarker(dataConverter));
}
return markers;
}
}
Original file line number Diff line number Diff line change
@@ -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<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 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class RootWorkflowThreadImpl extends WorkflowThreadImpl {
int priority,
boolean detached,
CancellationScopeImpl parentCancellationScope,
EventGroupScope eventGroupScope,
Runnable runnable,
WorkflowExecutorCache cache,
List<ContextPropagator> contextPropagators,
Expand All @@ -32,6 +33,7 @@ public class RootWorkflowThreadImpl extends WorkflowThreadImpl {
priority,
detached,
parentCancellationScope,
eventGroupScope,
runnable,
cache,
contextPropagators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ public <R> LocalActivityOutput<R> executeLocalActivity(LocalActivityInput<R> inp
long originalScheduledTime = System.currentTimeMillis();
CompletablePromise<LocalActivityCallback.Output> 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(),
Expand All @@ -475,6 +482,7 @@ public <R> LocalActivityOutput<R> executeLocalActivity(LocalActivityInput<R> inp
originalScheduledTime,
1,
null,
eventGroupScope,
localActivityResult);

// Avoid passing the input to the output handle as it causes the input to be retained for the
Expand Down Expand Up @@ -512,6 +520,7 @@ private void executeLocalActivityOverLocalRetryThreshold(
long originalScheduledTime,
int attempt,
@Nullable Failure previousExecutionFailure,
EventGroupScope eventGroupScope,
CompletablePromise<LocalActivityCallback.Output> result) {
CompletablePromise<LocalActivityCallback.Output> localExecutionResult =
executeLocalActivityLocally(
Expand All @@ -523,7 +532,8 @@ private void executeLocalActivityOverLocalRetryThreshold(
arguments,
originalScheduledTime,
attempt,
previousExecutionFailure);
previousExecutionFailure,
eventGroupScope);

localExecutionResult.handle(
(r, e) -> {
Expand All @@ -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(
Expand All @@ -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;
});
Expand Down Expand Up @@ -583,7 +591,8 @@ private CompletablePromise<LocalActivityCallback.Output> executeLocalActivityLoc
@Nullable LocalActivityArguments arguments,
long originalScheduledTime,
int attempt,
@Nullable Failure previousExecutionFailure) {
@Nullable Failure previousExecutionFailure,
EventGroupScope eventGroupScope) {

LocalActivityCallbackImpl callback = new LocalActivityCallbackImpl();
ExecuteLocalActivityParameters params =
Expand All @@ -596,7 +605,8 @@ private CompletablePromise<LocalActivityCallback.Output> executeLocalActivityLoc
arguments,
attempt,
originalScheduledTime,
previousExecutionFailure);
previousExecutionFailure,
eventGroupScope);
Functions.Proc cancellationCallback = replayContext.scheduleLocalActivityTask(params, callback);
CancellationScope.current()
.getCancellationRequest()
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1835,7 +1849,8 @@ public Optional<UpdateInfo> getCurrentUpdateInfo() {
}

private List<EventGroupMarker> eventGroupMarkers(List<EventGroup> 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 */
Expand Down
Loading
Loading