diff --git a/CHANGELOG.md b/CHANGELOG.md index 9153295fe1..b1a84aaf47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,14 @@ to docs, or any other relevant information. `upsertTypedSearchAttributes(UpsertSearchAttributesOptions, ...)`, and `upsertMemo(Map, UpsertMemoOptions)` methods. Implementations that don't extend `WorkflowOutboundCallsInterceptorBase` need to add them, and Base subclasses override them to see calls with options. +- **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now + accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed + input. `TemporalOperationStartContext` and `TemporalOperationCancelContext` have been removed. + Use `Nexus.getOperationContext().getInfo()` for service, operation, a read-only handler + invocation cancellation token, and the start request ID (`getRequestId()`, which returns `null` + in cancel handlers). + Request headers remain available to interceptors. Callbacks and Nexus links are no longer exposed + to Temporal operation handlers. ### Added - Experimental Event Groups associate related workflow commands so their history events can be diff --git a/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt b/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt index a009138538..fee95f2cf8 100644 --- a/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt +++ b/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt @@ -12,7 +12,6 @@ import io.temporal.common.converter.KotlinObjectMapperFactory import io.temporal.nexus.TemporalNexusClient import io.temporal.nexus.TemporalOperation import io.temporal.nexus.TemporalOperationResult -import io.temporal.nexus.TemporalOperationStartContext import io.temporal.testing.internal.SDKTestWorkflowRule import org.junit.Assert.assertEquals import org.junit.Rule @@ -43,7 +42,6 @@ class KotlinTemporalOperationTest { class KotlinSugarServiceImpl { @TemporalOperation fun greet( - ctx: TemporalOperationStartContext, client: TemporalNexusClient, input: String ): TemporalOperationResult { diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java index 279628b04f..6f853c4213 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java @@ -1,16 +1,34 @@ package io.temporal.internal.nexus; +import io.temporal.common.CancellationToken; import io.temporal.nexus.NexusOperationInfo; +import java.util.concurrent.CancellationException; +import javax.annotation.Nullable; class NexusInfoImpl implements NexusOperationInfo { private final String namespace; private final String taskQueue; private final String endpoint; + private final String service; + private final String operation; + private final @Nullable String requestId; + private final CancellationToken cancellationToken; - NexusInfoImpl(String namespace, String taskQueue, String endpoint) { + NexusInfoImpl( + String namespace, + String taskQueue, + String endpoint, + String service, + String operation, + @Nullable String requestId, + CancellationToken cancellationToken) { this.namespace = namespace; this.taskQueue = taskQueue; this.endpoint = endpoint; + this.service = service; + this.operation = operation; + this.requestId = requestId; + this.cancellationToken = cancellationToken; } @Override @@ -27,4 +45,24 @@ public String getTaskQueue() { public String getEndpoint() { return endpoint; } + + @Override + public String getService() { + return service; + } + + @Override + public String getOperation() { + return operation; + } + + @Override + public @Nullable String getRequestId() { + return requestId; + } + + @Override + public CancellationToken getCancellationToken() { + return cancellationToken; + } } diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index be76b8a91a..055d713d32 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -1,9 +1,12 @@ package io.temporal.internal.nexus; +import com.google.common.base.Strings; import io.nexusrpc.OperationException; import io.nexusrpc.handler.*; import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptor; import io.temporal.common.interceptors.WorkerInterceptor; +import io.temporal.internal.concurrent.structured.CancelSource; +import java.util.concurrent.CancellationException; public class TemporalInterceptorMiddleware implements OperationMiddleware { private final WorkerInterceptor[] interceptors; @@ -24,6 +27,9 @@ public OperationHandler intercept( } InternalNexusOperationContext temporalNexusContext = CurrentNexusOperationContext.get(); + CancelSource cancellationSource = + new CancelSource<>(() -> new CancellationException(context.getMethodCancellationReason())); + context.addMethodCancellationListener(cancellationSource::cancel); inboundCallsInterceptor.init( new RootNexusOperationOutboundCallsInterceptor( temporalNexusContext.getMetricsScope(), @@ -31,7 +37,11 @@ public OperationHandler intercept( new NexusInfoImpl( temporalNexusContext.getNamespace(), temporalNexusContext.getTaskQueue(), - temporalNexusContext.getEndpoint()))); + temporalNexusContext.getEndpoint(), + context.getService(), + context.getOperation(), + Strings.emptyToNull(temporalNexusContext.getRequestId()), + cancellationSource.token()))); return new OperationInterceptorConverter(inboundCallsInterceptor); } diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java index 4edfa72083..8f13a67a37 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java @@ -13,7 +13,6 @@ import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; @@ -62,7 +61,7 @@ private static final class TemporalOperationExtension implements MethodExtension } TemporalOperationHandler.StartHandler startHandler = - (ctx, client, input) -> invokeStartHandler(handle, ctx, client, input); + (client, input) -> invokeStartHandler(handle, client, input); return new TemporalOperationHandler(startHandler) {}; } @@ -78,13 +77,11 @@ private static void validateSignature(Method method) { "@TemporalOperation method " + method.getName() + " must not be static"); } Class[] paramTypes = method.getParameterTypes(); - if (paramTypes.length != 3 - || !TemporalOperationStartContext.class.equals(paramTypes[0]) - || !TemporalNexusClient.class.equals(paramTypes[1])) { + if (paramTypes.length != 2 || !TemporalNexusClient.class.equals(paramTypes[0])) { throw new IllegalArgumentException( "@TemporalOperation method " + method.getName() - + " must accept (TemporalOperationStartContext, TemporalNexusClient, I); got " + + " must accept (TemporalNexusClient, I); got " + describeSignature(method)); } if (!TemporalOperationResult.class.equals(method.getReturnType())) { @@ -112,7 +109,7 @@ private static void validateSignature(Method method) { private static void validateTypes(Method method, OperationDefinition operationDefinition) { Type expectedInputType = operationDefinition.getInputType(); - Type declaredInputType = method.getGenericParameterTypes()[2]; + Type declaredInputType = method.getGenericParameterTypes()[1]; if (!typesMatch(declaredInputType, expectedInputType)) { throw new IllegalArgumentException( "@TemporalOperation method " @@ -144,13 +141,9 @@ private static void validateTypes(Method method, OperationDefinition operationDe // Package-private for testing. @SuppressWarnings("unchecked") static TemporalOperationResult invokeStartHandler( - MethodHandle handle, - TemporalOperationStartContext ctx, - TemporalNexusClient client, - Object input) - throws OperationException { + MethodHandle handle, TemporalNexusClient client, Object input) throws OperationException { try { - return (TemporalOperationResult) handle.invoke(ctx, client, input); + return (TemporalOperationResult) handle.invoke(client, input); } catch (RuntimeException | Error | OperationException e) { // HandlerException is a RuntimeException and falls into the RuntimeException arm. throw e; diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index a3ee0e9a76..34e3a5a098 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -1,8 +1,12 @@ package io.temporal.nexus; +import io.temporal.common.CancellationToken; +import java.util.concurrent.CancellationException; +import javax.annotation.Nullable; + /** - * Temporal information about the Nexus Operation. Use {@link NexusOperationContext#getInfo()} from - * a Nexus Operation implementation to access. + * Temporal information about the Nexus Operation. Use {@link NexusOperationContext#getInfo() + * Nexus.getOperationContext().getInfo()} from a Nexus Operation implementation to access. */ public interface NexusOperationInfo { /** @@ -20,4 +24,37 @@ public interface NexusOperationInfo { * Supported from server v1.30.0. */ String getEndpoint(); + + /** + * @return Service name for the current Nexus request. + */ + String getService(); + + /** + * @return Operation name for the current Nexus request. + */ + String getOperation(); + + /** + * Returns the ID of the current Nexus start request, or {@code null} when no ID is available (for + * example, in a cancel handler). + * + *

Retries of the same start request carry the same ID. Treat the ID as an opaque string. + */ + @Nullable + default String getRequestId() { + return null; + } + + /** + * Returns a read-only token for observing cancellation of the current handler invocation. This is + * separate from a request to cancel the Nexus Operation itself. + * + *

When cancellation is requested, {@link CancellationToken#throwIfCancellationRequested()} + * throws a {@link CancellationException} whose message contains the invocation cancellation + * reason. + * + *

Cancellation callbacks may run on the Nexus timeout thread and must return promptly. + */ + CancellationToken getCancellationToken(); } diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java index bd26be9309..37d7bf7c53 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java @@ -15,14 +15,16 @@ * Temporal from within a Nexus operation handler. * *

Passed to {@link TemporalOperation}-annotated methods (and {@link - * TemporalOperationHandler.StartHandler} implementations) alongside the start context and input. + * TemporalOperationHandler.StartHandler} implementations) alongside the operation input. Access + * information about the current invocation through {@link NexusOperationContext#getInfo() + * Nexus.getOperationContext().getInfo()}. * *

Example usage to start a workflow from an operation: * *

{@code
  * @TemporalOperation
  * public TemporalOperationResult startTransfer(
- *     TemporalOperationStartContext ctx, TemporalNexusClient client, TransferInput input) {
+ *     TemporalNexusClient client, TransferInput input) {
  *   return client.startWorkflow(
  *       TransferWorkflow.class,
  *       TransferWorkflow::transfer,
@@ -39,7 +41,7 @@
  * 
{@code
  * @TemporalOperation
  * public TemporalOperationResult cancelOrder(
- *     TemporalOperationStartContext ctx, TemporalNexusClient client, CancelOrderInput input) {
+ *     TemporalNexusClient client, CancelOrderInput input) {
  *   client.getWorkflowClient()
  *       .newUntypedWorkflowStub("order-" + input.getOrderId())
  *       .signal("requestCancellation", input);
diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java
index a6fa9f5199..70ffa34571 100644
--- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java
+++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java
@@ -18,8 +18,7 @@
  *
  * 
    *
  • be {@code public}, - *
  • accept exactly three parameters: {@link TemporalOperationStartContext}, {@link - * TemporalNexusClient}, and the operation input type, + *
  • accept exactly two parameters: {@link TemporalNexusClient} and the operation input type, *
  • return {@link TemporalOperationResult}. *
* @@ -30,7 +29,7 @@ * public class TransferServiceImpl { * @TemporalOperation * public TemporalOperationResult transfer( - * TemporalOperationStartContext ctx, TemporalNexusClient client, TransferInput input) { + * TemporalNexusClient client, TransferInput input) { * return client.startWorkflow( * TransferWorkflow.class, * TransferWorkflow::transfer, @@ -42,6 +41,10 @@ * } * }
* + *

Access information about the current invocation through {@link NexusOperationContext#getInfo() + * Nexus.getOperationContext().getInfo()}, including its cancellation token and {@linkplain + * NexusOperationInfo#getRequestId() start request ID}. + * *

For custom cancel, or any other handler composition, use {@link OperationImpl} with a {@link * TemporalOperationHandler} subclass that overrides {@link * TemporalOperationHandler#cancelWorkflowRun}. Both annotations can coexist on the same {@link diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java deleted file mode 100644 index 963dc2981b..0000000000 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java +++ /dev/null @@ -1,88 +0,0 @@ -package io.temporal.nexus; - -import io.nexusrpc.handler.OperationCancelDetails; -import io.nexusrpc.handler.OperationContext; -import io.nexusrpc.handler.OperationMethodCancellationListener; -import io.temporal.common.Experimental; -import java.time.Instant; -import java.util.Map; -import java.util.Objects; -import org.jspecify.annotations.Nullable; - -/** - * Context for a Nexus cancel operation request, passed to {@link - * TemporalOperationHandler#cancelWorkflowRun}. - */ -@Experimental -public final class TemporalOperationCancelContext { - - private final OperationContext operationContext; - private final OperationCancelDetails operationCancelDetails; - - TemporalOperationCancelContext( - OperationContext operationContext, OperationCancelDetails operationCancelDetails) { - this.operationContext = Objects.requireNonNull(operationContext); - this.operationCancelDetails = Objects.requireNonNull(operationCancelDetails); - } - - /** Returns the service name for this operation. */ - public String getService() { - return operationContext.getService(); - } - - /** Returns the operation name. */ - public String getOperation() { - return operationContext.getOperation(); - } - - /** Returns the headers for this cancel request. The returned map is case-insensitive. */ - public Map getHeaders() { - return operationContext.getHeaders(); - } - - /** - * Returns the deadline for the operation handler method. This is the time by which the method - * should complete. This is not the operation's deadline. - */ - public @Nullable Instant getDeadline() { - return operationContext.getDeadline(); - } - - /** Returns the operation token identifying the operation to cancel. */ - public String getOperationToken() { - return operationCancelDetails.getOperationToken(); - } - - /** - * True if the handler method has been cancelled. Note, this is method cancellation, unrelated to - * operation cancellation. - */ - public boolean isMethodCancelled() { - return operationContext.isMethodCancelled(); - } - - /** - * Reason the handler method was cancelled, or null if not cancelled. Note, this is method - * cancellation, unrelated to operation cancellation. - */ - public @Nullable String getMethodCancellationReason() { - return operationContext.getMethodCancellationReason(); - } - - /** - * Add a listener for method cancellation. The listener is invoked immediately before this method - * returns if the method is already cancelled. The listener must not block and must not be - * registered from within another cancellation listener. - */ - public void addMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.addMethodCancellationListener(listener); - } - - /** - * Remove a method cancellation listener, if present. Must not be called from within another - * cancellation listener. - */ - public void removeMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.removeMethodCancellationListener(listener); - } -} diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java index 3ea9bf6139..4f99468216 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java @@ -21,7 +21,8 @@ * customize cancel behavior. Override {@link #cancelWorkflowRun}, {@link #cancelUpdateWorkflow}, or * {@link #cancelActivityExecution} to change how the corresponding cancellation is handled. The * {@link #start} and {@link #cancel} methods should not be overridden — they contain the core - * dispatch logic. + * dispatch logic. Operation information is available to both start and cancel handlers through + * {@link NexusOperationContext#getInfo() Nexus.getOperationContext().getInfo()}. * *

Custom-cancel example: * @@ -29,7 +30,7 @@ * @OperationImpl * public OperationHandler startTransfer() { * return new TemporalOperationHandler( - * (context, client, input) -> + * (client, input) -> * client.startWorkflow( * TransferWorkflow.class, * TransferWorkflow::transfer, @@ -38,8 +39,7 @@ * .setWorkflowId("transfer-" + input.getTransferId()) * .build())) { * @Override - * protected void cancelWorkflowRun( - * TemporalOperationCancelContext ctx, CancelWorkflowRunInput input) { + * protected void cancelWorkflowRun(CancelWorkflowRunInput input) { * // custom logic * } * }; @@ -60,8 +60,7 @@ public class TemporalOperationHandler implements OperationHandler { */ @FunctionalInterface public interface StartHandler { - TemporalOperationResult apply( - TemporalOperationStartContext context, TemporalNexusClient client, T input) + TemporalOperationResult apply(TemporalNexusClient client, T input) throws OperationException, HandlerException; } @@ -92,8 +91,7 @@ public final OperationStartResult start( TemporalNexusClient client = new TemporalNexusClientImpl(nexusCtx.getWorkflowClient(), ctx, details); - TemporalOperationStartContext startContext = new TemporalOperationStartContext(ctx, details); - TemporalOperationResult result = startHandler.apply(startContext, client, input); + TemporalOperationResult result = startHandler.apply(client, input); if (result.isSync()) { return OperationStartResult.newSyncBuilder(result.getSyncResult()).build(); @@ -116,20 +114,17 @@ public final void cancel(OperationContext ctx, OperationCancelDetails details) { HandlerException.ErrorType.BAD_REQUEST, "failed to parse operation token", e); } - TemporalOperationCancelContext cancelContext = new TemporalOperationCancelContext(ctx, details); switch (token.getType()) { case WORKFLOW_RUN: - cancelWorkflowRun(cancelContext, new CancelWorkflowRunInput(token.getWorkflowId())); + cancelWorkflowRun(new CancelWorkflowRunInput(token.getWorkflowId())); break; case WORKFLOW_UPDATE: cancelUpdateWorkflow( - cancelContext, new CancelUpdateWorkflowInput( token.getWorkflowId(), token.getRunId(), token.getUpdateId())); break; case ACTIVITY_EXECUTION: cancelActivityExecution( - cancelContext, new CancelActivityExecutionInput(token.getActivityId(), token.getRunId())); break; default: @@ -145,11 +140,9 @@ public final void cancel(OperationContext ctx, OperationCancelDetails details) { * *

Default behavior: cancels the underlying workflow. * - * @param context the cancel context * @param input describes the workflow run to cancel */ - protected void cancelWorkflowRun( - TemporalOperationCancelContext context, CancelWorkflowRunInput input) { + protected void cancelWorkflowRun(CancelWorkflowRunInput input) { WorkflowClient client = CurrentNexusOperationContext.get().getWorkflowClient(); client.newUntypedWorkflowStub(input.getWorkflowId()).cancel(); } @@ -161,11 +154,9 @@ protected void cancelWorkflowRun( *

Default behavior: not implemented. There is no server primitive to cancel an in-flight * workflow update. * - * @param context the cancel context * @param input describes the update to cancel */ - protected void cancelUpdateWorkflow( - TemporalOperationCancelContext context, CancelUpdateWorkflowInput input) { + protected void cancelUpdateWorkflow(CancelUpdateWorkflowInput input) { throw new HandlerException( HandlerException.ErrorType.NOT_IMPLEMENTED, new UnsupportedOperationException("cannot cancel an UpdateWorkflow operation")); @@ -177,11 +168,9 @@ protected void cancelUpdateWorkflow( * *

Default behavior: requests cancellation of the underlying standalone activity execution. * - * @param context the cancel context * @param input describes the activity execution to cancel */ - protected void cancelActivityExecution( - TemporalOperationCancelContext context, CancelActivityExecutionInput input) { + protected void cancelActivityExecution(CancelActivityExecutionInput input) { WorkflowClient wc = CurrentNexusOperationContext.get().getWorkflowClient(); ActivityClient ac = ActivityClient.newInstance( diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java deleted file mode 100644 index 493ccd5071..0000000000 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java +++ /dev/null @@ -1,117 +0,0 @@ -package io.temporal.nexus; - -import io.nexusrpc.Link; -import io.nexusrpc.handler.OperationContext; -import io.nexusrpc.handler.OperationMethodCancellationListener; -import io.nexusrpc.handler.OperationStartDetails; -import io.temporal.common.Experimental; -import java.time.Instant; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import org.jspecify.annotations.Nullable; - -/** - * Context for a Nexus start operation request, passed to {@link - * TemporalOperationHandler.StartHandler} alongside the {@link TemporalNexusClient} and input. - */ -@Experimental -public final class TemporalOperationStartContext { - - private final OperationContext operationContext; - private final OperationStartDetails operationStartDetails; - - TemporalOperationStartContext( - OperationContext operationContext, OperationStartDetails operationStartDetails) { - this.operationContext = Objects.requireNonNull(operationContext); - this.operationStartDetails = Objects.requireNonNull(operationStartDetails); - } - - /** Returns the service name for this operation. */ - public String getService() { - return operationContext.getService(); - } - - /** Returns the operation name. */ - public String getOperation() { - return operationContext.getOperation(); - } - - /** Returns the headers for this operation request. The returned map is case-insensitive. */ - public Map getHeaders() { - return operationContext.getHeaders(); - } - - /** - * Returns the deadline for the operation handler method. This is the time by which the method - * should complete. This is not the operation's deadline. - */ - public @Nullable Instant getDeadline() { - return operationContext.getDeadline(); - } - - /** Returns the request ID for this operation. */ - public String getRequestId() { - return operationStartDetails.getRequestId(); - } - - /** - * Optional callback URL for asynchronous operations to deliver results to. If present and the - * implementation is asynchronous, the implementation should ensure this callback is invoked with - * the result upon completion. - */ - public @Nullable String getCallbackUrl() { - return operationStartDetails.getCallbackUrl(); - } - - /** Headers to use on the callback if {@link #getCallbackUrl} is used. */ - public Map getCallbackHeaders() { - return operationStartDetails.getCallbackHeaders(); - } - - /** Links sent by the caller. Handlers may use these as metadata on associated resources. */ - public List getLinks() { - return operationStartDetails.getLinks(); - } - - /** - * True if the handler method has been cancelled. Note, this is method cancellation, unrelated to - * operation cancellation. - */ - public boolean isMethodCancelled() { - return operationContext.isMethodCancelled(); - } - - /** - * Reason the handler method was cancelled, or null if not cancelled. Note, this is method - * cancellation, unrelated to operation cancellation. - */ - public @Nullable String getMethodCancellationReason() { - return operationContext.getMethodCancellationReason(); - } - - /** - * Add a listener for method cancellation. The listener is invoked immediately before this method - * returns if the method is already cancelled. The listener must not block and must not be - * registered from within another cancellation listener. - */ - public void addMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.addMethodCancellationListener(listener); - } - - /** - * Remove a method cancellation listener, if present. Must not be called from within another - * cancellation listener. - */ - public void removeMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.removeMethodCancellationListener(listener); - } - - /** - * Associates links with the current operation to be propagated back to the caller. Links are only - * attached on successful responses. - */ - public void addResponseLinks(Link... links) { - operationContext.addLinks(links); - } -} diff --git a/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java b/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java index 8e32eadc1b..a8cb3a3754 100644 --- a/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java +++ b/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java @@ -390,7 +390,7 @@ public class ActivityNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, activityId) -> + (client, activityId) -> client.startActivity( LinkingActivity.class, LinkingActivity::execute, diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java index 141e213fb1..c026f35fbb 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java @@ -17,15 +17,24 @@ import io.temporal.api.nexus.v1.StartOperationResponse; import io.temporal.api.workflowservice.v1.PollNexusTaskQueueResponse; import io.temporal.client.WorkflowClient; +import io.temporal.common.CancellationToken; import io.temporal.common.converter.DataConverter; import io.temporal.common.converter.DefaultDataConverter; import io.temporal.common.interceptors.WorkerInterceptor; import io.temporal.common.reporter.TestStatsReporter; import io.temporal.internal.worker.NexusTask; import io.temporal.internal.worker.NexusTaskHandler; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.TemporalNexusClient; +import io.temporal.nexus.TemporalOperation; +import io.temporal.nexus.TemporalOperationResult; import io.temporal.workflow.shared.TestNexusServices; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import javax.annotation.Nullable; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -43,6 +52,13 @@ public void setUp() { metricsScope = new RootScopeBuilder().reporter(reporter).reportEvery(Duration.ofMillis(10)); } + @After + public void tearDown() { + // Timed-out tasks currently skip context cleanup. Keep the timeout tests isolated until that + // separate production issue is fixed. + CurrentNexusOperationContext.unset(); + } + @Test public void nexusTaskHandlerImplStartNoService() { WorkflowClient client = mock(WorkflowClient.class); @@ -105,6 +121,51 @@ public void startSyncTask() throws TimeoutException { String.class)); } + @Test + public void startRequestIdReachesContextFreeHandler() throws TimeoutException { + NexusTaskHandlerImpl handler = + new NexusTaskHandlerImpl( + mock(WorkflowClient.class), + NAMESPACE, + TASK_QUEUE, + dataConverter, + new WorkerInterceptor[] {}); + handler.registerNexusServiceImplementations(new Object[] {new RequestIdService()}); + handler.start(); + + Assert.assertEquals("opaque/id-1", requestIdSeenByHandler(handler, "opaque/id-1")); + Assert.assertEquals("opaque/id-2", requestIdSeenByHandler(handler, "opaque/id-2")); + Assert.assertNull(requestIdSeenByHandler(handler, "")); + } + + private String requestIdSeenByHandler(NexusTaskHandlerImpl handler, String requestId) + throws TimeoutException { + PollNexusTaskQueueResponse.Builder task = + PollNexusTaskQueueResponse.newBuilder() + .setRequest( + Request.newBuilder() + .setStartOperation( + StartOperationRequest.newBuilder() + .setService("TestNexusService1") + .setOperation("operation") + .setRequestId(requestId) + .setPayload(dataConverter.toPayload("input").get()))); + NexusTaskHandler.Result result = handler.handle(new NexusTask(task, null, null), metricsScope); + Assert.assertNull(result.getHandlerException()); + StartOperationResponse response = result.getResponse().getStartOperation(); + Assert.assertTrue("expected a successful start response", response.hasSyncSuccess()); + return dataConverter.fromPayload( + response.getSyncSuccess().getPayload(), String.class, String.class); + } + + @ServiceImpl(service = TestNexusServices.TestNexusService1.class) + public static class RequestIdService { + @TemporalOperation + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + return TemporalOperationResult.sync(Nexus.getOperationContext().getInfo().getRequestId()); + } + } + @Test public void nexusOperationContextExposesWorkerWorkflowClient() throws TimeoutException { WorkflowClient client = mock(WorkflowClient.class); @@ -159,6 +220,36 @@ public void syncTimeoutTask() { () -> nexusTaskHandlerImpl.handle(new NexusTask(task, null, null), metricsScope)); } + @Test + public void requestTimeoutReachesContextFreeHandlerCancellationToken() { + ContextFreeCancellationService service = new ContextFreeCancellationService(); + NexusTaskHandlerImpl handler = + new NexusTaskHandlerImpl( + mock(WorkflowClient.class), + NAMESPACE, + TASK_QUEUE, + dataConverter, + new WorkerInterceptor[] {}); + handler.registerNexusServiceImplementations(new Object[] {service}); + handler.start(); + PollNexusTaskQueueResponse.Builder task = + PollNexusTaskQueueResponse.newBuilder() + .setRequest( + Request.newBuilder() + .putHeader(Header.REQUEST_TIMEOUT, "100ms") + .setStartOperation( + StartOperationRequest.newBuilder() + .setService("TestNexusService1") + .setOperation("operation") + .setPayload(dataConverter.toPayload("input").get()))); + + Assert.assertThrows( + TimeoutException.class, + () -> handler.handle(new NexusTask(task, null, null), metricsScope)); + Assert.assertNotNull("handler should observe the request timeout", service.cancellation); + Assert.assertEquals("timeout", service.cancellation.getMessage()); + } + @Test public void startTaskWithUndeserializableInput() throws TimeoutException { WorkflowClient client = mock(WorkflowClient.class); @@ -343,6 +434,32 @@ public void failureResponseDropsCapturedResponseLinks() throws TimeoutException response.hasSyncSuccess() || response.hasAsyncSuccess()); } + @ServiceImpl(service = TestNexusServices.TestNexusService1.class) + public static class ContextFreeCancellationService { + private CancellationException cancellation; + + @TemporalOperation + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + CancellationToken token = + Nexus.getOperationContext().getInfo().getCancellationToken(); + CountDownLatch cancelled = new CountDownLatch(1); + try (CancellationToken.Registration ignored = token.onCancel(cancelled::countDown)) { + if (!cancelled.await(5, TimeUnit.SECONDS)) { + throw new IllegalStateException("Handler did not observe invocation cancellation"); + } + try { + token.throwIfCancellationRequested(); + } catch (CancellationException e) { + cancellation = e; + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw Nexus.wrap(e); + } + return TemporalOperationResult.sync(input); + } + } + /** * Handler that simulates what a real Nexus operation would do after issuing a signal: stash a * response link on the operation context, then return an async result. Lets us exercise the diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java new file mode 100644 index 0000000000..82032d46c8 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -0,0 +1,117 @@ +package io.temporal.internal.nexus; + +import static org.mockito.Mockito.mock; + +import io.nexusrpc.OperationException; +import io.nexusrpc.handler.OperationContext; +import io.nexusrpc.handler.OperationHandler; +import io.nexusrpc.handler.OperationMethodCanceller; +import io.nexusrpc.handler.OperationStartDetails; +import io.temporal.client.WorkflowClient; +import io.temporal.common.CancellationToken; +import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptor; +import io.temporal.common.interceptors.WorkerInterceptor; +import io.temporal.common.interceptors.WorkerInterceptorBase; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.TemporalOperationHandler; +import io.temporal.nexus.TemporalOperationResult; +import java.util.concurrent.CancellationException; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TemporalInterceptorMiddlewareTest { + + @Before + public void setUp() { + CurrentNexusOperationContext.set( + new InternalNexusOperationContext( + "namespace", "taskQueue", "endpoint", null, mock(WorkflowClient.class))); + } + + @After + public void tearDown() { + CurrentNexusOperationContext.unset(); + } + + @Test + public void headersRemainAccessibleToInterceptorWithContextFreeHandler() + throws OperationException { + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .putHeader("test-header", "test-value") + .build(); + AtomicReference interceptedHeader = new AtomicReference<>(); + WorkerInterceptor interceptor = + new WorkerInterceptorBase() { + @Override + public NexusOperationInboundCallsInterceptor interceptNexusOperation( + OperationContext context, NexusOperationInboundCallsInterceptor next) { + interceptedHeader.set(context.getHeaders().get("test-header")); + return next; + } + }; + OperationHandler handler = + new TemporalInterceptorMiddleware(new WorkerInterceptor[] {interceptor}) + .intercept( + operationContext, + TemporalOperationHandler.create( + (client, input) -> TemporalOperationResult.sync(input))); + + Object result = + handler + .start( + operationContext, + OperationStartDetails.newBuilder().setRequestId("request-id").build(), + "input") + .getSyncResult(); + Assert.assertEquals("input", result); + Assert.assertEquals( + "worker interceptor should see the request header", "test-value", interceptedHeader.get()); + } + + @Test + public void methodCancellationReachesNexusOperationInfo() { + OperationMethodCanceller canceller = new OperationMethodCanceller(); + CancellationToken token = interceptAndGetCancellationToken(canceller); + Assert.assertFalse(token.isCancellationRequested()); + + canceller.cancel("test reason"); + + Assert.assertTrue(token.isCancellationRequested()); + CancellationException exception = + Assert.assertThrows(CancellationException.class, token::throwIfCancellationRequested); + Assert.assertEquals("test reason", exception.getMessage()); + } + + @Test + public void cancellationBeforeInterceptionReachesNexusOperationInfo() { + OperationMethodCanceller canceller = new OperationMethodCanceller(); + canceller.cancel("early reason"); + CancellationToken token = interceptAndGetCancellationToken(canceller); + + Assert.assertTrue(token.isCancellationRequested()); + CancellationException exception = + Assert.assertThrows(CancellationException.class, token::throwIfCancellationRequested); + Assert.assertEquals("early reason", exception.getMessage()); + } + + private static CancellationToken interceptAndGetCancellationToken( + OperationMethodCanceller canceller) { + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .setMethodCanceller(canceller) + .build(); + TemporalInterceptorMiddleware middleware = + new TemporalInterceptorMiddleware(new WorkerInterceptor[0]); + middleware.intercept( + operationContext, OperationHandler.sync((context, details, input) -> input)); + return Nexus.getOperationContext().getInfo().getCancellationToken(); + } +} diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java index 8900000b92..e2eb07c84d 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java @@ -9,7 +9,6 @@ import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import org.junit.Assert; import org.junit.Test; @@ -95,12 +94,11 @@ public void invokeStartHandler_readsInstanceField() throws Exception { // read constructor-injected dependencies (workflow client, config, etc.) off `this`. StatefulHandler instance = new StatefulHandler("injected"); java.lang.reflect.Method m = - StatefulHandler.class.getMethod( - "op", TemporalOperationStartContext.class, TemporalNexusClient.class, String.class); + StatefulHandler.class.getMethod("op", TemporalNexusClient.class, String.class); java.lang.invoke.MethodHandle handle = java.lang.invoke.MethodHandles.lookup().unreflect(m).bindTo(instance); TemporalOperationResult result = - TemporalOperationProcessor.invokeStartHandler(handle, null, null, "input"); + TemporalOperationProcessor.invokeStartHandler(handle, null, "input"); Assert.assertEquals("injected:input", result.getSyncResult()); } @@ -111,14 +109,13 @@ public void invokeStartHandler_propagatesRuntimeExceptionUnwrapped() throws Exce // dispatch path. ThrowingHandler instance = new ThrowingHandler(); java.lang.reflect.Method m = - ThrowingHandler.class.getMethod( - "op", TemporalOperationStartContext.class, TemporalNexusClient.class, String.class); + ThrowingHandler.class.getMethod("op", TemporalNexusClient.class, String.class); java.lang.invoke.MethodHandle handle = java.lang.invoke.MethodHandles.lookup().unreflect(m).bindTo(instance); IllegalStateException thrown = Assert.assertThrows( IllegalStateException.class, - () -> TemporalOperationProcessor.invokeStartHandler(handle, null, null, "in")); + () -> TemporalOperationProcessor.invokeStartHandler(handle, null, "in")); Assert.assertEquals("user-thrown", thrown.getMessage()); // First user frame is at the top — no reflective wrapper class in between. Assert.assertEquals(ThrowingHandler.class.getName(), thrown.getStackTrace()[0].getClassName()); @@ -137,9 +134,7 @@ public void rejects_badArity() { IllegalArgumentException e = assertInvalid(() -> TemporalOperationProcessor.process(new BadArity())); Assert.assertTrue( - e.getMessage(), - e.getMessage() - .contains("must accept (TemporalOperationStartContext, TemporalNexusClient, I)")); + e.getMessage(), e.getMessage().contains("must accept (TemporalNexusClient, I)")); } @Test @@ -183,8 +178,7 @@ private static IllegalArgumentException assertInvalid(Runnable action) { @ServiceImpl(service = SingleOpService.class) public static class ValidSugar { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -192,7 +186,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class BadReturnType { @TemporalOperation - public String op(TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public String op(TemporalNexusClient client, String input) { return input; } } @@ -200,7 +194,7 @@ public String op(TemporalOperationStartContext ctx, TemporalNexusClient client, @ServiceImpl(service = SingleOpService.class) public static class BadArity { @TemporalOperation - public TemporalOperationResult op(TemporalNexusClient client, String input) { + public TemporalOperationResult op(String input) { return TemporalOperationResult.sync(input); } } @@ -209,9 +203,7 @@ public TemporalOperationResult op(TemporalNexusClient client, String inp public static class CompositeOk { @TemporalOperation public TemporalOperationResult> compose( - TemporalOperationStartContext ctx, - TemporalNexusClient client, - java.util.Map input) { + TemporalNexusClient client, java.util.Map input) { return TemporalOperationResult.sync(java.util.Collections.emptyList()); } } @@ -221,9 +213,7 @@ public static class CompositeBadInput { // Map value type is String instead of Integer. @TemporalOperation public TemporalOperationResult> compose( - TemporalOperationStartContext ctx, - TemporalNexusClient client, - java.util.Map input) { + TemporalNexusClient client, java.util.Map input) { return TemporalOperationResult.sync(java.util.Collections.emptyList()); } } @@ -233,9 +223,7 @@ public static class CompositeBadOutput { // Result list element type is Integer instead of String. @TemporalOperation public TemporalOperationResult> compose( - TemporalOperationStartContext ctx, - TemporalNexusClient client, - java.util.Map input) { + TemporalNexusClient client, java.util.Map input) { return TemporalOperationResult.sync(java.util.Collections.emptyList()); } } @@ -244,8 +232,7 @@ public TemporalOperationResult> compose( @ServiceImpl(service = SingleOpService.class) public static class RawReturnType { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -253,8 +240,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class NonPublicMethod { @TemporalOperation - TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -262,8 +248,7 @@ TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class StaticMethod { @TemporalOperation - public static TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public static TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -277,8 +262,7 @@ public StatefulHandler(String prefix) { } @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(prefix + ":" + input); } } @@ -286,8 +270,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class ThrowingHandler { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { throw new IllegalStateException("user-thrown"); } } @@ -295,8 +278,7 @@ public TemporalOperationResult op( @ServiceImpl(service = VoidIoService.class) public static class VoidIo { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, Void input) { + public TemporalOperationResult op(TemporalNexusClient client, Void input) { return TemporalOperationResult.sync(null); } } @@ -304,8 +286,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class DeclaresCheckedException { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) + public TemporalOperationResult op(TemporalNexusClient client, String input) throws java.io.IOException { return TemporalOperationResult.sync(input); } @@ -314,8 +295,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class DeclaresRuntimeException { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) + public TemporalOperationResult op(TemporalNexusClient client, String input) throws IllegalStateException { return TemporalOperationResult.sync(input); } @@ -324,8 +304,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class DeclaresNexusrpcCheckedExceptions { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) + public TemporalOperationResult op(TemporalNexusClient client, String input) throws io.nexusrpc.OperationException, io.nexusrpc.handler.HandlerException { return TemporalOperationResult.sync(input); } @@ -337,7 +316,7 @@ public static class DualAnnotated { @OperationImpl public OperationHandler op() { return new TemporalOperationHandler( - (ctx, client, input) -> TemporalOperationResult.sync(input)) {}; + (client, input) -> TemporalOperationResult.sync(input)) {}; } } } diff --git a/temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java b/temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java new file mode 100644 index 0000000000..bacb60ccee --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java @@ -0,0 +1,35 @@ +package io.temporal.nexus; + +import io.nexusrpc.handler.OperationCancelDetails; +import io.nexusrpc.handler.OperationContext; +import io.temporal.internal.nexus.OperationTokenUtil; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.Assert; +import org.junit.Test; + +public class TemporalOperationHandlerTest { + + @Test + public void cancelDispatchesTypedInputToContextFreeOverride() throws Exception { + AtomicReference receivedInput = new AtomicReference<>(); + TemporalOperationHandler handler = + new TemporalOperationHandler( + (client, input) -> TemporalOperationResult.sync(input)) { + @Override + protected void cancelActivityExecution(CancelActivityExecutionInput input) { + receivedInput.set(input); + } + }; + String operationToken = + OperationTokenUtil.generateActivityExecutionOperationToken( + "activity-id", "run-id", "namespace"); + + handler.cancel( + OperationContext.newBuilder().setService("service").setOperation("operation").build(), + OperationCancelDetails.newBuilder().setOperationToken(operationToken).build()); + + Assert.assertNotNull(receivedInput.get()); + Assert.assertEquals("activity-id", receivedInput.get().getActivityId()); + Assert.assertEquals("run-id", receivedInput.get().getRunId()); + } +} diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java index 51cb14b703..ab96a42b51 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java @@ -130,7 +130,7 @@ public class TestNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, activityId) -> { + (client, activityId) -> { nexusInvocationCount.incrementAndGet(); return client.startActivity( BlockingActivity.class, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java index 5e01ec9ea2..45c0a94f5c 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java @@ -157,7 +157,7 @@ public class TestNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, activityId) -> + (client, activityId) -> client.startActivity( BlockingActivity.class, BlockingActivity::execute, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index 776b36c3ec..932b3d0a38 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -11,6 +11,7 @@ import io.temporal.internal.nexus.OperationToken; import io.temporal.internal.nexus.OperationTokenType; import io.temporal.internal.nexus.OperationTokenUtil; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -93,13 +94,13 @@ public class TestNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( TestActivity.class, TestActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + context.getRequestId()) + .setId("act-" + Nexus.getOperationContext().getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index d15ec85d4c..12bdb813bc 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -16,7 +16,8 @@ import io.temporal.failure.CanceledFailure; import io.temporal.failure.NexusOperationFailure; import io.temporal.nexus.CancelActivityExecutionInput; -import io.temporal.nexus.TemporalOperationCancelContext; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -24,6 +25,7 @@ import io.temporal.workflow.*; import java.time.Duration; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -35,7 +37,7 @@ public class CancelActivityAsyncOperationTest extends BaseNexusTest { static final AtomicBoolean cancelled = new AtomicBoolean(false); - static final AtomicBoolean customCancelInvoked = new AtomicBoolean(false); + static final AtomicReference customCancelInfo = new AtomicReference<>(); @Rule public SDKTestWorkflowRule testWorkflowRule = @@ -54,7 +56,7 @@ protected SDKTestWorkflowRule getTestWorkflowRule() { @Before public void resetState() { cancelled.set(false); - customCancelInvoked.set(false); + customCancelInfo.set(null); } @Test(timeout = 60_000) @@ -92,7 +94,9 @@ public void testOverriddenActivityCancel() { // Acceptable. } // The overriding handler should have been called. - Assert.assertTrue("override should have been invoked", customCancelInvoked.get()); + NexusOperationInfo info = customCancelInfo.get(); + Assert.assertNotNull("override should have been invoked", info); + Assert.assertNull("cancel handler should have no start request ID", info.getRequestId()); // Default cancel was not invoked: activity ran until startToCloseTimeout fired // (no ActivityCompletionException raised on the worker). Assert.assertFalse("default cancel should not have been invoked", cancelled.get()); @@ -189,13 +193,13 @@ public class DefaultCancelNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( HeartbeatingActivity.class, HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + context.getRequestId()) + .setId("act-" + Nexus.getOperationContext().getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -243,21 +247,20 @@ public class OverriddenCancelNexusServiceImpl { @OperationImpl public OperationHandler operation() { return new TemporalOperationHandler( - (context, client, input) -> + (client, input) -> client.startActivity( HeartbeatingActivity.class, HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-override-" + context.getRequestId()) + .setId("act-override-" + Nexus.getOperationContext().getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) .build())) { @Override - protected void cancelActivityExecution( - TemporalOperationCancelContext ctx, CancelActivityExecutionInput input) { - customCancelInvoked.set(true); + protected void cancelActivityExecution(CancelActivityExecutionInput input) { + customCancelInfo.set(Nexus.getOperationContext().getInfo()); // Intentionally do NOT invoke default cancel; the activity will self-terminate via // startToCloseTimeout. } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java index 50d0223416..fc039b6abc 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java @@ -9,10 +9,11 @@ import io.temporal.client.WorkflowStub; import io.temporal.failure.CanceledFailure; import io.temporal.internal.Signal; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -128,14 +129,14 @@ public interface TestNexusCancelService { @ServiceImpl(service = TestNexusCancelService.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); return client.startWorkflow( WaitForCancelWorkflowInterface.class, WaitForCancelWorkflowInterface::execute, input, WorkflowOptions.newBuilder() - .setWorkflowId("generic-cancel-test-" + context.getService()) + .setWorkflowId("generic-cancel-test-" + info.getService()) .build()); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java index b319bc8c8a..390e32b738 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java @@ -8,10 +8,11 @@ import io.temporal.client.WorkflowOptions; import io.temporal.failure.ApplicationFailure; import io.temporal.failure.NexusOperationFailure; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -101,15 +102,15 @@ public interface TestNexusServiceDoubleStart { @ServiceImpl(service = TestNexusServiceDoubleStart.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { // First start should succeed but the workflow blocks indefinitely + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); client.startWorkflow( BlockingWorkflow.class, BlockingWorkflow::execute, input, WorkflowOptions.newBuilder() - .setWorkflowId("double-start-first-" + context.getService()) + .setWorkflowId("double-start-first-" + info.getService()) .build()); // Second start should throw return client.startWorkflow( @@ -117,7 +118,7 @@ public TemporalOperationResult operation( BlockingWorkflow::execute, input, WorkflowOptions.newBuilder() - .setWorkflowId("double-start-second-" + context.getService()) + .setWorkflowId("double-start-second-" + info.getService()) .build()); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java index 379fbea2a1..b49386fb71 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java @@ -13,16 +13,13 @@ import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; import io.temporal.workflow.*; import java.time.Duration; import java.util.List; -import java.util.Map; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import org.junit.Assert; import org.junit.Rule; @@ -39,13 +36,13 @@ public class GenericHandlerSignalDeduplicationTest { private static final String ONE_SIGNAL_MESSAGE = "Only one signal can be sent per Nexus operation invocation."; - private static final Map> retryRequestIds = new ConcurrentHashMap<>(); + private final List retryRequestIds = new CopyOnWriteArrayList<>(); @Rule public SDKTestWorkflowRule testWorkflowRule = SDKTestWorkflowRule.newBuilder() .setWorkflowTypes(CallerWorkflowImpl.class, CountSignalsWorkflowImpl.class) - .setNexusServiceImplementation(new TestNexusServiceImpl()) + .setNexusServiceImplementation(new TestNexusServiceImpl(retryRequestIds)) .build(); @Test @@ -76,10 +73,10 @@ public void signalDedupedAcrossHandlerRetry() { Assert.assertEquals("signaled-" + targetId, newCaller().execute("signalRetry", targetId)); - List requestIds = retryRequestIds.get(targetId); Assert.assertEquals( - "handler should fail once and then be redelivered once", 2, requestIds.size()); - Assert.assertEquals(requestIds.get(0), requestIds.get(1)); + "handler should fail once and then be redelivered once", 2, retryRequestIds.size()); + Assert.assertNotNull("the handler should receive a request ID", retryRequestIds.get(0)); + Assert.assertEquals(retryRequestIds.get(0), retryRequestIds.get(1)); CountSignalsWorkflow target = testWorkflowRule.getWorkflowClient().newWorkflowStub(CountSignalsWorkflow.class, targetId); @@ -168,9 +165,15 @@ public interface TestNexusService { @ServiceImpl(service = TestNexusService.class) public static class TestNexusServiceImpl { + private final List retryRequestIds; + + public TestNexusServiceImpl(List retryRequestIds) { + this.retryRequestIds = retryRequestIds; + } + @TemporalOperation public TemporalOperationResult doubleSignal( - TemporalOperationStartContext context, TemporalNexusClient client, String targetId) { + TemporalNexusClient client, String targetId) { client.getWorkflowClient().newUntypedWorkflowStub(targetId).signal("mySignal", targetId); // The first signal uses up the invocation's only signal, so a second one is rejected even // though it targets a different workflow. @@ -191,7 +194,7 @@ public TemporalOperationResult doubleSignal( @TemporalOperation public TemporalOperationResult signalRetry( - TemporalOperationStartContext context, TemporalNexusClient client, String targetId) { + TemporalNexusClient client, String targetId) { client .getWorkflowClient() .newUntypedWorkflowStub( @@ -202,10 +205,8 @@ public TemporalOperationResult signalRetry( .build()) .signalWithStart("mySignal", new Object[] {targetId}, new Object[] {}); - List requestIds = - retryRequestIds.computeIfAbsent(targetId, k -> new CopyOnWriteArrayList<>()); - requestIds.add(context.getRequestId()); - if (requestIds.size() == 1) { + retryRequestIds.add(Nexus.getOperationContext().getInfo().getRequestId()); + if (retryRequestIds.size() == 1) { throw new HandlerException( HandlerException.ErrorType.INTERNAL, "force handler redelivery after signalling", diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java index 8390a0f0a4..8e7205a730 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java @@ -6,7 +6,6 @@ import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -61,8 +60,7 @@ public interface TestNexusSyncService { @ServiceImpl(service = TestNexusSyncService.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { return TemporalOperationResult.sync("sync-" + input); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java index d3c23c0d7c..9061f5825d 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java @@ -4,10 +4,11 @@ import io.nexusrpc.Service; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -67,10 +68,11 @@ public interface TestNexusServiceProc { @ServiceImpl(service = TestNexusServiceProc.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, Integer input) { + public TemporalOperationResult operation(TemporalNexusClient client, Integer input) { + + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); String prefix = "generic-handler-test-proc" + input + "-"; - String workflowId = prefix + context.getService() + "-" + context.getOperation(); + String workflowId = prefix + info.getService() + "-" + info.getOperation(); WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowId(workflowId).build(); switch (input) { case 0: diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java index 31eec88d06..69defd154f 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java @@ -4,10 +4,11 @@ import io.nexusrpc.Service; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -68,10 +69,11 @@ public interface TestNexusServiceGeneric { @ServiceImpl(service = TestNexusServiceGeneric.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, Integer input) { + public TemporalOperationResult operation(TemporalNexusClient client, Integer input) { String prefix = "generic-handler-test-func" + input + "-"; - String workflowId = prefix + context.getService() + "-" + context.getOperation(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); + + String workflowId = prefix + info.getService() + "-" + info.getOperation(); WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowId(workflowId).build(); switch (input) { case 0: diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java index 71eb71ff07..aeab9133e9 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java @@ -4,10 +4,11 @@ import io.nexusrpc.Service; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -64,14 +65,14 @@ public interface TestNexusServiceUntyped { @ServiceImpl(service = TestNexusServiceUntyped.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); return client.startWorkflow( "func1", String.class, WorkflowOptions.newBuilder() .setWorkflowId( - "generic-handler-untyped-" + context.getService() + "-" + context.getOperation()) + "generic-handler-untyped-" + info.getService() + "-" + info.getOperation()) .build(), input); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java index 2db5c243e0..24ef842c1e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java @@ -472,7 +472,7 @@ public OperationHandler startWorkflow() { @OperationImpl public OperationHandler startActivity() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( BackingActivity.class, BackingActivity::process, @@ -491,7 +491,7 @@ public OperationHandler startActivity() { @OperationImpl public OperationHandler startAsyncActivity() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( AsyncActivity.class, AsyncActivity::process, @@ -505,7 +505,7 @@ public OperationHandler startAsyncActivity() { @OperationImpl public OperationHandler startStallingActivity() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( StallingActivity.class, StallingActivity::process, @@ -521,7 +521,7 @@ public OperationHandler startStallingActivity() { @OperationImpl public OperationHandler startUpdate() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startWorkflowUpdate( UpdateTargetWorkflow.class, backingId(input), diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index a98df3399e..6aa9700bae 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -6,11 +6,11 @@ import io.nexusrpc.handler.OperationImpl; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -140,21 +140,21 @@ public interface MixedService { @ServiceImpl(service = SugarService.class) public static class SugarServiceImpl { @TemporalOperation - public TemporalOperationResult start( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult start(TemporalNexusClient client, String input) { return client.startWorkflow( SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder().setWorkflowId("sugar-" + ctx.getRequestId()).build()); + WorkflowOptions.newBuilder() + .setWorkflowId("sugar-" + Nexus.getOperationContext().getInfo().getRequestId()) + .build()); } } @ServiceImpl(service = SyncSugarService.class) public static class SyncSugarServiceImpl { @TemporalOperation - public TemporalOperationResult greet( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult greet(TemporalNexusClient client, String input) { return TemporalOperationResult.sync("sync:" + input); } } @@ -162,19 +162,20 @@ public TemporalOperationResult greet( @ServiceImpl(service = MixedService.class) public static class MixedServiceImpl { @TemporalOperation - public TemporalOperationResult workflowOp( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult workflowOp(TemporalNexusClient client, String input) { return client.startWorkflow( SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder().setWorkflowId("mixed-" + ctx.getRequestId()).build()); + WorkflowOptions.newBuilder() + .setWorkflowId("mixed-" + Nexus.getOperationContext().getInfo().getRequestId()) + .build()); } @OperationImpl public OperationHandler legacyOp() { return new TemporalOperationHandler( - (ctx, client, input) -> TemporalOperationResult.sync("legacy:" + input)) {}; + (client, input) -> TemporalOperationResult.sync("legacy:" + input)) {}; } } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java index 143a9fd7fe..6f070e54dd 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java @@ -229,7 +229,7 @@ public static class TestNexusServiceImpl { @OperationImpl public OperationHandler update() { return TemporalOperationHandler.create( - (context, client, input) -> { + (client, input) -> { UpdateOptions.Builder optionsBuilder = UpdateOptions.newBuilder(String.class) .setUpdateName("setValue")