forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
438 lines (376 loc) · 15.9 KB
/
util.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
// 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 common
import (
"encoding/json"
"sync"
"time"
"github.com/robfig/cron"
"github.com/uber/cadence/common/logging"
"github.com/uber/cadence/common/metrics"
"go.uber.org/yarpc/yarpcerrors"
"golang.org/x/net/context"
farm "github.com/dgryski/go-farm"
"github.com/uber-common/bark"
"math/rand"
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/common/backoff"
)
const (
retryPersistenceOperationInitialInterval = 50 * time.Millisecond
retryPersistenceOperationMaxInterval = 10 * time.Second
retryPersistenceOperationExpirationInterval = 30 * time.Second
historyServiceOperationInitialInterval = 50 * time.Millisecond
historyServiceOperationMaxInterval = 10 * time.Second
historyServiceOperationExpirationInterval = 30 * time.Second
matchingServiceOperationInitialInterval = 1000 * time.Millisecond
matchingServiceOperationMaxInterval = 10 * time.Second
matchingServiceOperationExpirationInterval = 30 * time.Second
frontendServiceOperationInitialInterval = 200 * time.Millisecond
frontendServiceOperationMaxInterval = 5 * time.Second
frontendServiceOperationExpirationInterval = 15 * time.Second
publicClientOperationInitialInterval = 200 * time.Millisecond
publicClientOperationMaxInterval = 5 * time.Second
publicClientOperationExpirationInterval = 15 * time.Second
adminServiceOperationInitialInterval = 200 * time.Millisecond
adminServiceOperationMaxInterval = 5 * time.Second
adminServiceOperationExpirationInterval = 15 * time.Second
retryKafkaOperationInitialInterval = 50 * time.Millisecond
retryKafkaOperationMaxInterval = 10 * time.Second
retryKafkaOperationExpirationInterval = 30 * time.Second
retryBlobstoreClientInitialInterval = time.Second
retryBlobstoreClientMaxInterval = 10 * time.Second
retryBlobstoreClientExpirationInterval = time.Minute
// FailureReasonCompleteResultExceedsLimit is failureReason for complete result exceeds limit
FailureReasonCompleteResultExceedsLimit = "COMPLETE_RESULT_EXCEEDS_LIMIT"
// FailureReasonFailureDetailsExceedsLimit is failureReason for failure details exceeds limit
FailureReasonFailureDetailsExceedsLimit = "FAILURE_DETAILS_EXCEEDS_LIMIT"
// FailureReasonCancelDetailsExceedsLimit is failureReason for cancel details exceeds limit
FailureReasonCancelDetailsExceedsLimit = "CANCEL_DETAILS_EXCEEDS_LIMIT"
//FailureReasonHeartbeatExceedsLimit is failureReason for heartbeat exceeds limit
FailureReasonHeartbeatExceedsLimit = "HEARTBEAT_EXCEEDS_LIMIT"
// FailureReasonDecisionBlobSizeExceedsLimit is the failureReason for decision blob exceeds size limit
FailureReasonDecisionBlobSizeExceedsLimit = "DECISION_BLOB_SIZE_EXCEEDS_LIMIT"
// TerminateReasonSizeExceedsLimit is reason to terminate workflow when history size exceed limit
TerminateReasonSizeExceedsLimit = "HISTORY_SIZE_EXCEEDS_LIMIT"
)
var (
// ErrBlobSizeExceedsLimit is error for event blob size exceeds limit
ErrBlobSizeExceedsLimit = &workflow.BadRequestError{Message: "Blob data size exceeds limit."}
)
// AwaitWaitGroup calls Wait on the given wait
// Returns true if the Wait() call succeeded before the timeout
// Returns false if the Wait() did not return before the timeout
func AwaitWaitGroup(wg *sync.WaitGroup, timeout time.Duration) bool {
doneC := make(chan struct{})
go func() {
wg.Wait()
close(doneC)
}()
select {
case <-doneC:
return true
case <-time.After(timeout):
return false
}
}
// AddSecondsToBaseTime - Gets the UnixNano with given duration and base time.
func AddSecondsToBaseTime(baseTimeInNanoSec int64, durationInSeconds int64) int64 {
timeOut := time.Duration(durationInSeconds) * time.Second
return time.Unix(0, baseTimeInNanoSec).Add(timeOut).UnixNano()
}
// CreatePersistanceRetryPolicy creates a retry policy for persistence layer operations
func CreatePersistanceRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(retryPersistenceOperationInitialInterval)
policy.SetMaximumInterval(retryPersistenceOperationMaxInterval)
policy.SetExpirationInterval(retryPersistenceOperationExpirationInterval)
return policy
}
// CreateHistoryServiceRetryPolicy creates a retry policy for calls to history service
func CreateHistoryServiceRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(historyServiceOperationInitialInterval)
policy.SetMaximumInterval(historyServiceOperationMaxInterval)
policy.SetExpirationInterval(historyServiceOperationExpirationInterval)
return policy
}
// CreateMatchingServiceRetryPolicy creates a retry policy for calls to matching service
func CreateMatchingServiceRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(matchingServiceOperationInitialInterval)
policy.SetMaximumInterval(matchingServiceOperationMaxInterval)
policy.SetExpirationInterval(matchingServiceOperationExpirationInterval)
return policy
}
// CreateFrontendServiceRetryPolicy creates a retry policy for calls to frontend service
func CreateFrontendServiceRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(frontendServiceOperationInitialInterval)
policy.SetMaximumInterval(frontendServiceOperationMaxInterval)
policy.SetExpirationInterval(frontendServiceOperationExpirationInterval)
return policy
}
// CreateAdminServiceRetryPolicy creates a retry policy for calls to matching service
func CreateAdminServiceRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(adminServiceOperationInitialInterval)
policy.SetMaximumInterval(adminServiceOperationMaxInterval)
policy.SetExpirationInterval(adminServiceOperationExpirationInterval)
return policy
}
// CreatePublicClientRetryPolicy creates a retry policy for calls to frontend service
func CreatePublicClientRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(publicClientOperationInitialInterval)
policy.SetMaximumInterval(publicClientOperationMaxInterval)
policy.SetExpirationInterval(publicClientOperationExpirationInterval)
return policy
}
// CreateKafkaOperationRetryPolicy creates a retry policy for kafka operation
func CreateKafkaOperationRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(retryKafkaOperationInitialInterval)
policy.SetMaximumInterval(retryKafkaOperationMaxInterval)
policy.SetExpirationInterval(retryKafkaOperationExpirationInterval)
return policy
}
// CreateBlobstoreClientRetryPolicy creates a retry policy for blobstore client
func CreateBlobstoreClientRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(retryBlobstoreClientInitialInterval)
policy.SetMaximumInterval(retryBlobstoreClientMaxInterval)
policy.SetExpirationInterval(retryBlobstoreClientExpirationInterval)
return policy
}
// IsPersistenceTransientError checks if the error is a transient persistence error
func IsPersistenceTransientError(err error) bool {
switch err.(type) {
case *workflow.InternalServiceError, *workflow.ServiceBusyError:
return true
}
return false
}
// IsKafkaTransientError check if the error is a transient kafka error
func IsKafkaTransientError(err error) bool {
return true
}
// IsBlobstoreTransientError checks if the error is a retryable error.
func IsBlobstoreTransientError(err error) bool {
return !IsBlobstoreNonRetryableError(err)
}
// IsBlobstoreNonRetryableError checks if the error is a non retryable error.
func IsBlobstoreNonRetryableError(err error) bool {
switch err.(type) {
case *workflow.BadRequestError:
return true
case *workflow.EntityNotExistsError:
return true
}
return false
}
// IsServiceTransientError checks if the error is a retryable error.
func IsServiceTransientError(err error) bool {
return !IsServiceNonRetryableError(err)
}
// IsServiceNonRetryableError checks if the error is a non retryable error.
func IsServiceNonRetryableError(err error) bool {
switch err.(type) {
case *workflow.EntityNotExistsError:
return true
case *workflow.BadRequestError:
return true
case *workflow.DomainNotActiveError:
return true
case *workflow.CancellationAlreadyRequestedError:
return true
case *yarpcerrors.Status:
rpcErr := err.(*yarpcerrors.Status)
if rpcErr.Code() != yarpcerrors.CodeDeadlineExceeded {
return true
}
return false
}
return false
}
// IsWhitelistServiceTransientError checks if the error is a transient error.
func IsWhitelistServiceTransientError(err error) bool {
if err == context.DeadlineExceeded {
return true
}
switch err.(type) {
case *workflow.InternalServiceError:
return true
case *workflow.ServiceBusyError:
return true
case *workflow.LimitExceededError:
return true
case *h.ShardOwnershipLostError:
return true
case *yarpcerrors.Status:
// We only selectively retry the following yarpc errors client can safe retry with a backoff
if yarpcerrors.IsDeadlineExceeded(err) ||
yarpcerrors.IsUnavailable(err) ||
yarpcerrors.IsUnknown(err) ||
yarpcerrors.IsInternal(err) {
return true
}
return false
}
return false
}
// WorkflowIDToHistoryShard is used to map workflowID to a shardID
func WorkflowIDToHistoryShard(workflowID string, numberOfShards int) int {
hash := farm.Fingerprint32([]byte(workflowID))
return int(hash % uint32(numberOfShards))
}
// PrettyPrintHistory prints history in human readable format
func PrettyPrintHistory(history *workflow.History, logger bark.Logger) {
data, err := json.MarshalIndent(history, "", " ")
if err != nil {
logger.Errorf("Error serializing history: %v\n", err)
}
logger.Info("******************************************")
logger.Infof("History: %v", string(data))
logger.Info("******************************************")
}
// IsValidContext checks that the thrift context is not expired on cancelled.
// Returns nil if the context is still valid. Otherwise, returns the result of
// ctx.Err()
func IsValidContext(ctx context.Context) error {
ch := ctx.Done()
if ch != nil {
select {
case <-ch:
return ctx.Err()
default:
return nil
}
}
return nil
}
// GenerateRandomString is used for generate test string
func GenerateRandomString(n int) string {
rand.Seed(time.Now().UnixNano())
letterRunes := []rune("random")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
// CreateMatchingPollForDecisionTaskResponse create response for matching's PollForDecisionTask
func CreateMatchingPollForDecisionTaskResponse(historyResponse *h.RecordDecisionTaskStartedResponse, workflowExecution *workflow.WorkflowExecution, token []byte) *m.PollForDecisionTaskResponse {
matchingResp := &m.PollForDecisionTaskResponse{
WorkflowExecution: workflowExecution,
TaskToken: token,
Attempt: Int64Ptr(historyResponse.GetAttempt()),
WorkflowType: historyResponse.WorkflowType,
StartedEventId: historyResponse.StartedEventId,
StickyExecutionEnabled: historyResponse.StickyExecutionEnabled,
NextEventId: historyResponse.NextEventId,
DecisionInfo: historyResponse.DecisionInfo,
WorkflowExecutionTaskList: historyResponse.WorkflowExecutionTaskList,
EventStoreVersion: historyResponse.EventStoreVersion,
BranchToken: historyResponse.BranchToken,
}
if historyResponse.GetPreviousStartedEventId() != EmptyEventID {
matchingResp.PreviousStartedEventId = historyResponse.PreviousStartedEventId
}
return matchingResp
}
// MinInt32 return smaller one of two inputs int32
func MinInt32(a, b int32) int32 {
if a < b {
return a
}
return b
}
// ValidateRetryPolicy validates a retry policy
func ValidateRetryPolicy(policy *workflow.RetryPolicy) error {
if policy == nil {
// nil policy is valid which means no retry
return nil
}
if policy.GetInitialIntervalInSeconds() <= 0 {
return &workflow.BadRequestError{Message: "InitialIntervalInSeconds must be greater than 0 on retry policy."}
}
if policy.GetBackoffCoefficient() < 1 {
return &workflow.BadRequestError{Message: "BackoffCoefficient cannot be less than 1 on retry policy."}
}
if policy.GetMaximumIntervalInSeconds() < 0 {
return &workflow.BadRequestError{Message: "MaximumIntervalInSeconds cannot be less than 0 on retry policy."}
}
if policy.GetMaximumIntervalInSeconds() > 0 && policy.GetMaximumIntervalInSeconds() < policy.GetInitialIntervalInSeconds() {
return &workflow.BadRequestError{Message: "MaximumIntervalInSeconds cannot be less than InitialIntervalInSeconds on retry policy."}
}
if policy.GetMaximumAttempts() < 0 {
return &workflow.BadRequestError{Message: "MaximumAttempts cannot be less than 0 on retry policy."}
}
if policy.GetExpirationIntervalInSeconds() < 0 {
return &workflow.BadRequestError{Message: "ExpirationIntervalInSeconds cannot be less than 0 on retry policy."}
}
if policy.GetMaximumAttempts() == 0 && policy.GetExpirationIntervalInSeconds() == 0 {
return &workflow.BadRequestError{Message: "MaximumAttempts and ExpirationIntervalInSeconds are both 0. At least one of them must be specified."}
}
return nil
}
// ValidateCronSchedule validates a cron schedule spec
func ValidateCronSchedule(cronSchedule string) error {
if cronSchedule == "" {
return nil
}
if _, err := cron.Parse(cronSchedule); err != nil {
return &workflow.BadRequestError{Message: "Invalid CronSchedule."}
}
return nil
}
// CreateHistoryStartWorkflowRequest create a start workflow request for history
func CreateHistoryStartWorkflowRequest(domainID string, startRequest *workflow.StartWorkflowExecutionRequest) *h.StartWorkflowExecutionRequest {
histRequest := &h.StartWorkflowExecutionRequest{
DomainUUID: StringPtr(domainID),
StartRequest: startRequest,
}
if startRequest.RetryPolicy != nil && startRequest.RetryPolicy.GetExpirationIntervalInSeconds() > 0 {
expirationInSeconds := startRequest.RetryPolicy.GetExpirationIntervalInSeconds()
deadline := time.Now().Add(time.Second * time.Duration(expirationInSeconds))
histRequest.ExpirationTimestamp = Int64Ptr(deadline.Round(time.Millisecond).UnixNano())
}
return histRequest
}
// CheckEventBlobSizeLimit checks if a blob data exceeds limits. It logs a warning if it exceeds warnLimit,
// and return ErrBlobSizeExceedsLimit if it exceeds errorLimit.
func CheckEventBlobSizeLimit(actualSize, warnLimit, errorLimit int, domainID, workflowID, runID string, metricsClient metrics.Client, scope int, logger bark.Logger) error {
if metricsClient != nil {
metricsClient.RecordTimer(
scope,
metrics.EventBlobSize,
time.Duration(actualSize),
)
}
if actualSize > warnLimit {
if logger != nil {
logger.WithFields(bark.Fields{
logging.TagDomainID: domainID,
logging.TagWorkflowExecutionID: workflowID,
logging.TagWorkflowRunID: runID,
logging.TagSize: actualSize,
}).Warn("Blob size exceeds limit.")
}
if actualSize > errorLimit {
return ErrBlobSizeExceedsLimit
}
}
return nil
}