/
orchestrator.go
138 lines (126 loc) · 2.92 KB
/
orchestrator.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
package tcc
import (
"errors"
"sync"
"github.com/cenkalti/backoff"
"golang.org/x/sync/errgroup"
)
// Option can set option to service
// Option can be passed to NewService() and NewOrchestrator,
// if you pass it to both, the one which is passed to NewOrchestrator will be used
type Option func(s *orchestrator)
// WithMaxRetries sets limitation of retry times
func WithMaxRetries(maxRetries uint64) Option {
return func(o *orchestrator) {
o.backoff = backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries)
}
}
// Orchestrator can orchestrate multiple service
// First, call every service's try() asynchronously.
// If all the try succeeded, call every service's confirm().
// If even one of the services' try fails, every service's cancel will be called.
type Orchestrator interface {
Orchestrate() error
}
type orchestrator struct {
services []*Service
backoff backoff.BackOff
sync.Mutex
}
// NewOrchestrator returns interface Orchestrator
func NewOrchestrator(services []*Service, opts ...Option) Orchestrator {
maxRetries := uint64(10)
o := &orchestrator{
services: services,
backoff: backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries),
Mutex: sync.Mutex{},
}
for _, opt := range opts {
opt(o)
}
return o
}
// Orchestrate can handle all the passed Service's transaction
func (o *orchestrator) Orchestrate() error {
if tryErr := o.tryAll(); tryErr != nil {
if cancelErr := o.cancelAll(); cancelErr != nil {
return cancelErr
}
return tryErr
}
return o.confirmAll()
}
func (o *orchestrator) tryAll() error {
eg := errgroup.Group{}
for _, s := range o.services {
s := s
eg.Go(func() error {
s.tried = true
err := s.Try()
if err != nil {
return &Error{
failedPhase: ErrTryFailed,
err: err,
serviceName: s.name,
}
}
s.trySucceeded = true
return nil
})
}
return eg.Wait()
}
func (o *orchestrator) confirmAll() error {
eg := errgroup.Group{}
for _, s := range o.services {
s := s
eg.Go(func() error {
s.confirmed = true
if !s.trySucceeded {
return &Error{
failedPhase: ErrConfirmFailed,
err: errors.New("try did not succeed"),
serviceName: s.name,
}
}
o.Lock()
defer o.Unlock()
err := backoff.Retry(s.Confirm, o.backoff)
if err != nil {
return &Error{
failedPhase: ErrConfirmFailed,
err: err,
serviceName: s.name,
}
}
s.confirmSucceeded = true
return nil
})
}
return eg.Wait()
}
func (o *orchestrator) cancelAll() error {
eg := errgroup.Group{}
for _, s := range o.services {
s := s
eg.Go(func() error {
if !s.tried {
return nil
}
s.canceled = true
o.Lock()
defer o.Unlock()
err := backoff.Retry(s.Cancel, o.backoff)
if err != nil {
return &Error{
failedPhase: ErrCancelFailed,
err: err,
serviceName: s.name,
}
}
s.cancelSucceeded = true
return nil
})
}
return eg.Wait()
}