-
Notifications
You must be signed in to change notification settings - Fork 492
/
fsm.go
174 lines (148 loc) · 4.33 KB
/
fsm.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
/*
* Copyright (c) 2017, The Easegress Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package spec provides the spec for FaaS.
package spec
import (
"fmt"
)
type (
// FSM function is a finite state machine for managing faas function.
FSM struct {
currentState State
}
// Event is the event type generated by CLI or FaaSProvider.
Event string
// State is the FaaSFunction's state.
State string
// transition builds a role for state changing
transition struct {
From State
Event Event
To State
}
)
const (
// State value of FaaSFunction
// FailedState is the failed state
FailedState State = "failed"
// InitialState is the init state
InitialState State = "initial"
// ActiveState is the active state
ActiveState State = "active"
// InactiveState is the inactive state
InactiveState State = "inactive"
// DestroyedState - only for keep fsm working
DestroyedState State = "destroyed"
// Function event invoked by APIs.
// CreateEvent is the create event
CreateEvent Event = "create"
// StartEvent is the start event
StartEvent Event = "start"
// StopEvent is the stop event
StopEvent Event = "stop"
// UpdateEvent is the update event
UpdateEvent Event = "update"
// DeleteEvent is the delete event
DeleteEvent Event = "delete"
// Function Event invoked by FaaSProvider
// ReadyEvent is the ready event
ReadyEvent Event = "ready"
// PendingEvent is the pending event
PendingEvent Event = "pending"
// ErrorEvent is the error event
ErrorEvent Event = "error"
)
var (
validState = map[State]struct{}{
InitialState: {},
ActiveState: {},
InactiveState: {},
FailedState: {},
DestroyedState: {},
}
validEvent = map[Event]struct{}{
UpdateEvent: {},
DeleteEvent: {},
StopEvent: {},
StartEvent: {},
CreateEvent: {},
PendingEvent: {},
ErrorEvent: {},
ReadyEvent: {},
}
transitions = map[Event][]transition{}
)
func init() {
table := []transition{
{InitialState, UpdateEvent, InitialState},
{InitialState, DeleteEvent, DestroyedState},
{InitialState, ReadyEvent, ActiveState},
{InitialState, PendingEvent, InitialState},
{InitialState, ErrorEvent, FailedState},
{ActiveState, StopEvent, InactiveState},
{ActiveState, ErrorEvent, FailedState},
{ActiveState, ReadyEvent, ActiveState},
{ActiveState, PendingEvent, FailedState},
{InactiveState, UpdateEvent, InitialState},
{InactiveState, StartEvent, InactiveState},
{InactiveState, DeleteEvent, DestroyedState},
{InactiveState, ReadyEvent, ActiveState},
{InactiveState, PendingEvent, FailedState},
{InactiveState, ErrorEvent, FailedState},
{FailedState, DeleteEvent, DestroyedState},
{FailedState, UpdateEvent, InitialState},
{FailedState, ReadyEvent, InitialState},
{FailedState, ErrorEvent, FailedState},
{FailedState, PendingEvent, FailedState},
}
// using Event as the key
for _, t := range table {
transitions[t.Event] = append(transitions[t.Event], t)
}
}
// InitState returns the initial FSM state which is the `pending` state.
func InitState() State {
return InitialState
}
// InitFSM creates a finite state machine by given states
func InitFSM(state State) (*FSM, error) {
if _, exist := validState[state]; !exist {
return nil, fmt.Errorf("invalid state: %s", state)
}
return &FSM{
currentState: state,
}, nil
}
// Next turns the function status into properate state by given event.
func (fsm *FSM) Next(event Event) error {
if _, exist := validEvent[event]; !exist {
return fmt.Errorf("unknown event: %s", event)
}
if t, exist := transitions[event]; exist {
for _, v := range t {
if fsm.currentState == v.From {
fsm.currentState = v.To
return nil
}
}
}
return fmt.Errorf("invalid event: %s, currentState: %s", event, fsm.currentState)
}
// Current gets FSM current state.
func (fsm *FSM) Current() State {
return fsm.currentState
}