/
task.go
170 lines (155 loc) · 6.2 KB
/
task.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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// 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.
package matching
import (
commonpb "go.temporal.io/api/common/v1"
enumsspb "go.temporal.io/server/api/enums/v1"
"go.temporal.io/server/api/matchingservice/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/namespace"
)
type (
// genericTaskInfo contains the info for an activity or workflow task
genericTaskInfo struct {
*persistencespb.AllocatedTaskInfo
completionFunc func(*persistencespb.AllocatedTaskInfo, error)
}
// queryTaskInfo contains the info for a query task
queryTaskInfo struct {
taskID string
request *matchingservice.QueryWorkflowRequest
}
// startedTaskInfo contains info for any task received from
// another matching host. This type of task is already marked as started
startedTaskInfo struct {
workflowTaskInfo *matchingservice.PollWorkflowTaskQueueResponse
activityTaskInfo *matchingservice.PollActivityTaskQueueResponse
}
// internalTask represents an activity, workflow, query or started (received from another host).
// this struct is more like a union and only one of [ query, event, forwarded ] is
// non-nil for any given task
internalTask struct {
event *genericTaskInfo // non-nil for activity or workflow task that's locally generated
query *queryTaskInfo // non-nil for a query task that's locally sync matched
started *startedTaskInfo // non-nil for a task received from a parent partition which is already started
namespace namespace.Name
source enumsspb.TaskSource
forwardedFrom string // name of the child partition this task is forwarded from (empty if not forwarded)
responseC chan error // non-nil only where there is a caller waiting for response (sync-match)
backlogCountHint func() int64
}
)
func newInternalTask(
info *persistencespb.AllocatedTaskInfo,
completionFunc func(*persistencespb.AllocatedTaskInfo, error),
source enumsspb.TaskSource,
forwardedFrom string,
forSyncMatch bool,
) *internalTask {
task := &internalTask{
event: &genericTaskInfo{
AllocatedTaskInfo: info,
completionFunc: completionFunc,
},
source: source,
forwardedFrom: forwardedFrom,
}
if forSyncMatch {
task.responseC = make(chan error, 1)
}
return task
}
func newInternalQueryTask(
taskID string,
request *matchingservice.QueryWorkflowRequest,
) *internalTask {
return &internalTask{
query: &queryTaskInfo{
taskID: taskID,
request: request,
},
forwardedFrom: request.GetForwardedSource(),
responseC: make(chan error, 1),
}
}
func newInternalStartedTask(info *startedTaskInfo) *internalTask {
return &internalTask{started: info}
}
// isQuery returns true if the underlying task is a query task
func (task *internalTask) isQuery() bool {
return task.query != nil
}
// isStarted is true when this task is already marked as started
func (task *internalTask) isStarted() bool {
return task.started != nil
}
// isForwarded returns true if the underlying task is forwarded by a remote matching host
// forwarded tasks are already marked as started in history
func (task *internalTask) isForwarded() bool {
return task.forwardedFrom != ""
}
func (task *internalTask) isSyncMatchTask() bool {
return task.responseC != nil
}
func (task *internalTask) workflowExecution() *commonpb.WorkflowExecution {
switch {
case task.event != nil:
return &commonpb.WorkflowExecution{WorkflowId: task.event.Data.GetWorkflowId(), RunId: task.event.Data.GetRunId()}
case task.query != nil:
return task.query.request.GetQueryRequest().GetExecution()
case task.started != nil && task.started.workflowTaskInfo != nil:
return task.started.workflowTaskInfo.WorkflowExecution
case task.started != nil && task.started.activityTaskInfo != nil:
return task.started.activityTaskInfo.WorkflowExecution
}
return &commonpb.WorkflowExecution{}
}
// pollWorkflowTaskQueueResponse returns the poll response for a workflow task that is
// already marked as started. This method should only be called when isStarted() is true
func (task *internalTask) pollWorkflowTaskQueueResponse() *matchingservice.PollWorkflowTaskQueueResponse {
if task.isStarted() {
return task.started.workflowTaskInfo
}
return nil
}
// pollActivityTaskQueueResponse returns the poll response for an activity task that is
// already marked as started. This method should only be called when isStarted() is true
func (task *internalTask) pollActivityTaskQueueResponse() *matchingservice.PollActivityTaskQueueResponse {
if task.isStarted() {
return task.started.activityTaskInfo
}
return nil
}
// finish marks a task as finished. Should be called after a poller picks up a task
// and marks it as started. If the task is unable to marked as started, then this
// method should be called with a non-nil error argument.
func (task *internalTask) finish(err error) {
switch {
case task.responseC != nil:
task.responseC <- err
case task.event.completionFunc != nil:
// TODO: this probably should not be done synchronously in PollWorkflow/ActivityTaskQueue
task.event.completionFunc(task.event.AllocatedTaskInfo, err)
}
}