-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathworker.go
353 lines (304 loc) · 17.2 KB
/
worker.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
// Copyright (c) 2017-2020 Uber Technologies Inc.
// Portions of the Software are attributed to Copyright (c) 2020 Temporal 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 internal
import (
"context"
"time"
"github.com/opentracing/opentracing-go"
"github.com/uber-go/tally"
"go.uber.org/zap"
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
"go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/internal/common/auth"
)
type (
// WorkerOptions is used to configure a worker instance.
// The current timeout resolution implementation is in seconds and uses math.Ceil(d.Seconds()) as the duration. But is
// subjected to change in the future.
WorkerOptions struct {
// Optional: To set the maximum concurrent activity executions this worker can have.
// The zero value of this uses the default value.
// default: defaultMaxConcurrentActivityExecutionSize(1k)
MaxConcurrentActivityExecutionSize int
// Optional: Sets the rate limiting on number of activities that can be executed per second per
// worker. This can be used to limit resources used by the worker.
// Notice that the number is represented in float, so that you can set it to less than
// 1 if needed. For example, set the number to 0.1 means you want your activity to be executed
// once for every 10 seconds. This can be used to protect down stream services from flooding.
// The zero value of this uses the default value. Default: 100k
WorkerActivitiesPerSecond float64
// Optional: To set the maximum concurrent local activity executions this worker can have.
// The zero value of this uses the default value.
// default: 1k
MaxConcurrentLocalActivityExecutionSize int
// Optional: Sets the rate limiting on number of local activities that can be executed per second per
// worker. This can be used to limit resources used by the worker.
// Notice that the number is represented in float, so that you can set it to less than
// 1 if needed. For example, set the number to 0.1 means you want your local activity to be executed
// once for every 10 seconds. This can be used to protect down stream services from flooding.
// The zero value of this uses the default value. Default: 100k
WorkerLocalActivitiesPerSecond float64
// Optional: Sets the rate limiting on number of activities that can be executed per second.
// This is managed by the server and controls activities per second for your entire tasklist
// whereas WorkerActivityTasksPerSecond controls activities only per worker.
// Notice that the number is represented in float, so that you can set it to less than
// 1 if needed. For example, set the number to 0.1 means you want your activity to be executed
// once for every 10 seconds. This can be used to protect down stream services from flooding.
// The zero value of this uses the default value. Default: 100k
TaskListActivitiesPerSecond float64
// optional: Sets the maximum number of goroutines that will concurrently poll the
// cadence-server to retrieve activity tasks. Changing this value will affect the
// rate at which the worker is able to consume tasks from a task list.
// Default value is 2
MaxConcurrentActivityTaskPollers int
// optional: Sets the minimum number of goroutines that will concurrently poll the
// cadence-server to retrieve activity tasks. Changing this value will NOT affect the
// rate at which the worker is able to consume tasks from a task list,
// unless FeatureFlags.PollerAutoScalerEnabled is set to true.
// Default value is 1
MinConcurrentActivityTaskPollers int
// Optional: To set the maximum concurrent decision task executions this worker can have.
// The zero value of this uses the default value.
// default: defaultMaxConcurrentTaskExecutionSize(1k)
MaxConcurrentDecisionTaskExecutionSize int
// Optional: Sets the rate limiting on number of decision tasks that can be executed per second per
// worker. This can be used to limit resources used by the worker.
// The zero value of this uses the default value. Default: 100k
WorkerDecisionTasksPerSecond float64
// optional: Sets the maximum number of goroutines that will concurrently poll the
// cadence-server to retrieve decision tasks. Changing this value will affect the
// rate at which the worker is able to consume tasks from a task list.
// Default value is 2
MaxConcurrentDecisionTaskPollers int
// optional: Sets the minimum number of goroutines that will concurrently poll the
// cadence-server to retrieve decision tasks. If FeatureFlags.PollerAutoScalerEnabled is set to true,
// changing this value will NOT affect the rate at which the worker is able to consume tasks from a task list.
// Default value is 1
MinConcurrentDecisionTaskPollers int
// optional: Sets the interval of poller autoscaling, between which poller autoscaler changes the poller count
// based on poll result. It takes effect if FeatureFlags.PollerAutoScalerEnabled is set to true.
// Default value is 1 min
PollerAutoScalerCooldown time.Duration
// optional: Sets the target utilization rate between [0,1].
// Utilization Rate = pollResultWithTask / (pollResultWithTask + pollResultWithNoTask)
// It takes effect if FeatureFlags.PollerAutoScalerEnabled is set to true.
// Default value is 0.6
PollerAutoScalerTargetUtilization float64
// optional: Sets whether to start dry run mode of autoscaler.
// Default value is false
PollerAutoScalerDryRun bool
// Optional: Sets an identify that can be used to track this host for debugging.
// default: default identity that include hostname, groupName and process ID.
Identity string
// Optional: Defines the 'zone' or the failure group that the worker belongs to
IsolationGroup string
// Optional: Metrics to be reported. Metrics emitted by the cadence client are not prometheus compatible by
// default. To ensure metrics are compatible with prometheus make sure to create tally scope with sanitizer
// options set.
// var (
// _safeCharacters = []rune{'_'}
// _sanitizeOptions = tally.SanitizeOptions{
// NameCharacters: tally.ValidCharacters{
// Ranges: tally.AlphanumericRange,
// Characters: _safeCharacters,
// },
// KeyCharacters: tally.ValidCharacters{
// Ranges: tally.AlphanumericRange,
// Characters: _safeCharacters,
// },
// ValueCharacters: tally.ValidCharacters{
// Ranges: tally.AlphanumericRange,
// Characters: _safeCharacters,
// },
// ReplacementCharacter: tally.DefaultReplacementCharacter,
// }
// )
// opts := tally.ScopeOptions{
// Reporter: reporter,
// SanitizeOptions: &_sanitizeOptions,
// }
// scope, _ := tally.NewRootScope(opts, time.Second)
// default: no metrics.
MetricsScope tally.Scope
// Optional: Logger framework can use to log.
// default: default logger provided.
Logger *zap.Logger
// Optional: Enable logging in replay.
// In the workflow code you can use workflow.GetLogger(ctx) to write logs. By default, the logger will skip log
// entry during replay mode so you won't see duplicate logs. This option will enable the logging in replay mode.
// This is only useful for debugging purpose.
// default: false
EnableLoggingInReplay bool
// Optional: Disable running workflow workers.
// default: false
DisableWorkflowWorker bool
// Optional: Disable running activity workers.
// default: false
DisableActivityWorker bool
// Optional: Disable sticky execution.
// default: false
// Sticky Execution is to run the decision tasks for one workflow execution on same worker host. This is an
// optimization for workflow execution. When sticky execution is enabled, worker keeps the workflow state in
// memory. New decision task contains the new history events will be dispatched to the same worker. If this
// worker crashes, the sticky decision task will timeout after StickyScheduleToStartTimeout, and cadence server
// will clear the stickiness for that workflow execution and automatically reschedule a new decision task that
// is available for any worker to pick up and resume the progress.
DisableStickyExecution bool
// Optional: Sticky schedule to start timeout.
// default: 5s
// The resolution is seconds. See details about StickyExecution on the comments for DisableStickyExecution.
StickyScheduleToStartTimeout time.Duration
// Optional: sets context for activity. The context can be used to pass any configuration to activity
// like common logger for all activities.
BackgroundActivityContext context.Context
// Optional: Sets how decision worker deals with non-deterministic history events
// (presumably arising from non-deterministic workflow definitions or non-backward compatible workflow definition changes).
// default: NonDeterministicWorkflowPolicyBlockWorkflow, which just logs error but reply nothing back to server
NonDeterministicWorkflowPolicy NonDeterministicWorkflowPolicy
// Optional: Sets DataConverter to customize serialization/deserialization of arguments in Cadence
// default: defaultDataConverter, an combination of thriftEncoder and jsonEncoder
DataConverter DataConverter
// Optional: worker graceful shutdown timeout
// default: 0s
WorkerStopTimeout time.Duration
// Optional: Enable running session workers.
// Session workers is for activities within a session.
// Enable this option to allow worker to process sessions.
// default: false
EnableSessionWorker bool
// Uncomment this option when we support automatic reestablish failed sessions.
// Optional: The identifier of the resource consumed by sessions.
// It's the user's responsibility to ensure there's only one worker using this resourceID.
// For now, if user doesn't specify one, a new uuid will be used as the resourceID.
// SessionResourceID string
// Optional: Sets the maximum number of concurrently running sessions the resource support.
// default: 1000
MaxConcurrentSessionExecutionSize int
// Optional: Specifies factories used to instantiate workflow interceptor chain
// The chain is instantiated per each replay of a workflow execution
WorkflowInterceptorChainFactories []WorkflowInterceptorFactory
// Optional: Sets ContextPropagators that allows users to control the context information passed through a workflow
// default: no ContextPropagators
ContextPropagators []ContextPropagator
// Optional: Sets opentracing Tracer that is to be used to emit tracing information
// default: no tracer - opentracing.NoopTracer
Tracer opentracing.Tracer
// Optional: Enable worker for running shadowing workflows to replay existing workflows
// If set to true:
// 1. Worker will run in shadow mode and all other workers (decision, activity, session)
// will be disabled to prevent them from updating existing workflow states.
// 2. DataConverter, WorkflowInterceptorChainFactories, ContextPropagators, Tracer will be
// used as ReplayOptions and forwarded to the underlying WorkflowReplayer.
// The actual shadower activity worker will not use them.
// 3. TaskList will become Domain-TaskList, to prevent conflict across domains as there's
// only one shadowing domain which is responsible for shadowing workflows for all domains.
// default: false
EnableShadowWorker bool
// Optional: Configures shadowing workflow
// default: please check the documentation for ShadowOptions for default options
ShadowOptions ShadowOptions
// Optional: Flags to turn on/off some server side options
// default: all the features in the struct are turned off
FeatureFlags FeatureFlags
// Optional: Authorization interface to get the Auth Token
// default: No provider
Authorization auth.AuthorizationProvider
// Optional: Host is just string on the machine running the client
// default: empty string
Host string
}
)
// NonDeterministicWorkflowPolicy is an enum for configuring how client's decision task handler deals with
// mismatched history events (presumably arising from non-deterministic workflow definitions).
type NonDeterministicWorkflowPolicy int
const (
// NonDeterministicWorkflowPolicyBlockWorkflow is the default policy for handling detected non-determinism.
// This option simply logs to console with an error message that non-determinism is detected, but
// does *NOT* reply anything back to the server.
// It is chosen as default for backward compatibility reasons because it preserves the old behavior
// for handling non-determinism that we had before NonDeterministicWorkflowPolicy type was added to
// allow more configurability.
NonDeterministicWorkflowPolicyBlockWorkflow NonDeterministicWorkflowPolicy = iota
// NonDeterministicWorkflowPolicyFailWorkflow behaves exactly the same as Ignore, up until the very
// end of processing a decision task.
// Whereas default does *NOT* reply anything back to the server, fail workflow replies back with a request
// to fail the workflow execution.
NonDeterministicWorkflowPolicyFailWorkflow
)
// NewWorker creates an instance of worker for managing workflow and activity executions.
// service - thrift connection to the cadence server.
// domain - the name of the cadence domain.
// taskList - is the task list name you use to identify your client worker, also
//
// identifies group of workflow and activity implementations that are hosted by a single worker process.
//
// options - configure any worker specific options like logger, metrics, identity.
func NewWorker(
service workflowserviceclient.Interface,
domain string,
taskList string,
options WorkerOptions,
) *aggregatedWorker {
return newAggregatedWorker(service, domain, taskList, options)
}
// ReplayWorkflowExecution loads a workflow execution history from the Cadence service and executes a single decision task for it.
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
// The logger is the only optional parameter. Defaults to the noop logger.
// Deprecated: Global workflow replay methods are replaced by equivalent WorkflowReplayer instance methods.
// This method is kept to maintain backward compatibility and should not be used.
func ReplayWorkflowExecution(
ctx context.Context,
service workflowserviceclient.Interface,
logger *zap.Logger,
domain string,
execution WorkflowExecution,
) error {
r := NewWorkflowReplayer()
return r.ReplayWorkflowExecution(ctx, service, logger, domain, execution)
}
// ReplayWorkflowHistory executes a single decision task for the given history.
// Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
// The logger is an optional parameter. Defaults to the noop logger.
// Deprecated: Global workflow replay methods are replaced by equivalent WorkflowReplayer instance methods.
// This method is kept to maintain backward compatibility and should not be used.
func ReplayWorkflowHistory(logger *zap.Logger, history *shared.History) error {
r := NewWorkflowReplayer()
return r.ReplayWorkflowHistory(logger, history)
}
// ReplayWorkflowHistoryFromJSONFile executes a single decision task for the given json history file.
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
// The logger is an optional parameter. Defaults to the noop logger.
// Deprecated: Global workflow replay methods are replaced by equivalent WorkflowReplayer instance methods.
// This method is kept to maintain backward compatibility and should not be used.
func ReplayWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string) error {
r := NewWorkflowReplayer()
return r.ReplayWorkflowHistoryFromJSONFile(logger, jsonfileName)
}
// ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the given json history file upto provided
// lastEventID(inclusive).
// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
// The logger is an optional parameter. Defaults to the noop logger.
// Deprecated: Global workflow replay methods are replaced by equivalent WorkflowReplayer instance methods.
// This method is kept to maintain backward compatibility and should not be used.
func ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.Logger, jsonfileName string, lastEventID int64) error {
r := NewWorkflowReplayer()
return r.ReplayPartialWorkflowHistoryFromJSONFile(logger, jsonfileName, lastEventID)
}