forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskpoller.go
459 lines (398 loc) · 17.2 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
// 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"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/service/history"
"github.com/uber/cadence/service/matching"
"go.uber.org/yarpc"
)
type (
decisionTaskHandler func(execution *workflow.WorkflowExecution, wt *workflow.WorkflowType,
previousStartedEventID, startedEventID int64, history *workflow.History) ([]byte, []*workflow.Decision, error)
activityTaskHandler func(execution *workflow.WorkflowExecution, activityType *workflow.ActivityType,
activityID string, input []byte, takeToken []byte) ([]byte, bool, error)
queryHandler func(task *workflow.PollForDecisionTaskResponse) ([]byte, error)
// TaskPoller is used in integration tests to poll decision or activity tasks
TaskPoller struct {
Engine FrontendClient
Domain string
TaskList *workflow.TaskList
StickyTaskList *workflow.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)
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) (isQueryTask bool, newTask *workflow.RespondDecisionTaskCompletedResponse, err error) {
Loop:
for attempt := 0; attempt < retryCount; attempt++ {
taskList := p.TaskList
if pollStickyTaskList {
taskList = p.StickyTaskList
}
response, err1 := p.Engine.PollForDecisionTask(createContext(), &workflow.PollForDecisionTaskRequest{
Domain: common.StringPtr(p.Domain),
TaskList: taskList,
Identity: common.StringPtr(p.Identity),
})
if err1 == history.ErrDuplicate {
p.Logger.Info("Duplicate Decision task: Polling again.")
continue Loop
}
if err1 != nil {
return false, nil, err1
}
if response == nil || len(response.TaskToken) == 0 {
p.Logger.Info("Empty Decision task: Polling again.")
continue Loop
}
var events []*workflow.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 events == nil || len(events) == 0 {
p.Logger.Fatal("History Events are empty")
}
nextPageToken := response.NextPageToken
for nextPageToken != nil {
resp, err2 := p.Engine.GetWorkflowExecutionHistory(createContext(), &workflow.GetWorkflowExecutionHistoryRequest{
Domain: common.StringPtr(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 := &workflow.RespondQueryTaskCompletedRequest{TaskToken: response.TaskToken}
if err != nil {
completeType := workflow.QueryTaskCompletedTypeFailed
completeRequest.CompletedType = &completeType
completeRequest.ErrorMessage = common.StringPtr(err.Error())
} else {
completeType := workflow.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 *workflow.HistoryEvent
for _, e := range events {
if e.GetEventType() == workflow.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), common.Int64Default(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(), &workflow.RespondDecisionTaskFailedRequest{
TaskToken: response.TaskToken,
Cause: common.DecisionTaskFailedCausePtr(workflow.DecisionTaskFailedCauseWorkflowWorkerUnhandledFailure),
Details: []byte(err.Error()),
Identity: common.StringPtr(p.Identity),
})
}
p.Logger.Info("Completing Decision. Decisions", tag.Value(decisions))
if !respondStickyTaskList {
// non sticky tasklist
newTask, err := p.Engine.RespondDecisionTaskCompleted(createContext(), &workflow.RespondDecisionTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: common.StringPtr(p.Identity),
ExecutionContext: executionCtx,
Decisions: decisions,
ReturnNewDecisionTask: common.BoolPtr(forceCreateNewDecision),
ForceCreateNewDecisionTask: common.BoolPtr(forceCreateNewDecision),
})
return false, newTask, err
}
// sticky tasklist
newTask, err := p.Engine.RespondDecisionTaskCompleted(
createContext(),
&workflow.RespondDecisionTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: common.StringPtr(p.Identity),
ExecutionContext: executionCtx,
Decisions: decisions,
StickyAttributes: &workflow.StickyExecutionAttributes{
WorkerTaskList: p.StickyTaskList,
ScheduleToStartTimeoutSeconds: p.StickyScheduleToStartTimeoutSeconds,
},
ReturnNewDecisionTask: common.BoolPtr(forceCreateNewDecision),
ForceCreateNewDecisionTask: common.BoolPtr(forceCreateNewDecision),
},
yarpc.WithHeader(common.LibraryVersionHeaderName, "0.0.1"),
yarpc.WithHeader(common.FeatureVersionHeaderName, "1.0.0"),
yarpc.WithHeader(common.ClientImplHeaderName, "go"),
)
return false, newTask, err
}
return false, nil, matching.ErrNoTasks
}
// HandlePartialDecision for decision task
func (p *TaskPoller) HandlePartialDecision(response *workflow.PollForDecisionTaskResponse) (
*workflow.RespondDecisionTaskCompletedResponse, error) {
if response == nil || len(response.TaskToken) == 0 {
p.Logger.Info("Empty Decision task: Polling again.")
return nil, nil
}
var events []*workflow.HistoryEvent
history := response.History
if history == nil {
p.Logger.Fatal("History is nil")
}
events = history.Events
if events == nil || len(events) == 0 {
p.Logger.Fatal("History Events are empty")
}
nextPageToken := response.NextPageToken
for nextPageToken != nil {
resp, err2 := p.Engine.GetWorkflowExecutionHistory(createContext(), &workflow.GetWorkflowExecutionHistoryRequest{
Domain: common.StringPtr(p.Domain),
Execution: response.WorkflowExecution,
NextPageToken: nextPageToken,
})
if err2 != nil {
return nil, err2
}
events = append(events, resp.History.Events...)
nextPageToken = resp.NextPageToken
}
executionCtx, decisions, err := p.DecisionHandler(response.WorkflowExecution, response.WorkflowType,
common.Int64Default(response.PreviousStartedEventId), common.Int64Default(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(), &workflow.RespondDecisionTaskFailedRequest{
TaskToken: response.TaskToken,
Cause: common.DecisionTaskFailedCausePtr(workflow.DecisionTaskFailedCauseWorkflowWorkerUnhandledFailure),
Details: []byte(err.Error()),
Identity: common.StringPtr(p.Identity),
})
}
p.Logger.Info("Completing Decision. Decisions: %v", tag.Value(decisions))
// sticky tasklist
newTask, err := p.Engine.RespondDecisionTaskCompleted(
createContext(),
&workflow.RespondDecisionTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: common.StringPtr(p.Identity),
ExecutionContext: executionCtx,
Decisions: decisions,
StickyAttributes: &workflow.StickyExecutionAttributes{
WorkerTaskList: p.StickyTaskList,
ScheduleToStartTimeoutSeconds: p.StickyScheduleToStartTimeoutSeconds,
},
ReturnNewDecisionTask: common.BoolPtr(true),
ForceCreateNewDecisionTask: common.BoolPtr(true),
},
yarpc.WithHeader(common.LibraryVersionHeaderName, "0.0.1"),
yarpc.WithHeader(common.FeatureVersionHeaderName, "1.0.0"),
yarpc.WithHeader(common.ClientImplHeaderName, "go"),
)
return newTask, err
}
// PollAndProcessActivityTask for activity tasks
func (p *TaskPoller) PollAndProcessActivityTask(dropTask bool) error {
retry:
for attempt := 0; attempt < 5; attempt++ {
response, err1 := p.Engine.PollForActivityTask(createContext(), &workflow.PollForActivityTaskRequest{
Domain: common.StringPtr(p.Domain),
TaskList: p.TaskList,
Identity: common.StringPtr(p.Identity),
})
if err1 == history.ErrDuplicate {
p.Logger.Info("Duplicate Activity task: Polling again.")
continue retry
}
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(), &workflow.RespondActivityTaskCanceledRequest{
TaskToken: response.TaskToken,
Details: []byte("details"),
Identity: common.StringPtr(p.Identity),
})
}
if err2 != nil {
return p.Engine.RespondActivityTaskFailed(createContext(), &workflow.RespondActivityTaskFailedRequest{
TaskToken: response.TaskToken,
Reason: common.StringPtr(err2.Error()),
Details: []byte(err2.Error()),
Identity: common.StringPtr(p.Identity),
})
}
return p.Engine.RespondActivityTaskCompleted(createContext(), &workflow.RespondActivityTaskCompletedRequest{
TaskToken: response.TaskToken,
Identity: common.StringPtr(p.Identity),
Result: result,
})
}
return matching.ErrNoTasks
}
// PollAndProcessActivityTaskWithID is similar to PollAndProcessActivityTask but using RespondActivityTask...ByID
func (p *TaskPoller) PollAndProcessActivityTaskWithID(dropTask bool) error {
retry:
for attempt := 0; attempt < 5; attempt++ {
response, err1 := p.Engine.PollForActivityTask(createContext(), &workflow.PollForActivityTaskRequest{
Domain: common.StringPtr(p.Domain),
TaskList: p.TaskList,
Identity: common.StringPtr(p.Identity),
})
if err1 == history.ErrDuplicate {
p.Logger.Info("Duplicate Activity task: Polling again.")
continue retry
}
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(), &workflow.RespondActivityTaskCanceledByIDRequest{
Domain: common.StringPtr(p.Domain),
WorkflowID: common.StringPtr(response.WorkflowExecution.GetWorkflowId()),
RunID: common.StringPtr(response.WorkflowExecution.GetRunId()),
ActivityID: common.StringPtr(response.GetActivityId()),
Details: []byte("details"),
Identity: common.StringPtr(p.Identity),
})
}
if err2 != nil {
return p.Engine.RespondActivityTaskFailedByID(createContext(), &workflow.RespondActivityTaskFailedByIDRequest{
Domain: common.StringPtr(p.Domain),
WorkflowID: common.StringPtr(response.WorkflowExecution.GetWorkflowId()),
RunID: common.StringPtr(response.WorkflowExecution.GetRunId()),
ActivityID: common.StringPtr(response.GetActivityId()),
Reason: common.StringPtr(err2.Error()),
Details: []byte(err2.Error()),
Identity: common.StringPtr(p.Identity),
})
}
return p.Engine.RespondActivityTaskCompletedByID(createContext(), &workflow.RespondActivityTaskCompletedByIDRequest{
Domain: common.StringPtr(p.Domain),
WorkflowID: common.StringPtr(response.WorkflowExecution.GetWorkflowId()),
RunID: common.StringPtr(response.WorkflowExecution.GetRunId()),
ActivityID: common.StringPtr(response.GetActivityId()),
Identity: common.StringPtr(p.Identity),
Result: result,
})
}
return matching.ErrNoTasks
}
func createContext() context.Context {
ctx, _ := context.WithTimeout(context.Background(), 90*time.Second)
return ctx
}