forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset_workflow_test.go
432 lines (377 loc) · 15 KB
/
reset_workflow_test.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright (c) 2017-2021 Uber Technologies Inc.
// Portions of the Software are attributed to Copyright (c) 2020 Temporal 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.
package host
import (
"bytes"
"encoding/binary"
"strconv"
"github.com/pborman/uuid"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/types"
)
func (s *IntegrationSuite) TestResetWorkflow() {
id := "integration-reset-workflow-test"
wt := "integration-reset-workflow-test-type"
tl := "integration-reset-workflow-test-taskqueue"
identity := "worker1"
workflowType := &types.WorkflowType{Name: wt}
tasklist := &types.TaskList{Name: tl}
// Start workflow execution
request := &types.StartWorkflowExecutionRequest{
RequestID: uuid.New(),
Domain: s.domainName,
WorkflowID: id,
WorkflowType: workflowType,
TaskList: tasklist,
Input: nil,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(100),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(2),
Identity: identity,
}
we, err0 := s.engine.StartWorkflowExecution(createContext(), request)
s.NoError(err0)
s.Logger.Info("StartWorkflowExecution", tag.WorkflowRunID(we.GetRunID()))
// workflow logic
workflowComplete := false
activityData := int32(1)
activityCount := 3
isFirstTaskProcessed := false
isSecondTaskProcessed := false
var firstActivityCompletionEvent *types.HistoryEvent
wtHandler := func(execution *types.WorkflowExecution, wt *types.WorkflowType,
previousStartedEventID, startedEventID int64, history *types.History) ([]byte, []*types.Decision, error) {
if !isFirstTaskProcessed {
// Schedule 3 activities on first workflow task
isFirstTaskProcessed = true
buf := new(bytes.Buffer)
s.Nil(binary.Write(buf, binary.LittleEndian, activityData))
var scheduleActivityCommands []*types.Decision
for i := 1; i <= activityCount; i++ {
scheduleActivityCommands = append(scheduleActivityCommands, &types.Decision{
DecisionType: types.DecisionTypeScheduleActivityTask.Ptr(),
ScheduleActivityTaskDecisionAttributes: &types.ScheduleActivityTaskDecisionAttributes{
ActivityID: strconv.Itoa(i),
ActivityType: &types.ActivityType{Name: "ResetActivity"},
TaskList: tasklist,
Input: buf.Bytes(),
ScheduleToCloseTimeoutSeconds: common.Int32Ptr(100),
ScheduleToStartTimeoutSeconds: common.Int32Ptr(100),
StartToCloseTimeoutSeconds: common.Int32Ptr(50),
HeartbeatTimeoutSeconds: common.Int32Ptr(5),
},
})
}
return nil, scheduleActivityCommands, nil
} else if !isSecondTaskProcessed {
// Confirm one activity completion on second workflow task
isSecondTaskProcessed = true
for _, event := range history.Events[previousStartedEventID:] {
if event.GetEventType() == types.EventTypeActivityTaskCompleted {
firstActivityCompletionEvent = event
return nil, []*types.Decision{}, nil
}
}
}
// Complete workflow after reset
workflowComplete = true
return nil, []*types.Decision{{
DecisionType: types.DecisionTypeCompleteWorkflowExecution.Ptr(),
CompleteWorkflowExecutionDecisionAttributes: &types.CompleteWorkflowExecutionDecisionAttributes{
Result: []byte("Done."),
},
}}, nil
}
// activity handler
atHandler := func(execution *types.WorkflowExecution, activityType *types.ActivityType,
ActivityID string, input []byte, taskToken []byte) ([]byte, bool, error) {
return []byte("Activity Result."), false, nil
}
poller := &TaskPoller{
Engine: s.engine,
Domain: s.domainName,
TaskList: tasklist,
Identity: identity,
DecisionHandler: wtHandler,
ActivityHandler: atHandler,
Logger: s.Logger,
T: s.T(),
}
// Process first workflow decision task to schedule activities
_, err := poller.PollAndProcessDecisionTask(false, false)
s.Logger.Info("PollAndProcessWorkflowTask", tag.Error(err))
s.NoError(err)
// Process one activity task which also creates second workflow task
err = poller.PollAndProcessActivityTask(false)
s.Logger.Info("Poll and process first activity", tag.Error(err))
s.NoError(err)
// Process second workflow task which checks activity completion
_, err = poller.PollAndProcessDecisionTask(false, false)
s.Logger.Info("Poll and process second workflow task", tag.Error(err))
s.NoError(err)
// Find reset point (last completed decision task)
events := s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: we.GetRunID(),
})
var lastDecisionCompleted *types.HistoryEvent
for _, event := range events {
if event.GetEventType() == types.EventTypeDecisionTaskCompleted {
lastDecisionCompleted = event
}
}
// FIRST reset: Reset workflow execution, current is open
resp, err := s.engine.ResetWorkflowExecution(createContext(), &types.ResetWorkflowExecutionRequest{
Domain: s.domainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: id,
RunID: we.RunID,
},
Reason: "reset execution from test",
DecisionFinishEventID: lastDecisionCompleted.ID,
RequestID: uuid.New(),
})
s.NoError(err)
err = poller.PollAndProcessActivityTask(false)
s.Logger.Info("Poll and process second activity", tag.Error(err))
s.NoError(err)
err = poller.PollAndProcessActivityTask(false)
s.Logger.Info("Poll and process third activity", tag.Error(err))
s.NoError(err)
s.NotNil(firstActivityCompletionEvent)
s.False(workflowComplete)
// get the history of the first run again
events = s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: we.GetRunID(),
})
var firstRunStartTimestamp int64
var lastEvent *types.HistoryEvent
for _, event := range events {
if event.GetEventType() == types.EventTypeWorkflowExecutionStarted {
firstRunStartTimestamp = event.GetTimestamp()
}
if event.GetEventType() == types.EventTypeDecisionTaskCompleted {
lastDecisionCompleted = event
}
lastEvent = event
}
// assert the first run is closed, terminated by the previous reset
s.Equal(types.EventTypeWorkflowExecutionTerminated, lastEvent.GetEventType())
// check the start time of mutable state for the second run,
// it should be reset time although the start event is reused
descResp, err := s.engine.DescribeWorkflowExecution(createContext(), &types.DescribeWorkflowExecutionRequest{
Domain: s.domainName,
Execution: &types.WorkflowExecution{
WorkflowID: id,
RunID: resp.GetRunID(),
},
})
s.NoError(err)
s.True(descResp.WorkflowExecutionInfo.GetStartTime() > firstRunStartTimestamp)
// SECOND reset: reset the first run again, to exercise the code path of resetting closed workflow
resp, err = s.engine.ResetWorkflowExecution(createContext(), &types.ResetWorkflowExecutionRequest{
Domain: s.domainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: id,
RunID: we.GetRunID(),
},
Reason: "reset execution from test",
DecisionFinishEventID: lastDecisionCompleted.ID,
RequestID: uuid.New(),
})
s.NoError(err)
newRunID := resp.GetRunID()
_, err = poller.PollAndProcessDecisionTask(false, false)
s.Logger.Info("Poll and process final decision task", tag.Error(err))
s.NoError(err)
s.True(workflowComplete)
// get the history of the newRunID
events = s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: newRunID,
})
for _, event := range events {
if event.GetEventType() == types.EventTypeDecisionTaskCompleted {
lastDecisionCompleted = event
}
lastEvent = event
}
// assert the new run is closed, completed by decision task
s.Equal(types.EventTypeWorkflowExecutionCompleted, lastEvent.GetEventType())
// THIRD reset: reset the workflow run that is after a reset
_, err = s.engine.ResetWorkflowExecution(createContext(), &types.ResetWorkflowExecutionRequest{
Domain: s.domainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: id,
RunID: newRunID,
},
Reason: "reset execution from test",
DecisionFinishEventID: lastDecisionCompleted.ID,
RequestID: uuid.New(),
})
s.NoError(err)
}
func (s *IntegrationSuite) TestResetWorkflow_NoDecisionTaskCompleted() {
id := "integration-reset-workflow-test-no-decision-completed"
wt := "integration-reset-workflow-test-type--no-decision-completed"
tl := "integration-reset-workflow-test-taskqueue-no-decision-completed"
identity := "worker1"
workflowType := &types.WorkflowType{Name: wt}
tasklist := &types.TaskList{Name: tl}
// Start workflow execution
request := &types.StartWorkflowExecutionRequest{
RequestID: uuid.New(),
Domain: s.domainName,
WorkflowID: id,
WorkflowType: workflowType,
TaskList: tasklist,
Input: nil,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(100),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(2),
Identity: identity,
}
we, err0 := s.engine.StartWorkflowExecution(createContext(), request)
s.NoError(err0)
// Find reset point (last completed decision task)
events := s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: we.GetRunID(),
})
var lastDecisionScheduled *types.HistoryEvent
for _, event := range events {
if event.GetEventType() == types.EventTypeDecisionTaskScheduled {
lastDecisionScheduled = event
}
}
// FIRST reset: Reset workflow execution, current is open
_, err := s.engine.ResetWorkflowExecution(createContext(), &types.ResetWorkflowExecutionRequest{
Domain: s.domainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: id,
RunID: we.RunID,
},
Reason: "reset execution from test",
DecisionFinishEventID: lastDecisionScheduled.ID + 1,
RequestID: uuid.New(),
})
s.NoError(err)
// get the history of the first run again
events = s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: we.GetRunID(),
})
var lastEvent *types.HistoryEvent
for _, event := range events {
lastEvent = event
}
// assert the first run is closed, terminated by the previous reset
s.Equal(types.EventTypeWorkflowExecutionTerminated, lastEvent.GetEventType())
// SECOND reset: reset the first run again, to exercise the code path of resetting closed workflow
resp, err := s.engine.ResetWorkflowExecution(createContext(), &types.ResetWorkflowExecutionRequest{
Domain: s.domainName,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: id,
RunID: we.GetRunID(),
},
Reason: "reset execution from test",
DecisionFinishEventID: lastDecisionScheduled.ID + 1,
RequestID: uuid.New(),
})
s.NoError(err)
newRunID := resp.GetRunID()
workflowComplete := false
activityData := int32(1)
isFirstTaskProcessed := false
wtHandler := func(execution *types.WorkflowExecution, wt *types.WorkflowType,
previousStartedEventID, startedEventID int64, history *types.History) ([]byte, []*types.Decision, error) {
if !isFirstTaskProcessed {
// Schedule 3 activities on first workflow task
isFirstTaskProcessed = true
buf := new(bytes.Buffer)
s.Nil(binary.Write(buf, binary.LittleEndian, activityData))
var scheduleActivityCommands []*types.Decision
scheduleActivityCommands = append(scheduleActivityCommands, &types.Decision{
DecisionType: types.DecisionTypeScheduleActivityTask.Ptr(),
ScheduleActivityTaskDecisionAttributes: &types.ScheduleActivityTaskDecisionAttributes{
ActivityID: "1",
ActivityType: &types.ActivityType{Name: "ResetActivity"},
TaskList: tasklist,
Input: buf.Bytes(),
ScheduleToCloseTimeoutSeconds: common.Int32Ptr(100),
ScheduleToStartTimeoutSeconds: common.Int32Ptr(100),
StartToCloseTimeoutSeconds: common.Int32Ptr(50),
HeartbeatTimeoutSeconds: common.Int32Ptr(5),
},
})
return nil, scheduleActivityCommands, nil
}
// Complete workflow after reset
workflowComplete = true
return nil, []*types.Decision{{
DecisionType: types.DecisionTypeCompleteWorkflowExecution.Ptr(),
CompleteWorkflowExecutionDecisionAttributes: &types.CompleteWorkflowExecutionDecisionAttributes{
Result: []byte("Done."),
},
}}, nil
}
// activity handler
atHandler := func(execution *types.WorkflowExecution, activityType *types.ActivityType,
activityID string, input []byte, taskToken []byte) ([]byte, bool, error) {
return []byte("Activity Result."), false, nil
}
poller := &TaskPoller{
Engine: s.engine,
Domain: s.domainName,
TaskList: tasklist,
Identity: identity,
DecisionHandler: wtHandler,
ActivityHandler: atHandler,
Logger: s.Logger,
T: s.T(),
}
// Process first workflow decision task to schedule activities
_, err = poller.PollAndProcessDecisionTask(false, false)
s.Logger.Info("PollAndProcessWorkflowTask", tag.Error(err))
s.NoError(err)
s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: newRunID,
})
// Process one activity task which also creates second workflow task
err = poller.PollAndProcessActivityTask(false)
s.Logger.Info("Poll and process first activity", tag.Error(err))
s.NoError(err)
_, err = poller.PollAndProcessDecisionTask(false, false)
s.Logger.Info("Poll and process final decision task", tag.Error(err))
s.NoError(err)
s.True(workflowComplete)
// get the history of the newRunID
events = s.getHistory(s.domainName, &types.WorkflowExecution{
WorkflowID: id,
RunID: newRunID,
})
for _, event := range events {
lastEvent = event
}
// assert the new run is closed, completed by decision task
s.Equal(types.EventTypeWorkflowExecutionCompleted, lastEvent.GetEventType())
}