|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Uber Technologies, Inc. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +package updateworkflow |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "time" |
| 30 | + |
| 31 | + commonpb "go.temporal.io/api/common/v1" |
| 32 | + updatepb "go.temporal.io/api/update/v1" |
| 33 | + "go.temporal.io/api/workflowservice/v1" |
| 34 | + |
| 35 | + enumsspb "go.temporal.io/server/api/enums/v1" |
| 36 | + "go.temporal.io/server/api/historyservice/v1" |
| 37 | + "go.temporal.io/server/api/matchingservice/v1" |
| 38 | + "go.temporal.io/server/common/definition" |
| 39 | + "go.temporal.io/server/common/namespace" |
| 40 | + "go.temporal.io/server/service/history/api" |
| 41 | + "go.temporal.io/server/service/history/consts" |
| 42 | + "go.temporal.io/server/service/history/shard" |
| 43 | + "go.temporal.io/server/service/history/workflow" |
| 44 | +) |
| 45 | + |
| 46 | +func Invoke( |
| 47 | + ctx context.Context, |
| 48 | + req *historyservice.UpdateWorkflowExecutionRequest, |
| 49 | + shardCtx shard.Context, |
| 50 | + workflowConsistencyChecker api.WorkflowConsistencyChecker, |
| 51 | + matchingClient matchingservice.MatchingServiceClient, |
| 52 | +) (_ *historyservice.UpdateWorkflowExecutionResponse, retErr error) { |
| 53 | + |
| 54 | + weCtx, err := workflowConsistencyChecker.GetWorkflowContext( |
| 55 | + ctx, |
| 56 | + nil, |
| 57 | + api.BypassMutableStateConsistencyPredicate, |
| 58 | + definition.NewWorkflowKey( |
| 59 | + req.NamespaceId, |
| 60 | + req.Request.WorkflowExecution.WorkflowId, |
| 61 | + req.Request.WorkflowExecution.RunId, |
| 62 | + ), |
| 63 | + ) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + defer func() { weCtx.GetReleaseFn()(retErr) }() |
| 68 | + |
| 69 | + ms := weCtx.GetMutableState() |
| 70 | + if !ms.IsWorkflowExecutionRunning() { |
| 71 | + return nil, consts.ErrWorkflowCompleted |
| 72 | + } |
| 73 | + |
| 74 | + if req.GetRequest().GetFirstExecutionRunId() != "" && ms.GetExecutionInfo().GetFirstExecutionRunId() != req.GetRequest().GetFirstExecutionRunId() { |
| 75 | + return nil, consts.ErrWorkflowExecutionNotFound |
| 76 | + } |
| 77 | + |
| 78 | + upd, duplicate, removeFn := ms.UpdateRegistry().Add(req.GetRequest().GetRequest()) |
| 79 | + if removeFn != nil { |
| 80 | + defer removeFn() |
| 81 | + } |
| 82 | + |
| 83 | + // If WT is scheduled, but not started, updates will be attached to it, when WT is started. |
| 84 | + // If WT has already started, new speculative WT will be created when started WT completes. |
| 85 | + // If update is duplicate, then WT for this update was already created. |
| 86 | + createNewWorkflowTask := !ms.HasPendingWorkflowTask() && duplicate == false |
| 87 | + |
| 88 | + if createNewWorkflowTask { |
| 89 | + // This will try not to add an event but will create speculative WT in mutable state. |
| 90 | + // Task generation will be skipped if WT is created as speculative. |
| 91 | + wt, err := ms.AddWorkflowTaskScheduledEvent(false, enumsspb.WORKFLOW_TASK_TYPE_SPECULATIVE) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + if wt.Type != enumsspb.WORKFLOW_TASK_TYPE_SPECULATIVE { |
| 96 | + // This should never happen because WT is created as normal (despite speculative is requested) |
| 97 | + // only if there were buffered events and because there were no pending WT, there can't be buffered events. |
| 98 | + return nil, consts.ErrWorkflowTaskStateInconsistent |
| 99 | + } |
| 100 | + |
| 101 | + // It is important to release workflow lock before calling matching. |
| 102 | + weCtx.GetReleaseFn()(nil) |
| 103 | + err = addWorkflowTaskToMatching(ctx, shardCtx, ms, matchingClient, wt, namespace.ID(req.GetNamespaceId())) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + } else { |
| 108 | + weCtx.GetReleaseFn()(nil) |
| 109 | + } |
| 110 | + |
| 111 | + updOutcome, err := upd.WaitOutcome(ctx) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + resp := &historyservice.UpdateWorkflowExecutionResponse{ |
| 116 | + Response: &workflowservice.UpdateWorkflowExecutionResponse{ |
| 117 | + UpdateRef: &updatepb.UpdateRef{ |
| 118 | + WorkflowExecution: &commonpb.WorkflowExecution{ |
| 119 | + WorkflowId: weCtx.GetWorkflowKey().WorkflowID, |
| 120 | + RunId: weCtx.GetWorkflowKey().RunID, |
| 121 | + }, |
| 122 | + UpdateId: req.GetRequest().GetRequest().GetMeta().GetUpdateId(), |
| 123 | + }, |
| 124 | + Outcome: updOutcome, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + return resp, nil |
| 129 | +} |
| 130 | + |
| 131 | +// TODO (alex-update): Consider moving this func to a better place. |
| 132 | +func addWorkflowTaskToMatching( |
| 133 | + ctx context.Context, |
| 134 | + shardCtx shard.Context, |
| 135 | + ms workflow.MutableState, |
| 136 | + matchingClient matchingservice.MatchingServiceClient, |
| 137 | + task *workflow.WorkflowTaskInfo, |
| 138 | + nsID namespace.ID, |
| 139 | +) error { |
| 140 | + // TODO (alex-update): Timeout calculation is copied from somewhere else. Extract func instead? |
| 141 | + var taskScheduleToStartTimeout *time.Duration |
| 142 | + if ms.TaskQueue().GetName() != task.TaskQueue.GetName() { |
| 143 | + taskScheduleToStartTimeout = ms.GetExecutionInfo().StickyScheduleToStartTimeout |
| 144 | + } else { |
| 145 | + taskScheduleToStartTimeout = ms.GetExecutionInfo().WorkflowRunTimeout |
| 146 | + } |
| 147 | + |
| 148 | + wfKey := ms.GetWorkflowKey() |
| 149 | + clock, err := shardCtx.NewVectorClock() |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + _, err = matchingClient.AddWorkflowTask(ctx, &matchingservice.AddWorkflowTaskRequest{ |
| 155 | + NamespaceId: nsID.String(), |
| 156 | + Execution: &commonpb.WorkflowExecution{ |
| 157 | + WorkflowId: wfKey.WorkflowID, |
| 158 | + RunId: wfKey.RunID, |
| 159 | + }, |
| 160 | + TaskQueue: task.TaskQueue, |
| 161 | + ScheduledEventId: task.ScheduledEventID, |
| 162 | + ScheduleToStartTimeout: taskScheduleToStartTimeout, |
| 163 | + Clock: clock, |
| 164 | + }) |
| 165 | + if err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments