-
Notifications
You must be signed in to change notification settings - Fork 805
/
workflow.go
321 lines (267 loc) · 9 KB
/
workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination workflow_mock.go -self_package github.com/uber/cadence/service/history/execution
package execution
import (
"context"
"fmt"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)
const (
// IdentityHistoryService is the service role identity
IdentityHistoryService = "history-service"
// WorkflowTerminationIdentity is the component which decides to terminate the workflow
WorkflowTerminationIdentity = "worker-service"
// WorkflowTerminationReason is the reason for terminating workflow due to version conflict
WorkflowTerminationReason = "Terminate Workflow Due To Version Conflict."
)
type (
// Workflow is the interface for NDC workflow
Workflow interface {
GetContext() Context
GetMutableState() MutableState
GetReleaseFn() ReleaseFunc
GetVectorClock() (int64, int64, error)
HappensAfter(that Workflow) (bool, error)
Revive() error
SuppressBy(incomingWorkflow Workflow) (TransactionPolicy, error)
FlushBufferedEvents() error
}
workflowImpl struct {
clusterMetadata cluster.Metadata
ctx context.Context
context Context
mutableState MutableState
releaseFn ReleaseFunc
logger log.Logger
metricsClient metrics.Client
}
)
// NewWorkflow creates a new NDC workflow
func NewWorkflow(
ctx context.Context,
clusterMetadata cluster.Metadata,
context Context,
mutableState MutableState,
releaseFn ReleaseFunc,
logger log.Logger,
metricsClient metrics.Client,
) Workflow {
return &workflowImpl{
ctx: ctx,
clusterMetadata: clusterMetadata,
context: context,
mutableState: mutableState,
releaseFn: releaseFn,
logger: logger,
metricsClient: metricsClient,
}
}
func (r *workflowImpl) GetContext() Context {
return r.context
}
func (r *workflowImpl) GetMutableState() MutableState {
return r.mutableState
}
func (r *workflowImpl) GetReleaseFn() ReleaseFunc {
return r.releaseFn
}
func (r *workflowImpl) GetVectorClock() (int64, int64, error) {
lastWriteVersion, err := r.mutableState.GetLastWriteVersion()
if err != nil {
return 0, 0, err
}
lastEventTaskID := r.mutableState.GetExecutionInfo().LastEventTaskID
return lastWriteVersion, lastEventTaskID, nil
}
func (r *workflowImpl) HappensAfter(
that Workflow,
) (bool, error) {
thisLastWriteVersion, thisLastEventTaskID, err := r.GetVectorClock()
if err != nil {
return false, err
}
thatLastWriteVersion, thatLastEventTaskID, err := that.GetVectorClock()
if err != nil {
return false, err
}
return workflowHappensAfter(
thisLastWriteVersion,
thisLastEventTaskID,
thatLastWriteVersion,
thatLastEventTaskID,
), nil
}
func (r *workflowImpl) Revive() error {
state, _ := r.mutableState.GetWorkflowStateCloseStatus()
if state != persistence.WorkflowStateZombie {
return nil
} else if state == persistence.WorkflowStateCompleted {
// workflow already finished
return nil
}
// workflow is in zombie state, need to set the state correctly accordingly
state = persistence.WorkflowStateCreated
if r.mutableState.HasProcessedOrPendingDecision() {
state = persistence.WorkflowStateRunning
}
return r.mutableState.UpdateWorkflowStateCloseStatus(
state,
persistence.WorkflowCloseStatusNone,
)
}
func (r *workflowImpl) SuppressBy(
incomingWorkflow Workflow,
) (TransactionPolicy, error) {
// NOTE: READ BEFORE MODIFICATION
//
// if the workflow to be suppressed has last write version being local active
// then use active logic to terminate this workflow
// if the workflow to be suppressed has last write version being remote active
// then turn this workflow into a zombie
lastWriteVersion, lastEventTaskID, err := r.GetVectorClock()
if err != nil {
return TransactionPolicyActive, err
}
incomingLastWriteVersion, incomingLastEventTaskID, err := incomingWorkflow.GetVectorClock()
if err != nil {
return TransactionPolicyActive, err
}
if workflowHappensAfter(
lastWriteVersion,
lastEventTaskID,
incomingLastWriteVersion,
incomingLastEventTaskID) {
return TransactionPolicyActive, &types.InternalServiceError{
Message: "nDCWorkflow cannot suppress workflow by older workflow",
}
}
// if workflow is in zombie or finished state, keep as is
if !r.mutableState.IsWorkflowExecutionRunning() {
return TransactionPolicyPassive, nil
}
lastWriteCluster, err := r.clusterMetadata.ClusterNameForFailoverVersion(lastWriteVersion)
if err != nil {
return TransactionPolicyActive, err
}
currentCluster := r.clusterMetadata.GetCurrentClusterName()
if currentCluster == lastWriteCluster {
return TransactionPolicyActive, r.terminateWorkflow(lastWriteVersion, incomingLastWriteVersion, r.logger, r.metricsClient)
}
return TransactionPolicyPassive, r.zombiefyWorkflow()
}
func (r *workflowImpl) FlushBufferedEvents() error {
if !r.mutableState.IsWorkflowExecutionRunning() {
return nil
}
if !r.mutableState.HasBufferedEvents() {
return nil
}
lastWriteVersion, _, err := r.GetVectorClock()
if err != nil {
return err
}
lastWriteCluster, err := r.clusterMetadata.ClusterNameForFailoverVersion(lastWriteVersion)
if err != nil {
return err
}
currentCluster := r.clusterMetadata.GetCurrentClusterName()
if lastWriteCluster != currentCluster {
return &types.InternalServiceError{
Message: "nDCWorkflow encounter workflow with buffered events but last write not from current cluster",
}
}
return r.failDecision(lastWriteVersion, true)
}
func (r *workflowImpl) failDecision(
lastWriteVersion int64,
scheduleNewDecision bool,
) error {
// do not persist the change right now, NDC requires transaction
if err := r.mutableState.UpdateCurrentVersion(lastWriteVersion, true); err != nil {
return err
}
decision, ok := r.mutableState.GetInFlightDecision()
if !ok {
return nil
}
if err := FailDecision(r.mutableState, decision, types.DecisionTaskFailedCauseFailoverCloseDecision); err != nil {
return err
}
if scheduleNewDecision {
return ScheduleDecision(r.mutableState)
}
return nil
}
func (r *workflowImpl) terminateWorkflow(
lastWriteVersion int64,
incomingLastWriteVersion int64,
logger log.Logger,
metricsClient metrics.Client,
) error {
eventBatchFirstEventID := r.GetMutableState().GetNextEventID()
if err := r.failDecision(lastWriteVersion, false); err != nil {
return err
}
// do not persist the change right now, NDC requires transaction
if err := r.mutableState.UpdateCurrentVersion(lastWriteVersion, true); err != nil {
return err
}
_, err := r.mutableState.AddWorkflowExecutionTerminatedEvent(
eventBatchFirstEventID,
WorkflowTerminationReason,
[]byte(fmt.Sprintf("terminated by version: %v", incomingLastWriteVersion)),
WorkflowTerminationIdentity,
)
domainName := r.mutableState.GetDomainEntry().GetInfo().Name
logger.Info(
"Workflow execution terminated.",
tag.WorkflowDomainName(domainName),
tag.WorkflowID(r.mutableState.GetExecutionInfo().WorkflowID),
tag.WorkflowRunID(r.mutableState.GetExecutionInfo().RunID),
tag.WorkflowTerminationReason(WorkflowTerminationReason),
)
scopeWithDomainTag := metricsClient.Scope(metrics.HistoryTerminateWorkflowExecutionScope).Tagged(metrics.DomainTag(domainName))
scopeWithDomainTag.IncCounter(metrics.WorkflowTerminateCounterPerDomain)
return err
}
func (r *workflowImpl) zombiefyWorkflow() error {
return r.mutableState.GetExecutionInfo().UpdateWorkflowStateCloseStatus(
persistence.WorkflowStateZombie,
persistence.WorkflowCloseStatusNone,
)
}
func workflowHappensAfter(
thisLastWriteVersion int64,
thisLastEventTaskID int64,
thatLastWriteVersion int64,
thatLastEventTaskID int64,
) bool {
if thisLastWriteVersion != thatLastWriteVersion {
return thisLastWriteVersion > thatLastWriteVersion
}
// thisLastWriteVersion == thatLastWriteVersion
return thisLastEventTaskID > thatLastEventTaskID
}