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

[Deployments] Sync workflow before activity tqs and batch updates #7003

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
112 changes: 89 additions & 23 deletions service/worker/deployment/deployment_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package deployment

import (
"fmt"
"slices"
"time"

"github.com/pborman/uuid"
Expand All @@ -38,6 +39,7 @@ import (
"go.temporal.io/sdk/workflow"
deploymentspb "go.temporal.io/server/api/deployment/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/util"
)

type (
Expand Down Expand Up @@ -300,34 +302,15 @@ func (d *DeploymentWorkflowRunner) handleSyncState(ctx workflow.Context, args *d
}

// sync to task queues
syncReq := &deploymentspb.SyncUserDataRequest{
Deployment: d.State.Deployment,
}
for tqName, byType := range d.State.TaskQueueFamilies {
for tqType, data := range byType.TaskQueues {
syncReq.Sync = append(syncReq.Sync, &deploymentspb.SyncUserDataRequest_SyncUserData{
Name: tqName,
Type: enumspb.TaskQueueType(tqType),
Data: d.dataWithTime(data),
})
}
if workflow.GetVersion(ctx, "syncToTaskQueues", workflow.DefaultVersion, 0) == workflow.DefaultVersion {
err = d.syncToTaskQueues1(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: should we call these syncToTaskQueues0 and syncToTaskQueues1 to match the version?

} else {
err = d.syncToTaskQueues2(ctx)
}
activityCtx := workflow.WithActivityOptions(ctx, defaultActivityOptions)
var syncRes deploymentspb.SyncUserDataResponse
err = workflow.ExecuteActivity(activityCtx, d.a.SyncUserData, syncReq).Get(ctx, &syncRes)
if err != nil {
// TODO: if this fails, should we roll back anything?
return nil, err
}
if len(syncRes.TaskQueueMaxVersions) > 0 {
// wait for propagation
err = workflow.ExecuteActivity(activityCtx, d.a.CheckUserDataPropagation, &deploymentspb.CheckUserDataPropagationRequest{
TaskQueueMaxVersions: syncRes.TaskQueueMaxVersions,
}).Get(ctx, nil)
if err != nil {
return nil, err
}
}
}

// apply changes to metadata
Expand All @@ -346,6 +329,89 @@ func (d *DeploymentWorkflowRunner) handleSyncState(ctx workflow.Context, args *d
}, nil
}

func (d *DeploymentWorkflowRunner) syncToTaskQueues1(ctx workflow.Context) error {
syncReq := &deploymentspb.SyncUserDataRequest{
Deployment: d.State.Deployment,
}
for tqName, byType := range d.State.TaskQueueFamilies {
for tqType, data := range byType.TaskQueues {
syncReq.Sync = append(syncReq.Sync, &deploymentspb.SyncUserDataRequest_SyncUserData{
Name: tqName,
Type: enumspb.TaskQueueType(tqType),
Data: d.dataWithTime(data),
})
}
}
activityCtx := workflow.WithActivityOptions(ctx, defaultActivityOptions)
var syncRes deploymentspb.SyncUserDataResponse
err := workflow.ExecuteActivity(activityCtx, d.a.SyncUserData, syncReq).Get(ctx, &syncRes)
if err != nil {
return err
}
if len(syncRes.TaskQueueMaxVersions) > 0 {
// wait for propagation
checkReq := &deploymentspb.CheckUserDataPropagationRequest{TaskQueueMaxVersions: syncRes.TaskQueueMaxVersions}
err = workflow.ExecuteActivity(activityCtx, d.a.CheckUserDataPropagation, checkReq).Get(ctx, nil)
if err != nil {
return err
}
}
return nil
}

func (d *DeploymentWorkflowRunner) syncToTaskQueues2(ctx workflow.Context) error {
const batchSize = 100
type nameDataPair struct {
name string
data *deploymentspb.TaskQueueData
}

activityCtx := workflow.WithActivityOptions(ctx, defaultActivityOptions)
tqNames := workflow.DeterministicKeys(d.State.TaskQueueFamilies)

for _, tqType := range []enumspb.TaskQueueType{
// Sync and wait for workflow task queues, then activity task queues, to ensure that
// workflows don't bounce between deployments due to activity/wft starts.
enumspb.TASK_QUEUE_TYPE_WORKFLOW,
enumspb.TASK_QUEUE_TYPE_ACTIVITY,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We want to support Nexus tasks too. they should be synced with activities I believe.

} {
var toSync []nameDataPair
for _, tqName := range tqNames {
if data := d.State.TaskQueueFamilies[tqName].TaskQueues[int32(tqType)]; data != nil {
toSync = append(toSync, nameDataPair{name: tqName, data: data})
}
}

for batch := range slices.Chunk(toSync, batchSize) {
syncReq := &deploymentspb.SyncUserDataRequest{
Deployment: d.State.Deployment,
Sync: util.MapSlice(batch, func(pair nameDataPair) *deploymentspb.SyncUserDataRequest_SyncUserData {
return &deploymentspb.SyncUserDataRequest_SyncUserData{
Name: pair.name,
Type: tqType,
Data: d.dataWithTime(pair.data),
}
}),
}
var syncRes deploymentspb.SyncUserDataResponse
err := workflow.ExecuteActivity(activityCtx, d.a.SyncUserData, syncReq).Get(ctx, &syncRes)
if err != nil {
return err
}
if len(syncRes.TaskQueueMaxVersions) > 0 {
// wait for propagation
checkReq := &deploymentspb.CheckUserDataPropagationRequest{TaskQueueMaxVersions: syncRes.TaskQueueMaxVersions}
err = workflow.ExecuteActivity(activityCtx, d.a.CheckUserDataPropagation, checkReq).Get(ctx, nil)
if err != nil {
return err
}
}
}
}

return nil
}

func (d *DeploymentWorkflowRunner) dataWithTime(data *deploymentspb.TaskQueueData) *deploymentspb.TaskQueueData {
data = common.CloneProto(data)
data.LastBecameCurrentTime = d.State.LastBecameCurrentTime
Expand Down
Loading