forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskpoller.go
467 lines (407 loc) · 15.7 KB
/
taskpoller.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) 2017 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.
package host
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/service/matching"
)
type (
decisionTaskHandler func(execution *types.WorkflowExecution, wt *types.WorkflowType,
previousStartedEventID, startedEventID int64, history *types.History) ([]byte, []*types.Decision, error)
activityTaskHandler func(execution *types.WorkflowExecution, activityType *types.ActivityType,
ActivityID string, input []byte, takeToken []byte) ([]byte, bool, error)
queryHandler func(task *types.PollForDecisionTaskResponse) ([]byte, error)
// TaskPoller is used in integration tests to poll decision or activity tasks
TaskPoller struct {
Engine FrontendClient
Domain string
TaskList *types.TaskList
StickyTaskList *types.TaskList
StickyScheduleToStartTimeoutSeconds *int32
Identity string
DecisionHandler decisionTaskHandler
ActivityHandler activityTaskHandler
QueryHandler queryHandler
Logger log.Logger
T *testing.T
}
)
// PollAndProcessDecisionTask for decision tasks
func (p *TaskPoller) PollAndProcessDecisionTask(dumpHistory bool, dropTask bool) (isQueryTask bool, err error) {
return p.PollAndProcessDecisionTaskWithAttempt(dumpHistory, dropTask, false, false, int64(0))
}
// PollAndProcessDecisionTaskWithSticky for decision tasks
func (p *TaskPoller) PollAndProcessDecisionTaskWithSticky(dumpHistory bool, dropTask bool) (isQueryTask bool, err error) {
return p.PollAndProcessDecisionTaskWithAttempt(dumpHistory, dropTask, true, true, int64(0))
}
// PollAndProcessDecisionTaskWithoutRetry for decision tasks
func (p *TaskPoller) PollAndProcessDecisionTaskWithoutRetry(dumpHistory bool, dropTask bool) (isQueryTask bool, err error) {
return p.PollAndProcessDecisionTaskWithAttemptAndRetry(dumpHistory, dropTask, false, false, int64(0), 1)
}
// PollAndProcessDecisionTaskWithAttempt for decision tasks
func (p *TaskPoller) PollAndProcessDecisionTaskWithAttempt(
dumpHistory bool,
dropTask bool,
pollStickyTaskList bool,
respondStickyTaskList bool,
decisionAttempt int64,
) (isQueryTask bool, err error) {
return p.PollAndProcessDecisionTaskWithAttemptAndRetry(
dumpHistory,
dropTask,
pollStickyTaskList,
respondStickyTaskList,
decisionAttempt,
5)
}
// PollAndProcessDecisionTaskWithAttemptAndRetry for decision tasks
func (p *TaskPoller) PollAndProcessDecisionTaskWithAttemptAndRetry(
dumpHistory bool,
dropTask bool,
pollStickyTaskList bool,
respondStickyTaskList bool,
decisionAttempt int64,
retryCount int,
) (isQueryTask bool, err error) {
isQueryTask, _, err = p.PollAndProcessDecisionTaskWithAttemptAndRetryAndForceNewDecision(
dumpHistory,
dropTask,
pollStickyTaskList,
respondStickyTaskList,
decisionAttempt,
retryCount,
false,
nil)
return isQueryTask, err
}
// PollAndProcessDecisionTaskWithAttemptAndRetryAndForceNewDecision for decision tasks
func (p *TaskPoller) PollAndProcessDecisionTaskWithAttemptAndRetryAndForceNewDecision(
dumpHistory bool,
dropTask bool,
pollStickyTaskList bool,
respondStickyTaskList bool,
decisionAttempt int64,
retryCount int,
forceCreateNewDecision bool,
queryResult *types.WorkflowQueryResult,
) (isQueryTask bool, newTask *types.RespondDecisionTaskCompletedResponse, err error) {
Loop:
for attempt := 0; attempt < retryCount; attempt++ {
taskList := p.TaskList
if pollStickyTaskList {
taskList = p.StickyTaskList
}
response, err1 := p.Engine.PollForDecisionTask(createContext(), &types.PollForDecisionTaskRequest{
Domain: p.Domain,
TaskList: taskList,
Identity: p.Identity,
})
if err1 != nil {
return false, nil, err1
}
if response == nil || len(response.TaskToken) == 0 {
p.Logger.Info("Empty Decision task: Polling again.")
continue Loop
}
if response.GetNextEventID() == 0 {
p.Logger.Fatal("NextEventID is not set for decision or query task")
}
var events []*types.HistoryEvent
if response.Query == nil || !pollStickyTaskList {
// if not query task, should have some history events
// for non sticky query, there should be events returned
history := response.History
if history == nil {
p.Logger.Fatal("History is nil")
}
events = history.Events
if len(events) == 0 {
p.Logger.Fatal("History Events are empty")
}
nextPageToken := response.NextPageToken
for nextPageToken != nil {
resp, err2 := p.Engine.GetWorkflowExecutionHistory(createContext(), &types.GetWorkflowExecutionHistoryRequest{
Domain: p.Domain,
Execution: response.WorkflowExecution,
NextPageToken: nextPageToken,
})
if err2 != nil {
return false, nil, err2
}
events = append(events, resp.History.Events...)
nextPageToken = resp.NextPageToken
}
} else {
// for sticky query, there should be NO events returned
// since worker side already has the state machine and we do not intend to update that.
history := response.History
nextPageToken := response.NextPageToken
if !(history == nil || (len(history.Events) == 0 && nextPageToken == nil)) {
// if history is not nil, and contains events or next token
p.Logger.Fatal("History is not empty for sticky query")
}
}
if dropTask {
p.Logger.Info("Dropping Decision task: ")
return false, nil, nil
}
if dumpHistory {
common.PrettyPrintHistory(response.History, p.Logger)
}
// handle query task response
if response.Query != nil {
blob, err := p.QueryHandler(response)
completeRequest := &types.RespondQueryTaskCompletedRequest{TaskToken: response.TaskToken}
if err != nil {
completeType := types.QueryTaskCompletedTypeFailed
completeRequest.CompletedType = &completeType
completeRequest.ErrorMessage = err.Error()
} else {
completeType := types.QueryTaskCompletedTypeCompleted
completeRequest.CompletedType = &completeType
completeRequest.QueryResult = blob
}
return true, nil, p.Engine.RespondQueryTaskCompleted(createContext(), completeRequest)
}
// handle normal decision task / non query task response
var lastDecisionScheduleEvent *types.HistoryEvent
for _, e := range events {
if e.GetEventType() == types.EventTypeDecisionTaskScheduled {
lastDecisionScheduleEvent = e
}
}
if lastDecisionScheduleEvent != nil && decisionAttempt > 0 {
require.Equal(p.T, decisionAttempt, lastDecisionScheduleEvent.DecisionTaskScheduledEventAttributes.GetAttempt())
}
executionCtx, decisions, err := p.DecisionHandler(response.WorkflowExecution, response.WorkflowType,
common.Int64Default(response.PreviousStartedEventID), response.StartedEventID, response.History)
if err != nil {
p.Logger.Info("Failing Decision. Decision handler failed with error", tag.Error(err))
return isQueryTask, nil, p.Engine.RespondDecisionTaskFailed(createContext(), &types.RespondDecisionTaskFailedRequest{
TaskToken: response.TaskToken,
Cause: types.DecisionTaskFailedCauseWorkflowWorkerUnhandledFailure.Ptr(),
Details: []byte(err.Error()),
Identity: p.Identity,
})
}
p.Logger.Info("Completing Decision. Decisions", tag.Value(decisions))
if !respondStickyTaskList {
// non sticky tasklist
newTask, err := p.Engine.RespondDecisionTaskCompleted(createContext(), &types.RespondDecisionTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: p.Identity,
ExecutionContext: executionCtx,
Decisions: decisions,
ReturnNewDecisionTask: forceCreateNewDecision,
ForceCreateNewDecisionTask: forceCreateNewDecision,
QueryResults: getQueryResults(response.GetQueries(), queryResult),
})
return false, newTask, err
}
// sticky tasklist
newTask, err := p.Engine.RespondDecisionTaskCompleted(
createContext(),
&types.RespondDecisionTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: p.Identity,
ExecutionContext: executionCtx,
Decisions: decisions,
StickyAttributes: &types.StickyExecutionAttributes{
WorkerTaskList: p.StickyTaskList,
ScheduleToStartTimeoutSeconds: p.StickyScheduleToStartTimeoutSeconds,
},
ReturnNewDecisionTask: forceCreateNewDecision,
ForceCreateNewDecisionTask: forceCreateNewDecision,
QueryResults: getQueryResults(response.GetQueries(), queryResult),
},
)
return false, newTask, err
}
return false, nil, matching.ErrNoTasks
}
// HandlePartialDecision for decision task
func (p *TaskPoller) HandlePartialDecision(response *types.PollForDecisionTaskResponse) (
*types.RespondDecisionTaskCompletedResponse, error) {
if response == nil || len(response.TaskToken) == 0 {
p.Logger.Info("Empty Decision task: Polling again.")
return nil, nil
}
var events []*types.HistoryEvent
history := response.History
if history == nil {
p.Logger.Fatal("History is nil")
}
events = history.Events
if len(events) == 0 {
p.Logger.Fatal("History Events are empty")
}
executionCtx, decisions, err := p.DecisionHandler(response.WorkflowExecution, response.WorkflowType,
common.Int64Default(response.PreviousStartedEventID), response.StartedEventID, response.History)
if err != nil {
p.Logger.Info("Failing Decision. Decision handler failed with error: %v", tag.Error(err))
return nil, p.Engine.RespondDecisionTaskFailed(createContext(), &types.RespondDecisionTaskFailedRequest{
TaskToken: response.TaskToken,
Cause: types.DecisionTaskFailedCauseWorkflowWorkerUnhandledFailure.Ptr(),
Details: []byte(err.Error()),
Identity: p.Identity,
})
}
p.Logger.Info("Completing Decision. Decisions: %v", tag.Value(decisions))
// sticky tasklist
newTask, err := p.Engine.RespondDecisionTaskCompleted(
createContext(),
&types.RespondDecisionTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: p.Identity,
ExecutionContext: executionCtx,
Decisions: decisions,
StickyAttributes: &types.StickyExecutionAttributes{
WorkerTaskList: p.StickyTaskList,
ScheduleToStartTimeoutSeconds: p.StickyScheduleToStartTimeoutSeconds,
},
ReturnNewDecisionTask: true,
ForceCreateNewDecisionTask: true,
},
)
return newTask, err
}
// PollAndProcessActivityTask for activity tasks
func (p *TaskPoller) PollAndProcessActivityTask(dropTask bool) error {
for attempt := 0; attempt < 5; attempt++ {
response, err1 := p.Engine.PollForActivityTask(createContext(), &types.PollForActivityTaskRequest{
Domain: p.Domain,
TaskList: p.TaskList,
Identity: p.Identity,
})
if err1 != nil {
return err1
}
if response == nil || len(response.TaskToken) == 0 {
p.Logger.Info("Empty Activity task: Polling again.")
return nil
}
if dropTask {
p.Logger.Info("Dropping Activity task: ")
return nil
}
p.Logger.Debug("Received Activity task", tag.Value(response))
result, cancel, err2 := p.ActivityHandler(response.WorkflowExecution, response.ActivityType, response.ActivityID,
response.Input, response.TaskToken)
if cancel {
p.Logger.Info("Executing RespondActivityTaskCanceled")
return p.Engine.RespondActivityTaskCanceled(createContext(), &types.RespondActivityTaskCanceledRequest{
TaskToken: response.TaskToken,
Details: []byte("details"),
Identity: p.Identity,
})
}
if err2 != nil {
return p.Engine.RespondActivityTaskFailed(createContext(), &types.RespondActivityTaskFailedRequest{
TaskToken: response.TaskToken,
Reason: common.StringPtr(err2.Error()),
Details: []byte(err2.Error()),
Identity: p.Identity,
})
}
return p.Engine.RespondActivityTaskCompleted(createContext(), &types.RespondActivityTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: p.Identity,
Result: result,
})
}
return matching.ErrNoTasks
}
// PollAndProcessActivityTaskWithID is similar to PollAndProcessActivityTask but using RespondActivityTask...ByID
func (p *TaskPoller) PollAndProcessActivityTaskWithID(dropTask bool) error {
for attempt := 0; attempt < 5; attempt++ {
response, err1 := p.Engine.PollForActivityTask(createContext(), &types.PollForActivityTaskRequest{
Domain: p.Domain,
TaskList: p.TaskList,
Identity: p.Identity,
})
if err1 != nil {
return err1
}
if response == nil || len(response.TaskToken) == 0 {
p.Logger.Info("Empty Activity task: Polling again.")
return nil
}
if response.GetActivityID() == "" {
p.Logger.Info("Empty ActivityID")
return nil
}
if dropTask {
p.Logger.Info("Dropping Activity task: ")
return nil
}
p.Logger.Debug("Received Activity task: %v", tag.Value(response))
result, cancel, err2 := p.ActivityHandler(response.WorkflowExecution, response.ActivityType, response.ActivityID,
response.Input, response.TaskToken)
if cancel {
p.Logger.Info("Executing RespondActivityTaskCanceled")
return p.Engine.RespondActivityTaskCanceledByID(createContext(), &types.RespondActivityTaskCanceledByIDRequest{
Domain: p.Domain,
WorkflowID: response.WorkflowExecution.GetWorkflowID(),
RunID: response.WorkflowExecution.GetRunID(),
ActivityID: response.GetActivityID(),
Details: []byte("details"),
Identity: p.Identity,
})
}
if err2 != nil {
return p.Engine.RespondActivityTaskFailedByID(createContext(), &types.RespondActivityTaskFailedByIDRequest{
Domain: p.Domain,
WorkflowID: response.WorkflowExecution.GetWorkflowID(),
RunID: response.WorkflowExecution.GetRunID(),
ActivityID: response.GetActivityID(),
Reason: common.StringPtr(err2.Error()),
Details: []byte(err2.Error()),
Identity: p.Identity,
})
}
return p.Engine.RespondActivityTaskCompletedByID(createContext(), &types.RespondActivityTaskCompletedByIDRequest{
Domain: p.Domain,
WorkflowID: response.WorkflowExecution.GetWorkflowID(),
RunID: response.WorkflowExecution.GetRunID(),
ActivityID: response.GetActivityID(),
Identity: p.Identity,
Result: result,
})
}
return matching.ErrNoTasks
}
func createContext() context.Context {
ctx, _ := context.WithTimeout(context.Background(), 90*time.Second)
return ctx
}
func getQueryResults(queries map[string]*types.WorkflowQuery, queryResult *types.WorkflowQueryResult) map[string]*types.WorkflowQueryResult {
result := make(map[string]*types.WorkflowQueryResult)
for k := range queries {
result[k] = queryResult
}
return result
}