-
Notifications
You must be signed in to change notification settings - Fork 787
/
matchingEngine.go
602 lines (548 loc) · 22.1 KB
/
matchingEngine.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// 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 matching
import (
"context"
"errors"
"math"
"sync"
"github.com/pborman/uuid"
"github.com/uber-common/bark"
h "github.com/uber/cadence/.gen/go/history"
m "github.com/uber/cadence/.gen/go/matching"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/client/history"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/client"
"github.com/uber/cadence/common/logging"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
)
// Implements matching.Engine
// TODO: Switch implementation from lock/channel based to a partitioned agent
// to simplify code and reduce possiblity of synchronization errors.
type matchingEngineImpl struct {
taskManager persistence.TaskManager
historyService history.Client
tokenSerializer common.TaskTokenSerializer
logger bark.Logger
metricsClient metrics.Client
taskListsLock sync.RWMutex // locks mutation of taskLists
taskLists map[taskListID]taskListManager // Convert to LRU cache
config *Config
queryMapLock sync.Mutex
// map from query TaskID (which is a UUID generated in QueryWorkflow() call) to a channel that QueryWorkflow()
// will block and wait for. The RespondQueryTaskCompleted() call will send the data through that channel which will
// unblock QueryWorkflow() call.
queryTaskMap map[string]chan *workflow.RespondQueryTaskCompletedRequest
}
type taskListID struct {
domainID string
taskListName string
taskType int
}
type pollerIDCtxKey string
type identityCtxKey string
var (
// EmptyPollForDecisionTaskResponse is the response when there are no decision tasks to hand out
emptyPollForDecisionTaskResponse = &m.PollForDecisionTaskResponse{}
// EmptyPollForActivityTaskResponse is the response when there are no activity tasks to hand out
emptyPollForActivityTaskResponse = &workflow.PollForActivityTaskResponse{}
persistenceOperationRetryPolicy = common.CreatePersistanceRetryPolicy()
historyServiceOperationRetryPolicy = common.CreateHistoryServiceRetryPolicy()
// ErrNoTasks is exported temporarily for integration test
ErrNoTasks = errors.New("No tasks")
errPumpClosed = errors.New("Task list pump closed its channel")
pollerIDKey pollerIDCtxKey = "pollerID"
identityKey identityCtxKey = "identity"
)
func (t *taskListID) String() string {
var r string
if t.taskType == persistence.TaskListTypeActivity {
r += "activity"
} else {
r += "decision"
}
r += " task list \""
r += t.taskListName
r += "\""
return r
}
var _ Engine = (*matchingEngineImpl)(nil) // Asserts that interface is indeed implemented
// NewEngine creates an instance of matching engine
func NewEngine(taskManager persistence.TaskManager,
historyService history.Client,
config *Config,
logger bark.Logger,
metricsClient metrics.Client) Engine {
return &matchingEngineImpl{
taskManager: taskManager,
historyService: historyService,
tokenSerializer: common.NewJSONTaskTokenSerializer(),
taskLists: make(map[taskListID]taskListManager),
logger: logger.WithFields(bark.Fields{
logging.TagWorkflowComponent: logging.TagValueMatchingEngineComponent,
}),
metricsClient: metricsClient,
config: config,
queryTaskMap: make(map[string]chan *workflow.RespondQueryTaskCompletedRequest),
}
}
func (e *matchingEngineImpl) Start() {
// As task lists are initialized lazily nothing is done on startup at this point.
}
func (e *matchingEngineImpl) Stop() {
// Executes Stop() on each task list outside of lock
for _, l := range e.getTaskLists(math.MaxInt32) {
l.Stop()
}
}
func (e *matchingEngineImpl) getTaskLists(maxCount int) (lists []taskListManager) {
e.taskListsLock.RLock()
defer e.taskListsLock.RUnlock()
lists = make([]taskListManager, 0, len(e.taskLists))
count := 0
for _, tlMgr := range e.taskLists {
lists = append(lists, tlMgr)
count++
if count >= maxCount {
break
}
}
return
}
func (e *matchingEngineImpl) String() string {
// Executes taskList.String() on each task list outside of lock
var r string
for _, l := range e.getTaskLists(1000) {
r += "\n"
r += l.String()
}
return r
}
// Returns taskListManager for a task list. If not already cached gets new range from DB and
// if successful creates one.
func (e *matchingEngineImpl) getTaskListManager(taskList *taskListID) (taskListManager, error) {
// The first check is an optimization so almost all requests will have a task list manager
// and return avoiding the write lock
e.taskListsLock.RLock()
if result, ok := e.taskLists[*taskList]; ok {
e.taskListsLock.RUnlock()
return result, nil
}
e.taskListsLock.RUnlock()
// If it gets here, write lock and check again in case a task list is created between the two locks
e.taskListsLock.Lock()
if result, ok := e.taskLists[*taskList]; ok {
e.taskListsLock.Unlock()
return result, nil
}
mgr := newTaskListManager(e, taskList, e.config)
e.taskLists[*taskList] = mgr
e.taskListsLock.Unlock()
logging.LogTaskListLoadingEvent(e.logger, taskList.taskListName, taskList.taskType)
err := mgr.Start()
if err != nil {
logging.LogTaskListLoadingFailedEvent(e.logger, taskList.taskListName, taskList.taskType, err)
return nil, err
}
logging.LogTaskListLoadedEvent(e.logger, taskList.taskListName, taskList.taskType)
return mgr, nil
}
// For use in tests
func (e *matchingEngineImpl) updateTaskList(taskList *taskListID, mgr taskListManager) {
e.taskListsLock.Lock()
defer e.taskListsLock.Unlock()
e.taskLists[*taskList] = mgr
}
func (e *matchingEngineImpl) removeTaskListManager(id *taskListID) {
e.taskListsLock.Lock()
defer e.taskListsLock.Unlock()
delete(e.taskLists, *id)
}
// AddDecisionTask either delivers task directly to waiting poller or save it into task list persistence.
func (e *matchingEngineImpl) AddDecisionTask(addRequest *m.AddDecisionTaskRequest) error {
domainID := *addRequest.DomainUUID
taskListName := *addRequest.TaskList.Name
e.logger.Debugf("Received AddDecisionTask for taskList=%v, WorkflowID=%v, RunID=%v, ScheduleToStartTimeout=%v",
addRequest.TaskList.GetName(), addRequest.Execution.GetWorkflowId(), addRequest.Execution.GetRunId(),
addRequest.GetScheduleToStartTimeoutSeconds())
taskList := newTaskListID(domainID, taskListName, persistence.TaskListTypeDecision)
tlMgr, err := e.getTaskListManager(taskList)
if err != nil {
return err
}
taskInfo := &persistence.TaskInfo{
DomainID: domainID,
RunID: *addRequest.Execution.RunId,
WorkflowID: *addRequest.Execution.WorkflowId,
ScheduleID: *addRequest.ScheduleId,
ScheduleToStartTimeout: *addRequest.ScheduleToStartTimeoutSeconds,
}
return tlMgr.AddTask(addRequest.Execution, taskInfo)
}
// AddActivityTask either delivers task directly to waiting poller or save it into task list persistence.
func (e *matchingEngineImpl) AddActivityTask(addRequest *m.AddActivityTaskRequest) error {
domainID := *addRequest.DomainUUID
sourceDomainID := *addRequest.SourceDomainUUID
taskListName := *addRequest.TaskList.Name
e.logger.Debugf("Received AddActivityTask for taskList=%v WorkflowID=%v, RunID=%v",
taskListName, addRequest.Execution.WorkflowId, addRequest.Execution.RunId)
taskList := newTaskListID(domainID, taskListName, persistence.TaskListTypeActivity)
tlMgr, err := e.getTaskListManager(taskList)
if err != nil {
return err
}
taskInfo := &persistence.TaskInfo{
DomainID: sourceDomainID,
RunID: *addRequest.Execution.RunId,
WorkflowID: *addRequest.Execution.WorkflowId,
ScheduleID: *addRequest.ScheduleId,
ScheduleToStartTimeout: *addRequest.ScheduleToStartTimeoutSeconds,
}
return tlMgr.AddTask(addRequest.Execution, taskInfo)
}
// PollForDecisionTask tries to get the decision task using exponential backoff.
func (e *matchingEngineImpl) PollForDecisionTask(ctx context.Context, req *m.PollForDecisionTaskRequest) (
*m.PollForDecisionTaskResponse, error) {
domainID := req.GetDomainUUID()
pollerID := req.GetPollerID()
request := req.PollRequest
taskListName := request.TaskList.GetName()
e.logger.Debugf("Received PollForDecisionTask for taskList=%v", taskListName)
pollLoop:
for {
err := common.IsValidContext(ctx)
if err != nil {
return nil, err
}
// Add frontend generated pollerID to context so tasklistMgr can support cancellation of
// long-poll when frontend calls CancelOutstandingPoll API
pollerCtx := context.WithValue(ctx, pollerIDKey, pollerID)
pollerCtx = context.WithValue(pollerCtx, identityKey, request.GetIdentity())
taskList := newTaskListID(domainID, taskListName, persistence.TaskListTypeDecision)
tCtx, err := e.getTask(pollerCtx, taskList, nil)
if err != nil {
// TODO: Is empty poll the best reply for errPumpClosed?
if err == ErrNoTasks || err == errPumpClosed {
return emptyPollForDecisionTaskResponse, nil
}
return nil, err
}
if tCtx.queryTaskInfo != nil {
// for query task, we don't need to update history to record decision task started. but we need to know
// the NextEventID so front end knows what are the history events to load for this decision task.
mutableStateResp, err := e.historyService.GetMutableState(ctx, &h.GetMutableStateRequest{
DomainUUID: req.DomainUUID,
Execution: &tCtx.workflowExecution,
})
if err != nil {
// will notify query client that the query task failed
completeType := workflow.QueryTaskCompletedTypeFailed
e.RespondQueryTaskCompleted(ctx, &m.RespondQueryTaskCompletedRequest{
TaskID: common.StringPtr(tCtx.queryTaskInfo.taskID),
CompletedRequest: &workflow.RespondQueryTaskCompletedRequest{
CompletedType: &completeType,
ErrorMessage: common.StringPtr("server internal error: failed to get nextID " + err.Error()),
},
})
return emptyPollForDecisionTaskResponse, nil
}
clientFeature := client.NewFeatureImpl(
mutableStateResp.GetClientLibraryVersion(),
mutableStateResp.GetClientFeatureVersion(),
mutableStateResp.GetClientImpl(),
)
isStickyEnabled := false
if len(mutableStateResp.StickyTaskList.GetName()) != 0 && clientFeature.SupportStickyQuery() {
isStickyEnabled = true
}
resp := &h.RecordDecisionTaskStartedResponse{
PreviousStartedEventId: mutableStateResp.NextEventId,
NextEventId: mutableStateResp.NextEventId,
WorkflowType: mutableStateResp.WorkflowType,
StickyExecutionEnabled: common.BoolPtr(isStickyEnabled),
}
tCtx.completeTask(nil)
return e.createPollForDecisionTaskResponse(tCtx, resp), nil
}
// Generate a unique requestId for this task which will be used for all retries
requestID := uuid.New()
resp, err := tCtx.RecordDecisionTaskStartedWithRetry(&h.RecordDecisionTaskStartedRequest{
DomainUUID: common.StringPtr(domainID),
WorkflowExecution: &tCtx.workflowExecution,
ScheduleId: &tCtx.info.ScheduleID,
TaskId: &tCtx.info.TaskID,
RequestId: common.StringPtr(requestID),
PollRequest: request,
})
if err != nil {
switch err.(type) {
case *workflow.EntityNotExistsError, *h.EventAlreadyStartedError:
e.logger.Debugf("Duplicated decision task taskList=%v, taskID=%v",
taskListName, tCtx.info.TaskID)
tCtx.completeTask(nil)
default:
tCtx.completeTask(err)
}
continue pollLoop
}
tCtx.completeTask(nil)
return e.createPollForDecisionTaskResponse(tCtx, resp), nil
}
}
// pollForActivityTaskOperation takes one task from the task manager, update workflow execution history, mark task as
// completed and return it to user. If a task from task manager is already started, return an empty response, without
// error. Timeouts handled by the timer queue.
func (e *matchingEngineImpl) PollForActivityTask(ctx context.Context, req *m.PollForActivityTaskRequest) (
*workflow.PollForActivityTaskResponse, error) {
domainID := req.GetDomainUUID()
pollerID := req.GetPollerID()
request := req.PollRequest
taskListName := request.TaskList.GetName()
e.logger.Debugf("Received PollForActivityTask for taskList=%v", taskListName)
pollLoop:
for {
err := common.IsValidContext(ctx)
if err != nil {
return nil, err
}
taskList := newTaskListID(domainID, taskListName, persistence.TaskListTypeActivity)
var maxDispatch *float64
if request.TaskListMetadata != nil {
maxDispatch = request.TaskListMetadata.MaxTasksPerSecond
}
// Add frontend generated pollerID to context so tasklistMgr can support cancellation of
// long-poll when frontend calls CancelOutstandingPoll API
pollerCtx := context.WithValue(ctx, pollerIDKey, pollerID)
pollerCtx = context.WithValue(pollerCtx, identityKey, request.GetIdentity())
tCtx, err := e.getTask(pollerCtx, taskList, maxDispatch)
if err != nil {
// TODO: Is empty poll the best reply for errPumpClosed?
if err == ErrNoTasks || err == errPumpClosed {
return emptyPollForActivityTaskResponse, nil
}
return nil, err
}
// Generate a unique requestId for this task which will be used for all retries
requestID := uuid.New()
resp, err := tCtx.RecordActivityTaskStartedWithRetry(&h.RecordActivityTaskStartedRequest{
DomainUUID: common.StringPtr(domainID),
WorkflowExecution: &tCtx.workflowExecution,
ScheduleId: &tCtx.info.ScheduleID,
TaskId: &tCtx.info.TaskID,
RequestId: common.StringPtr(requestID),
PollRequest: request,
})
if err != nil {
switch err.(type) {
case *workflow.EntityNotExistsError, *h.EventAlreadyStartedError:
e.logger.Debugf("Duplicated activity task taskList=%v, taskID=%v",
taskListName, tCtx.info.TaskID)
tCtx.completeTask(nil)
default:
tCtx.completeTask(err)
}
continue pollLoop
}
tCtx.completeTask(nil)
return e.createPollForActivityTaskResponse(tCtx, resp), nil
}
}
// QueryWorkflow creates a DecisionTask with query data, send it through sync match channel, wait for that DecisionTask
// to be processed by worker, and then return the query result.
func (e *matchingEngineImpl) QueryWorkflow(ctx context.Context, queryRequest *m.QueryWorkflowRequest) (*workflow.QueryWorkflowResponse, error) {
domainID := queryRequest.GetDomainUUID()
taskListName := *queryRequest.TaskList.Name
taskList := newTaskListID(domainID, taskListName, persistence.TaskListTypeDecision)
tlMgr, err := e.getTaskListManager(taskList)
if err != nil {
return nil, err
}
queryTask := &queryTaskInfo{
queryRequest: queryRequest,
taskID: uuid.New(),
}
err = tlMgr.SyncMatchQueryTask(ctx, queryTask)
if err != nil {
return nil, err
}
queryResultCh := make(chan *workflow.RespondQueryTaskCompletedRequest, 1)
e.queryMapLock.Lock()
e.queryTaskMap[queryTask.taskID] = queryResultCh
e.queryMapLock.Unlock()
defer func() {
e.queryMapLock.Lock()
delete(e.queryTaskMap, queryTask.taskID)
e.queryMapLock.Unlock()
}()
select {
case result := <-queryResultCh:
if *result.CompletedType == workflow.QueryTaskCompletedTypeFailed {
return nil, &workflow.QueryFailedError{Message: result.GetErrorMessage()}
}
return &workflow.QueryWorkflowResponse{QueryResult: result.QueryResult}, nil
case <-ctx.Done():
return nil, &workflow.QueryFailedError{Message: "timeout: workflow worker is not responding"}
}
}
func (e *matchingEngineImpl) RespondQueryTaskCompleted(ctx context.Context, request *m.RespondQueryTaskCompletedRequest) error {
e.queryMapLock.Lock()
queryResultCh, ok := e.queryTaskMap[request.GetTaskID()]
e.queryMapLock.Unlock()
if !ok {
e.metricsClient.IncCounter(metrics.MatchingRespondQueryTaskCompletedScope, metrics.RespondQueryTaskFailedCounter)
return &workflow.EntityNotExistsError{Message: "query task not found, or already expired"}
}
queryResultCh <- request.CompletedRequest
return nil
}
func (e *matchingEngineImpl) CancelOutstandingPoll(ctx context.Context, request *m.CancelOutstandingPollRequest) error {
domainID := request.GetDomainUUID()
taskListType := int(request.GetTaskListType())
taskListName := request.TaskList.GetName()
pollerID := request.GetPollerID()
taskList := newTaskListID(domainID, taskListName, taskListType)
tlMgr, err := e.getTaskListManager(taskList)
if err != nil {
return err
}
tlMgr.CancelPoller(pollerID)
return nil
}
func (e *matchingEngineImpl) DescribeTaskList(ctx context.Context, request *m.DescribeTaskListRequest) (*workflow.DescribeTaskListResponse, error) {
domainID := request.GetDomainUUID()
taskListType := persistence.TaskListTypeDecision
if request.DescRequest.GetTaskListType() == workflow.TaskListTypeActivity {
taskListType = persistence.TaskListTypeActivity
}
taskListName := request.DescRequest.TaskList.GetName()
taskList := newTaskListID(domainID, taskListName, taskListType)
tlMgr, err := e.getTaskListManager(taskList)
if err != nil {
return nil, err
}
pollers := []*workflow.PollerInfo{}
for _, poller := range tlMgr.GetAllPollerInfo() {
pollers = append(pollers, &workflow.PollerInfo{
Identity: common.StringPtr(poller.identity),
LastAccessTime: common.Int64Ptr(poller.lastAccessTime.UnixNano()),
})
}
return &workflow.DescribeTaskListResponse{Pollers: pollers}, nil
}
// Loads a task from persistence and wraps it in a task context
func (e *matchingEngineImpl) getTask(
ctx context.Context, taskList *taskListID, maxDispatchPerSecond *float64,
) (*taskContext, error) {
tlMgr, err := e.getTaskListManager(taskList)
if err != nil {
return nil, err
}
return tlMgr.GetTaskContext(ctx, maxDispatchPerSecond)
}
func (e *matchingEngineImpl) unloadTaskList(id *taskListID) {
e.taskListsLock.Lock()
tlMgr, ok := e.taskLists[*id]
if ok {
delete(e.taskLists, *id)
}
e.taskListsLock.Unlock()
if ok {
tlMgr.Stop()
}
}
// Populate the decision task response based on context and scheduled/started events.
func (e *matchingEngineImpl) createPollForDecisionTaskResponse(context *taskContext,
historyResponse *h.RecordDecisionTaskStartedResponse) *m.PollForDecisionTaskResponse {
task := context.info
response := &m.PollForDecisionTaskResponse{}
response.WorkflowExecution = workflowExecutionPtr(context.workflowExecution)
if context.queryTaskInfo != nil {
// for a query task
queryRequest := context.queryTaskInfo.queryRequest
token := &common.QueryTaskToken{
DomainID: *queryRequest.DomainUUID,
TaskList: *queryRequest.TaskList.Name,
TaskID: context.queryTaskInfo.taskID,
}
response.TaskToken, _ = e.tokenSerializer.SerializeQueryTaskToken(token)
response.Query = context.queryTaskInfo.queryRequest.QueryRequest.Query
} else {
token := &common.TaskToken{
DomainID: task.DomainID,
WorkflowID: task.WorkflowID,
RunID: task.RunID,
ScheduleID: historyResponse.GetScheduledEventId(),
ScheduleAttempt: historyResponse.GetAttempt(),
}
response.TaskToken, _ = e.tokenSerializer.Serialize(token)
response.WorkflowType = historyResponse.WorkflowType
response.Attempt = common.Int64Ptr(historyResponse.GetAttempt())
}
if historyResponse.GetPreviousStartedEventId() != common.EmptyEventID {
response.PreviousStartedEventId = historyResponse.PreviousStartedEventId
}
response.WorkflowType = historyResponse.WorkflowType
response.StartedEventId = historyResponse.StartedEventId
response.StickyExecutionEnabled = historyResponse.StickyExecutionEnabled
response.BacklogCountHint = common.Int64Ptr(context.backlogCountHint)
response.NextEventId = historyResponse.NextEventId
response.DecisionInfo = historyResponse.DecisionInfo
return response
}
// Populate the activity task response based on context and scheduled/started events.
func (e *matchingEngineImpl) createPollForActivityTaskResponse(context *taskContext,
historyResponse *h.RecordActivityTaskStartedResponse) *workflow.PollForActivityTaskResponse {
task := context.info
startedEvent := historyResponse.StartedEvent
scheduledEvent := historyResponse.ScheduledEvent
if scheduledEvent.ActivityTaskScheduledEventAttributes == nil {
panic("GetActivityTaskScheduledEventAttributes is not set")
}
attributes := scheduledEvent.ActivityTaskScheduledEventAttributes
if attributes.ActivityId == nil {
panic("ActivityTaskScheduledEventAttributes.ActivityID is not set")
}
response := &workflow.PollForActivityTaskResponse{}
response.ActivityId = attributes.ActivityId
response.ActivityType = attributes.ActivityType
response.Input = attributes.Input
response.WorkflowExecution = workflowExecutionPtr(context.workflowExecution)
response.ScheduledTimestamp = common.Int64Ptr(*scheduledEvent.Timestamp)
response.ScheduleToCloseTimeoutSeconds = common.Int32Ptr(*attributes.ScheduleToCloseTimeoutSeconds)
response.StartedTimestamp = common.Int64Ptr(*startedEvent.Timestamp)
response.StartToCloseTimeoutSeconds = common.Int32Ptr(*attributes.StartToCloseTimeoutSeconds)
response.HeartbeatTimeoutSeconds = common.Int32Ptr(*attributes.HeartbeatTimeoutSeconds)
token := &common.TaskToken{
DomainID: task.DomainID,
WorkflowID: task.WorkflowID,
RunID: task.RunID,
ScheduleID: task.ScheduleID,
}
response.TaskToken, _ = e.tokenSerializer.Serialize(token)
return response
}
func newTaskListID(domainID, taskListName string, taskType int) *taskListID {
return &taskListID{domainID: domainID, taskListName: taskListName, taskType: taskType}
}
func workflowExecutionPtr(execution workflow.WorkflowExecution) *workflow.WorkflowExecution {
return &execution
}