Skip to content

Commit

Permalink
Fix registry to resolve functions with -fm suffix (#1012)
Browse files Browse the repository at this point in the history
With recent changes -fm suffix was dropped from function names.
This caused errors when resolving existing scheduled workflows and
activities.

With this fix we check for exact match and then check for registered
function without -fm suffix for backwards compatibility.
  • Loading branch information
vytautas-karpavicius committed Jul 24, 2020
1 parent 1f29ced commit 65840cc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func (r *registry) getWorkflowAlias(fnName string) (string, bool) {
func (r *registry) getWorkflowFn(fnName string) (interface{}, bool) {
r.Lock() // do not defer for Unlock to call next.getWorkflowFn without lock
fn, ok := r.workflowFuncMap[fnName]
if !ok { // if exact match is not found, check for backwards compatible name without -fm suffix
fn, ok = r.workflowFuncMap[strings.TrimSuffix(fnName, "-fm")]
}
if !ok && r.next != nil {
r.Unlock()
return r.next.getWorkflowFn(fnName)
Expand Down Expand Up @@ -263,6 +266,9 @@ func (r *registry) addActivityWithLock(fnName string, a activity) {
func (r *registry) GetActivity(fnName string) (activity, bool) {
r.Lock() // do not defer for Unlock to call next.GetActivity without lock
a, ok := r.activityFuncMap[fnName]
if !ok { // if exact match is not found, check for backwards compatible name without -fm suffix
a, ok = r.activityFuncMap[strings.TrimSuffix(fnName, "-fm")]
}
if !ok && r.next != nil {
r.Unlock()
return r.next.GetActivity(fnName)
Expand Down
25 changes: 25 additions & 0 deletions internal/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
)

func TestWorkflowRegistration(t *testing.T) {
w := &testWorkflowStruct{}
tests := []struct {
msg string
register func(r *registry)
workflowType string
altWorkflowType string
resolveByFunction interface{}
resolveByAlias string
}{
Expand All @@ -57,6 +59,13 @@ func TestWorkflowRegistration(t *testing.T) {
resolveByFunction: testWorkflowFunction,
resolveByAlias: "workflow.alias",
},
{
msg: "register workflow struct function (backwards compatible)",
register: func(r *registry) { r.RegisterWorkflow(w.Method) },
workflowType: "go.uber.org/cadence/internal.(*testWorkflowStruct).Method",
altWorkflowType: "go.uber.org/cadence/internal.(*testWorkflowStruct).Method-fm",
resolveByFunction: w.Method,
},
}

for _, tt := range tests {
Expand All @@ -72,6 +81,12 @@ func TestWorkflowRegistration(t *testing.T) {
_, ok := r.getWorkflowFn(tt.workflowType)
require.True(t, ok)

// Verify workflow is resolved from alternative (backwards compatible) workflow type
if len(tt.altWorkflowType) > 0 {
_, ok = r.getWorkflowFn(tt.altWorkflowType)
require.True(t, ok)
}

// Verify resolving by function reference
workflowType = getWorkflowFunctionName(r, tt.resolveByFunction)
require.Equal(t, tt.workflowType, workflowType)
Expand All @@ -90,6 +105,7 @@ func TestActivityRegistration(t *testing.T) {
msg string
register func(r *registry)
activityType string
altActivityType string
resolveByFunction interface{}
resolveByAlias string
}{
Expand Down Expand Up @@ -120,6 +136,7 @@ func TestActivityRegistration(t *testing.T) {
msg: "register activity struct",
register: func(r *registry) { r.RegisterActivity(&testActivityStruct{}) },
activityType: "go.uber.org/cadence/internal.(*testActivityStruct).Method",
altActivityType: "go.uber.org/cadence/internal.(*testActivityStruct).Method-fm",
resolveByFunction: (&testActivityStruct{}).Method,
},
{
Expand Down Expand Up @@ -153,6 +170,12 @@ func TestActivityRegistration(t *testing.T) {
_, ok := r.GetActivity(tt.activityType)
require.True(t, ok)

// Verify activity is resolved from alternative (backwards compatible) activity type
if len(tt.altActivityType) > 0 {
_, ok = r.GetActivity(tt.altActivityType)
require.True(t, ok)
}

// Verify resolving by function reference
activityType = getActivityFunctionName(r, tt.resolveByFunction)
require.Equal(t, tt.activityType, activityType, "resolve by function reference")
Expand All @@ -166,8 +189,10 @@ func TestActivityRegistration(t *testing.T) {
}
}

type testWorkflowStruct struct{}
type testActivityStruct struct{}

func (ts *testWorkflowStruct) Method(ctx Context) error { return nil }
func (ts *testActivityStruct) Method() error { return nil }

func testActivityFunction() error { return nil }
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/activity.cancel.sm.repro.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"version": -24,
"workflowExecutionStartedEventAttributes": {
"workflowType": {
"name": "go.uber.org/cadence/test.(*Workflows).ActivityCancelRepro"
"name": "go.uber.org/cadence/test.(*Workflows).ActivityCancelRepro-fm"
},
"taskList": {
"name": "tl-1"
Expand Down

0 comments on commit 65840cc

Please sign in to comment.