-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathsession.go
943 lines (747 loc) · 19.8 KB
/
session.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
package wfexec
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/expr"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/pkg/logger"
"go.uber.org/zap"
)
type (
Session struct {
// Session identifier
id uint64
workflowID uint64
// steps graph
g *Graph
started time.Time
// state channel (ie work queue)
qState chan *State
// crash channel
qErr chan error
// locks concurrent executions
execLock chan struct{}
// delayed states (waiting for the right time)
delayed map[uint64]*delayed
// prompted
prompted map[uint64]*prompted
// how often we check for delayed states and how often idle stat is checked in Wait()
workerInterval time.Duration
// only one worker routine per session
workerLock chan struct{}
statusChange chan int
// holds final result
result *expr.Vars
err error
mux sync.RWMutex
// debug logger
log *zap.Logger
dumpStacktraceOnPanic bool
eventHandler StateChangeHandler
callStack []uint64
}
StateChangeHandler func(SessionStatus, *State, *Session)
SessionOpt func(*Session)
Frame struct {
CreatedAt time.Time `json:"createdAt"`
SessionID uint64 `json:"sessionID"`
StateID uint64 `json:"stateID"`
Input *expr.Vars `json:"input"`
Scope *expr.Vars `json:"scope"`
Results *expr.Vars `json:"results"`
ParentID uint64 `json:"parentID"`
StepID uint64 `json:"stepID"`
NextSteps []uint64 `json:"nextSteps"`
// How much time from the 1st step to the start of this step in milliseconds
ElapsedTime uint `json:"elapsedTime"`
// How much time it took to execute this step in milliseconds
StepTime uint `json:"stepTime"`
Action string `json:"action,omitempty"`
Error string `json:"error,omitempty"`
}
// ExecRequest is passed to Exec() functions and contains all information
// for step execution
ExecRequest struct {
SessionID uint64
StateID uint64
// Current input received on session resume
Input *expr.Vars
// Current scope
Scope *expr.Vars
// Helps with gateway join/merge steps
// that needs info about the step it's currently merging
Parent Step
}
SessionStatus int
callStackCtxKey struct{}
)
const (
sessionStateChanBuf = 512
sessionConcurrentExec = 32
)
const (
SessionActive SessionStatus = iota
SessionPrompted
SessionDelayed
SessionFailed
SessionCompleted
SessionCanceled
)
var (
// wrapper around nextID that will aid service testing
nextID = func() uint64 {
return id.Next()
}
// wrapper around time.Now() that will aid service testing
now = func() *time.Time {
c := time.Now()
return &c
}
errCanceled = fmt.Errorf("canceled")
)
func (s SessionStatus) String() string {
switch s {
case SessionActive:
return "active"
case SessionPrompted:
return "prompted"
case SessionDelayed:
return "delayed"
case SessionFailed:
return "failed"
case SessionCompleted:
return "completed"
case SessionCanceled:
return "aborted"
}
return "UNKNOWN-SESSION-STATUS"
}
func NewSession(ctx context.Context, g *Graph, oo ...SessionOpt) *Session {
s := &Session{
g: g,
id: nextID(),
started: *now(),
qState: make(chan *State, sessionStateChanBuf),
qErr: make(chan error, 1),
execLock: make(chan struct{}, sessionConcurrentExec),
delayed: make(map[uint64]*delayed),
prompted: make(map[uint64]*prompted),
//workerInterval: time.Millisecond,
workerInterval: time.Millisecond * 250, // debug mode rate
workerLock: make(chan struct{}, 1),
log: zap.NewNop(),
eventHandler: func(SessionStatus, *State, *Session) {
// noop
},
}
for _, o := range oo {
o(s)
}
s.log = s.log.
With(zap.Uint64("sessionID", s.id))
s.callStack = append(s.callStack, s.id)
go s.worker(ctx)
return s
}
func (s *Session) Status() SessionStatus {
s.mux.RLock()
defer s.mux.RUnlock()
switch {
case s.err != nil:
if s.err == errCanceled {
return SessionCanceled
}
return SessionFailed
case len(s.prompted) > 0:
return SessionPrompted
case len(s.delayed) > 0:
return SessionDelayed
case s.result == nil:
return SessionActive
default:
return SessionCompleted
}
}
func (s *Session) ID() uint64 {
return s.id
}
func (s *Session) Idle() bool {
return s.Status() != SessionActive
}
func (s *Session) Error() error {
s.mux.RLock()
defer s.mux.RUnlock()
return s.err
}
func (s *Session) Result() *expr.Vars {
s.mux.RLock()
defer s.mux.RUnlock()
return s.result
}
func (s *Session) Exec(ctx context.Context, step Step, scope *expr.Vars) error {
s.mux.RLock()
defer s.mux.RUnlock()
err := func() error {
if s.g.Len() == 0 {
return fmt.Errorf("refusing to execute without steps")
}
if len(s.g.Parents(step)) > 0 {
return fmt.Errorf("cannot execute step with parents")
}
return nil
}()
if err != nil {
// send nil to error queue to trigger worker shutdown
// session error must be set to update session status
s.qErr <- err
return err
}
if scope == nil {
scope, _ = expr.NewVars(nil)
}
return s.enqueue(ctx, NewState(s, auth.GetIdentityFromContext(ctx), nil, step, scope))
}
// UserPendingPrompts prompts fn returns all owner's pending prompts on this session
func (s *Session) UserPendingPrompts(ownerId uint64) (out []*PendingPrompt) {
if ownerId == 0 {
return
}
defer s.mux.RUnlock()
s.mux.RLock()
out = make([]*PendingPrompt, 0, len(s.prompted))
for _, p := range s.prompted {
if p.ownerId != ownerId {
continue
}
pending := p.toPending()
pending.SessionID = s.id
out = append(out, pending)
}
return
}
// AllPendingPrompts returns all pending prompts for all user
func (s *Session) AllPendingPrompts() (out []*PendingPrompt) {
defer s.mux.RUnlock()
s.mux.RLock()
return s.pendingPrompts(s.prompted)
}
// UnsentPendingPrompts returns unsent pending prompts for all user
func (s *Session) UnsentPendingPrompts() (out []*PendingPrompt) {
defer s.mux.RUnlock()
s.mux.RLock()
aux := s.pendingPrompts(s.prompted)
for _, p := range aux {
if p.Original.sent {
continue
}
out = append(out, p)
}
return
}
func (s *Session) pendingPrompts(prompted map[uint64]*prompted) (out []*PendingPrompt) {
out = make([]*PendingPrompt, 0, len(prompted))
for _, p := range prompted {
pending := p.toPending()
pending.SessionID = s.id
out = append(out, pending)
}
return
}
func (s *Session) Resume(ctx context.Context, stateId uint64, input *expr.Vars) (*ResumedPrompt, error) {
defer s.mux.Unlock()
s.mux.Lock()
var (
i = auth.GetIdentityFromContext(ctx)
p, has = s.prompted[stateId]
)
if !has {
return nil, fmt.Errorf("unexisting state")
}
if i == nil || p.ownerId != i.Identity() {
return nil, fmt.Errorf("state access denied")
}
delete(s.prompted, stateId)
// setting received input to state
p.state.input = input
if err := s.enqueue(ctx, p.state); err != nil {
return nil, err
}
return p.toResumed(), nil
}
func (s *Session) canEnqueue(st *State) error {
if st == nil {
return fmt.Errorf("state is nil")
}
// when the step is completed right away, it is considered as special
if st.step == nil && st.completed == nil {
return fmt.Errorf("state step is nil")
}
return nil
}
func (s *Session) enqueue(ctx context.Context, st *State) error {
if err := s.canEnqueue(st); err != nil {
return err
}
if st.stateId == 0 {
st.stateId = nextID()
}
select {
case <-ctx.Done():
return ctx.Err()
case s.qState <- st:
s.log.Debug("add step to queue")
return nil
}
}
// Wait does not wait for the whole wf to be complete but until:
// - context timeout
// - idle state
// - error in error queue
func (s *Session) Wait(ctx context.Context) error {
return s.WaitUntil(ctx, SessionFailed, SessionDelayed, SessionCompleted)
}
// WaitUntil blocks until workflow session gets into expected status
//
func (s *Session) WaitUntil(ctx context.Context, expected ...SessionStatus) error {
indexed := make(map[SessionStatus]bool)
for _, status := range expected {
indexed[status] = true
}
// already at the expected status
if indexed[s.Status()] {
return s.err
}
s.log.Debug(
"waiting for status change",
zap.Any("expecting", expected),
zap.Duration("interval", s.workerInterval),
)
waitCheck := time.NewTicker(s.workerInterval)
defer waitCheck.Stop()
for {
select {
case <-waitCheck.C:
if indexed[s.Status()] {
s.log.Debug("waiting complete", zap.Stringer("status", s.Status()))
// nothing in the pipeline
return s.err
}
case <-ctx.Done():
s.log.Debug("wait context canceled", zap.Error(ctx.Err()))
s.Cancel()
return s.err
}
}
}
func (s *Session) worker(ctx context.Context) {
defer s.Stop()
// making sure
defer close(s.workerLock)
s.workerLock <- struct{}{}
workerTicker := time.NewTicker(s.workerInterval)
defer workerTicker.Stop()
for {
select {
case <-ctx.Done():
s.log.Debug("worker context done", zap.Error(ctx.Err()))
return
case <-workerTicker.C:
s.queueScheduledSuspended()
case st := <-s.qState:
if st == nil {
// stop worker
s.log.Debug("completed")
return
}
s.log.Debug("pulled state from queue", zap.Uint64("stateID", st.stateId))
if st.step == nil {
// We should not terminate if the session contains any delayed or prompted steps.
status := s.Status()
if status == SessionPrompted || status == SessionDelayed {
break
}
s.log.Debug("done, setting results and stopping the worker")
func() {
// mini lambda fn to ensure we can properly unlock with defer
s.mux.Lock()
defer s.mux.Unlock()
// with merge we are making sure
// that result != nil even if state scope is
s.result = (&expr.Vars{}).MustMerge(st.scope)
}()
// Call event handler with completed status
s.eventHandler(SessionCompleted, st, s)
return
}
// add empty struct to chan to lock and to have control over number of concurrent go processes
// this will block if number of items in execLock chan reached value of sessionConcurrentExec
s.execLock <- struct{}{}
go func() {
defer func() {
// remove protection that prevents multiple
// steps executing at the same time
<-s.execLock
}()
var (
err error
log = s.log.With(zap.Uint64("stateID", st.stateId))
)
nxt, err := s.exec(ctx, log, st)
if err != nil && st.err == nil {
// override the error from the execution
st.err = err
}
st.completed = now()
status := s.Status()
if st.err != nil {
st.err = fmt.Errorf(
"workflow %d step %d execution failed: %w",
s.workflowID,
st.step.ID(),
st.err,
)
s.mux.Lock()
// when the err handler is defined, the error was handled and should not kill the workflow
if !st.errHandled {
// We need to force failed session status
// because it's not set early enough to pick it up with s.Status()
status = SessionFailed
// pushing step execution error into error queue
// to break worker loop
s.qErr <- st.err
}
s.mux.Unlock()
}
s.log.Debug(
"executed",
zap.Uint64("stateID", st.stateId),
zap.Stringer("status", status),
zap.Error(st.err),
)
s.eventHandler(status, st, s)
for _, n := range nxt {
if n.step != nil {
log.Debug("next step queued", zap.Uint64("nextStepId", n.step.ID()))
} else {
log.Debug("next step queued", zap.Uint64("nextStepId", 0))
}
if err = s.enqueue(ctx, n); err != nil {
log.Error("unable to enqueue", zap.Error(err))
return
}
}
}()
case err := <-s.qErr:
s.mux.Lock()
defer s.mux.Unlock()
if err == nil {
// stop worker
return
}
// set final error on session
s.err = err
return
}
}
}
func (s *Session) Cancel() {
s.log.Warn("canceling")
s.qErr <- errCanceled
s.eventHandler(SessionCanceled, nil, s)
}
func (s *Session) Stop() {
s.log.Debug("stopping worker")
s.qErr <- nil
}
func (s *Session) Suspended() bool {
defer s.mux.RUnlock()
s.mux.RLock()
return len(s.delayed) > 0
}
func (s *Session) queueScheduledSuspended() {
defer s.mux.Unlock()
s.mux.Lock()
for id, sus := range s.delayed {
if !sus.resumeAt.IsZero() && sus.resumeAt.After(*now()) {
continue
}
delete(s.delayed, id)
// Set state input when step is resumed
sus.state.input = &expr.Vars{}
sus.state.input.Set("resumed", true)
sus.state.input.Set("resumeAt", sus.resumeAt)
s.qState <- sus.state
}
}
// executes single step, resolves response and schedule following steps for execution
func (s *Session) exec(ctx context.Context, log *zap.Logger, st *State) (nxt []*State, err error) {
st.created = *now()
defer func() {
reason := recover()
if reason == nil {
return
}
var perr error
// normalize error and set it to state
switch reason := reason.(type) {
case error:
perr = fmt.Errorf("step %d crashed: %w", st.step.ID(), reason)
default:
perr = fmt.Errorf("step %d crashed: %v", st.step.ID(), reason)
}
if s.dumpStacktraceOnPanic {
fmt.Printf("Error: %v\n", perr)
println(string(debug.Stack()))
}
s.qErr <- perr
}()
var (
result ExecResponse
scope = (&expr.Vars{}).MustMerge(st.scope)
currLoop = st.loopCurr()
)
if st.step != nil {
log = log.With(zap.Uint64("stepID", st.step.ID()))
}
{
if currLoop != nil && currLoop.Is(st.step) {
result = currLoop
} else {
// push logger to context but raise the stacktrace level to panic
// to prevent overly verbose traces
ctx = logger.ContextWithValue(ctx, log)
stepCtx := SetContextCallStack(ctx, s.callStack)
result, st.err = st.step.Exec(stepCtx, st.MakeRequest())
if iterator, isIterator := result.(Iterator); isIterator && st.err == nil {
// Exec fn returned an iterator, adding loop to stack
st.newLoop(iterator)
if err = iterator.Start(ctx, scope); err != nil {
return
}
}
}
if st.err != nil {
if st.errHandler == nil {
// no error handler set
return nil, st.err
}
// handling error with error handling
// step set in one of the previous steps
log.Warn("step execution error handled",
zap.Uint64("errorHandlerStepId", st.errHandler.ID()),
zap.Error(st.err),
)
_ = expr.Assign(scope, "error", expr.Must(expr.NewString(st.err.Error())))
// copy error handler & disable it on state to prevent inf. loop
// in case of another error in the error-handling branch
eh := st.errHandler
st.errHandler = nil
st.errHandled = true
return []*State{st.Next(eh, scope)}, nil
}
switch l := result.(type) {
case Iterator:
st.action = "iterator initialized"
// add looper to state
var (
n Step
)
n, result, st.err = l.Next(ctx, scope)
if st.err != nil {
return nil, st.err
}
if n == nil {
st.next = st.loopEnd()
} else {
st.next = Steps{n}
}
}
log.Debug("step executed", zap.String("resultType", fmt.Sprintf("%T", result)))
switch result := result.(type) {
case *expr.Vars:
// most common (successful) result
// session will continue with configured child steps
st.results = result
scope = scope.MustMerge(st.results)
case *errHandler:
st.action = "error handler initialized"
// this step sets error handling step on current state
// and continues on the current path
st.errHandler = result.handler
// find step that's not error handler and
// use it for the next step
for _, c := range s.g.Children(st.step) {
if c != st.errHandler {
st.next = Steps{c}
break
}
}
case *loopBreak:
st.action = "loop break"
if currLoop == nil {
return nil, fmt.Errorf("break step not inside a loop")
}
// jump out of the loop
st.next = st.loopEnd()
log.Debug("breaking from iterator")
case *loopContinue:
st.action = "loop continue"
if currLoop == nil {
return nil, fmt.Errorf("continue step not inside a loop")
}
// jump back to iterator
st.next = Steps{currLoop.Iterator()}
log.Debug("continuing with next iteration")
case *partial:
st.action = "partial"
// *partial is returned when step needs to be executed again
// it's used mainly for join gateway step that should be called multiple times (one for each parent path)
return
case *termination:
st.action = "termination"
// terminate all activities, all delayed tasks and exit right away
log.Debug("termination", zap.Int("delayed", len(s.delayed)))
s.mux.Lock()
s.delayed = nil
s.mux.Unlock()
return []*State{FinalState(s, scope)}, nil
case *delayed:
st.action = "delayed"
log.Debug("session delayed", zap.Time("at", result.resumeAt))
result.state = st
s.mux.Lock()
s.delayed[st.stateId] = result
s.mux.Unlock()
return
case *resumed:
st.action = "resumed"
log.Debug("session resumed")
case *prompted:
st.action = "prompted"
if result.ownerId == 0 {
return nil, fmt.Errorf("without an owner")
}
result.state = st
s.mux.Lock()
s.prompted[st.stateId] = result
s.mux.Unlock()
return
case Steps:
st.action = "next-steps"
// session continues with set of specified steps
// steps MUST be configured in a graph as step's children
st.next = result
case Step:
st.action = "next-step"
// session continues with a specified step
// step MUST be configured in a graph as step's child
st.next = Steps{result}
default:
return nil, fmt.Errorf("unknown exec response type %T", result)
}
}
if len(st.next) == 0 {
// step's exec did not return next steps (only gateway steps, iterators and loops controls usually do that)
//
// rely on graph and get next (children) steps from there
st.next = s.g.Children(st.step)
} else {
// children returned from step's exec
// do a quick sanity check
cc := s.g.Children(st.step)
if len(cc) > 0 && !cc.Contains(st.next...) {
return nil, fmt.Errorf("inconsistent relationship")
}
}
if currLoop != nil && len(st.next) == 0 {
// gracefully handling last step of iteration branch
// that does not point back to the iterator step
st.next = Steps{currLoop.Iterator()}
log.Debug("last step in iteration branch, going back", zap.Uint64("backStepId", st.next[0].ID()))
}
if len(st.next) == 0 {
log.Debug("zero paths, finalizing")
// using state to transport results and complete the worker loop
return []*State{FinalState(s, scope)}, nil
}
nxt = make([]*State, len(st.next))
for i, step := range st.next {
nn := st.Next(step, scope)
if err = s.canEnqueue(nn); err != nil {
log.Error("unable to queue", zap.Error(err))
return
}
nxt[i] = nn
}
return nxt, nil
}
func SetWorkerInterval(i time.Duration) SessionOpt {
return func(s *Session) {
s.workerInterval = i
}
}
func SetHandler(fn StateChangeHandler) SessionOpt {
return func(s *Session) {
s.eventHandler = fn
}
}
func SetWorkflowID(workflowID uint64) SessionOpt {
return func(s *Session) {
s.workflowID = workflowID
}
}
func SetLogger(log *zap.Logger) SessionOpt {
return func(s *Session) {
s.log = log
}
}
func SetDumpStacktraceOnPanic(dump bool) SessionOpt {
return func(s *Session) {
s.dumpStacktraceOnPanic = dump
}
}
func SetCallStack(id ...uint64) SessionOpt {
return func(s *Session) {
s.callStack = id
}
}
func (ss Steps) hash() map[Step]bool {
out := make(map[Step]bool)
for _, s := range ss {
out[s] = true
}
return out
}
func (ss Steps) Contains(steps ...Step) bool {
hash := ss.hash()
for _, s1 := range steps {
if !hash[s1] {
return false
}
}
return true
}
func (ss Steps) IDs() []uint64 {
if len(ss) == 0 {
return nil
}
var ids = make([]uint64, len(ss))
for i := range ss {
ids[i] = ss[i].ID()
}
return ids
}
func SetContextCallStack(ctx context.Context, ss []uint64) context.Context {
return context.WithValue(ctx, callStackCtxKey{}, ss)
}
func GetContextCallStack(ctx context.Context) []uint64 {
v := ctx.Value(callStackCtxKey{})
if v == nil {
return nil
}
return v.([]uint64)
}