-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[SDK Ergonomics] NEXUS-519: Support Query-backed Nexus Operations #11274
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/nexus-rpc/sdk-go/nexus" | ||
| commonpb "go.temporal.io/api/common/v1" | ||
| enumspb "go.temporal.io/api/enums/v1" | ||
| querypb "go.temporal.io/api/query/v1" | ||
| apitemporalnexus "go.temporal.io/api/temporalnexus" | ||
| "go.temporal.io/api/workflowservice/v1" | ||
| "go.temporal.io/sdk/client" | ||
| "go.temporal.io/sdk/worker" | ||
| "go.temporal.io/sdk/workflow" | ||
| "go.temporal.io/server/common/nexus/nexustest" | ||
| "go.temporal.io/server/common/payloads" | ||
| "go.temporal.io/server/tests/testcore" | ||
| ) | ||
|
|
||
| func (s *NexusWorkflowTestSuite) TestNexusOperationBackedByQuery(chasmEnabled bool) { | ||
| env := s.newTestEnv(chasmEnabled) | ||
| ctx := s.Context() | ||
| taskQueue := testcore.RandomizeStr(s.T().Name()) | ||
|
|
||
| queryType := "status" | ||
| signalName := "done" | ||
| handlerWorkflowID := testcore.RandomizeStr(s.T().Name() + "-handler") | ||
| handlerWf := func(ctx workflow.Context) error { | ||
| _ = workflow.SetQueryHandler(ctx, queryType, func() (string, error) { | ||
| return "handler-status", nil | ||
| }) | ||
| workflow.GetSignalChannel(ctx, signalName).Receive(ctx, nil) | ||
| return nil | ||
| } | ||
|
|
||
| h := nexustest.Handler{ | ||
| OnStartOperation: func( | ||
| ctx context.Context, | ||
| service, operation string, | ||
| input *nexus.LazyValue, | ||
| options nexus.StartOperationOptions, | ||
| ) (nexus.HandlerStartOperationResult[any], error) { | ||
| resp, err := env.FrontendClient().QueryWorkflow(ctx, &workflowservice.QueryWorkflowRequest{ | ||
| Namespace: env.Namespace().String(), | ||
| Execution: &commonpb.WorkflowExecution{WorkflowId: handlerWorkflowID}, | ||
| Query: &querypb.WorkflowQuery{QueryType: queryType}, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // simulate the stitching that will be done by the SDKs eventually | ||
| nexus.AddHandlerLinks(ctx, apitemporalnexus.ConvertLinkWorkflowToNexusLink(resp.GetLink().GetWorkflow())) | ||
|
|
||
| var result string | ||
| if err := payloads.Decode(resp.GetQueryResult(), &result); err != nil { | ||
| return nil, err | ||
| } | ||
| return &nexus.HandlerStartOperationResultSync[any]{Value: result}, nil | ||
| }, | ||
| } | ||
| endpointName := env.createRandomExternalNexusServer(ctx, s.T(), h) | ||
|
|
||
| callerWF := func(ctx workflow.Context) (string, error) { | ||
| c := workflow.NewNexusClient(endpointName, "service") | ||
| fut := c.ExecuteOperation(ctx, "operation", "input", workflow.NexusOperationOptions{}) | ||
| var result string | ||
| err := fut.Get(ctx, &result) | ||
| return result, err | ||
| } | ||
|
|
||
| w := worker.New(env.SdkClient(), taskQueue, worker.Options{}) | ||
| w.RegisterWorkflow(callerWF) | ||
| w.RegisterWorkflow(handlerWf) | ||
| s.NoError(w.Start()) | ||
| defer w.Stop() | ||
|
|
||
| handlerRun, err := env.SdkClient().ExecuteWorkflow(ctx, client.StartWorkflowOptions{ | ||
| ID: handlerWorkflowID, | ||
| TaskQueue: taskQueue, | ||
| }, handlerWf) | ||
| s.NoError(err) | ||
|
|
||
| callerRun, err := env.SdkClient().ExecuteWorkflow(ctx, client.StartWorkflowOptions{ | ||
| TaskQueue: taskQueue, | ||
| }, callerWF) | ||
| s.NoError(err) | ||
|
|
||
| var result string | ||
| s.NoError(callerRun.Get(ctx, &result)) | ||
| s.Equal("handler-status", result) | ||
|
|
||
| // verify the nexus operation completed event carries a link to the handler's workflow | ||
| hist := env.GetHistory(env.Namespace().String(), &commonpb.WorkflowExecution{WorkflowId: callerRun.GetID()}) | ||
| completedEvent := s.RequireHistoryEvent(hist, enumspb.EVENT_TYPE_NEXUS_OPERATION_COMPLETED) | ||
| s.Len(completedEvent.GetLinks(), 1) | ||
| workflowLink := completedEvent.GetLinks()[0].GetWorkflow() | ||
| s.NotNil(workflowLink, "completed event must carry a link of type workflow") | ||
| s.Equal(env.Namespace().String(), workflowLink.GetNamespace()) | ||
| s.Equal(handlerWorkflowID, workflowLink.GetWorkflowId()) | ||
| s.Equal(handlerRun.GetRunID(), workflowLink.GetRunId()) | ||
| s.Equal("Query processed", workflowLink.GetReason()) | ||
|
|
||
| s.NoError(env.SdkClient().SignalWorkflow(ctx, handlerWorkflowID, "", signalName, nil)) | ||
| s.NoError(handlerRun.Get(ctx, nil)) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.