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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,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.
- `@TemporalOperation` methods can return their output directly for synchronous completion.
Existing methods returning `TemporalOperationResult`, including asynchronous results, remain
supported.
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,30 @@ 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could add a check that we are in a workflow here

if (!WorkflowInternal.isWorkflowThread()) {
throw new IllegalStateException("Cannot be called by non workflow thread");
}
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 +76,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,116 @@
package io.temporal.internal.sync;

import com.google.common.base.Preconditions;
import io.temporal.api.sdk.v1.EventGroupMarker;
import io.temporal.common.converter.DataConverter;
import io.temporal.workflow.EventGroup;
import io.temporal.workflow.Functions;
import java.util.*;
import javax.annotation.Nullable;

/**
* The Event Groups that apply to commands issued from the current workflow thread, either because
* they were entered with {@link EventGroup#run}, or implicitly because the thread runs a signal or
* update handler.
*/
final class EventGroupScope {
static final EventGroupScope EMPTY = new EventGroupScope(Collections.emptyMap(), null);

private static final ThreadLocal<EventGroupScope> currentScope =
ThreadLocal.withInitial(() -> EMPTY);

static EventGroupScope current() {
return currentScope.get();
}

static <R> R runInInboundEventScope(long eventId, Functions.Func<R> func) {
EventGroupMarker marker =
EventGroupMarker.newBuilder()
.setInboundEvent(EventGroupMarker.InboundEvent.newBuilder().setInboundEventId(eventId))
.build();

// Runtime-registered handlers do not inherit scoped EGs from the thread that registered them
return new EventGroupScope(Collections.emptyMap(), marker).run(func);
}

static <R> R runInInboundUpdateScope(String updateId, Functions.Func<R> func) {
EventGroupMarker marker =
EventGroupMarker.newBuilder()
.setInboundUpdate(
EventGroupMarker.InboundUpdate.newBuilder().setInboundUpdateId(updateId))
.build();

// Runtime-registered handlers do not inherit scoped EGs from the thread that registered them
return new EventGroupScope(Collections.emptyMap(), marker).run(func);
}

/** Keyed by group ID, to make merging scopes easier. */
private final Map<String, EventGroupImpl> explicitGroups;

private final @Nullable EventGroupMarker implicitGroup;

private EventGroupScope(
Map<String, EventGroupImpl> explicitGroups, @Nullable EventGroupMarker implicitGroup) {
this.explicitGroups = explicitGroups;
this.implicitGroup = implicitGroup;
}

EventGroupScope withExplicitGroups(Collection<EventGroup> additionalGroups) {
if (additionalGroups.isEmpty()) {
return this;
}
Map<String, EventGroupImpl> merged = new LinkedHashMap<>(explicitGroups);
for (EventGroup group : additionalGroups) {
Preconditions.checkArgument(
group instanceof EventGroupImpl,
"Event groups must be created with Workflow.newEventGroup, got %s",
group);
merged.putIfAbsent(group.getId(), (EventGroupImpl) group);
}
return new EventGroupScope(merged, implicitGroup);
}

/** Run the procedure with this scope as the current one. */
void run(Functions.Proc proc) {
run(
() -> {
proc.apply();
return null;
});
}

/** Run the function with this scope as the current one. */
<R> R run(Functions.Func<R> func) {
EventGroupScope previous = current();
try {
currentScope.set(this);
return func.apply();
} finally {
EventGroupScope after = currentScope.get();
currentScope.set(previous);
if (after != this) {
throw new Error("Unexpected Event Group Scope on exit");
}
}
}

/**
* @param directGroups groups attached directly to the command, through its options
* @param dataConverter converter used to encode labels, including any payload codec
*/
List<EventGroupMarker> toMarkers(
Collection<EventGroup> directGroups, DataConverter dataConverter) {
EventGroupScope effective = withExplicitGroups(directGroups);
if (effective.explicitGroups.isEmpty() && effective.implicitGroup == null) {
return Collections.emptyList();
}
List<EventGroupMarker> markers = new ArrayList<>(effective.explicitGroups.size() + 1);
for (EventGroupImpl group : effective.explicitGroups.values()) {
markers.add(group.toMarker(dataConverter));
}
if (effective.implicitGroup != null) {
markers.add(effective.implicitGroup);
}
return markers;
}
}
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
Loading
Loading