Skip to content

Commit d769f01

Browse files
committed
change usage of internal.GetVersionOption to GetVersionOption.apply interface
1 parent 7f3bf63 commit d769f01

File tree

6 files changed

+60
-86
lines changed

6 files changed

+60
-86
lines changed

internal/interceptors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type WorkflowInterceptor interface {
6363
GetSignalChannel(ctx Context, signalName string) Channel
6464
SideEffect(ctx Context, f func(ctx Context) interface{}) Value
6565
MutableSideEffect(ctx Context, id string, f func(ctx Context) interface{}, equals func(a, b interface{}) bool) Value
66-
GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) Version
66+
GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version
6767
SetQueryHandler(ctx Context, queryType string, handler interface{}) error
6868
IsReplaying(ctx Context) bool
6969
HasLastCompletionResult(ctx Context) bool
@@ -158,7 +158,7 @@ func (t *WorkflowInterceptorBase) MutableSideEffect(ctx Context, id string, f fu
158158
}
159159

160160
// GetVersion forwards to t.Next
161-
func (t *WorkflowInterceptorBase) GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) Version {
161+
func (t *WorkflowInterceptorBase) GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version {
162162
return t.Next.GetVersion(ctx, changeID, minSupported, maxSupported, opts...)
163163
}
164164

internal/internal_event_handlers.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func validateVersion(changeID string, version, minSupported, maxSupported Versio
602602
}
603603
}
604604

605-
func (wc *workflowEnvironmentImpl) GetVersion(changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) Version {
605+
func (wc *workflowEnvironmentImpl) GetVersion(changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version {
606606
// Check if the changeID already has a version assigned
607607
// If it does, validate the version against the min and max supported versions
608608
// ensuring it is within the acceptable range
@@ -611,10 +611,10 @@ func (wc *workflowEnvironmentImpl) GetVersion(changeID string, minSupported, max
611611
return version
612612
}
613613

614-
// Use the first option if they're present
615-
var options GetVersionOptions
616-
if len(opts) > 0 {
617-
options = opts[0]
614+
// Apply the functional options to get the configuration
615+
config := &getVersionConfig{}
616+
for _, opt := range opts {
617+
opt.apply(config)
618618
}
619619

620620
var version Version
@@ -625,11 +625,11 @@ func (wc *workflowEnvironmentImpl) GetVersion(changeID string, minSupported, max
625625
version = DefaultVersion
626626

627627
// If ExecuteWithVersion option is used, use the custom version provided
628-
case options.CustomVersion != nil:
629-
version = *options.CustomVersion
628+
case config.CustomVersion != nil:
629+
version = *config.CustomVersion
630630

631631
// If ExecuteWithMinVersion option is set, use the minimum supported version
632-
case options.UseMinVersion:
632+
case config.UseMinVersion:
633633
version = minSupported
634634

635635
// Otherwise, use the maximum supported version

internal/internal_worker_base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type (
7676
localActivityClient
7777
workflowTimerClient
7878
SideEffect(f func() ([]byte, error), callback resultHandler)
79-
GetVersion(changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) Version
79+
GetVersion(changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version
8080
WorkflowInfo() *WorkflowInfo
8181
Complete(result []byte, err error)
8282
RegisterCancelHandler(handler func())

internal/internal_workflow_testsuite.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ func (env *testWorkflowEnvironmentImpl) SideEffect(f func() ([]byte, error), cal
19281928
callback(f())
19291929
}
19301930

1931-
func (env *testWorkflowEnvironmentImpl) GetVersion(changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) (retVersion Version) {
1931+
func (env *testWorkflowEnvironmentImpl) GetVersion(changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) (retVersion Version) {
19321932
if mockVersion, ok := env.getMockedVersion(changeID, changeID, minSupported, maxSupported); ok {
19331933
// GetVersion for changeID is mocked
19341934
env.UpsertSearchAttributes(createSearchAttributesForChangeVersion(changeID, mockVersion, env.changeVersions))
@@ -1948,21 +1948,21 @@ func (env *testWorkflowEnvironmentImpl) GetVersion(changeID string, minSupported
19481948
return version
19491949
}
19501950

1951-
// Use the first option if they're present
1952-
var options GetVersionOptions
1953-
if len(opts) > 0 {
1954-
options = opts[0]
1951+
// Apply the functional options to get the configuration
1952+
config := &getVersionConfig{}
1953+
for _, opt := range opts {
1954+
opt.apply(config)
19551955
}
19561956

19571957
// Determine the version to use based on the options provided
19581958
var version Version
19591959
switch {
19601960
// If ExecuteWithVersion option is used, use the custom version provided
1961-
case options.CustomVersion != nil:
1962-
version = *options.CustomVersion
1961+
case config.CustomVersion != nil:
1962+
version = *config.CustomVersion
19631963

19641964
// If ExecuteWithMinVersion option is set, use the minimum supported version
1965-
case options.UseMinVersion:
1965+
case config.UseMinVersion:
19661966
version = minSupported
19671967

19681968
// Otherwise, use the maximum supported version

internal/workflow.go

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,8 +1567,13 @@ const DefaultVersion Version = -1
15671567
// CadenceChangeVersion is used as search attributes key to find workflows with specific change version.
15681568
const CadenceChangeVersion = "CadenceChangeVersion"
15691569

1570-
// GetVersionOptions contains options for GetVersion
1571-
type GetVersionOptions struct {
1570+
// GetVersionOption is an interface for functional options that can be applied to GetVersion
1571+
type GetVersionOption interface {
1572+
apply(*getVersionConfig)
1573+
}
1574+
1575+
// getVersionConfig holds the configuration for GetVersion
1576+
type getVersionConfig struct {
15721577
// CustomVersion is used to force GetVersion to return a specific version
15731578
// instead of maxSupported version. Set up via ExecuteWithVersion option.
15741579
CustomVersion *Version
@@ -1578,75 +1583,39 @@ type GetVersionOptions struct {
15781583
UseMinVersion bool
15791584
}
15801585

1586+
// customVersionOption forces GetVersion to return a specific version
1587+
type customVersionOption Version
1588+
1589+
func (c customVersionOption) apply(config *getVersionConfig) {
1590+
version := Version(c)
1591+
config.CustomVersion = &version
1592+
}
1593+
15811594
// ExecuteWithVersion forces a specific version to be returned when GetVersion is executed for the first time,
1582-
// instead of returning maxSupported version. This option can be used when you want to separate the versioning of the workflow code and
1583-
// activation of the new logic in the workflow code, to ensure that your changes can be safely rolled back, if needed.
1584-
//
1585-
// For example, initially a workflow has the following code:
1586-
//
1587-
// err = workflow.ExecuteActivity(ctx, foo).Get(ctx, nil)
1588-
//
1589-
// It should be updated to:
1590-
//
1591-
// err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1592-
//
1593-
// Following the steps below, your changes will be forward and backward compatible, keeping possible a safe rollback
1594-
// to the previous version of the workflow code:
1595-
//
1596-
// 1. Keep execution of foo activity, add a support of bar activity
1597-
//
1598-
// v := GetVersion(ctx, "fooChange", DefaultVersion, 1, ExecuteWithVersion(DefaultVersion))
1599-
// if v == DefaultVersion {
1600-
// err = workflow.ExecuteActivity(ctx, foo).Get(ctx, nil)
1601-
// } else {
1602-
// err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1603-
// }
1604-
//
1605-
// The code above supports replaying of workflow execution with both versions DefaultVersion and 1.
1606-
// All new workflow executions will execute foo activity, because GetVersion with ExecuteWithMinVersion option
1607-
// returns DefaultVersion, that will be recorded into the workflow history.
1608-
//
1609-
// 2. Enable execution of bar activity
1610-
//
1611-
// v := GetVersion(ctx, "fooChange", DefaultVersion, 1)
1612-
// if v == DefaultVersion {
1613-
// err = workflow.ExecuteActivity(ctx, foo).Get(ctx, nil)
1614-
// } else {
1615-
// err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1616-
// }
1617-
//
1618-
// The code above supports replaying of workflow execution with both versions DefaultVersion and 1.
1619-
// All new workflow executions will execute bar activity, because
1620-
// GetVersion returns the maximum supported version - 1, that will be recorded into the workflow history.
1621-
//
1622-
// 3. Remove a support of foo activity:
1623-
//
1624-
// GetVersion(ctx, "fooChange", 1, 1)
1625-
// err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1626-
//
1627-
// When there are no workflow executions running DefaultVersion the support of foo activity can be removed.
1628-
//
1629-
// ExecuteWithVersion option is useful when you want to ensure that your changes can be safely rolled back if needed.
1630-
func ExecuteWithVersion(version Version) GetVersionOptions {
1631-
return GetVersionOptions{
1632-
CustomVersion: &version,
1633-
}
1595+
// instead of returning maxSupported version. Check public documentation of workflow.ExecuteWithVersion
1596+
func ExecuteWithVersion(version Version) GetVersionOption {
1597+
return customVersionOption(version)
1598+
}
1599+
1600+
// useMinVersionOption forces GetVersion to return minSupported version
1601+
type useMinVersionOption struct{}
1602+
1603+
func (useMinVersionOption) apply(config *getVersionConfig) {
1604+
config.UseMinVersion = true
16341605
}
16351606

16361607
// ExecuteWithMinVersion forces minSupported version to be returned when GetVersion is executed for the first time,
1637-
// instead of returning maxSupported version. The option is equivalent to ExecuteWithVersion(minSupportedVersion).
1638-
// Check the ExecuteWithVersion documentation for more details.
1639-
func ExecuteWithMinVersion() GetVersionOptions {
1640-
return GetVersionOptions{
1641-
UseMinVersion: true,
1642-
}
1608+
// instead of returning maxSupported version. Check public documentation of workflow.ExecuteWithMinVersion
1609+
func ExecuteWithMinVersion() GetVersionOption {
1610+
return useMinVersionOption{}
16431611
}
16441612

16451613
// GetVersion is used to safely perform backwards incompatible changes to workflow definitions.
16461614
// It is not allowed to update workflow code while there are workflows running as it is going to break
16471615
// determinism. The solution is to have both old code that is used to replay existing workflows
16481616
// as well as the new one that is used when it is executed for the first time.
1649-
// GetVersion returns maxSupported version (to return another version, check GetVersionOption),
1617+
// GetVersion returns maxSupported version
1618+
// (to return another version for a potential safe rollback, check documentation for ExecuteWithVersion and ExecuteWithMinVersion),
16501619
// when is executed for the first time. This version is recorded into the
16511620
// workflow history as a marker event. Even if maxSupported version is changed the version that was recorded is
16521621
// returned on replay. DefaultVersion constant contains version of code that wasn't versioned before.
@@ -1708,12 +1677,12 @@ func ExecuteWithMinVersion() GetVersionOptions {
17081677
// } else {
17091678
// err = workflow.ExecuteActivity(ctx, qux, data).Get(ctx, nil)
17101679
// }
1711-
func GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) Version {
1680+
func GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version {
17121681
i := getWorkflowInterceptor(ctx)
17131682
return i.GetVersion(ctx, changeID, minSupported, maxSupported, opts...)
17141683
}
17151684

1716-
func (wc *workflowEnvironmentInterceptor) GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOptions) Version {
1685+
func (wc *workflowEnvironmentInterceptor) GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version {
17171686
return wc.env.GetVersion(changeID, minSupported, maxSupported, opts...)
17181687
}
17191688

workflow/workflow.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type (
5959
Info = internal.WorkflowInfo
6060

6161
RegistryInfo = internal.RegistryWorkflowInfo
62+
63+
// GetVersionOption is used to specify options for GetVersion
64+
GetVersionOption = internal.GetVersionOption
6265
)
6366

6467
// Register - registers a workflow function with the framework.
@@ -401,22 +404,24 @@ const DefaultVersion Version = internal.DefaultVersion
401404
// err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
402405
//
403406
// When there are no workflow executions running DefaultVersion the support of foo activity can be removed.
404-
func ExecuteWithVersion(version Version) internal.GetVersionOptions {
407+
func ExecuteWithVersion(version Version) GetVersionOption {
405408
return internal.ExecuteWithVersion(version)
406409
}
407410

408411
// ExecuteWithMinVersion forces minSupported version to be returned when GetVersion is executed for the first time,
409412
// instead of returning maxSupported version. The option is equivalent to ExecuteWithVersion(minSupportedVersion).
410413
// Check the ExecuteWithVersion documentation for more details.
411-
func ExecuteWithMinVersion() internal.GetVersionOptions {
414+
func ExecuteWithMinVersion() GetVersionOption {
412415
return internal.ExecuteWithMinVersion()
413416
}
414417

415418
// GetVersion is used to safely perform backwards incompatible changes to workflow definitions.
416419
// It is not allowed to update workflow code while there are workflows running as it is going to break
417420
// determinism. The solution is to have both old code that is used to replay existing workflows
418421
// as well as the new one that is used when it is executed for the first time.
419-
// GetVersion returns maxSupported version when is executed for the first time. This version is recorded into the
422+
// GetVersion returns maxSupported version
423+
// (to return another version for a potential safe rollback, check documentation for ExecuteWithVersion and ExecuteWithMinVersion),
424+
// when is executed for the first time. This version is recorded into the
420425
// workflow history as a marker event. Even if maxSupported version is changed the version that was recorded is
421426
// returned on replay. DefaultVersion constant contains version of code that wasn't versioned before.
422427
// For example initially workflow has the following code:
@@ -477,7 +482,7 @@ func ExecuteWithMinVersion() internal.GetVersionOptions {
477482
// } else {
478483
// err = workflow.ExecuteActivity(ctx, qux, data).Get(ctx, nil)
479484
// }
480-
func GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...internal.GetVersionOptions) Version {
485+
func GetVersion(ctx Context, changeID string, minSupported, maxSupported Version, opts ...GetVersionOption) Version {
481486
return internal.GetVersion(ctx, changeID, minSupported, maxSupported, opts...)
482487
}
483488

0 commit comments

Comments
 (0)