-
Notifications
You must be signed in to change notification settings - Fork 755
/
commandChecker.go
734 lines (619 loc) · 24.9 KB
/
commandChecker.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// 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 history
import (
"fmt"
"strings"
"time"
"github.com/pborman/uuid"
commandpb "go.temporal.io/api/command/v1"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/backoff"
"go.temporal.io/server/common/cache"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/failure"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/primitives/timestamp"
"go.temporal.io/server/common/searchattribute"
"go.temporal.io/server/service/history/configs"
)
type (
commandAttrValidator struct {
namespaceCache cache.NamespaceCache
config *configs.Config
maxIDLengthLimit int
searchAttributesValidator *searchattribute.Validator
getDefaultActivityRetrySettings dynamicconfig.MapPropertyFnWithNamespaceFilter
getDefaultWorkflowRetrySettings dynamicconfig.MapPropertyFnWithNamespaceFilter
enableCrossNamespaceCommands dynamicconfig.BoolPropertyFn
}
workflowSizeChecker struct {
blobSizeLimitWarn int
blobSizeLimitError int
historySizeLimitWarn int
historySizeLimitError int
historyCountLimitWarn int
historyCountLimitError int
completedID int64
mutableState mutableState
searchAttributesValidator *searchattribute.Validator
executionStats *persistencespb.ExecutionStats
metricsScope metrics.Scope
logger log.Logger
}
)
const (
reservedTaskQueuePrefix = "/_sys/"
)
func newCommandAttrValidator(
namespaceCache cache.NamespaceCache,
config *configs.Config,
searchAttributesValidator *searchattribute.Validator,
) *commandAttrValidator {
return &commandAttrValidator{
namespaceCache: namespaceCache,
config: config,
maxIDLengthLimit: config.MaxIDLengthLimit(),
searchAttributesValidator: searchAttributesValidator,
getDefaultActivityRetrySettings: config.DefaultActivityRetryPolicy,
getDefaultWorkflowRetrySettings: config.DefaultWorkflowRetryPolicy,
enableCrossNamespaceCommands: config.EnableCrossNamespaceCommands,
}
}
func newWorkflowSizeChecker(
blobSizeLimitWarn int,
blobSizeLimitError int,
historySizeLimitWarn int,
historySizeLimitError int,
historyCountLimitWarn int,
historyCountLimitError int,
completedID int64,
mutableState mutableState,
searchAttributesValidator *searchattribute.Validator,
executionStats *persistencespb.ExecutionStats,
metricsScope metrics.Scope,
logger log.Logger,
) *workflowSizeChecker {
return &workflowSizeChecker{
blobSizeLimitWarn: blobSizeLimitWarn,
blobSizeLimitError: blobSizeLimitError,
historySizeLimitWarn: historySizeLimitWarn,
historySizeLimitError: historySizeLimitError,
historyCountLimitWarn: historyCountLimitWarn,
historyCountLimitError: historyCountLimitError,
completedID: completedID,
mutableState: mutableState,
searchAttributesValidator: searchAttributesValidator,
executionStats: executionStats,
metricsScope: metricsScope,
logger: logger,
}
}
func (c *workflowSizeChecker) failWorkflowIfPayloadSizeExceedsLimit(
commandTypeTag metrics.Tag,
payloadSize int,
message string,
) (bool, error) {
executionInfo := c.mutableState.GetExecutionInfo()
executionState := c.mutableState.GetExecutionState()
err := common.CheckEventBlobSizeLimit(
payloadSize,
c.blobSizeLimitWarn,
c.blobSizeLimitError,
executionInfo.NamespaceId,
executionInfo.WorkflowId,
executionState.RunId,
c.metricsScope.Tagged(commandTypeTag),
c.logger,
tag.BlobSizeViolationOperation(commandTypeTag.Value()),
)
if err == nil {
return false, nil
}
attributes := &commandpb.FailWorkflowExecutionCommandAttributes{
Failure: failure.NewServerFailure(message, true),
}
if _, err := c.mutableState.AddFailWorkflowEvent(c.completedID, enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, attributes); err != nil {
return false, err
}
return true, nil
}
func (c *workflowSizeChecker) failWorkflowIfSearchAttributesSizeExceedsLimit(
searchAttributes *commonpb.SearchAttributes,
namespace string,
commandTypeTag metrics.Tag,
) (bool, error) {
c.metricsScope.Tagged(commandTypeTag).RecordDistribution(metrics.SearchAttributesSize, searchAttributes.Size())
err := c.searchAttributesValidator.ValidateSize(searchAttributes, namespace)
if err == nil {
return false, nil
}
c.logger.Warn("Search attributes size exceeds limits. Fail workflow.", tag.Error(err), tag.WorkflowNamespace(namespace))
attributes := &commandpb.FailWorkflowExecutionCommandAttributes{
Failure: failure.NewServerFailure(err.Error(), true),
}
if _, err := c.mutableState.AddFailWorkflowEvent(c.completedID, enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, attributes); err != nil {
return false, err
}
return true, nil
}
func (v *commandAttrValidator) validateActivityScheduleAttributes(
namespaceID string,
targetNamespaceID string,
attributes *commandpb.ScheduleActivityTaskCommandAttributes,
runTimeout time.Duration,
) error {
if err := v.validateCrossNamespaceCall(
namespaceID,
targetNamespaceID,
); err != nil {
return err
}
if attributes == nil {
return serviceerror.NewInvalidArgument("ScheduleActivityTaskCommandAttributes is not set on command.")
}
defaultTaskQueueName := ""
if _, err := v.validateTaskQueue(attributes.TaskQueue, defaultTaskQueueName); err != nil {
return err
}
if attributes.GetActivityId() == "" {
return serviceerror.NewInvalidArgument("ActivityId is not set on command.")
}
if attributes.ActivityType == nil || attributes.ActivityType.GetName() == "" {
return serviceerror.NewInvalidArgument("ActivityType is not set on command.")
}
if err := v.validateActivityRetryPolicy(attributes); err != nil {
return err
}
if len(attributes.GetActivityId()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("ActivityID exceeds length limit.")
}
if len(attributes.GetActivityType().GetName()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("ActivityType exceeds length limit.")
}
if len(attributes.GetNamespace()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("Namespace exceeds length limit.")
}
// Only attempt to deduce and fill in unspecified timeouts only when all timeouts are non-negative.
if timestamp.DurationValue(attributes.GetScheduleToCloseTimeout()) < 0 || timestamp.DurationValue(attributes.GetScheduleToStartTimeout()) < 0 ||
timestamp.DurationValue(attributes.GetStartToCloseTimeout()) < 0 || timestamp.DurationValue(attributes.GetHeartbeatTimeout()) < 0 {
return serviceerror.NewInvalidArgument("A valid timeout may not be negative.")
}
validScheduleToClose := timestamp.DurationValue(attributes.GetScheduleToCloseTimeout()) > 0
validScheduleToStart := timestamp.DurationValue(attributes.GetScheduleToStartTimeout()) > 0
validStartToClose := timestamp.DurationValue(attributes.GetStartToCloseTimeout()) > 0
if validScheduleToClose {
if validScheduleToStart {
attributes.ScheduleToStartTimeout = timestamp.MinDurationPtr(attributes.GetScheduleToStartTimeout(),
attributes.GetScheduleToCloseTimeout())
} else {
attributes.ScheduleToStartTimeout = attributes.GetScheduleToCloseTimeout()
}
if validStartToClose {
attributes.StartToCloseTimeout = timestamp.MinDurationPtr(attributes.GetStartToCloseTimeout(),
attributes.GetScheduleToCloseTimeout())
} else {
attributes.StartToCloseTimeout = attributes.GetScheduleToCloseTimeout()
}
} else if validStartToClose {
// We are in !validScheduleToClose due to the first if above
attributes.ScheduleToCloseTimeout = &runTimeout
if !validScheduleToStart {
attributes.ScheduleToStartTimeout = &runTimeout
}
} else {
// Deduction failed as there's not enough information to fill in missing timeouts.
return serviceerror.NewInvalidArgument("A valid StartToClose or ScheduleToCloseTimeout is not set on command.")
}
// ensure activity timeout never larger than workflow timeout
if runTimeout > 0 {
if timestamp.DurationValue(attributes.GetScheduleToCloseTimeout()) > runTimeout {
attributes.ScheduleToCloseTimeout = &runTimeout
}
if timestamp.DurationValue(attributes.GetScheduleToStartTimeout()) > runTimeout {
attributes.ScheduleToStartTimeout = &runTimeout
}
if timestamp.DurationValue(attributes.GetStartToCloseTimeout()) > runTimeout {
attributes.StartToCloseTimeout = &runTimeout
}
if timestamp.DurationValue(attributes.GetHeartbeatTimeout()) > runTimeout {
attributes.HeartbeatTimeout = &runTimeout
}
}
attributes.HeartbeatTimeout = timestamp.MinDurationPtr(attributes.GetHeartbeatTimeout(), attributes.GetStartToCloseTimeout())
return nil
}
func (v *commandAttrValidator) validateTimerScheduleAttributes(
attributes *commandpb.StartTimerCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("StartTimerCommandAttributes is not set on command.")
}
if attributes.GetTimerId() == "" {
return serviceerror.NewInvalidArgument("TimerId is not set on command.")
}
if len(attributes.GetTimerId()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("TimerId exceeds length limit.")
}
if timestamp.DurationValue(attributes.GetStartToFireTimeout()) <= 0 {
return serviceerror.NewInvalidArgument("A valid StartToFireTimeout is not set on command.")
}
return nil
}
func (v *commandAttrValidator) validateActivityCancelAttributes(
attributes *commandpb.RequestCancelActivityTaskCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("RequestCancelActivityTaskCommandAttributes is not set on command.")
}
if attributes.GetScheduledEventId() <= 0 {
return serviceerror.NewInvalidArgument("ScheduledEventId is not set on command.")
}
return nil
}
func (v *commandAttrValidator) validateTimerCancelAttributes(
attributes *commandpb.CancelTimerCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("CancelTimerCommandAttributes is not set on command.")
}
if attributes.GetTimerId() == "" {
return serviceerror.NewInvalidArgument("TimerId is not set on command.")
}
if len(attributes.GetTimerId()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("TimerId exceeds length limit.")
}
return nil
}
func (v *commandAttrValidator) validateRecordMarkerAttributes(
attributes *commandpb.RecordMarkerCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("RecordMarkerCommandAttributes is not set on command.")
}
if attributes.GetMarkerName() == "" {
return serviceerror.NewInvalidArgument("MarkerName is not set on command.")
}
if len(attributes.GetMarkerName()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("MarkerName exceeds length limit.")
}
return nil
}
func (v *commandAttrValidator) validateCompleteWorkflowExecutionAttributes(
attributes *commandpb.CompleteWorkflowExecutionCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("CompleteWorkflowExecutionCommandAttributes is not set on command.")
}
return nil
}
func (v *commandAttrValidator) validateFailWorkflowExecutionAttributes(
attributes *commandpb.FailWorkflowExecutionCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("FailWorkflowExecutionCommandAttributes is not set on command.")
}
if attributes.GetFailure() == nil {
return serviceerror.NewInvalidArgument("Failure is not set on command.")
}
return nil
}
func (v *commandAttrValidator) validateCancelWorkflowExecutionAttributes(
attributes *commandpb.CancelWorkflowExecutionCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("CancelWorkflowExecutionCommandAttributes is not set on command.")
}
return nil
}
func (v *commandAttrValidator) validateCancelExternalWorkflowExecutionAttributes(
namespaceID string,
targetNamespaceID string,
initiatedChildExecutionsInSession map[string]struct{},
attributes *commandpb.RequestCancelExternalWorkflowExecutionCommandAttributes,
) error {
if err := v.validateCrossNamespaceCall(
namespaceID,
targetNamespaceID,
); err != nil {
return err
}
if attributes == nil {
return serviceerror.NewInvalidArgument("RequestCancelExternalWorkflowExecutionCommandAttributes is not set on command.")
}
if attributes.GetWorkflowId() == "" {
return serviceerror.NewInvalidArgument("WorkflowId is not set on command.")
}
if len(attributes.GetNamespace()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("Namespace exceeds length limit.")
}
if len(attributes.GetWorkflowId()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("WorkflowId exceeds length limit.")
}
runID := attributes.GetRunId()
if runID != "" && uuid.Parse(runID) == nil {
return serviceerror.NewInvalidArgument("Invalid RunId set on command.")
}
if _, ok := initiatedChildExecutionsInSession[attributes.GetWorkflowId()]; ok {
return serviceerror.NewInvalidArgument("Start and RequestCancel for child workflow is not allowed in same workflow task.")
}
return nil
}
func (v *commandAttrValidator) validateSignalExternalWorkflowExecutionAttributes(
namespaceID string,
targetNamespaceID string,
attributes *commandpb.SignalExternalWorkflowExecutionCommandAttributes,
) error {
if err := v.validateCrossNamespaceCall(
namespaceID,
targetNamespaceID,
); err != nil {
return err
}
if attributes == nil {
return serviceerror.NewInvalidArgument("SignalExternalWorkflowExecutionCommandAttributes is not set on command.")
}
if attributes.Execution == nil {
return serviceerror.NewInvalidArgument("Execution is nil on command.")
}
if attributes.Execution.GetWorkflowId() == "" {
return serviceerror.NewInvalidArgument("WorkflowId is not set on command.")
}
if len(attributes.GetNamespace()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("Namespace exceeds length limit.")
}
if len(attributes.Execution.GetWorkflowId()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("WorkflowId exceeds length limit.")
}
targetRunID := attributes.Execution.GetRunId()
if targetRunID != "" && uuid.Parse(targetRunID) == nil {
return serviceerror.NewInvalidArgument("Invalid RunId set on command.")
}
if attributes.GetSignalName() == "" {
return serviceerror.NewInvalidArgument("SignalName is not set on command.")
}
return nil
}
func (v *commandAttrValidator) validateUpsertWorkflowSearchAttributes(
namespace string,
attributes *commandpb.UpsertWorkflowSearchAttributesCommandAttributes,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("UpsertWorkflowSearchAttributesCommandAttributes is not set on command.")
}
if attributes.SearchAttributes == nil {
return serviceerror.NewInvalidArgument("SearchAttributes is not set on command.")
}
if len(attributes.GetSearchAttributes().GetIndexedFields()) == 0 {
return serviceerror.NewInvalidArgument("IndexedFields is empty on command.")
}
return v.searchAttributesValidator.ValidateAndLog(attributes.GetSearchAttributes(), namespace)
}
func (v *commandAttrValidator) validateContinueAsNewWorkflowExecutionAttributes(
namespace string,
attributes *commandpb.ContinueAsNewWorkflowExecutionCommandAttributes,
executionInfo *persistencespb.WorkflowExecutionInfo,
) error {
if attributes == nil {
return serviceerror.NewInvalidArgument("ContinueAsNewWorkflowExecutionCommandAttributes is not set on command.")
}
// Inherit workflow type from previous execution if not provided on command
if attributes.WorkflowType == nil || attributes.WorkflowType.GetName() == "" {
attributes.WorkflowType = &commonpb.WorkflowType{Name: executionInfo.WorkflowTypeName}
}
if len(attributes.WorkflowType.GetName()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("WorkflowType exceeds length limit.")
}
// Inherit Taskqueue from previous execution if not provided on command
taskQueue, err := v.validateTaskQueue(attributes.TaskQueue, executionInfo.TaskQueue)
if err != nil {
return err
}
attributes.TaskQueue = taskQueue
if timestamp.DurationValue(attributes.GetWorkflowRunTimeout()) < 0 {
return serviceerror.NewInvalidArgument("Invalid WorkflowRunTimeout.")
}
if timestamp.DurationValue(attributes.GetWorkflowTaskTimeout()) < 0 {
return serviceerror.NewInvalidArgument("Invalid WorkflowTaskTimeout.")
}
if timestamp.DurationValue(attributes.GetBackoffStartInterval()) < 0 {
return serviceerror.NewInvalidArgument("Invalid BackoffStartInterval.")
}
if timestamp.DurationValue(attributes.GetWorkflowRunTimeout()) == 0 {
attributes.WorkflowRunTimeout = timestamp.DurationPtr(timestamp.DurationValue(executionInfo.WorkflowRunTimeout))
}
if timestamp.DurationValue(attributes.GetWorkflowTaskTimeout()) == 0 {
attributes.WorkflowTaskTimeout = timestamp.DurationPtr(timestamp.DurationValue(executionInfo.DefaultWorkflowTaskTimeout))
}
return v.searchAttributesValidator.ValidateAndLog(attributes.GetSearchAttributes(), namespace)
}
func (v *commandAttrValidator) validateStartChildExecutionAttributes(
namespaceID string,
targetNamespaceID string,
targetNamespace string,
attributes *commandpb.StartChildWorkflowExecutionCommandAttributes,
parentInfo *persistencespb.WorkflowExecutionInfo,
defaultWorkflowTaskTimeoutFn dynamicconfig.DurationPropertyFnWithNamespaceFilter,
) error {
if err := v.validateCrossNamespaceCall(
namespaceID,
targetNamespaceID,
); err != nil {
return err
}
if attributes == nil {
return serviceerror.NewInvalidArgument("StartChildWorkflowExecutionCommandAttributes is not set on command.")
}
if attributes.GetWorkflowId() == "" {
return serviceerror.NewInvalidArgument("Required field WorkflowId is not set on command.")
}
if attributes.WorkflowType == nil || attributes.WorkflowType.GetName() == "" {
return serviceerror.NewInvalidArgument("Required field WorkflowType is not set on command.")
}
if len(attributes.GetNamespace()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("Namespace exceeds length limit.")
}
if len(attributes.GetWorkflowId()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("WorkflowId exceeds length limit.")
}
if len(attributes.WorkflowType.GetName()) > v.maxIDLengthLimit {
return serviceerror.NewInvalidArgument("WorkflowType exceeds length limit.")
}
if timestamp.DurationValue(attributes.GetWorkflowExecutionTimeout()) < 0 {
return serviceerror.NewInvalidArgument("Invalid WorkflowExecutionTimeout.")
}
if timestamp.DurationValue(attributes.GetWorkflowRunTimeout()) < 0 {
return serviceerror.NewInvalidArgument("Invalid WorkflowRunTimeout.")
}
if timestamp.DurationValue(attributes.GetWorkflowTaskTimeout()) < 0 {
return serviceerror.NewInvalidArgument("Invalid WorkflowTaskTimeout.")
}
if err := v.validateWorkflowRetryPolicy(attributes); err != nil {
return err
}
if err := backoff.ValidateSchedule(attributes.GetCronSchedule()); err != nil {
return err
}
if err := v.searchAttributesValidator.ValidateAndLog(attributes.GetSearchAttributes(), targetNamespace); err != nil {
return err
}
// Inherit taskqueue from parent workflow execution if not provided on command
taskQueue, err := v.validateTaskQueue(attributes.TaskQueue, parentInfo.TaskQueue)
if err != nil {
return err
}
attributes.TaskQueue = taskQueue
// workflow execution timeout is left as is
// if workflow execution timeout == 0 -> infinity
attributes.WorkflowRunTimeout = timestamp.DurationPtr(
common.OverrideWorkflowRunTimeout(
timestamp.DurationValue(attributes.GetWorkflowRunTimeout()),
timestamp.DurationValue(attributes.GetWorkflowExecutionTimeout()),
),
)
attributes.WorkflowTaskTimeout = timestamp.DurationPtr(
common.OverrideWorkflowTaskTimeout(
targetNamespace,
timestamp.DurationValue(attributes.GetWorkflowTaskTimeout()),
timestamp.DurationValue(attributes.GetWorkflowRunTimeout()),
defaultWorkflowTaskTimeoutFn,
),
)
return nil
}
func (v *commandAttrValidator) validateTaskQueue(
taskQueue *taskqueuepb.TaskQueue,
defaultVal string,
) (*taskqueuepb.TaskQueue, error) {
if taskQueue == nil {
taskQueue = &taskqueuepb.TaskQueue{
Kind: enumspb.TASK_QUEUE_KIND_NORMAL,
}
}
if taskQueue.GetName() == "" {
if defaultVal == "" {
return taskQueue, serviceerror.NewInvalidArgument("missing task queue name")
}
taskQueue.Name = defaultVal
return taskQueue, nil
}
name := taskQueue.GetName()
if len(name) > v.maxIDLengthLimit {
return taskQueue, serviceerror.NewInvalidArgument(fmt.Sprintf("task queue name exceeds length limit of %v", v.maxIDLengthLimit))
}
if strings.HasPrefix(name, reservedTaskQueuePrefix) {
return taskQueue, serviceerror.NewInvalidArgument(fmt.Sprintf("task queue name cannot start with reserved prefix %v", reservedTaskQueuePrefix))
}
return taskQueue, nil
}
func (v *commandAttrValidator) validateActivityRetryPolicy(
attributes *commandpb.ScheduleActivityTaskCommandAttributes,
) error {
if attributes.RetryPolicy == nil {
attributes.RetryPolicy = &commonpb.RetryPolicy{}
}
defaultActivityRetrySettings := common.FromConfigToDefaultRetrySettings(v.getDefaultActivityRetrySettings(attributes.GetNamespace()))
common.EnsureRetryPolicyDefaults(attributes.RetryPolicy, defaultActivityRetrySettings)
return common.ValidateRetryPolicy(attributes.RetryPolicy)
}
func (v *commandAttrValidator) validateWorkflowRetryPolicy(
attributes *commandpb.StartChildWorkflowExecutionCommandAttributes,
) error {
if attributes.RetryPolicy == nil {
// By default, if the user does not explicitly set a retry policy for a Child Workflow, do not perform any retries.
return nil
}
// Otherwise, for any unset fields on the retry policy, set with defaults
defaultWorkflowRetrySettings := common.FromConfigToDefaultRetrySettings(v.getDefaultWorkflowRetrySettings(attributes.GetNamespace()))
common.EnsureRetryPolicyDefaults(attributes.RetryPolicy, defaultWorkflowRetrySettings)
return common.ValidateRetryPolicy(attributes.RetryPolicy)
}
func (v *commandAttrValidator) validateCrossNamespaceCall(
namespaceID string,
targetNamespaceID string,
) error {
// same name, no check needed
if namespaceID == targetNamespaceID {
return nil
}
if !v.enableCrossNamespaceCommands() {
return serviceerror.NewInvalidArgument("cross namespace commands are not allowed")
}
namespaceEntry, err := v.namespaceCache.GetNamespaceByID(namespaceID)
if err != nil {
return err
}
targetNamespaceEntry, err := v.namespaceCache.GetNamespaceByID(targetNamespaceID)
if err != nil {
return err
}
// both local namespace
if !namespaceEntry.IsGlobalNamespace() && !targetNamespaceEntry.IsGlobalNamespace() {
return nil
}
namespaceClusters := namespaceEntry.GetReplicationConfig().Clusters
targetNamespaceClusters := targetNamespaceEntry.GetReplicationConfig().Clusters
// one is local namespace, another one is global namespace or both global namespace
// treat global namespace with one replication cluster as local namespace
if len(namespaceClusters) == 1 && len(targetNamespaceClusters) == 1 {
if namespaceClusters[0] == targetNamespaceClusters[0] {
return nil
}
return v.createCrossNamespaceCallError(namespaceEntry, targetNamespaceEntry)
}
return v.createCrossNamespaceCallError(namespaceEntry, targetNamespaceEntry)
}
func (v *commandAttrValidator) createCrossNamespaceCallError(
namespaceEntry *cache.NamespaceCacheEntry,
targetNamespaceEntry *cache.NamespaceCacheEntry,
) error {
return serviceerror.NewInvalidArgument(fmt.Sprintf("unable to process cross namespace command between %v and %v", namespaceEntry.GetInfo().Name, targetNamespaceEntry.GetInfo().Name))
}