Files
f97a858310 Callback for workflow update support (#9614)
## What changed?
Added support for Nexus workflow update completion callbacks via CHASM.
This allows a Nexus caller to be notified when a workflow update
completes by attaching completion callbacks to the update request.

## Why?

Nexus operations that target workflow updates need a way to receive
completion notifications. Without this, a Nexus caller that sends an
update has no async mechanism to learn when the update finishes.
Completion callbacks enable the same async notification pattern that
already exists for workflow-level Nexus operations.

## How did you test it?
- [ ] built
- [x] run locally and tested manually
- [ ] covered by existing tests
- [x] added new unit test(s)
- [x] added new functional test(s)

## Potential risks
Touches speculative workflow updates, they are always hard to reason
about. Tried to compensate with lots of test coverage.

Note: Needs this API PR
https://github.com/temporalio/api/pull/742/changes

---

> [!NOTE]
> **High Risk**
> Touches workflow update state machine and mutable state event handling
to persist/trigger per-update callbacks, including close/retry/reset
paths, which is complex and can affect correctness of update outcomes
and callback delivery.
> 
> **Overview**
> Adds **workflow update completion callbacks** via CHASM so Nexus
callers can register callbacks on `UpdateWorkflowExecution` and have
them fired on update completion or workflow close.
> 
> This introduces a `WorkflowUpdate` CHASM component with new
`UpdateState` protobuf (including persisted `rejection_failure`), stores
update callbacks under `Workflow.Updates`, and extends callback
processing to handle *update-level* callbacks on update completion,
rejection (including reset/reapply), and on run transitions
(retry/timeout/continue-as-new) where update callbacks must fire even if
workflow-level callbacks are inherited.
> 
> It also adds dynamic config gates/limits
(`EnableWorkflowUpdateCallbacks`, `MaxCallbacksPerUpdateID`), updates
`DescribeWorkflow` to surface update callback triggers, extends mutable
state/history builder APIs to carry per-update callback options in
`WorkflowExecutionOptionsUpdated`, and adds `Update.AttachCallbacks`
logic to persist/flush callbacks (including buffering while `stateSent`,
request-id dedup, and stricter validation requiring `request_id` when
callbacks are present).
> 
> <sup>Reviewed by [Cursor Bugbot]() for commit
4484fee104d73344ef5370c468008440c7df1c6a. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>

---------

Co-authored-by: long-nt-tran <[email protected]>
2026-05-19 18:31:15 -04:00

363 lines
13 KiB
Go

package updateworkflow
import (
"context"
"time"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
updatepb "go.temporal.io/api/update/v1"
"go.temporal.io/api/workflowservice/v1"
enumsspb "go.temporal.io/server/api/enums/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/api/matchingservice/v1"
taskqueuespb "go.temporal.io/server/api/taskqueue/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/effect"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/namespace"
serviceerrors "go.temporal.io/server/common/serviceerror"
"go.temporal.io/server/common/worker_versioning"
"go.temporal.io/server/service/history/api"
"go.temporal.io/server/service/history/consts"
historyi "go.temporal.io/server/service/history/interfaces"
"go.temporal.io/server/service/history/workflow"
"go.temporal.io/server/service/history/workflow/update"
"google.golang.org/protobuf/types/known/durationpb"
)
const (
// Fail update fast if workflow task keeps failing (attempt >= 3).
failUpdateWorkflowTaskAttemptCount = 3
)
type Updater struct {
shardCtx historyi.ShardContext
workflowConsistencyChecker api.WorkflowConsistencyChecker
matchingClient matchingservice.MatchingServiceClient
req *historyservice.UpdateWorkflowExecutionRequest
namespaceID namespace.ID
wfKey definition.WorkflowKey
upd *update.Update
directive *taskqueuespb.TaskVersionDirective
// Variables referencing mutable state data.
// WARNING: any references to mutable state data *have to* be copied
// to avoid data races when used outside the workflow lease.
taskQueue *taskqueuepb.TaskQueue
priority *commonpb.Priority
normalTaskQueueName string
scheduledEventID int64
scheduleToStartTimeout time.Duration
workflowTaskStamp int32
}
func NewUpdater(
shardCtx historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
matchingClient matchingservice.MatchingServiceClient,
request *historyservice.UpdateWorkflowExecutionRequest,
) *Updater {
return &Updater{
shardCtx: shardCtx,
workflowConsistencyChecker: workflowConsistencyChecker,
matchingClient: matchingClient,
req: request,
namespaceID: namespace.ID(request.GetNamespaceId()),
}
}
func (u *Updater) Invoke(
ctx context.Context,
) (*historyservice.UpdateWorkflowExecutionResponse, error) {
wfKey := definition.NewWorkflowKey(
u.req.NamespaceId,
u.req.Request.WorkflowExecution.WorkflowId,
u.req.Request.WorkflowExecution.RunId,
)
err := api.GetAndUpdateWorkflowWithNew(
ctx,
nil,
wfKey,
func(lease api.WorkflowLease) (*api.UpdateWorkflowAction, error) {
ms := lease.GetMutableState()
updateReg := lease.GetContext().UpdateRegistry(ctx)
return u.ApplyRequest(ctx, updateReg, ms)
},
nil,
u.shardCtx,
u.workflowConsistencyChecker,
)
if err != nil {
return nil, err
}
return u.OnSuccess(ctx)
}
func (u *Updater) ApplyRequest(
ctx context.Context,
updateReg update.Registry,
ms historyi.MutableState,
) (*api.UpdateWorkflowAction, error) {
if u.req.GetRequest().GetFirstExecutionRunId() != "" &&
ms.GetExecutionInfo().GetFirstExecutionRunId() != u.req.GetRequest().GetFirstExecutionRunId() {
return nil, consts.ErrWorkflowExecutionNotFound
}
u.wfKey = ms.GetWorkflowKey()
updateRequest := u.req.GetRequest().GetRequest()
updateID := updateRequest.GetMeta().GetUpdateId()
if !ms.IsWorkflowExecutionRunning() {
// If the WF is not running anymore, use an existing Update, if it exists for the requested ID.
// This ensures that repeated Update requests with the same ID see the same result.
if u.upd = updateReg.Find(ctx, updateID); u.upd != nil {
return &api.UpdateWorkflowAction{Noop: true}, nil
}
return nil, consts.ErrWorkflowCompleted
}
// We don't accept the request to update the workflow if the workflow is paused.
if ms.IsWorkflowExecutionStatusPaused() {
return nil, serviceerror.NewFailedPrecondition("Workflow is paused. Cannot update the workflow.")
}
if ms.GetExecutionInfo().WorkflowTaskAttempt >= failUpdateWorkflowTaskAttemptCount {
// If workflow task is constantly failing, the update to that workflow will also fail.
// Additionally, workflow update can't "fix" workflow state because updates (delivered with messages)
// are applied after events.
// Failing API call fast here to prevent wasting resources for an update that will fail.
u.shardCtx.GetLogger().Info("Fail update fast due to WorkflowTask in failed state.",
tag.WorkflowNamespace(u.req.Request.Namespace),
tag.WorkflowNamespaceID(u.wfKey.NamespaceID),
tag.WorkflowID(u.wfKey.WorkflowID),
tag.WorkflowRunID(u.wfKey.RunID))
return nil, serviceerror.NewWorkflowNotReady("Unable to perform workflow execution update due to Workflow Task in failed state.")
}
// If workflow attempted to close itself on previous WFT completion,
// and has another WFT running, which most likely will try to complete workflow again,
// then don't admit new updates.
if ms.IsWorkflowCloseAttempted() && ms.HasStartedWorkflowTask() {
u.shardCtx.GetLogger().Info("Fail update because workflow is closing.",
tag.WorkflowNamespace(u.req.Request.Namespace),
tag.WorkflowNamespaceID(u.wfKey.NamespaceID),
tag.WorkflowID(u.wfKey.WorkflowID),
tag.WorkflowRunID(u.wfKey.RunID))
return nil, consts.ErrWorkflowClosing
}
var (
alreadyExisted bool
err error
)
if u.upd, alreadyExisted, err = updateReg.FindOrCreate(ctx, updateID); err != nil {
return nil, err
}
if err = u.upd.Admit(updateRequest, workflow.WithEffects(effect.Immediate(ctx), ms)); err != nil {
return nil, err
}
callbacksAttached, err := u.upd.AttachCallbacks(updateRequest, workflow.WithEffects(effect.Immediate(ctx), ms))
if err != nil {
return nil, err
}
if callbacksAttached {
return &api.UpdateWorkflowAction{
Noop: false,
CreateWorkflowTask: false,
}, nil
}
// If WT is scheduled, but not started, updates will be attached to it, when WT is started.
// If WT has already started, new speculative WT will be created when started WT completes.
// If update is duplicate, then WT for this update was already created.
if alreadyExisted || ms.HasPendingWorkflowTask() {
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
// This will try not to add an event but will create speculative WT in mutable state.
newWorkflowTask, err := ms.AddWorkflowTaskScheduledEvent(false, enumsspb.WORKFLOW_TASK_TYPE_SPECULATIVE)
if err != nil {
return nil, err
}
if newWorkflowTask.Type != enumsspb.WORKFLOW_TASK_TYPE_SPECULATIVE {
// This means that a normal WFT was created despite a speculative WFT having been requested. It implies that
// there were buffered events. But because there was no pending WFT, there can't be buffered events. Therefore
// this should never happen.
return nil, consts.ErrWorkflowTaskStateInconsistent
}
u.scheduledEventID = newWorkflowTask.ScheduledEventID
u.workflowTaskStamp = newWorkflowTask.Stamp
if _, scheduleToStartTimeoutPtr := ms.TaskQueueScheduleToStartTimeout(ms.CurrentTaskQueue().Name); scheduleToStartTimeoutPtr != nil {
u.scheduleToStartTimeout = scheduleToStartTimeoutPtr.AsDuration()
}
u.taskQueue = common.CloneProto(newWorkflowTask.TaskQueue)
u.priority = common.CloneProto(ms.GetExecutionInfo().Priority)
u.normalTaskQueueName = ms.GetExecutionInfo().TaskQueue
u.directive = worker_versioning.MakeDirectiveForWorkflowTask(
ms.GetInheritedBuildId(),
ms.GetAssignedBuildId(),
ms.GetMostRecentWorkerVersionStamp(),
ms.HasCompletedAnyWorkflowTask(),
ms.GetEffectiveVersioningBehavior(),
ms.GetEffectiveDeployment(),
ms.GetVersioningRevisionNumber(),
ms.GetShouldUseRampingVersion(),
)
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
func (u *Updater) OnSuccess(
ctx context.Context,
) (*historyservice.UpdateWorkflowExecutionResponse, error) {
usesSpeculativeWFT := u.scheduledEventID != common.EmptyEventID
if usesSpeculativeWFT {
// Speculative WFT was created and needs to be added directly to matching w/o transfer task.
// TODO (alex): This code is copied from transferQueueActiveTaskExecutor.processWorkflowTask.
// Helper function needs to be extracted to avoid code duplication.
err := u.addWorkflowTaskToMatching(ctx)
if _, isStickyWorkerUnavailable := err.(*serviceerrors.StickyWorkerUnavailable); isStickyWorkerUnavailable {
// If sticky worker is unavailable, switch to original normal task queue.
u.taskQueue = &taskqueuepb.TaskQueue{
Name: u.normalTaskQueueName,
Kind: enumspb.TASK_QUEUE_KIND_NORMAL,
}
err = u.addWorkflowTaskToMatching(ctx)
}
if err != nil {
u.shardCtx.GetLogger().Warn("Unable to add WorkflowTask directly to matching.",
tag.WorkflowNamespace(u.req.Request.Namespace),
tag.WorkflowNamespaceID(u.wfKey.NamespaceID),
tag.WorkflowID(u.wfKey.WorkflowID),
tag.WorkflowRunID(u.wfKey.RunID),
tag.Error(err))
// Intentionally just log error here and don't return it to the client.
// If adding speculative WT to matching failed with error,
// this error can't be handled outside of WF lock and can't be returned to the client (because it is not a client error).
// This speculative WT will be timed out in tasks.SpeculativeWorkflowTaskScheduleToStartTimeout (5) seconds,
// and new normal WT will be scheduled.
// If subsequent attempt succeeds within current context timeout, caller of this API will get a valid response.
err = nil
}
}
namespaceID := namespace.ID(u.req.GetNamespaceId())
ns, err := u.shardCtx.GetNamespaceRegistry().GetNamespaceByID(namespaceID)
if err != nil {
return nil, err
}
serverTimeout := u.shardCtx.GetConfig().LongPollExpirationInterval(ns.Name().String())
waitStage := u.req.GetRequest().GetWaitPolicy().GetLifecycleStage()
// If the long-poll times out due to serverTimeout then return a non-error empty response.
status, err := u.upd.WaitLifecycleStage(ctx, waitStage, serverTimeout)
if err != nil {
return nil, err
}
resp := u.CreateResponse(u.wfKey, status.Outcome, status.Stage)
// Attach a link to the response. For accepted/completed updates, use a WorkflowEvent link
// with a RequestIdReference pointing to the accepted event. For rejected updates (stage
// COMPLETED with a failure outcome and no acceptance), use a Workflow link since rejected
// updates don't write any event to history.
requestID := u.req.GetRequest().GetRequest().GetRequestId()
if status.Outcome.GetFailure() != nil && status.Stage == enumspb.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_COMPLETED {
// Rejected update: no event in history, link to the workflow itself.
resp.Response.Link = &commonpb.Link{
Variant: &commonpb.Link_Workflow_{
Workflow: &commonpb.Link_Workflow{
Namespace: u.req.Request.Namespace,
WorkflowId: u.wfKey.WorkflowID,
RunId: u.wfKey.RunID,
Reason: "Update rejected",
},
},
}
} else if status.Stage == enumspb.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ACCEPTED || status.Stage == enumspb.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_COMPLETED {
// Accepted or completed update: link to the accepted event.
resp.Response.Link = &commonpb.Link{
Variant: &commonpb.Link_WorkflowEvent_{
WorkflowEvent: &commonpb.Link_WorkflowEvent{
Namespace: u.req.Request.Namespace,
WorkflowId: u.wfKey.WorkflowID,
RunId: u.wfKey.RunID,
Reference: &commonpb.Link_WorkflowEvent_RequestIdRef{
RequestIdRef: &commonpb.Link_WorkflowEvent_RequestIdReference{
RequestId: requestID,
EventType: enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_UPDATE_ACCEPTED,
},
},
},
},
}
}
return resp, nil
}
// TODO (alex-update): Consider moving this func to a better place.
func (u *Updater) addWorkflowTaskToMatching(ctx context.Context) error {
clock, err := u.shardCtx.NewVectorClock()
if err != nil {
return err
}
_, err = u.matchingClient.AddWorkflowTask(ctx, &matchingservice.AddWorkflowTaskRequest{
NamespaceId: u.namespaceID.String(),
Execution: &commonpb.WorkflowExecution{
WorkflowId: u.wfKey.WorkflowID,
RunId: u.wfKey.RunID,
},
TaskQueue: u.taskQueue,
ScheduledEventId: u.scheduledEventID,
ScheduleToStartTimeout: durationpb.New(u.scheduleToStartTimeout),
Clock: clock,
VersionDirective: u.directive,
Priority: u.priority,
Stamp: u.workflowTaskStamp,
})
if err != nil {
return err
}
return nil
}
func (u *Updater) CreateResponse(
wfKey definition.WorkflowKey,
outcome *updatepb.Outcome,
stage enumspb.UpdateWorkflowExecutionLifecycleStage,
) *historyservice.UpdateWorkflowExecutionResponse {
return &historyservice.UpdateWorkflowExecutionResponse{
Response: &workflowservice.UpdateWorkflowExecutionResponse{
UpdateRef: &updatepb.UpdateRef{
WorkflowExecution: &commonpb.WorkflowExecution{
WorkflowId: wfKey.WorkflowID,
RunId: wfKey.RunID,
},
UpdateId: u.req.GetRequest().GetRequest().GetMeta().GetUpdateId(),
},
Outcome: outcome,
Stage: stage,
},
}
}