forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
220 lines (188 loc) · 6.76 KB
/
batch.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
// Copyright (c) 2019 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 canary
import (
"context"
"fmt"
"time"
"go.uber.org/cadence"
"go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/client"
"go.uber.org/cadence/workflow"
)
func init() {
registerWorkflow(batchWorkflow, wfTypeBatch)
registerWorkflow(batchWorkflowParent, wfTypeBatchParent)
registerWorkflow(batchWorkflowChild, wfTypeBatchChild)
registerActivity(verifyBatchActivity, activityTypeVerifyBatch)
registerActivity(startBatchWorkflow, activityTypeStartBatch)
}
const (
// TODO: to get rid of them:
// after batch job has an API, we should use the API: https://github.com/uber/cadence/issues/2225
sysDomainName = "cadence-system"
sysBatchWFTypeName = "cadence-sys-batch-workflow"
systemBatcherTaskListName = "cadence-sys-batcher-tasklist"
// there are two level, so totally 5*5 + 5 == 30 descendants
// default batch RPS is 50, so it will takes ~1 seconds to terminate all
numChildrenPerLevel = 5
)
type (
// BatchParams is from server repo
// TODO: to get rid of it:
// after batch job has an API, we should use the API: https://github.com/uber/cadence/issues/2225
BatchParams struct {
DomainName string
Query string
Reason string
BatchType string
}
)
func batchWorkflow(ctx workflow.Context, scheduledTimeNanos int64, domain string) error {
profile, err := beginWorkflow(ctx, wfTypeBatch, scheduledTimeNanos)
if err != nil {
return err
}
cwo := workflow.ChildWorkflowOptions{
ExecutionStartToCloseTimeout: childWorkflowTimeout,
TaskStartToCloseTimeout: decisionTaskTimeout,
}
ctx = workflow.WithChildOptions(ctx, cwo)
var fs []workflow.ChildWorkflowFuture
for i := 0; i < numChildrenPerLevel; i++ {
f := workflow.ExecuteChildWorkflow(ctx, wfTypeBatchParent, scheduledTimeNanos)
fs = append(fs, f)
}
// waiting for all workflow started
for i := 0; i < numChildrenPerLevel; i++ {
err := fs[i].GetChildWorkflowExecution().Get(ctx, nil)
if err != nil {
return profile.end(err)
}
}
// waiting for visibility
if err := workflow.Sleep(ctx, time.Second*2); err != nil {
return profile.end(err)
}
retryPolicy := &cadence.RetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2,
MaximumInterval: time.Second * 12,
ExpirationInterval: time.Second * 3,
MaximumAttempts: 4,
}
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
TaskList: taskListName,
ScheduleToStartTimeout: scheduleToStartTimeout,
StartToCloseTimeout: activityTaskTimeout,
RetryPolicy: retryPolicy,
})
startTime := workflow.Now(ctx).Format(time.RFC3339)
err = workflow.ExecuteActivity(ctx, activityTypeStartBatch, domain, startTime).Get(ctx, nil)
if err != nil {
return profile.end(err)
}
// waiting for visibility
if err := workflow.Sleep(ctx, time.Second*2); err != nil {
return profile.end(err)
}
err = workflow.ExecuteActivity(ctx, activityTypeVerifyBatch, domain, startTime).Get(ctx, nil)
return profile.end(err)
}
func batchWorkflowParent(ctx workflow.Context, scheduledTimeNanos int64) error {
profile, err := beginWorkflow(ctx, wfTypeBatchParent, scheduledTimeNanos)
if err != nil {
return err
}
cwo := workflow.ChildWorkflowOptions{
ExecutionStartToCloseTimeout: childWorkflowTimeout,
TaskStartToCloseTimeout: decisionTaskTimeout,
}
ctx = workflow.WithChildOptions(ctx, cwo)
var fs []workflow.ChildWorkflowFuture
for i := 0; i < numChildrenPerLevel; i++ {
f := workflow.ExecuteChildWorkflow(ctx, wfTypeBatchChild, scheduledTimeNanos)
fs = append(fs, f)
}
// waiting for all workflow to finish
for i := 0; i < numChildrenPerLevel; i++ {
err := fs[i].Get(ctx, nil)
if err != nil {
return profile.end(err)
}
}
return profile.end(err)
}
func batchWorkflowChild(ctx workflow.Context, scheduledTimeNanos int64) error {
profile, err := beginWorkflow(ctx, wfTypeBatchChild, scheduledTimeNanos)
if err != nil {
return err
}
if err := workflow.Sleep(ctx, time.Hour); err != nil {
return profile.end(err)
}
return profile.end(err)
}
func startBatchWorkflow(ctx context.Context, domain, startTime string) error {
sdkClient := getContextValue(ctx, ctxKeyActivitySystemClient).(*activityContext).cadence
params := BatchParams{
DomainName: domain,
Query: "WorkflowType = '" + wfTypeBatchParent + "' AND CloseTime = missing AND StartTime <'" + startTime + "' ",
Reason: "batch canary",
BatchType: "terminate",
}
options := client.StartWorkflowOptions{
ExecutionStartToCloseTimeout: childWorkflowTimeout,
DecisionTaskStartToCloseTimeout: decisionTaskTimeout,
TaskList: systemBatcherTaskListName,
}
run, err := sdkClient.ExecuteWorkflow(ctx, options, sysBatchWFTypeName, params)
if err != nil {
return err
}
err = run.Get(ctx, nil)
return err
}
func verifyBatchActivity(ctx context.Context, domain, startTime string) error {
svClient := getActivityContext(ctx).cadence.Service
q1 := "WorkflowType = '" + wfTypeBatchParent + "' AND CloseTime = missing AND StartTime <'" + startTime + "' "
resp, err := svClient.CountWorkflowExecutions(ctx, &shared.CountWorkflowExecutionsRequest{
Domain: &domain,
Query: &q1,
})
if err != nil {
return err
}
if resp.GetCount() > 0 {
return fmt.Errorf("still seeing open workflows for %v , %v, %v", wfTypeBatchParent, q1, resp.GetCount())
}
q2 := "WorkflowType = '" + wfTypeBatchChild + "' AND CloseTime = missing AND StartTime <'" + startTime + "' "
resp, err = svClient.CountWorkflowExecutions(ctx, &shared.CountWorkflowExecutionsRequest{
Domain: &domain,
Query: &q2,
})
if err != nil {
return err
}
if resp.GetCount() > 0 {
return fmt.Errorf("still seeing open workflows for %v, %v, %v", wfTypeBatchChild, q2, resp.GetCount())
}
return nil
}