/
internal_decision_state_machine.go
1023 lines (891 loc) · 34.3 KB
/
internal_decision_state_machine.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 internal
import (
"container/list"
"fmt"
s "go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/encoded"
"go.uber.org/cadence/internal/common"
"go.uber.org/cadence/internal/common/util"
)
type (
decisionState int32
decisionType int32
decisionID struct {
decisionType decisionType
id string
}
decisionStateMachine interface {
getState() decisionState
getID() decisionID
isDone() bool
getDecision() *s.Decision // return nil if there is no decision in current state
cancel()
handleStartedEvent()
handleCancelInitiatedEvent()
handleCanceledEvent()
handleCancelFailedEvent()
handleCompletionEvent()
handleInitiationFailedEvent()
handleInitiatedEvent()
handleDecisionSent()
setData(data interface{})
getData() interface{}
}
decisionStateMachineBase struct {
id decisionID
state decisionState
history []string
data interface{}
helper *decisionsHelper
}
activityDecisionStateMachine struct {
*decisionStateMachineBase
attributes *s.ScheduleActivityTaskDecisionAttributes
}
timerDecisionStateMachine struct {
*decisionStateMachineBase
attributes *s.StartTimerDecisionAttributes
canceled bool
}
childWorkflowDecisionStateMachine struct {
*decisionStateMachineBase
attributes *s.StartChildWorkflowExecutionDecisionAttributes
}
naiveDecisionStateMachine struct {
*decisionStateMachineBase
decision *s.Decision
}
// only possible state transition is: CREATED->SENT->INITIATED->COMPLETED
cancelExternalWorkflowDecisionStateMachine struct {
*naiveDecisionStateMachine
}
signalExternalWorkflowDecisionStateMachine struct {
*naiveDecisionStateMachine
}
// only possible state transition is: CREATED->SENT->COMPLETED
markerDecisionStateMachine struct {
*naiveDecisionStateMachine
}
decisionsHelper struct {
orderedDecisions *list.List
decisions map[decisionID]*list.Element
scheduledEventIDToActivityID map[int64]string
scheduledEventIDToCancellationID map[int64]string
scheduledEventIDToSignalID map[int64]string
}
)
const (
decisionStateCreated decisionState = 0
decisionStateDecisionSent decisionState = 1
decisionStateCanceledBeforeInitiated decisionState = 2
decisionStateInitiated decisionState = 3
decisionStateStarted decisionState = 4
decisionStateCanceledAfterInitiated decisionState = 5
decisionStateCanceledAfterStarted decisionState = 6
decisionStateCancellationDecisionSent decisionState = 7
decisionStateCompletedAfterCancellationDecisionSent decisionState = 8
decisionStateCompleted decisionState = 9
)
const (
decisionTypeActivity decisionType = 0
decisionTypeChildWorkflow decisionType = 1
decisionTypeCancellation decisionType = 2
decisionTypeMarker decisionType = 3
decisionTypeTimer decisionType = 4
decisionTypeSignal decisionType = 5
)
const (
eventCancel = "cancel"
eventDecisionSent = "handleDecisionSent"
eventInitiated = "handleInitiatedEvent"
eventInitiationFailed = "handleInitiationFailedEvent"
eventStarted = "handleStartedEvent"
eventCompletion = "handleCompletionEvent"
eventCancelInitiated = "handleCancelInitiatedEvent"
eventCancelFailed = "handleCancelFailedEvent"
eventCanceled = "handleCanceledEvent"
)
const (
sideEffectMarkerName = "SideEffect"
versionMarkerName = "Version"
localActivityMarkerName = "LocalActivity"
mutableSideEffectMarkerName = "MutableSideEffect"
)
func (d decisionState) String() string {
switch d {
case decisionStateCreated:
return "Created"
case decisionStateDecisionSent:
return "DecisionSent"
case decisionStateCanceledBeforeInitiated:
return "CanceledBeforeInitiated"
case decisionStateInitiated:
return "Initiated"
case decisionStateStarted:
return "Started"
case decisionStateCanceledAfterInitiated:
return "CanceledAfterInitiated"
case decisionStateCanceledAfterStarted:
return "CanceledAfterStarted"
case decisionStateCancellationDecisionSent:
return "CancellationDecisionSent"
case decisionStateCompletedAfterCancellationDecisionSent:
return "CompletedAfterCancellationDecisionSent"
case decisionStateCompleted:
return "Completed"
default:
return "Unknown"
}
}
func (d decisionType) String() string {
switch d {
case decisionTypeActivity:
return "Activity"
case decisionTypeChildWorkflow:
return "ChildWorkflow"
case decisionTypeCancellation:
return "Cancellation"
case decisionTypeMarker:
return "Marker"
case decisionTypeTimer:
return "Timer"
case decisionTypeSignal:
return "Signal"
default:
return "Unknown"
}
}
func (d decisionID) String() string {
return fmt.Sprintf("DecisionType: %v, ID: %v", d.decisionType, d.id)
}
func makeDecisionID(decisionType decisionType, id string) decisionID {
return decisionID{decisionType: decisionType, id: id}
}
func (h *decisionsHelper) newDecisionStateMachineBase(decisionType decisionType, id string) *decisionStateMachineBase {
return &decisionStateMachineBase{
id: makeDecisionID(decisionType, id),
state: decisionStateCreated,
history: []string{decisionStateCreated.String()},
helper: h,
}
}
func (h *decisionsHelper) newActivityDecisionStateMachine(attributes *s.ScheduleActivityTaskDecisionAttributes) *activityDecisionStateMachine {
base := h.newDecisionStateMachineBase(decisionTypeActivity, attributes.GetActivityId())
return &activityDecisionStateMachine{
decisionStateMachineBase: base,
attributes: attributes,
}
}
func (h *decisionsHelper) newTimerDecisionStateMachine(attributes *s.StartTimerDecisionAttributes) *timerDecisionStateMachine {
base := h.newDecisionStateMachineBase(decisionTypeTimer, attributes.GetTimerId())
return &timerDecisionStateMachine{
decisionStateMachineBase: base,
attributes: attributes,
}
}
func (h *decisionsHelper) newChildWorkflowDecisionStateMachine(attributes *s.StartChildWorkflowExecutionDecisionAttributes) *childWorkflowDecisionStateMachine {
base := h.newDecisionStateMachineBase(decisionTypeChildWorkflow, attributes.GetWorkflowId())
return &childWorkflowDecisionStateMachine{
decisionStateMachineBase: base,
attributes: attributes,
}
}
func (h *decisionsHelper) newNaiveDecisionStateMachine(decisionType decisionType, id string, decision *s.Decision) *naiveDecisionStateMachine {
base := h.newDecisionStateMachineBase(decisionType, id)
return &naiveDecisionStateMachine{
decisionStateMachineBase: base,
decision: decision,
}
}
func (h *decisionsHelper) newMarkerDecisionStateMachine(id string, attributes *s.RecordMarkerDecisionAttributes) *markerDecisionStateMachine {
d := createNewDecision(s.DecisionTypeRecordMarker)
d.RecordMarkerDecisionAttributes = attributes
return &markerDecisionStateMachine{
naiveDecisionStateMachine: h.newNaiveDecisionStateMachine(decisionTypeMarker, id, d),
}
}
func (h *decisionsHelper) newCancelExternalWorkflowStateMachine(attributes *s.RequestCancelExternalWorkflowExecutionDecisionAttributes, cancellationID string) *cancelExternalWorkflowDecisionStateMachine {
d := createNewDecision(s.DecisionTypeRequestCancelExternalWorkflowExecution)
d.RequestCancelExternalWorkflowExecutionDecisionAttributes = attributes
return &cancelExternalWorkflowDecisionStateMachine{
naiveDecisionStateMachine: h.newNaiveDecisionStateMachine(decisionTypeCancellation, cancellationID, d),
}
}
func (h *decisionsHelper) newSignalExternalWorkflowStateMachine(attributes *s.SignalExternalWorkflowExecutionDecisionAttributes, signalID string) *signalExternalWorkflowDecisionStateMachine {
d := createNewDecision(s.DecisionTypeSignalExternalWorkflowExecution)
d.SignalExternalWorkflowExecutionDecisionAttributes = attributes
return &signalExternalWorkflowDecisionStateMachine{
naiveDecisionStateMachine: h.newNaiveDecisionStateMachine(decisionTypeSignal, signalID, d),
}
}
func (d *decisionStateMachineBase) getState() decisionState {
return d.state
}
func (d *decisionStateMachineBase) getID() decisionID {
return d.id
}
func (d *decisionStateMachineBase) isDone() bool {
return d.state == decisionStateCompleted || d.state == decisionStateCompletedAfterCancellationDecisionSent
}
func (d *decisionStateMachineBase) setData(data interface{}) {
d.data = data
}
func (d *decisionStateMachineBase) getData() interface{} {
return d.data
}
func (d *decisionStateMachineBase) moveState(newState decisionState, event string) {
d.history = append(d.history, event)
d.state = newState
d.history = append(d.history, newState.String())
if newState == decisionStateCompleted {
if elem, ok := d.helper.decisions[d.getID()]; ok {
d.helper.orderedDecisions.Remove(elem)
delete(d.helper.decisions, d.getID())
}
}
}
func (d *decisionStateMachineBase) failStateTransition(event string) {
// this is when we detect illegal state transition, likely due to ill history sequence or nondeterministic decider code
panic(fmt.Sprintf("invalid state transition: attempt to %v, %v", event, d))
}
func (d *decisionStateMachineBase) handleDecisionSent() {
switch d.state {
case decisionStateCreated:
d.moveState(decisionStateDecisionSent, eventDecisionSent)
}
}
func (d *decisionStateMachineBase) cancel() {
switch d.state {
case decisionStateCompleted, decisionStateCompletedAfterCancellationDecisionSent:
// No op. This is legit. People could cancel context after timer/activity is done.
case decisionStateCreated:
d.moveState(decisionStateCompleted, eventCancel)
case decisionStateDecisionSent:
d.moveState(decisionStateCanceledBeforeInitiated, eventCancel)
case decisionStateInitiated:
d.moveState(decisionStateCanceledAfterInitiated, eventCancel)
default:
d.failStateTransition(eventCancel)
}
}
func (d *decisionStateMachineBase) handleInitiatedEvent() {
switch d.state {
case decisionStateDecisionSent:
d.moveState(decisionStateInitiated, eventInitiated)
case decisionStateCanceledBeforeInitiated:
d.moveState(decisionStateCanceledAfterInitiated, eventInitiated)
default:
d.failStateTransition(eventInitiated)
}
}
func (d *decisionStateMachineBase) handleInitiationFailedEvent() {
switch d.state {
case decisionStateInitiated, decisionStateDecisionSent, decisionStateCanceledBeforeInitiated:
d.moveState(decisionStateCompleted, eventInitiationFailed)
default:
d.failStateTransition(eventInitiationFailed)
}
}
func (d *decisionStateMachineBase) handleStartedEvent() {
d.history = append(d.history, eventStarted)
}
func (d *decisionStateMachineBase) handleCompletionEvent() {
switch d.state {
case decisionStateCanceledAfterInitiated, decisionStateInitiated:
d.moveState(decisionStateCompleted, eventCompletion)
case decisionStateCancellationDecisionSent:
d.moveState(decisionStateCompletedAfterCancellationDecisionSent, eventCompletion)
default:
d.failStateTransition(eventCompletion)
}
}
func (d *decisionStateMachineBase) handleCancelInitiatedEvent() {
d.history = append(d.history, eventCancelInitiated)
switch d.state {
case decisionStateCancellationDecisionSent:
// No state change
default:
d.failStateTransition(eventCancelInitiated)
}
}
func (d *decisionStateMachineBase) handleCancelFailedEvent() {
switch d.state {
case decisionStateCompletedAfterCancellationDecisionSent:
d.moveState(decisionStateCompleted, eventCancelFailed)
default:
d.failStateTransition(eventCancelFailed)
}
}
func (d *decisionStateMachineBase) handleCanceledEvent() {
switch d.state {
case decisionStateCancellationDecisionSent:
d.moveState(decisionStateCompleted, eventCanceled)
default:
d.failStateTransition(eventCanceled)
}
}
func (d *decisionStateMachineBase) String() string {
return fmt.Sprintf("%v, state=%v, isDone()=%v, history=%v",
d.id, d.state, d.isDone(), d.history)
}
func (d *activityDecisionStateMachine) getDecision() *s.Decision {
switch d.state {
case decisionStateCreated:
decision := createNewDecision(s.DecisionTypeScheduleActivityTask)
decision.ScheduleActivityTaskDecisionAttributes = d.attributes
return decision
case decisionStateCanceledAfterInitiated:
decision := createNewDecision(s.DecisionTypeRequestCancelActivityTask)
decision.RequestCancelActivityTaskDecisionAttributes = &s.RequestCancelActivityTaskDecisionAttributes{
ActivityId: d.attributes.ActivityId,
}
return decision
default:
return nil
}
}
func (d *activityDecisionStateMachine) handleDecisionSent() {
switch d.state {
case decisionStateCanceledAfterInitiated:
d.moveState(decisionStateCancellationDecisionSent, eventDecisionSent)
default:
d.decisionStateMachineBase.handleDecisionSent()
}
}
func (d *activityDecisionStateMachine) handleCancelFailedEvent() {
switch d.state {
case decisionStateCancellationDecisionSent:
d.moveState(decisionStateInitiated, eventCancelFailed)
default:
d.decisionStateMachineBase.handleCancelFailedEvent()
}
}
func (d *timerDecisionStateMachine) cancel() {
d.canceled = true
d.decisionStateMachineBase.cancel()
}
func (d *timerDecisionStateMachine) isDone() bool {
return d.state == decisionStateCompleted || d.canceled
}
func (d *timerDecisionStateMachine) handleDecisionSent() {
switch d.state {
case decisionStateCanceledAfterInitiated:
d.moveState(decisionStateCancellationDecisionSent, eventDecisionSent)
default:
d.decisionStateMachineBase.handleDecisionSent()
}
}
func (d *timerDecisionStateMachine) handleCancelFailedEvent() {
switch d.state {
case decisionStateCancellationDecisionSent:
d.moveState(decisionStateInitiated, eventCancelFailed)
default:
d.decisionStateMachineBase.handleCancelFailedEvent()
}
}
func (d *timerDecisionStateMachine) getDecision() *s.Decision {
switch d.state {
case decisionStateCreated:
decision := createNewDecision(s.DecisionTypeStartTimer)
decision.StartTimerDecisionAttributes = d.attributes
return decision
case decisionStateCanceledAfterInitiated:
decision := createNewDecision(s.DecisionTypeCancelTimer)
decision.CancelTimerDecisionAttributes = &s.CancelTimerDecisionAttributes{
TimerId: d.attributes.TimerId,
}
return decision
default:
return nil
}
}
func (d *childWorkflowDecisionStateMachine) getDecision() *s.Decision {
switch d.state {
case decisionStateCreated:
decision := createNewDecision(s.DecisionTypeStartChildWorkflowExecution)
decision.StartChildWorkflowExecutionDecisionAttributes = d.attributes
return decision
case decisionStateCanceledAfterStarted:
decision := createNewDecision(s.DecisionTypeRequestCancelExternalWorkflowExecution)
decision.RequestCancelExternalWorkflowExecutionDecisionAttributes = &s.RequestCancelExternalWorkflowExecutionDecisionAttributes{
Domain: d.attributes.Domain,
WorkflowId: d.attributes.WorkflowId,
ChildWorkflowOnly: common.BoolPtr(true),
}
return decision
default:
return nil
}
}
func (d *childWorkflowDecisionStateMachine) handleDecisionSent() {
switch d.state {
case decisionStateCanceledAfterStarted:
d.moveState(decisionStateCancellationDecisionSent, eventDecisionSent)
default:
d.decisionStateMachineBase.handleDecisionSent()
}
}
func (d *childWorkflowDecisionStateMachine) handleStartedEvent() {
switch d.state {
case decisionStateInitiated:
d.moveState(decisionStateStarted, eventStarted)
case decisionStateCanceledAfterInitiated:
d.moveState(decisionStateCanceledAfterStarted, eventStarted)
default:
d.decisionStateMachineBase.handleStartedEvent()
}
}
func (d *childWorkflowDecisionStateMachine) handleCancelFailedEvent() {
switch d.state {
case decisionStateCancellationDecisionSent:
d.moveState(decisionStateStarted, eventCancelFailed)
default:
d.decisionStateMachineBase.handleCancelFailedEvent()
}
}
func (d *childWorkflowDecisionStateMachine) cancel() {
switch d.state {
case decisionStateStarted:
d.moveState(decisionStateCanceledAfterStarted, eventCancel)
default:
d.decisionStateMachineBase.cancel()
}
}
func (d *childWorkflowDecisionStateMachine) handleCanceledEvent() {
switch d.state {
case decisionStateStarted:
d.moveState(decisionStateCompleted, eventCanceled)
default:
d.decisionStateMachineBase.handleCanceledEvent()
}
}
func (d *childWorkflowDecisionStateMachine) handleCompletionEvent() {
switch d.state {
case decisionStateStarted, decisionStateCanceledAfterStarted:
d.moveState(decisionStateCompleted, eventCompletion)
default:
d.decisionStateMachineBase.handleCompletionEvent()
}
}
func (d *naiveDecisionStateMachine) getDecision() *s.Decision {
switch d.state {
case decisionStateCreated:
return d.decision
default:
return nil
}
}
func (d *naiveDecisionStateMachine) cancel() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleCompletionEvent() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleInitiatedEvent() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleInitiationFailedEvent() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleStartedEvent() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleCanceledEvent() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleCancelFailedEvent() {
panic("unsupported operation")
}
func (d *naiveDecisionStateMachine) handleCancelInitiatedEvent() {
panic("unsupported operation")
}
func (d *cancelExternalWorkflowDecisionStateMachine) handleInitiatedEvent() {
switch d.state {
case decisionStateDecisionSent:
d.moveState(decisionStateInitiated, eventInitiated)
default:
d.failStateTransition(eventInitiated)
}
}
func (d *cancelExternalWorkflowDecisionStateMachine) handleCompletionEvent() {
switch d.state {
case decisionStateInitiated:
d.moveState(decisionStateCompleted, eventCompletion)
default:
d.failStateTransition(eventCompletion)
}
}
func (d *signalExternalWorkflowDecisionStateMachine) handleInitiatedEvent() {
switch d.state {
case decisionStateDecisionSent:
d.moveState(decisionStateInitiated, eventInitiated)
default:
d.failStateTransition(eventInitiated)
}
}
func (d *signalExternalWorkflowDecisionStateMachine) handleCompletionEvent() {
switch d.state {
case decisionStateInitiated:
d.moveState(decisionStateCompleted, eventCompletion)
default:
d.failStateTransition(eventCompletion)
}
}
func (d *markerDecisionStateMachine) handleDecisionSent() {
// Marker decision state machine is considered as completed once decision is sent.
// For SideEffect/Version markers, when the history event is applied, there is no marker decision state machine yet
// because we preload those marker events.
// For local activity, when we apply the history event, we use it to create the marker state machine, there is no
// other event to drive it to completed state.
switch d.state {
case decisionStateCreated:
d.moveState(decisionStateCompleted, eventDecisionSent)
}
}
func newDecisionsHelper() *decisionsHelper {
return &decisionsHelper{
orderedDecisions: list.New(),
decisions: make(map[decisionID]*list.Element),
scheduledEventIDToActivityID: make(map[int64]string),
scheduledEventIDToCancellationID: make(map[int64]string),
scheduledEventIDToSignalID: make(map[int64]string),
}
}
func (h *decisionsHelper) getDecision(id decisionID) decisionStateMachine {
decision, ok := h.decisions[id]
if !ok {
panicMsg := fmt.Sprintf("unknown decision %v, possible causes are nondeterministic workflow definition code"+
" or incompatible change in the workflow definition", id)
panic(panicMsg)
}
// Move the last update decision state machine to the back of the list.
// Otherwise decisions (like timer cancellations) can end up out of order.
h.orderedDecisions.MoveToBack(decision)
return decision.Value.(decisionStateMachine)
}
func (h *decisionsHelper) addDecision(decision decisionStateMachine) {
if _, ok := h.decisions[decision.getID()]; ok {
panicMsg := fmt.Sprintf("adding duplicate decision %v", decision)
panic(panicMsg)
}
element := h.orderedDecisions.PushBack(decision)
h.decisions[decision.getID()] = element
}
func (h *decisionsHelper) scheduleActivityTask(attributes *s.ScheduleActivityTaskDecisionAttributes) decisionStateMachine {
decision := h.newActivityDecisionStateMachine(attributes)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) requestCancelActivityTask(activityID string) decisionStateMachine {
id := makeDecisionID(decisionTypeActivity, activityID)
decision := h.getDecision(id)
decision.cancel()
return decision
}
func (h *decisionsHelper) handleActivityTaskClosed(activityID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeActivity, activityID))
decision.handleCompletionEvent()
return decision
}
func (h *decisionsHelper) handleActivityTaskScheduled(scheduledEventID int64, activityID string) {
h.scheduledEventIDToActivityID[scheduledEventID] = activityID
decision := h.getDecision(makeDecisionID(decisionTypeActivity, activityID))
decision.handleInitiatedEvent()
}
func (h *decisionsHelper) handleActivityTaskCancelRequested(activityID string) {
decision := h.getDecision(makeDecisionID(decisionTypeActivity, activityID))
decision.handleCancelInitiatedEvent()
}
func (h *decisionsHelper) handleActivityTaskCanceled(activityID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeActivity, activityID))
decision.handleCanceledEvent()
return decision
}
func (h *decisionsHelper) handleRequestCancelActivityTaskFailed(activityID string) {
decision := h.getDecision(makeDecisionID(decisionTypeActivity, activityID))
decision.handleCancelFailedEvent()
}
func (h *decisionsHelper) getActivityID(event *s.HistoryEvent) string {
var scheduledEventID int64 = -1
switch event.GetEventType() {
case s.EventTypeActivityTaskCanceled:
scheduledEventID = event.ActivityTaskCanceledEventAttributes.GetScheduledEventId()
case s.EventTypeActivityTaskCompleted:
scheduledEventID = event.ActivityTaskCompletedEventAttributes.GetScheduledEventId()
case s.EventTypeActivityTaskFailed:
scheduledEventID = event.ActivityTaskFailedEventAttributes.GetScheduledEventId()
case s.EventTypeActivityTaskTimedOut:
scheduledEventID = event.ActivityTaskTimedOutEventAttributes.GetScheduledEventId()
default:
panic(fmt.Sprintf("unexpected event type %v", event.GetEventType()))
}
activityID, ok := h.scheduledEventIDToActivityID[scheduledEventID]
if !ok {
panic(fmt.Sprintf("unable to find activity ID for the event %v", util.HistoryEventToString(event)))
}
return activityID
}
func (h *decisionsHelper) recordVersionMarker(changeID string, version Version, dataConverter encoded.DataConverter) decisionStateMachine {
markerID := fmt.Sprintf("%v_%v", versionMarkerName, changeID)
details, err := encodeArgs(dataConverter, []interface{}{changeID, version})
if err != nil {
panic(err)
}
recordMarker := &s.RecordMarkerDecisionAttributes{
MarkerName: common.StringPtr(versionMarkerName),
Details: details, // Keep
}
decision := h.newMarkerDecisionStateMachine(markerID, recordMarker)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) recordSideEffectMarker(sideEffectID int32, data []byte) decisionStateMachine {
markerID := fmt.Sprintf("%v_%v", sideEffectMarkerName, sideEffectID)
attributes := &s.RecordMarkerDecisionAttributes{
MarkerName: common.StringPtr(sideEffectMarkerName),
Details: data,
}
decision := h.newMarkerDecisionStateMachine(markerID, attributes)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) recordLocalActivityMarker(activityID string, result []byte) decisionStateMachine {
markerID := fmt.Sprintf("%v_%v", localActivityMarkerName, activityID)
attributes := &s.RecordMarkerDecisionAttributes{
MarkerName: common.StringPtr(localActivityMarkerName),
Details: result,
}
decision := h.newMarkerDecisionStateMachine(markerID, attributes)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) recordMutableSideEffectMarker(mutableSideEffectID string, data []byte) decisionStateMachine {
markerID := fmt.Sprintf("%v_%v", mutableSideEffectMarkerName, mutableSideEffectID)
attributes := &s.RecordMarkerDecisionAttributes{
MarkerName: common.StringPtr(mutableSideEffectMarkerName),
Details: data,
}
decision := h.newMarkerDecisionStateMachine(markerID, attributes)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) startChildWorkflowExecution(attributes *s.StartChildWorkflowExecutionDecisionAttributes) decisionStateMachine {
decision := h.newChildWorkflowDecisionStateMachine(attributes)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) handleStartChildWorkflowExecutionInitiated(workflowID string) {
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleInitiatedEvent()
}
func (h *decisionsHelper) handleStartChildWorkflowExecutionFailed(workflowID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleInitiationFailedEvent()
return decision
}
func (h *decisionsHelper) requestCancelExternalWorkflowExecution(domain, workflowID, runID string, cancellationID string, childWorkflowOnly bool) decisionStateMachine {
if childWorkflowOnly {
// For cancellation of child workflow only, we do not use cancellation ID
// since the child workflow cancellation go through the existing child workflow
// state machine, and we use workflow ID as identifier
// we also do not use run ID, since child workflow can do continue-as-new
// which will have different run ID
// there will be server side validation that target workflow is child workflow
// sanity check that cancellation ID is not set
if len(cancellationID) != 0 {
panic("cancellation on child workflow should not use cancellation ID")
}
// sanity check that run ID is not set
if len(runID) != 0 {
panic("cancellation on child workflow should not use run ID")
}
// targeting child workflow
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.cancel()
return decision
}
// For cancellation of external workflow, we have to use cancellation ID
// to identify different cancellation request (decision) / response (history event)
// client can also use this code path to cancel its own child workflow, however, there will
// be no server side validation that target workflow is the child
// sanity check that cancellation ID is set
if len(cancellationID) == 0 {
panic("cancellation on external workflow should use cancellation ID")
}
attributes := &s.RequestCancelExternalWorkflowExecutionDecisionAttributes{
Domain: common.StringPtr(domain),
WorkflowId: common.StringPtr(workflowID),
RunId: common.StringPtr(runID),
Control: []byte(cancellationID),
ChildWorkflowOnly: common.BoolPtr(false),
}
decision := h.newCancelExternalWorkflowStateMachine(attributes, cancellationID)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) handleRequestCancelExternalWorkflowExecutionInitiated(initiatedeventID int64, workflowID, cancellationID string) {
if h.isCancelExternalWorkflowEventForChildWorkflow(cancellationID) {
// this is cancellation for child workflow only
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleCancelInitiatedEvent()
} else {
// this is cancellation for external workflow
h.scheduledEventIDToCancellationID[initiatedeventID] = cancellationID
decision := h.getDecision(makeDecisionID(decisionTypeCancellation, cancellationID))
decision.handleInitiatedEvent()
}
}
func (h *decisionsHelper) handleExternalWorkflowExecutionCancelRequested(initiatedeventID int64, workflowID string) (bool, decisionStateMachine) {
var decision decisionStateMachine
cancellationID, isExternal := h.scheduledEventIDToCancellationID[initiatedeventID]
if !isExternal {
decision = h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
// no state change for child workflow, it is still in CancellationDecisionSent
} else {
// this is cancellation for external workflow
decision = h.getDecision(makeDecisionID(decisionTypeCancellation, cancellationID))
decision.handleCompletionEvent()
}
return isExternal, decision
}
func (h *decisionsHelper) handleRequestCancelExternalWorkflowExecutionFailed(initiatedeventID int64, workflowID string) (bool, decisionStateMachine) {
var decision decisionStateMachine
cancellationID, isExternal := h.scheduledEventIDToCancellationID[initiatedeventID]
if !isExternal {
// this is cancellation for child workflow only
decision = h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleCancelFailedEvent()
} else {
// this is cancellation for external workflow
decision = h.getDecision(makeDecisionID(decisionTypeCancellation, cancellationID))
decision.handleCompletionEvent()
}
return isExternal, decision
}
func (h *decisionsHelper) signalExternalWorkflowExecution(domain, workflowID, runID, signalName string, input []byte, signalID string, childWorkflowOnly bool) decisionStateMachine {
attributes := &s.SignalExternalWorkflowExecutionDecisionAttributes{
Domain: common.StringPtr(domain),
Execution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: common.StringPtr(runID),
},
SignalName: common.StringPtr(signalName),
Input: input,
Control: []byte(signalID),
ChildWorkflowOnly: common.BoolPtr(childWorkflowOnly),
}
decision := h.newSignalExternalWorkflowStateMachine(attributes, signalID)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) handleSignalExternalWorkflowExecutionInitiated(initiatedEventID int64, signalID string) {
h.scheduledEventIDToSignalID[initiatedEventID] = signalID
decision := h.getDecision(makeDecisionID(decisionTypeSignal, signalID))
decision.handleInitiatedEvent()
}
func (h *decisionsHelper) handleSignalExternalWorkflowExecutionCompleted(initiatedEventID int64) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeSignal, h.getSignalID(initiatedEventID)))
decision.handleCompletionEvent()
return decision
}
func (h *decisionsHelper) handleSignalExternalWorkflowExecutionFailed(initiatedEventID int64) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeSignal, h.getSignalID(initiatedEventID)))
decision.handleCompletionEvent()
return decision
}
func (h *decisionsHelper) getSignalID(initiatedEventID int64) string {
signalID, ok := h.scheduledEventIDToSignalID[initiatedEventID]
if !ok {
panic(fmt.Sprintf("unable to find signal ID: %v", initiatedEventID))
}
return signalID
}
func (h *decisionsHelper) startTimer(attributes *s.StartTimerDecisionAttributes) decisionStateMachine {
decision := h.newTimerDecisionStateMachine(attributes)
h.addDecision(decision)
return decision
}
func (h *decisionsHelper) cancelTimer(timerID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeTimer, timerID))
decision.cancel()
return decision
}
func (h *decisionsHelper) handleTimerClosed(timerID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeTimer, timerID))
decision.handleCompletionEvent()
return decision
}
func (h *decisionsHelper) handleTimerStarted(timerID string) {
decision := h.getDecision(makeDecisionID(decisionTypeTimer, timerID))
decision.handleInitiatedEvent()
}
func (h *decisionsHelper) handleTimerCanceled(timerID string) {
decision := h.getDecision(makeDecisionID(decisionTypeTimer, timerID))
decision.handleCanceledEvent()
}
func (h *decisionsHelper) handleCancelTimerFailed(timerID string) {
decision := h.getDecision(makeDecisionID(decisionTypeTimer, timerID))
decision.handleCancelFailedEvent()
}
func (h *decisionsHelper) handleChildWorkflowExecutionStarted(workflowID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleStartedEvent()
return decision
}
func (h *decisionsHelper) handleChildWorkflowExecutionClosed(workflowID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleCompletionEvent()
return decision
}
func (h *decisionsHelper) handleChildWorkflowExecutionCanceled(workflowID string) decisionStateMachine {
decision := h.getDecision(makeDecisionID(decisionTypeChildWorkflow, workflowID))
decision.handleCanceledEvent()
return decision
}
func (h *decisionsHelper) getDecisions(markAsSent bool) []*s.Decision {
var result []*s.Decision
for curr := h.orderedDecisions.Front(); curr != nil; {
next := curr.Next() // get next item here as we might need to remove curr in the loop
d := curr.Value.(decisionStateMachine)
decision := d.getDecision()
if decision != nil {
result = append(result, decision)
}