-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_fsm.go
More file actions
389 lines (350 loc) · 11.5 KB
/
node_fsm.go
File metadata and controls
389 lines (350 loc) · 11.5 KB
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
package multinode
import (
"fmt"
)
// nodeState represents the current state of the node
// Node is a FSM (finite state machine)
type nodeState int
// isInitializing returns true if the node is still doing the initial dial & verification.
func (n nodeState) isInitializing() bool {
return n == nodeStateUndialed || n == nodeStateDialed
}
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"
case nodeStateFinalizedBlockOutOfSync:
return "FinalizedBlockOutOfSync"
case nodeStateFinalizedStateNotAvailable:
return "FinalizedStateNotAvailable"
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
// nodeStateFinalizedBlockOutOfSync - node is lagging behind on latest finalized block
nodeStateFinalizedBlockOutOfSync
// nodeStateFinalizedStateNotAvailable - node cannot serve historical state at finalized block
nodeStateFinalizedStateNotAvailable
// 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.recalculateState()
}
func (n *node[CHAIN_ID, HEAD, RPC]) getCachedState() nodeState {
n.stateMu.RLock()
defer n.stateMu.RUnlock()
return n.state
}
func (n *node[CHAIN_ID, HEAD, RPC]) recalculateState() nodeState {
if n.state != nodeStateAlive {
return n.state
}
// double check that node is not lagging on finalized block
if n.nodePoolCfg.EnforceRepeatableRead() && n.isFinalizedBlockOutOfSync() {
return nodeStateFinalizedBlockOutOfSync
}
return nodeStateAlive
}
func (n *node[CHAIN_ID, HEAD, RPC]) isFinalizedBlockOutOfSync() bool {
if n.poolInfoProvider == nil {
return false
}
highestObservedByCaller := n.poolInfoProvider.HighestUserObservations()
latest, rpcHighest := n.rpc.GetInterceptedChainInfo()
isOutOfSync := false
if n.chainCfg.FinalityTagEnabled() {
isOutOfSync = latest.FinalizedBlockNumber < highestObservedByCaller.FinalizedBlockNumber-int64(n.chainCfg.FinalizedBlockOffset())
} else {
isOutOfSync = latest.BlockNumber < highestObservedByCaller.BlockNumber-int64(n.chainCfg.FinalizedBlockOffset())
}
if isOutOfSync {
n.lfcLog.Debugw("finalized block is out of sync", "rpcLatestChainInfo", latest, "rpcHighest", rpcHighest, "highestObservedByCaller", highestObservedByCaller)
}
return isOutOfSync
}
// StateAndLatest returns nodeState with the latest ChainInfo observed by Node during current lifecycle.
func (n *node[CHAIN_ID, HEAD, RPC]) StateAndLatest() (nodeState, ChainInfo) {
n.stateMu.RLock()
defer n.stateMu.RUnlock()
latest, _ := n.rpc.GetInterceptedChainInfo()
return n.recalculateState(), latest
}
// HighestUserObservations - returns highest ChainInfo ever observed by external user of the Node
func (n *node[CHAIN_ID, HEAD, RPC]) HighestUserObservations() ChainInfo {
_, highestUserObservations := n.rpc.GetInterceptedChainInfo()
return highestUserObservations
}
func (n *node[CHAIN_ID, HEAD, RPC]) SetPoolChainInfoProvider(poolInfoProvider PoolChainInfoProvider) {
n.poolInfoProvider = poolInfoProvider
}
// 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()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToAlive(ctx, n.name)
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateDialed, nodeStateInvalidChainID, nodeStateSyncing, nodeStateFinalizedStateNotAvailable:
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()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToAlive(ctx, n.name)
n.metrics.IncrementNodeTransitionsToInSync(ctx, n.name)
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(syncIssues syncStatus) {
n.transitionToOutOfSync(func() {
n.lfcLog.Errorw("RPC Node is out of sync", "nodeState", n.state, "syncIssues", syncIssues)
n.wg.Add(1)
go n.outOfSyncLoop(syncIssues)
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToOutOfSync(fn func()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToOutOfSync(ctx, n.name)
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateAlive, nodeStateOutOfSync:
n.rpc.Close()
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()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToUnreachable(ctx, n.name)
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateUndialed, nodeStateDialed, nodeStateAlive, nodeStateOutOfSync, nodeStateInvalidChainID, nodeStateSyncing:
n.rpc.Close()
n.state = nodeStateUnreachable
default:
panic(transitionFail(n.state, nodeStateUnreachable))
}
fn()
}
func (n *node[CHAIN_ID, HEAD, RPC]) declareState(state nodeState) {
if n.getCachedState() == nodeStateClosed {
return
}
switch state {
case nodeStateInvalidChainID:
n.declareInvalidChainID()
case nodeStateUnreachable:
n.declareUnreachable()
case nodeStateSyncing:
n.declareSyncing()
case nodeStateAlive:
n.declareAlive()
case nodeStateFinalizedStateNotAvailable:
n.declareFinalizedStateNotAvailable()
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()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToInvalidChainID(ctx, n.name)
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateDialed, nodeStateOutOfSync, nodeStateSyncing:
n.rpc.Close()
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()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToSyncing(ctx, n.name)
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateDialed, nodeStateOutOfSync, nodeStateInvalidChainID:
n.rpc.Close()
n.state = nodeStateSyncing
default:
panic(transitionFail(n.state, nodeStateSyncing))
}
if !n.nodePoolCfg.NodeIsSyncingEnabled() {
panic("unexpected transition to nodeStateSyncing, while it's disabled")
}
fn()
}
func (n *node[CHAIN_ID, HEAD, RPC]) declareFinalizedStateNotAvailable() {
n.transitionToFinalizedStateNotAvailable(func() {
n.lfcLog.Errorw("RPC Node cannot serve finalized state", "nodeState", n.state)
n.wg.Add(1)
go n.finalizedStateNotAvailableLoop()
})
}
func (n *node[CHAIN_ID, HEAD, RPC]) transitionToFinalizedStateNotAvailable(fn func()) {
ctx, cancel := n.stopCh.NewCtx()
defer cancel()
n.metrics.IncrementNodeTransitionsToFinalizedStateNotAvailable(ctx, n.name)
n.stateMu.Lock()
defer n.stateMu.Unlock()
if n.state == nodeStateClosed {
return
}
switch n.state {
case nodeStateAlive:
n.rpc.Close()
n.state = nodeStateFinalizedStateNotAvailable
default:
panic(transitionFail(n.state, nodeStateFinalizedStateNotAvailable))
}
fn()
}
func transitionFail(from nodeState, to nodeState) string {
return fmt.Sprintf("cannot transition from %#v to %#v", from, to)
}