-
Notifications
You must be signed in to change notification settings - Fork 214
Add Temporal Nexus Operation Handler #2842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Quinn-With-Two-Ns
wants to merge
9
commits into
temporalio:master
Choose a base branch
from
Quinn-With-Two-Ns:temporal-nexus-operation-handler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a88535
Add Temporal Nexus Operation Handler
Quinn-With-Two-Ns 380e112
Update
Quinn-With-Two-Ns deb19d4
Update some docs
Quinn-With-Two-Ns ba2fbb6
Update calling convention
Quinn-With-Two-Ns 1e424f3
Code review
Quinn-With-Two-Ns 77502a8
Address feedback
Quinn-With-Two-Ns e03fa5d
Add check for duplicate handler start
Quinn-With-Two-Ns f2c65fc
Add check for duplicate handler start
Quinn-With-Two-Ns d448f63
Fix race
Quinn-With-Two-Ns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusStartWorkflowHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package io.temporal.internal.nexus; | ||
|
|
||
| import static io.temporal.internal.common.LinkConverter.workflowEventToNexusLink; | ||
| import static io.temporal.internal.common.NexusUtil.nexusProtoLinkToLink; | ||
|
|
||
| import io.nexusrpc.handler.HandlerException; | ||
| import io.nexusrpc.handler.OperationContext; | ||
| import io.nexusrpc.handler.OperationStartDetails; | ||
| import io.temporal.api.common.v1.Link; | ||
| import io.temporal.api.common.v1.WorkflowExecution; | ||
| import io.temporal.api.enums.v1.EventType; | ||
| import io.temporal.internal.client.NexusStartWorkflowRequest; | ||
| import io.temporal.internal.client.NexusStartWorkflowResponse; | ||
| import java.net.URISyntaxException; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * Shared helper for starting a workflow from a Nexus operation and attaching workflow links to the | ||
| * operation context. Used by both {@code WorkflowRunOperationImpl} and {@code | ||
| * TemporalNexusClientImpl}. | ||
| */ | ||
| public class NexusStartWorkflowHelper { | ||
|
|
||
| /** | ||
| * Starts a workflow via the provided invoker function, attaches workflow links to the operation | ||
| * context, and returns the response. | ||
| * | ||
| * @param ctx the operation context (links will be attached as a side-effect) | ||
| * @param details the operation start details containing requestId, callback, links | ||
| * @param invoker function that starts the workflow given a {@link NexusStartWorkflowRequest} | ||
| * @return the {@link NexusStartWorkflowResponse} containing the operation token and workflow | ||
| * execution | ||
| */ | ||
| public static NexusStartWorkflowResponse startWorkflowAndAttachLinks( | ||
| OperationContext ctx, | ||
| OperationStartDetails details, | ||
| Function<NexusStartWorkflowRequest, NexusStartWorkflowResponse> invoker) { | ||
| InternalNexusOperationContext nexusCtx = CurrentNexusOperationContext.get(); | ||
|
|
||
| NexusStartWorkflowRequest nexusRequest = | ||
| new NexusStartWorkflowRequest( | ||
| details.getRequestId(), | ||
| details.getCallbackUrl(), | ||
| details.getCallbackHeaders(), | ||
| nexusCtx.getTaskQueue(), | ||
| details.getLinks()); | ||
|
|
||
| NexusStartWorkflowResponse response = invoker.apply(nexusRequest); | ||
| WorkflowExecution workflowExec = response.getWorkflowExecution(); | ||
|
|
||
| // If the start workflow response returned a link use it, otherwise | ||
| // create the link information about the new workflow and return to the caller. | ||
| Link.WorkflowEvent workflowEventLink = | ||
| nexusCtx.getStartWorkflowResponseLink().hasWorkflowEvent() | ||
| ? nexusCtx.getStartWorkflowResponseLink().getWorkflowEvent() | ||
| : null; | ||
| if (workflowEventLink == null) { | ||
| workflowEventLink = | ||
| Link.WorkflowEvent.newBuilder() | ||
| .setNamespace(nexusCtx.getNamespace()) | ||
| .setWorkflowId(workflowExec.getWorkflowId()) | ||
| .setRunId(workflowExec.getRunId()) | ||
| .setEventRef( | ||
| Link.WorkflowEvent.EventReference.newBuilder() | ||
| .setEventType(EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED)) | ||
| .build(); | ||
| } | ||
| io.temporal.api.nexus.v1.Link nexusLink = workflowEventToNexusLink(workflowEventLink); | ||
| if (nexusLink != null) { | ||
| try { | ||
| ctx.addLinks(nexusProtoLinkToLink(nexusLink)); | ||
| } catch (URISyntaxException e) { | ||
| throw new HandlerException(HandlerException.ErrorType.INTERNAL, "failed to parse URI", e); | ||
| } | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| private NexusStartWorkflowHelper() {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,33 +15,45 @@ public class OperationTokenUtil { | |
| private static final Base64.Encoder encoder = Base64.getUrlEncoder().withoutPadding(); | ||
|
|
||
| /** | ||
| * Load a workflow run operation token from an operation token. | ||
| * Load and validate an operation token without asserting the token type. Use this for cancel | ||
| * dispatch where the token type determines the cancel behavior. | ||
| * | ||
| * @throws IllegalArgumentException if the operation token is invalid | ||
| * @throws IllegalArgumentException if the operation token is malformed or has invalid structure | ||
| */ | ||
| public static WorkflowRunOperationToken loadWorkflowRunOperationToken(String operationToken) { | ||
| WorkflowRunOperationToken token; | ||
| public static OperationToken loadOperationToken(String operationToken) { | ||
| OperationToken token; | ||
| try { | ||
| JavaType reference = mapper.getTypeFactory().constructType(WorkflowRunOperationToken.class); | ||
| JavaType reference = mapper.getTypeFactory().constructType(OperationToken.class); | ||
| token = mapper.readValue(decoder.decode(operationToken), reference); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Failed to parse operation token: " + e.getMessage()); | ||
| } | ||
| if (!token.getType().equals(OperationTokenType.WORKFLOW_RUN)) { | ||
| throw new IllegalArgumentException( | ||
| "Invalid workflow run token: incorrect operation token type: " + token.getType()); | ||
| } | ||
| if (token.getVersion() != null && token.getVersion() != 0) { | ||
| throw new IllegalArgumentException("Invalid workflow run token: unexpected version field"); | ||
| throw new IllegalArgumentException("Invalid operation token: unexpected version field"); | ||
| } | ||
| if (Strings.isNullOrEmpty(token.getWorkflowId())) { | ||
| throw new IllegalArgumentException("Invalid workflow run token: missing workflow ID (wid)"); | ||
| throw new IllegalArgumentException("Invalid operation token: missing workflow ID (wid)"); | ||
|
Comment on lines
34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this check be moved into |
||
| } | ||
| return token; | ||
|
Quinn-With-Two-Ns marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Load a workflow run operation token, asserting that the token type is {@link | ||
| * OperationTokenType#WORKFLOW_RUN}. | ||
| * | ||
| * @throws IllegalArgumentException if the operation token is invalid or not a workflow run token | ||
| */ | ||
| public static OperationToken loadWorkflowRunOperationToken(String operationToken) { | ||
| OperationToken token = loadOperationToken(operationToken); | ||
| if (!token.getType().equals(OperationTokenType.WORKFLOW_RUN)) { | ||
| throw new IllegalArgumentException( | ||
| "Invalid workflow run token: incorrect operation token type: " + token.getType()); | ||
| } | ||
| return token; | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to extract the workflow Id from an operation token. | ||
| * Extract the workflow ID from a workflow run operation token. | ||
| * | ||
| * @throws IllegalArgumentException if the operation token is invalid | ||
| */ | ||
|
|
@@ -52,7 +64,9 @@ public static String loadWorkflowIdFromOperationToken(String operationToken) { | |
| /** Generate a workflow run operation token from a workflow ID and namespace. */ | ||
| public static String generateWorkflowRunOperationToken(String workflowId, String namespace) | ||
| throws JsonProcessingException { | ||
| String json = ow.writeValueAsString(new WorkflowRunOperationToken(namespace, workflowId)); | ||
| String json = | ||
| ow.writeValueAsString( | ||
| new OperationToken(OperationTokenType.WORKFLOW_RUN, namespace, workflowId)); | ||
| return encoder.encodeToString(json.getBytes()); | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With more types of tokens upcoming, should we favor something like static constructors for this like OperationToken.NewWorkflowRunToken() that takes the required fields for the workflow run type?
I guess this is a bit like what
OperationTokenUtilprovides so feel free to dismiss this!