-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
node_fsm.go
323 lines (293 loc) · 10 KB
/
node_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
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
package client
import (
"fmt"
"math/big"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
promPoolRPCNodeTransitionsToAlive = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_alive",
Help: transitionString(nodeStateAlive),
}, []string{"chainID", "nodeName"})
promPoolRPCNodeTransitionsToInSync = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_in_sync",
Help: fmt.Sprintf("%s to %s", transitionString(nodeStateOutOfSync), nodeStateAlive),
}, []string{"chainID", "nodeName"})
promPoolRPCNodeTransitionsToOutOfSync = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_out_of_sync",
Help: transitionString(nodeStateOutOfSync),
}, []string{"chainID", "nodeName"})
promPoolRPCNodeTransitionsToUnreachable = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_unreachable",
Help: transitionString(nodeStateUnreachable),
}, []string{"chainID", "nodeName"})
promPoolRPCNodeTransitionsToInvalidChainID = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_invalid_chain_id",
Help: transitionString(nodeStateInvalidChainID),
}, []string{"chainID", "nodeName"})
promPoolRPCNodeTransitionsToUnusable = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_unusable",
Help: transitionString(nodeStateUnusable),
}, []string{"chainID", "nodeName"})
promPoolRPCNodeTransitionsToSyncing = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pool_rpc_node_num_transitions_to_syncing",
Help: transitionString(nodeStateSyncing),
}, []string{"chainID", "nodeName"})
)
// nodeState represents the current state of the node
// Node is a FSM (finite state machine)
type nodeState int
func (n nodeState) String() string {
switch n {
case nodeStateUndialed:
return "Undialed"
case nodeStateDialed:
return "Dialed"
case nodeStateInvalidChainID:
return "InvalidChainID"
case nodeStateAlive:
return "Alive"
case nodeStateUnreachable:
return "Unreachable"
case nodeStateUnusable:
return "Unusable"
case nodeStateOutOfSync:
return "OutOfSync"
case nodeStateClosed:
return "Closed"
case nodeStateSyncing:
return "Syncing"
default:
return fmt.Sprintf("nodeState(%d)", n)
}
}
// GoString prints a prettier state
func (n nodeState) GoString() string {
return fmt.Sprintf("nodeState%s(%d)", n.String(), n)
}
const (
// nodeStateUndialed is the first state of a virgin node
nodeStateUndialed = nodeState(iota)
// nodeStateDialed is after a node has successfully dialed but before it has verified the correct chain ID
nodeStateDialed
// nodeStateInvalidChainID is after chain ID verification failed
nodeStateInvalidChainID
// nodeStateAlive is a healthy node after chain ID verification succeeded
nodeStateAlive
// nodeStateUnreachable is a node that cannot be dialed or has disconnected
nodeStateUnreachable
// nodeStateOutOfSync is a node that is accepting connections but exceeded
// the failure threshold without sending any new heads. It will be
// disconnected, then put into a revive loop and re-awakened after redial
// if a new head arrives
nodeStateOutOfSync
// nodeStateUnusable is a sendonly node that has an invalid URL that can never be reached
nodeStateUnusable
// nodeStateClosed is after the connection has been closed and the node is at the end of its lifecycle
nodeStateClosed
// nodeStateSyncing is a node that is actively back-filling blockchain. Usually, it's a newly set up node that is
// still syncing the chain. The main difference from `nodeStateOutOfSync` is that it represents state relative
// to other primary nodes configured in the MultiNode. In contrast, `nodeStateSyncing` represents the internal state of
// the node (RPC).
nodeStateSyncing
// nodeStateLen tracks the number of states
nodeStateLen
)
// allNodeStates represents all possible states a node can be in
var allNodeStates []nodeState
func init() {
for s := nodeState(0); s < nodeStateLen; s++ {
allNodeStates = append(allNodeStates, s)
}
}
// FSM methods
// State allows reading the current state of the node.
func (n *node[CHAIN_ID, HEAD, RPC]) State() nodeState {
n.stateMu.RLock()
defer n.stateMu.RUnlock()
return n.state
}
func (n *node[CHAIN_ID, HEAD, RPC]) StateAndLatest() (nodeState, int64, *big.Int) {
n.stateMu.RLock()
defer n.stateMu.RUnlock()
return n.state, n.stateLatestBlockNumber, n.stateLatestTotalDifficulty
}
// setState is only used by internal state management methods.
// This is low-level; care should be taken by the caller to ensure the new state is a valid transition.
// State changes should always be synchronous: only one goroutine at a time should change state.
// n.stateMu should not be locked for long periods of time because external clients expect a timely response from n.State()
func (n *node[CHAIN_ID, HEAD, RPC]) setState(s nodeState) {
n.stateMu.Lock()
defer n.stateMu.Unlock()
n.state = s
}
// declareXXX methods change the state and pass conrol off the new state
// management goroutine
func (n *node[CHAIN_ID, HEAD, RPC]) declareAlive() {
n.transitionToAlive(func() {
n.lfcLog.Infow("RPC Node is online", "nodeState", n.state)
n.wg.Add(1)
go n.aliveLoop()
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToAlive(fn func()) {
promPoolRPCNodeTransitionsToAlive.WithLabelValues(n.chainID.String(), n.name).Inc()
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateDialed, nodeStateInvalidChainID, nodeStateSyncing:
n.state = nodeStateAlive
default:
panic(transitionFail(n.state, nodeStateAlive))
}
fn()
}
// declareInSync puts a node back into Alive state, allowing it to be used by
// pool consumers again
func (n *node[CHAIN_ID, HEAD, RPC]) declareInSync() {
n.transitionToInSync(func() {
n.lfcLog.Infow("RPC Node is back in sync", "nodeState", n.state)
n.wg.Add(1)
go n.aliveLoop()
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToInSync(fn func()) {
promPoolRPCNodeTransitionsToAlive.WithLabelValues(n.chainID.String(), n.name).Inc()
promPoolRPCNodeTransitionsToInSync.WithLabelValues(n.chainID.String(), n.name).Inc()
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateOutOfSync, nodeStateSyncing:
n.state = nodeStateAlive
default:
panic(transitionFail(n.state, nodeStateAlive))
}
fn()
}
// declareOutOfSync puts a node into OutOfSync state, disconnecting all current
// clients and making it unavailable for use until back in-sync.
func (n *node[CHAIN_ID, HEAD, RPC]) declareOutOfSync(isOutOfSync func(num int64, td *big.Int) bool) {
n.transitionToOutOfSync(func() {
n.lfcLog.Errorw("RPC Node is out of sync", "nodeState", n.state)
n.wg.Add(1)
go n.outOfSyncLoop(isOutOfSync)
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToOutOfSync(fn func()) {
promPoolRPCNodeTransitionsToOutOfSync.WithLabelValues(n.chainID.String(), n.name).Inc()
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateAlive:
n.disconnectAll()
n.state = nodeStateOutOfSync
default:
panic(transitionFail(n.state, nodeStateOutOfSync))
}
fn()
}
func (n *node[CHAIN_ID, HEAD, RPC]) declareUnreachable() {
n.transitionToUnreachable(func() {
n.lfcLog.Errorw("RPC Node is unreachable", "nodeState", n.state)
n.wg.Add(1)
go n.unreachableLoop()
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToUnreachable(fn func()) {
promPoolRPCNodeTransitionsToUnreachable.WithLabelValues(n.chainID.String(), n.name).Inc()
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateUndialed, nodeStateDialed, nodeStateAlive, nodeStateOutOfSync, nodeStateInvalidChainID, nodeStateSyncing:
n.disconnectAll()
n.state = nodeStateUnreachable
default:
panic(transitionFail(n.state, nodeStateUnreachable))
}
fn()
}
func (n *node[CHAIN_ID, HEAD, RPC]) declareState(state nodeState) {
if n.State() == nodeStateClosed {
return
}
switch state {
case nodeStateInvalidChainID:
n.declareInvalidChainID()
case nodeStateUnreachable:
n.declareUnreachable()
case nodeStateSyncing:
n.declareSyncing()
case nodeStateAlive:
n.declareAlive()
default:
panic(fmt.Sprintf("%#v state declaration is not implemented", state))
}
}
func (n *node[CHAIN_ID, HEAD, RPC]) declareInvalidChainID() {
n.transitionToInvalidChainID(func() {
n.lfcLog.Errorw("RPC Node has the wrong chain ID", "nodeState", n.state)
n.wg.Add(1)
go n.invalidChainIDLoop()
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToInvalidChainID(fn func()) {
promPoolRPCNodeTransitionsToInvalidChainID.WithLabelValues(n.chainID.String(), n.name).Inc()
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateDialed, nodeStateOutOfSync, nodeStateSyncing:
n.disconnectAll()
n.state = nodeStateInvalidChainID
default:
panic(transitionFail(n.state, nodeStateInvalidChainID))
}
fn()
}
func (n *node[CHAIN_ID, HEAD, RPC]) declareSyncing() {
n.transitionToSyncing(func() {
n.lfcLog.Errorw("RPC Node is syncing", "nodeState", n.state)
n.wg.Add(1)
go n.syncingLoop()
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToSyncing(fn func()) {
promPoolRPCNodeTransitionsToSyncing.WithLabelValues(n.chainID.String(), n.name).Inc()
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateDialed, nodeStateOutOfSync, nodeStateInvalidChainID:
n.disconnectAll()
n.state = nodeStateSyncing
default:
panic(transitionFail(n.state, nodeStateSyncing))
}
if !n.nodePoolCfg.NodeIsSyncingEnabled() {
panic("unexpected transition to nodeStateSyncing, while it's disabled")
}
fn()
}
func transitionString(state nodeState) string {
return fmt.Sprintf("Total number of times node has transitioned to %s", state)
}
func transitionFail(from nodeState, to nodeState) string {
return fmt.Sprintf("cannot transition from %#v to %#v", from, to)
}