Skip to content
Open
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
35 changes: 35 additions & 0 deletions features/deployment_versioning/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/temporalio/features/harness/go/harness"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
Expand Down Expand Up @@ -69,6 +70,40 @@ func WaitForWorkflowRunning(r *harness.Runner, ctx context.Context, handle clien
})
}

func WaitForWorkerDeploymentRoutingConfigPropagation(
r *harness.Runner,
ctx context.Context,
deploymentName string,
expectedCurrentBuildID string,
expectedRampingBuildID string,
) error {
return r.DoUntilEventually(ctx, 300*time.Millisecond, 10*time.Second,
func() bool {
resp, err := r.Client.WorkflowService().DescribeWorkerDeployment(ctx, &workflowservice.DescribeWorkerDeploymentRequest{
Namespace: r.Namespace,
DeploymentName: deploymentName,
})
if err != nil {
return false
}
if resp.GetWorkerDeploymentInfo().GetRoutingConfig().GetCurrentDeploymentVersion().GetBuildId() != expectedCurrentBuildID {
return false
}
if resp.GetWorkerDeploymentInfo().GetRoutingConfig().GetRampingDeploymentVersion().GetBuildId() != expectedRampingBuildID {
return false
}
switch resp.GetWorkerDeploymentInfo().GetRoutingConfigUpdateState() {
case enumspb.ROUTING_CONFIG_UPDATE_STATE_COMPLETED:
return true
case enumspb.ROUTING_CONFIG_UPDATE_STATE_UNSPECIFIED:
return true // not implemented
case enumspb.ROUTING_CONFIG_UPDATE_STATE_IN_PROGRESS:
return false
}
return false
})
}

func SetCurrent(r *harness.Runner, ctx context.Context, deploymentName string, version worker.WorkerDeploymentVersion) error {
dHandle := r.Client.WorkerDeploymentClient().GetHandle(deploymentName)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Upgrade on Continue-as-New

This snippet demonstrates how a pinned Workflow can upgrade to a new Worker Deployment Version at Continue-as-New boundaries.

## Pattern

Long-running Workflows that use Continue-as-New can upgrade to newer Worker Deployment Versions without patching by:

1. Checking `GetContinueAsNewSuggested()` periodically
2. Looking for `ContinueAsNewSuggestedReasonTargetWorkerDeploymentVersionChanged`
3. Using `ContinueAsNewVersioningBehaviorAutoUpgrade` when continuing

## Use Cases

- Entity Workflows running for months or years
- Batch processing Workflows that checkpoint with Continue-as-New
- AI agent Workflows with long sleeps waiting for user input
175 changes: 175 additions & 0 deletions features/deployment_versioning/upgrade_on_continue_as_new/feature.go
Copy link
Member

@cretz cretz Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The features repo is for asserting conformance across languages. It's basically a test suite. There is no assertion aspect or even use of this workflow. I wonder if samples-go would fit what you're wanting a bit more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package upgrade_on_continue_as_new

import (
"context"
"fmt"
"time"

"github.com/google/uuid"
"github.com/temporalio/features/features/deployment_versioning"
"github.com/temporalio/features/harness/go/harness"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)

var Feature = harness.Feature{
Workflows: []interface{}{
harness.WorkflowWithOptions{
Workflow: ContinueAsNewWithVersionUpgrade,
Options: workflow.RegisterOptions{
Name: "ContinueAsNewWithVersionUpgrade",
VersioningBehavior: workflow.VersioningBehaviorPinned,
},
},
},
WorkerOptions: worker.Options{
DeploymentOptions: worker.DeploymentOptions{
UseVersioning: true,
Version: v1,
},
},
Execute: Execute,
ExpectRunResult: "v2.0",
}

// @@@SNIPSTART upgrade-on-continue-as-new-go
// ContinueAsNewWithVersionUpgrade demonstrates how a pinned Workflow can
// upgrade to a new Worker Deployment Version at Continue-as-New boundaries.
// The Workflow checks for the TARGET_WORKER_DEPLOYMENT_VERSION_CHANGED reason
// and uses AutoUpgrade behavior to move to the new version.
func ContinueAsNewWithVersionUpgrade(ctx workflow.Context, attempt int) (string, error) {
if attempt > 0 {
// After continuing as new, return the version
return "v1.0", nil
}

// Check continue-as-new-suggested periodically
for {
err := workflow.Sleep(ctx, 10*time.Millisecond)
if err != nil {
return "", err
}

info := workflow.GetInfo(ctx)
if info.GetContinueAsNewSuggested() {
for _, reason := range info.GetContinueAsNewSuggestedReasons() {
if reason == workflow.ContinueAsNewSuggestedReasonTargetWorkerDeploymentVersionChanged {
// A new Worker Deployment Version is available
// Continue-as-New with upgrade to the new version
return "", workflow.NewContinueAsNewErrorWithOptions(
ctx,
workflow.ContinueAsNewErrorOptions{
InitialVersioningBehavior: workflow.ContinueAsNewVersioningBehaviorAutoUpgrade,
},
"ContinueAsNewWithVersionUpgrade",
attempt+1,
)
}
}
}
}
}

// @@@SNIPEND

func ContinueAsNewWithVersionUpgradeV2(
ctx workflow.Context,
attempt int,
) (string, error) {
return "v2.0", nil
}

var deploymentName = uuid.NewString()
var v1 = worker.WorkerDeploymentVersion{
DeploymentName: deploymentName,
BuildID: "1.0",
}
var v2 = worker.WorkerDeploymentVersion{
DeploymentName: deploymentName,
BuildID: "2.0",
}

var worker2 worker.Worker

func Execute(ctx context.Context, r *harness.Runner) (client.WorkflowRun, error) {
if supported := deployment_versioning.ServerSupportsDeployments(ctx, r); !supported {
return nil, r.Skip(fmt.Sprintf("server does not support deployment versioning"))
}

// Two workers:
// 1.0) and 2.0) both with no default versioning behavior
// SetCurrent to 1.0
// Workflow (annotated as Pinned):
// - Start and wait for continue-as-new-suggested boolean
// - If continue-as-new-suggested is true and the reason is version-changed, continue as new with AutoUpgrade behavior
// Verify workflow returns 2.0.

// Define v2.0 of the workflow and start polling
worker2 = worker.New(r.Client, r.TaskQueue, worker.Options{
DeploymentOptions: worker.DeploymentOptions{
UseVersioning: true,
Version: v2,
},
})
worker2.RegisterWorkflowWithOptions(ContinueAsNewWithVersionUpgradeV2, workflow.RegisterOptions{
Name: "ContinueAsNewWithVersionUpgrade",
VersioningBehavior: workflow.VersioningBehaviorPinned,
})
if err := worker2.Start(); err != nil {
return nil, err
}

// Wait for the deployment to be ready
dHandle := r.Client.WorkerDeploymentClient().GetHandle(deploymentName)
if err := deployment_versioning.WaitForDeployment(r, ctx, dHandle); err != nil {
return nil, err
}

// Wait for version 1.0 to be ready
if err := deployment_versioning.WaitForDeploymentVersion(r, ctx, dHandle, v1); err != nil {
return nil, err
}
// Set version 1.0 as current
if err := deployment_versioning.SetCurrent(r, ctx, deploymentName, v1); err != nil {
return nil, err
}

// Wait for v1.0-as-Current Deployment Routing Config to be propagated to all task queues
if err := deployment_versioning.WaitForWorkerDeploymentRoutingConfigPropagation(r, ctx, deploymentName, v1.BuildID, ""); err != nil {
return nil, err
}

run, err := r.Client.ExecuteWorkflow(ctx, client.StartWorkflowOptions{
TaskQueue: r.TaskQueue,
ID: "continueasnew-with-version-upgrade",
WorkflowExecutionTimeout: 1 * time.Minute,
}, "ContinueAsNewWithVersionUpgrade", 0)
if err != nil {
return nil, err
}

// Wait for workflow to complete one WFT on v1.0
if err := deployment_versioning.WaitForWorkflowRunning(r, ctx, run); err != nil {
return nil, err
}
// Wait for version 2.0 to be ready
if err := deployment_versioning.WaitForDeploymentVersion(r, ctx, dHandle, v2); err != nil {
return nil, err
}
// Set version 2.0 as current
if err := deployment_versioning.SetCurrent(r, ctx, deploymentName, v2); err != nil {
return nil, err
}
// Wait for v2.0-as-Current Deployment Routing Config to be propagated to all task queues
if err := deployment_versioning.WaitForWorkerDeploymentRoutingConfigPropagation(r, ctx, deploymentName, v2.BuildID, ""); err != nil {
return nil, err
}
return run, nil
}

func CheckHistory(ctx context.Context, r *harness.Runner, run client.WorkflowRun) error {
// Shut down the 2.0 worker
worker2.Stop()
return r.CheckHistoryDefault(ctx, run)
}
2 changes: 2 additions & 0 deletions features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
deployment_versioning_routing_pinned "github.com/temporalio/features/features/deployment_versioning/routing_pinned"
deployment_versioning_routing_with_override "github.com/temporalio/features/features/deployment_versioning/routing_with_override"
deployment_versioning_routing_with_ramp "github.com/temporalio/features/features/deployment_versioning/routing_with_ramp"
deployment_versioning_upgrade_on_continue_as_new "github.com/temporalio/features/features/deployment_versioning/upgrade_on_continue_as_new"
eager_activity_non_remote_activities_worker "github.com/temporalio/features/features/eager_activity/non_remote_activities_worker"
eager_workflow_successful_start "github.com/temporalio/features/features/eager_workflow/successful_start"
query_successful_query "github.com/temporalio/features/features/query/successful_query"
Expand Down Expand Up @@ -90,6 +91,7 @@ func init() {
deployment_versioning_routing_pinned.Feature,
deployment_versioning_routing_with_override.Feature,
deployment_versioning_routing_with_ramp.Feature,
deployment_versioning_upgrade_on_continue_as_new.Feature,
eager_activity_non_remote_activities_worker.Feature,
eager_workflow_successful_start.Feature,
query_successful_query.Feature,
Expand Down
2 changes: 1 addition & 1 deletion features/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/temporalio/features/harness/go v0.0.0-00010101000000-000000000000
github.com/uber-go/tally/v4 v4.1.1
github.com/urfave/cli/v2 v2.3.0
go.temporal.io/api v1.53.0
go.temporal.io/api v1.62.0
go.temporal.io/sdk v1.37.0
go.temporal.io/sdk/contrib/tally v0.2.0
golang.org/x/mod v0.17.0
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ require (
github.com/golang/mock v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/nexus-rpc/sdk-go v0.3.0 // indirect
github.com/nexus-rpc/sdk-go v0.5.1 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
github.com/uber-go/tally/v4 v4.1.7 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.temporal.io/api v1.53.0 // indirect
go.temporal.io/api v1.62.0 // indirect
go.temporal.io/sdk/contrib/tally v0.2.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand All @@ -50,4 +50,5 @@ require (
replace (
github.com/temporalio/features/features => ./features
github.com/temporalio/features/harness/go => ./harness/go
go.temporal.io/sdk => ../sdk-go
)
Loading
Loading