-
Notifications
You must be signed in to change notification settings - Fork 19
/
state_machine.go
221 lines (192 loc) · 5.91 KB
/
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
package assets
import (
"context"
"reflect"
"github.com/filecoin-project/go-statemachine"
"golang.org/x/xerrors"
)
// Plan prepares a plan for asset pulling
func (m *Manager) Plan(events []statemachine.Event, user interface{}) (interface{}, uint64, error) {
next, processed, err := m.plan(events, user.(*AssetPullingInfo))
if err != nil || next == nil {
return nil, processed, nil
}
return func(ctx statemachine.Context, si AssetPullingInfo) error {
err := next(ctx, si)
if err != nil {
log.Errorf("unhandled error (%s): %+v", si.CID, err)
return nil
}
return nil
}, processed, nil
}
// maps asset states to their corresponding planner functions
var planners = map[AssetState]func(events []statemachine.Event, state *AssetPullingInfo) (uint64, error){
// external import
UndefinedState: planOne(
on(AssetStartPulls{}, SeedSelect),
),
SeedSelect: planOne(
on(PullRequestSent{}, SeedPulling),
on(SelectFailed{}, SeedFailed),
on(SkipStep{}, CandidatesSelect),
),
SeedPulling: planOne(
on(PullSucceed{}, CandidatesSelect),
on(PullFailed{}, SeedFailed),
apply(PulledResult{}),
),
CandidatesSelect: planOne(
on(PullRequestSent{}, CandidatesPulling),
on(SkipStep{}, EdgesSelect),
on(SelectFailed{}, CandidatesFailed),
),
CandidatesPulling: planOne(
on(PullFailed{}, CandidatesFailed),
on(PullSucceed{}, EdgesSelect),
apply(PulledResult{}),
),
EdgesSelect: planOne(
on(PullRequestSent{}, EdgesPulling),
on(SelectFailed{}, EdgesFailed),
on(SkipStep{}, Servicing),
),
EdgesPulling: planOne(
on(PullFailed{}, EdgesFailed),
on(PullSucceed{}, Servicing),
apply(PulledResult{}),
),
SeedFailed: planOne(
on(AssetRePull{}, SeedSelect),
),
CandidatesFailed: planOne(
on(AssetRePull{}, CandidatesSelect),
),
EdgesFailed: planOne(
on(AssetRePull{}, EdgesSelect),
),
Remove: planOne(
on(AssetStartPulls{}, SeedSelect),
),
}
// plan creates a plan for the next asset pulling action based on the given events and asset state
func (m *Manager) plan(events []statemachine.Event, state *AssetPullingInfo) (func(statemachine.Context, AssetPullingInfo) error, uint64, error) {
p := planners[state.State]
if p == nil {
if len(events) == 1 {
if _, ok := events[0].User.(globalMutator); ok {
p = planOne() // in case we're in a really weird state, allow restart / update state / remove
}
}
if p == nil {
return nil, 0, xerrors.Errorf("planner for state %s not found", state.State)
}
}
processed, err := p(events, state)
if err != nil {
return nil, processed, xerrors.Errorf("running planner for state %s failed: %w", state.State, err)
}
log.Debugf("%s: %s", state.Hash, state.State)
switch state.State {
// Happy path
case SeedSelect:
return m.handleSeedSelect, processed, nil
case SeedPulling:
return m.handleSeedPulling, processed, nil
case CandidatesSelect:
return m.handleCandidatesSelect, processed, nil
case EdgesSelect:
return m.handleEdgesSelect, processed, nil
case CandidatesPulling:
return m.handleCandidatesPulling, processed, nil
case EdgesPulling:
return m.handleEdgesPulling, processed, nil
case Servicing:
return m.handleServicing, processed, nil
case SeedFailed, CandidatesFailed, EdgesFailed:
return m.handlePullsFailed, processed, nil
case Remove:
return nil, processed, nil
// Fatal errors
case UndefinedState:
log.Error("asset update with undefined state!")
default:
log.Errorf("unexpected asset update state: %s", state.State)
}
return nil, processed, nil
}
// prepares a single plan for a given asset state, allowing for one event at a time
func planOne(ts ...func() (mut mutator, next func(info *AssetPullingInfo) (more bool, err error))) func(events []statemachine.Event, state *AssetPullingInfo) (uint64, error) {
return func(events []statemachine.Event, state *AssetPullingInfo) (uint64, error) {
eloop:
for i, event := range events {
if gm, ok := event.User.(globalMutator); ok {
gm.applyGlobal(state)
return uint64(i + 1), nil
}
for _, t := range ts {
mut, next := t()
if reflect.TypeOf(event.User) != reflect.TypeOf(mut) {
continue
}
if err, isErr := event.User.(error); isErr {
log.Warnf("asset %s got error event %T: %+v", state.CID, event.User, err)
}
event.User.(mutator).apply(state)
more, err := next(state)
if err != nil || !more {
return uint64(i + 1), err
}
continue eloop
}
_, ok := event.User.(Ignorable)
if ok {
continue
}
return uint64(i + 1), xerrors.Errorf("planner for state %s received unexpected event %T (%+v)", state.State, event.User, event)
}
return uint64(len(events)), nil
}
}
// on is a utility function to handle state transitions
func on(mut mutator, next AssetState) func() (mutator, func(*AssetPullingInfo) (bool, error)) {
return func() (mutator, func(*AssetPullingInfo) (bool, error)) {
return mut, func(state *AssetPullingInfo) (bool, error) {
state.State = next
return false, nil
}
}
}
// apply like `on`, but doesn't change state
func apply(mut mutator) func() (mutator, func(*AssetPullingInfo) (bool, error)) {
return func() (mutator, func(*AssetPullingInfo) (bool, error)) {
return mut, func(state *AssetPullingInfo) (bool, error) {
return true, nil
}
}
}
// initStateMachines init all asset state machines
func (m *Manager) initStateMachines(ctx context.Context) error {
// initialization
defer m.stateMachineWait.Done()
list, err := m.ListAssets()
if err != nil {
return err
}
for _, asset := range list {
if err := m.assetStateMachines.Send(asset.Hash, PullAssetRestart{}); err != nil {
log.Errorf("initStateMachines asset send %s , err %s", asset.CID, err.Error())
continue
}
m.addOrResetAssetTicker(asset.Hash.String())
}
return nil
}
// ListAssets load asset pull infos from state machine
func (m *Manager) ListAssets() ([]AssetPullingInfo, error) {
var list []AssetPullingInfo
if err := m.assetStateMachines.List(&list); err != nil {
return nil, err
}
return list, nil
}