Skip to content
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

Fix support for typed search attributes in child workflow options #1434

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/internal_event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (wc *workflowEnvironmentImpl) ExecuteChildWorkflow(
callback(nil, err)
return
}
searchAttr, err := serializeUntypedSearchAttributes(params.SearchAttributes)
searchAttr, err := serializeSearchAttributes(params.SearchAttributes, params.TypedSearchAttributes)
if err != nil {
if wc.sdkFlags.tryUse(SDKFlagChildWorkflowErrorExecution, !wc.isReplay) {
startedHandler(WorkflowExecution{}, &ChildWorkflowExecutionAlreadyStartedError{})
Expand Down
7 changes: 7 additions & 0 deletions internal/internal_workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ func (env *testWorkflowEnvironmentImpl) newTestWorkflowEnvironmentForChild(param
childEnv.workflowInfo.CronSchedule = cronSchedule
childEnv.workflowInfo.ParentWorkflowNamespace = env.workflowInfo.Namespace
childEnv.workflowInfo.ParentWorkflowExecution = &env.workflowInfo.WorkflowExecution

searchAttrs, err := serializeSearchAttributes(params.SearchAttributes, params.TypedSearchAttributes)
if err != nil {
return nil, err
}
childEnv.workflowInfo.SearchAttributes = searchAttrs

childEnv.runTimeout = params.WorkflowRunTimeout
if workflowHandler, ok := env.runningWorkflows[params.WorkflowID]; ok {
// duplicate workflow ID
Expand Down
34 changes: 34 additions & 0 deletions internal/internal_workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3437,6 +3437,40 @@ func (s *WorkflowTestSuiteUnitTest) Test_ChildWorkflowRetry() {
s.Equal("retry-done", result)
}

func (s *WorkflowTestSuiteUnitTest) Test_ChildWorkflowTypedSearchAttributes() {

childWorkflowFn := func(ctx Context) error {
sa := GetTypedSearchAttributes(ctx)
foo, ok := sa.GetString(NewSearchAttributeKeyString("foo"))
if !s.True(ok) || !s.Equal("bar", foo) {
return NewApplicationErrorWithOptions("invalid foo search attribute", "Internal", ApplicationErrorOptions{
Cause: fmt.Errorf("expected foo search attribute to equal 'bar', got: %v", sa.untypedValue),
NonRetryable: true,
})
}
return nil
}

workflowFn := func(ctx Context) error {
cwo := ChildWorkflowOptions{
WorkflowRunTimeout: time.Minute,
TypedSearchAttributes: NewSearchAttributes(
NewSearchAttributeKeyString("foo").ValueSet("bar"),
),
}
ctx = WithChildWorkflowOptions(ctx, cwo)
return ExecuteChildWorkflow(ctx, childWorkflowFn).Get(ctx, nil)
}

env := s.NewTestWorkflowEnvironment()
env.RegisterWorkflow(childWorkflowFn)
env.RegisterWorkflow(workflowFn)
env.ExecuteWorkflow(workflowFn)

s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
}

func (s *WorkflowTestSuiteUnitTest) Test_SignalChildWorkflowRetry() {
childWorkflowFn := func(ctx Context) (string, error) {
info := GetWorkflowInfo(ctx)
Expand Down
2 changes: 2 additions & 0 deletions internal/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ func (wc *workflowEnvironmentInterceptor) ExecuteChildWorkflow(ctx Context, chil
options.ContextPropagators = workflowOptionsFromCtx.ContextPropagators
options.Memo = workflowOptionsFromCtx.Memo
options.SearchAttributes = workflowOptionsFromCtx.SearchAttributes
options.TypedSearchAttributes = workflowOptionsFromCtx.TypedSearchAttributes
options.VersioningIntent = workflowOptionsFromCtx.VersioningIntent

header, err := workflowHeaderPropagated(ctx, options.ContextPropagators)
Expand Down Expand Up @@ -1456,6 +1457,7 @@ func GetChildWorkflowOptions(ctx Context) ChildWorkflowOptions {
CronSchedule: opts.CronSchedule,
Memo: opts.Memo,
SearchAttributes: opts.SearchAttributes,
TypedSearchAttributes: opts.TypedSearchAttributes,
ParentClosePolicy: opts.ParentClosePolicy,
VersioningIntent: opts.VersioningIntent,
}
Expand Down
19 changes: 19 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,25 @@ func (ts *IntegrationTestSuite) TestWorkflowTypedSearchAttributes() {
ts.NoError(ts.executeWorkflowWithOption(options, ts.workflows.UpsertTypedSearchAttributesWorkflow, nil, false))
}

func (ts *IntegrationTestSuite) TestChildWorkflowTypedSearchAttributes() {
options := ts.startWorkflowOptions("test-child-wf-typed-search-attributes")
// Need to disable eager workflow start until https://github.com/temporalio/temporal/pull/5124 fixed
options.EnableEagerStart = false
// Create initial set of search attributes
stringKey := temporal.NewSearchAttributeKeyString("CustomStringField")
keywordKey := temporal.NewSearchAttributeKeyKeyword("CustomKeywordField")
options.TypedSearchAttributes = temporal.NewSearchAttributes(
stringKey.ValueSet("CustomStringFieldValue"),
keywordKey.ValueSet("foo"),
)
var result testSearchAttributes
ts.NoError(ts.executeWorkflowWithOption(options, ts.workflows.ChildWorkflowSuccessWithTypedSearchAttributes, &result))
ts.Equal("CustomStringFieldValue", result.SearchAttributes["CustomStringField"].Value.(string))
ts.Equal(enumspb.INDEXED_VALUE_TYPE_TEXT, result.SearchAttributes["CustomStringField"].Type)
ts.Equal("foo", result.SearchAttributes["CustomKeywordField"].Value.(string))
ts.Equal(enumspb.INDEXED_VALUE_TYPE_KEYWORD, result.SearchAttributes["CustomKeywordField"].Type)
}

func (ts *IntegrationTestSuite) TestLargeQueryResultError() {
ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
defer cancel()
Expand Down
37 changes: 37 additions & 0 deletions test/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,41 @@ func (w *Workflows) ChildWorkflowSuccessWithParentClosePolicyAbandon(ctx workflo
return childWE.ID, err
}

type (
testSearchAttribute struct {
Type enumspb.IndexedValueType
Value any
}

testSearchAttributes struct {
SearchAttributes map[string]testSearchAttribute
}
)

func (w *Workflows) ChildWorkflowSuccessWithTypedSearchAttributes(ctx workflow.Context) (result testSearchAttributes, err error) {
cwo := workflow.GetChildWorkflowOptions(ctx)
cwo.TypedSearchAttributes = workflow.GetTypedSearchAttributes(ctx)
err = workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, cwo), w.GetTypedSearchAttributes).Get(ctx, &result)
return
}

func (w *Workflows) GetTypedSearchAttributes(ctx workflow.Context) (result testSearchAttributes, err error) {
tsa := workflow.GetTypedSearchAttributes(ctx)
result.SearchAttributes = map[string]testSearchAttribute{}
for _, k := range workflow.DeterministicKeysFunc(tsa.GetUntypedValues(), func(a, b temporal.SearchAttributeKey) int {
if a.GetName() < b.GetName() {
return -1
}
return 1
}) {
result.SearchAttributes[k.GetName()] = testSearchAttribute{
Value: tsa.GetUntypedValues()[k],
Type: k.GetValueType(),
}
}
return result, nil
}

func (w *Workflows) childWorkflowWaitOnContextCancel(ctx workflow.Context) error {
var err error
// Wait for the workflow to be cancelled
Expand Down Expand Up @@ -2913,6 +2948,8 @@ func (w *Workflows) register(worker worker.Worker) {
worker.RegisterWorkflow(w.ChildWorkflowSuccess)
worker.RegisterWorkflow(w.ChildWorkflowSuccessWithParentClosePolicyTerminate)
worker.RegisterWorkflow(w.ChildWorkflowSuccessWithParentClosePolicyAbandon)
worker.RegisterWorkflow(w.ChildWorkflowSuccessWithTypedSearchAttributes)
worker.RegisterWorkflow(w.GetTypedSearchAttributes)
worker.RegisterWorkflow(w.ChildWorkflowCancelUnusualTransitionsRepro)
worker.RegisterWorkflow(w.ChildWorkflowDuplicatePanicRepro)
worker.RegisterWorkflow(w.ChildWorkflowDuplicateGetExecutionStuckRepro)
Expand Down
Loading