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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface WorkflowOutboundCallsInterceptor {

final class ActivityInput<R> {
private final String activityName;
private final @Nullable String activityId;
private final Class<R> resultClass;
private final Type resultType;
private final Object[] args;
Expand All @@ -54,7 +55,19 @@ public ActivityInput(
Object[] args,
ActivityOptions options,
Header header) {
this(activityName, null, resultClass, resultType, args, options, header);
}

public ActivityInput(
String activityName,
@Nullable String activityId,
Class<R> resultClass,
Type resultType,
Object[] args,
ActivityOptions options,
Header header) {
this.activityName = activityName;
this.activityId = activityId;
this.resultClass = resultClass;
this.resultType = resultType;
this.args = args;
Expand All @@ -66,6 +79,12 @@ public String getActivityName() {
return activityName;
}

/** Returns the caller-supplied Activity ID, or {@code null} if the SDK should generate one. */
@Nullable
public String getActivityId() {
return activityId;
}

public Class<R> getResultClass() {
return resultClass;
}
Expand Down Expand Up @@ -107,6 +126,7 @@ public Promise<R> getResult() {

final class LocalActivityInput<R> {
private final String activityName;
private final @Nullable String activityId;
private final Class<R> resultClass;
private final Type resultType;
private final Object[] args;
Expand All @@ -120,7 +140,19 @@ public LocalActivityInput(
Object[] args,
LocalActivityOptions options,
Header header) {
this(activityName, null, resultClass, resultType, args, options, header);
}

public LocalActivityInput(
String activityName,
@Nullable String activityId,
Class<R> resultClass,
Type resultType,
Object[] args,
LocalActivityOptions options,
Header header) {
this.activityName = activityName;
this.activityId = activityId;
this.resultClass = resultClass;
this.resultType = resultType;
this.args = args;
Expand All @@ -132,6 +164,12 @@ public String getActivityName() {
return activityName;
}

/** Returns the caller-supplied Activity ID, or {@code null} if the SDK should generate one. */
@Nullable
public String getActivityId() {
return activityId;
}

public Class<R> getResultClass() {
return resultClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import io.temporal.activity.ActivityOptions;
import io.temporal.common.MethodRetry;
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
import io.temporal.workflow.ActivityInvocationOptions;
import io.temporal.workflow.ActivityStub;
import io.temporal.workflow.Functions;
import io.temporal.workflow.Promise;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
Expand Down Expand Up @@ -46,22 +48,44 @@ private ActivityInvocationHandler(
@Override
protected Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, String activityName) {
Function<Object[], Object> function;
ActivityOptions merged =
ActivityOptions.newBuilder(options)
.mergeActivityOptions(this.activityMethodOptions.get(activityName))
.mergeMethodRetry(methodRetry)
.build();
if (merged.getStartToCloseTimeout() == null && merged.getScheduleToCloseTimeout() == null) {

if (ActivityInvocationInternal.isActive()) {
ActivityInvocationOptions invocationOptions = ActivityInvocationInternal.consumeOptions();
ActivityStub stub =
newStub(ActivityStubImpl.resolveOptions(merged, invocationOptions), merged, activityName);
return (a) -> {
Promise<?> result =
stub.executeAsync(
activityName,
method.getReturnType(),
method.getGenericReturnType(),
invocationOptions,
a);
ActivityInvocationInternal.setResult(result);
return null;
};
}

ActivityStub stub = newStub(merged, merged, activityName);
return (a) ->
stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
}

private ActivityStub newStub(
ActivityOptions effectiveOptions, ActivityOptions stubOptions, String activityName) {
if (effectiveOptions.getStartToCloseTimeout() == null
&& effectiveOptions.getScheduleToCloseTimeout() == null) {
throw new IllegalArgumentException(
"Both StartToCloseTimeout and ScheduleToCloseTimeout aren't specified for "
+ activityName
+ " activity. Please set at least one of the above through the ActivityStub or WorkflowImplementationOptions.");
}
ActivityStub stub = ActivityStubImpl.newInstance(merged, activityExecutor, assertReadOnly);
function =
(a) -> stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
return function;
return ActivityStubImpl.newInstance(stubOptions, activityExecutor, assertReadOnly);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.temporal.internal.sync;

import io.temporal.workflow.ActivityInvocationOptions;
import io.temporal.workflow.Functions;
import io.temporal.workflow.Promise;
import java.util.Objects;

/** Captures one typed Activity proxy invocation and its result Promise. */
final class ActivityInvocationInternal {

private static final ThreadLocal<State> invocation = new ThreadLocal<>();

private ActivityInvocationInternal() {}

static <R> Promise<R> invoke(
ActivityInvocationOptions options, Functions.Proc invocationFunction) {
if (invocation.get() != null) {
throw new IllegalStateException("Already invoking an Activity with invocation options");
}

State state = new State(Objects.requireNonNull(options, "options"));
invocation.set(state);
try {
invocationFunction.apply();
return state.getResult();
Comment on lines +23 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve transformations performed by activity lambdas

When the supplied function is a lambda that transforms the activity result, such as () -> activities.getNumber() + 1, the proxy captures the underlying Activity Promise but returns a default value to the lambda; this call then discards the lambda's computed result and returns the captured promise unchecked. Consequently the example resolves to the raw Activity value rather than the incremented value, and lambdas that dereference object results can throw NullPointerException. Either preserve the function's result semantics or reject anything other than a direct Activity invocation.

Useful? React with 👍 / 👎.

} finally {
invocation.remove();
}
}

static ActivityInvocationOptions consumeOptions() {
State state = invocation.get();
if (state == null) {
throw new IllegalStateException("Not invoking an Activity with invocation options");
}
if (state.consumed) {
throw new IllegalStateException("ActivityInvocationOptions can apply to only one invocation");
}
state.consumed = true;
return state.options;
}

static boolean isActive() {
return invocation.get() != null;
}

static <R> void setResult(Promise<R> result) {
State state = invocation.get();
if (state == null) {
throw new IllegalStateException("Not invoking an Activity with invocation options");
}
if (state.result != null) {
throw new IllegalStateException("ActivityInvocationOptions can apply to only one invocation");
}
state.result = Objects.requireNonNull(result, "result");
}

private static final class State {
private final ActivityInvocationOptions options;
private boolean consumed;
private Promise<?> result;

private State(ActivityInvocationOptions options) {
this.options = options;
}

@SuppressWarnings("unchecked")
private <R> Promise<R> getResult() {
if (!consumed || result == null) {
throw new IllegalArgumentException(
"activityMethod must invoke an Activity stub created through Workflow.newActivityStub "
+ "or Workflow.newLocalActivityStub");
}
return (Promise<R>) result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Defaults;
import io.temporal.failure.ActivityFailure;
import io.temporal.workflow.ActivityInvocationOptions;
import io.temporal.workflow.ActivityStub;
import io.temporal.workflow.Promise;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -40,4 +41,50 @@ public <R> Promise<R> executeAsync(String activityName, Class<R> resultClass, Ob
@Override
public abstract <R> Promise<R> executeAsync(
String activityName, Class<R> resultClass, Type resultType, Object... args);

@Override
public <R> R execute(
String activityName,
Class<R> resultClass,
ActivityInvocationOptions options,
Object... args) {
return execute(activityName, resultClass, resultClass, options, args);
}

@Override
public <R> R execute(
String activityName,
Class<R> resultClass,
Type resultType,
ActivityInvocationOptions options,
Object... args) {
Promise<R> result = executeAsync(activityName, resultClass, resultType, options, args);
if (AsyncInternal.isAsync()) {
AsyncInternal.setAsyncResult(result);
return Defaults.defaultValue(resultClass);
}
try {
return result.get();
} catch (ActivityFailure e) {
e.setStackTrace(Thread.currentThread().getStackTrace());
throw e;
}
}

@Override
public <R> Promise<R> executeAsync(
String activityName,
Class<R> resultClass,
ActivityInvocationOptions options,
Object... args) {
return executeAsync(activityName, resultClass, resultClass, options, args);
}

@Override
public abstract <R> Promise<R> executeAsync(
String activityName,
Class<R> resultClass,
Type resultType,
ActivityInvocationOptions options,
Object... args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import io.temporal.activity.ActivityOptions;
import io.temporal.common.interceptors.Header;
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
import io.temporal.workflow.ActivityInvocationOptions;
import io.temporal.workflow.ActivityStub;
import io.temporal.workflow.Functions;
import io.temporal.workflow.Promise;
import java.lang.reflect.Type;
import java.util.Objects;

final class ActivityStubImpl extends ActivityStubBase {
protected final ActivityOptions options;
Expand All @@ -31,14 +33,50 @@ static ActivityStub newInstance(
this.assertReadOnly = assertReadOnly;
}

static ActivityOptions resolveOptions(
ActivityOptions options, ActivityInvocationOptions invocationOptions) {
ActivityOptions invocationActivityOptions = invocationOptions.getActivityOptions();
if (invocationActivityOptions == null) {
return options;
}
return ActivityOptions.newBuilder(invocationActivityOptions).validateAndBuildWithDefaults();
}

@Override
public <R> Promise<R> executeAsync(
String activityName, Class<R> resultClass, Type resultType, Object... args) {
return executeAsyncInternal(activityName, resultClass, resultType, null, options, args);
}

@Override
public <R> Promise<R> executeAsync(
String activityName,
Class<R> resultClass,
Type resultType,
ActivityInvocationOptions invocationOptions,
Object... args) {
Objects.requireNonNull(invocationOptions, "invocationOptions");
return executeAsyncInternal(
activityName,
resultClass,
resultType,
invocationOptions.getActivityId(),
resolveOptions(options, invocationOptions),
args);
}

private <R> Promise<R> executeAsyncInternal(
String activityName,
Class<R> resultClass,
Type resultType,
String activityId,
ActivityOptions options,
Object... args) {
this.assertReadOnly.apply();
return activityExecutor
.executeActivity(
new WorkflowOutboundCallsInterceptor.ActivityInput<>(
activityName, resultClass, resultType, args, options, Header.empty()))
activityName, activityId, resultClass, resultType, args, options, Header.empty()))
.getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import io.temporal.activity.LocalActivityOptions;
import io.temporal.common.MethodRetry;
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
import io.temporal.workflow.ActivityInvocationOptions;
import io.temporal.workflow.ActivityStub;
import io.temporal.workflow.Functions;
import io.temporal.workflow.Promise;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
Expand Down Expand Up @@ -47,17 +49,31 @@ private LocalActivityInvocationHandler(
@Override
public Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, String activityName) {
Function<Object[], Object> function;
LocalActivityOptions mergedOptions =
LocalActivityOptions.newBuilder(options)
.mergeActivityOptions(activityMethodOptions.get(activityName))
.setMethodRetry(methodRetry)
.build();
ActivityStub stub =
LocalActivityStubImpl.newInstance(mergedOptions, activityExecutor, assertReadOnly);
function =
(a) -> stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
return function;

if (ActivityInvocationInternal.isActive()) {
ActivityInvocationOptions invocationOptions = ActivityInvocationInternal.consumeOptions();
return (a) -> {
Promise<?> result =
stub.executeAsync(
activityName,
method.getReturnType(),
method.getGenericReturnType(),
invocationOptions,
a);
ActivityInvocationInternal.setResult(result);
return null;
};
}

return (a) ->
stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
}

@Override
Expand Down
Loading
Loading