-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmulti_node.go
More file actions
391 lines (349 loc) · 11.8 KB
/
multi_node.go
File metadata and controls
391 lines (349 loc) · 11.8 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
390
391
package multinode
import (
"context"
"fmt"
"math/big"
"slices"
"sync"
"time"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
)
var ErrNodeError = fmt.Errorf("no live nodes available")
type multiNodeMetrics interface {
RecordNodeStates(ctx context.Context, state string, count int64)
}
// MultiNode is a generalized multi node client interface that includes methods to interact with different chains.
// It also handles multiple node RPC connections simultaneously.
type MultiNode[
CHAIN_ID ID,
RPC any,
] struct {
services.Service
eng *services.Engine
primaryNodes []Node[CHAIN_ID, RPC]
sendOnlyNodes []SendOnlyNode[CHAIN_ID, RPC]
chainID CHAIN_ID
lggr logger.SugaredLogger
metrics multiNodeMetrics
selectionMode string
nodeSelector NodeSelector[CHAIN_ID, RPC]
leaseDuration time.Duration
leaseTicker *time.Ticker
chainFamily string
reportInterval time.Duration
deathDeclarationDelay time.Duration
activeMu sync.RWMutex
activeNode Node[CHAIN_ID, RPC]
}
func NewMultiNode[
CHAIN_ID ID,
RPC any,
](
lggr logger.Logger,
metrics multiNodeMetrics,
selectionMode string, // type of the "best" RPC selector (e.g HighestHead, RoundRobin, etc.)
leaseDuration time.Duration, // defines interval on which new "best" RPC should be selected
primaryNodes []Node[CHAIN_ID, RPC],
sendOnlyNodes []SendOnlyNode[CHAIN_ID, RPC],
chainID CHAIN_ID, // configured chain ID (used to verify that passed primaryNodes belong to the same chain)
chainFamily string, // name of the chain family - used in the metrics
deathDeclarationDelay time.Duration,
) *MultiNode[CHAIN_ID, RPC] {
nodeSelector := newNodeSelector(selectionMode, primaryNodes)
// Prometheus' default interval is 15s, set this to under 7.5s to avoid
// aliasing (see: https://en.wikipedia.org/wiki/Nyquist_frequency)
const reportInterval = 6500 * time.Millisecond
c := &MultiNode[CHAIN_ID, RPC]{
metrics: metrics,
primaryNodes: primaryNodes,
sendOnlyNodes: sendOnlyNodes,
chainID: chainID,
selectionMode: selectionMode,
nodeSelector: nodeSelector,
leaseDuration: leaseDuration,
chainFamily: chainFamily,
reportInterval: reportInterval,
deathDeclarationDelay: deathDeclarationDelay,
}
c.Service, c.eng = services.Config{
Name: "MultiNode",
Start: c.start,
Close: c.close,
}.NewServiceEngine(logger.With(lggr, "chainID", chainID.String()))
c.lggr = c.eng.SugaredLogger
c.lggr.Debugf("The MultiNode is configured to use NodeSelectionMode: %s", selectionMode)
return c
}
func (c *MultiNode[CHAIN_ID, RPC]) ChainID() CHAIN_ID {
return c.chainID
}
func (c *MultiNode[CHAIN_ID, RPC]) DoAll(ctx context.Context, do func(ctx context.Context, rpc RPC, isSendOnly bool)) error {
return c.eng.IfNotStopped(func() error {
callsCompleted := 0
for _, n := range c.primaryNodes {
select {
case <-ctx.Done():
return ctx.Err()
default:
if n.State() != nodeStateAlive {
continue
}
do(ctx, n.RPC(), false)
callsCompleted++
}
}
for _, n := range c.sendOnlyNodes {
select {
case <-ctx.Done():
return ctx.Err()
default:
if n.State() != nodeStateAlive {
continue
}
do(ctx, n.RPC(), true)
}
}
if callsCompleted == 0 {
return ErrNodeError
}
return nil
})
}
func (c *MultiNode[CHAIN_ID, RPC]) NodeStates() map[string]string {
states := map[string]string{}
for _, n := range c.primaryNodes {
states[n.Name()] = n.State().String()
}
for _, n := range c.sendOnlyNodes {
states[n.Name()] = n.State().String()
}
return states
}
// Start starts every node in the pool
//
// Nodes handle their own redialing and runloops, so this function does not
// return any error if the nodes aren't available
func (c *MultiNode[CHAIN_ID, RPC]) start(ctx context.Context) error {
if len(c.primaryNodes) == 0 {
return fmt.Errorf("no available nodes for chain %s", c.chainID.String())
}
var ms services.MultiStart
for _, n := range c.primaryNodes {
if n.ConfiguredChainID().String() != c.chainID.String() {
return ms.CloseBecause(fmt.Errorf("node %s has configured chain ID %s which does not match multinode configured chain ID of %s", n.String(), n.ConfiguredChainID().String(), c.chainID.String()))
}
n.SetPoolChainInfoProvider(c)
// node will handle its own redialing and automatic recovery
if err := ms.Start(ctx, n); err != nil {
return err
}
}
for _, s := range c.sendOnlyNodes {
if s.ConfiguredChainID().String() != c.chainID.String() {
return ms.CloseBecause(fmt.Errorf("sendonly node %s has configured chain ID %s which does not match multinode configured chain ID of %s", s.String(), s.ConfiguredChainID().String(), c.chainID.String()))
}
if err := ms.Start(ctx, s); err != nil {
return err
}
}
c.eng.Go(c.runLoop)
if c.leaseDuration.Seconds() > 0 && c.selectionMode != NodeSelectionModeRoundRobin && c.selectionMode != NodeSelectionModeRandomRPC {
c.lggr.Infof("The MultiNode will switch to best node every %s", c.leaseDuration.String())
c.eng.Go(c.checkLeaseLoop)
} else {
c.lggr.Info("Best node switching is disabled")
}
return nil
}
// Close tears down the MultiNode and closes all nodes
func (c *MultiNode[CHAIN_ID, RPC]) close() error {
return services.CloseAll(services.MultiCloser(c.primaryNodes), services.MultiCloser(c.sendOnlyNodes))
}
// SelectRPC returns an RPC of an active node. If there are no active nodes it returns an error, but tolerates undialed
// nodes by waiting for initial dial.
// Call this method from your chain-specific client implementation to access any chain-specific rpc calls.
func (c *MultiNode[CHAIN_ID, RPC]) SelectRPC(ctx context.Context) (rpc RPC, err error) {
n, err := c.selectNode(ctx)
if err != nil {
return rpc, err
}
return n.RPC(), nil
}
// selectNode returns the active Node, if it is still nodeStateAlive, otherwise it selects a new one from the NodeSelector.
func (c *MultiNode[CHAIN_ID, RPC]) selectNode(ctx context.Context) (node Node[CHAIN_ID, RPC], err error) {
if c.selectionMode == NodeSelectionModeRandomRPC {
return c.awaitNodeSelection(ctx)
}
c.activeMu.RLock()
node = c.activeNode
c.activeMu.RUnlock()
if node != nil && node.State() == nodeStateAlive {
return // still alive
}
// select a new one
c.activeMu.Lock()
defer c.activeMu.Unlock()
node = c.activeNode
if node != nil && node.State() == nodeStateAlive {
return // another goroutine beat us here
}
var prevNodeName string
if c.activeNode != nil {
prevNodeName = c.activeNode.String()
c.activeNode.UnsubscribeAllExceptAliveLoop()
}
c.activeNode, err = c.awaitNodeSelection(ctx)
if err != nil {
return nil, err
}
c.lggr.Debugw("Switched to a new active node due to prev node heath issues", "prevNode", prevNodeName, "newNode", c.activeNode.String())
return c.activeNode, err
}
// awaitNodeSelection blocks until nodeSelector returns a live node or all nodes
// finish initializing. Returns ErrNodeError when no live nodes are available.
func (c *MultiNode[CHAIN_ID, RPC]) awaitNodeSelection(ctx context.Context) (Node[CHAIN_ID, RPC], error) {
for {
node := c.nodeSelector.Select()
if node != nil {
return node, nil
}
if slices.ContainsFunc(c.primaryNodes, func(n Node[CHAIN_ID, RPC]) bool {
return n.State().isInitializing()
}) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(100 * time.Millisecond):
continue
}
}
c.lggr.Criticalw("No live RPC nodes available", "NodeSelectionMode", c.nodeSelector.Name())
c.eng.EmitHealthErr(fmt.Errorf("no live nodes available for chain %s", c.chainID.String()))
return nil, ErrNodeError
}
}
// LatestChainInfo - returns number of live nodes available in the pool, so we can prevent the last alive node in a pool from being marked as out-of-sync.
// Return highest ChainInfo most recently received by the alive nodes.
// E.g. If Node A's the most recent block is 10 and highest 15 and for Node B it's - 12 and 14. This method will return 12.
func (c *MultiNode[CHAIN_ID, RPC]) LatestChainInfo() (int, ChainInfo) {
var nLiveNodes int
ch := ChainInfo{
TotalDifficulty: big.NewInt(0),
}
for _, n := range c.primaryNodes {
if s, nodeChainInfo := n.StateAndLatest(); s == nodeStateAlive {
nLiveNodes++
ch.BlockNumber = max(ch.BlockNumber, nodeChainInfo.BlockNumber)
ch.FinalizedBlockNumber = max(ch.FinalizedBlockNumber, nodeChainInfo.FinalizedBlockNumber)
ch.TotalDifficulty = MaxTotalDifficulty(ch.TotalDifficulty, nodeChainInfo.TotalDifficulty)
}
}
return nLiveNodes, ch
}
// HighestUserObservations - returns highest ChainInfo ever observed by any user of the MultiNode
func (c *MultiNode[CHAIN_ID, RPC]) HighestUserObservations() ChainInfo {
ch := ChainInfo{
TotalDifficulty: big.NewInt(0),
}
for _, n := range c.primaryNodes {
nodeChainInfo := n.HighestUserObservations()
ch.BlockNumber = max(ch.BlockNumber, nodeChainInfo.BlockNumber)
ch.FinalizedBlockNumber = max(ch.FinalizedBlockNumber, nodeChainInfo.FinalizedBlockNumber)
ch.TotalDifficulty = MaxTotalDifficulty(ch.TotalDifficulty, nodeChainInfo.TotalDifficulty)
}
return ch
}
func (c *MultiNode[CHAIN_ID, RPC]) checkLease() {
bestNode := c.nodeSelector.Select()
for _, n := range c.primaryNodes {
// Terminate client subscriptions. Services are responsible for reconnecting, which will be routed to the new
// best node. Only terminate connections with more than 1 subscription to account for the aliveLoop subscription
if n.State() == nodeStateAlive && n != bestNode {
c.lggr.Infof("Switching to best node from %q to %q", n.String(), bestNode.String())
n.UnsubscribeAllExceptAliveLoop()
}
}
c.activeMu.Lock()
defer c.activeMu.Unlock()
if bestNode != c.activeNode {
if c.activeNode != nil {
c.activeNode.UnsubscribeAllExceptAliveLoop()
}
c.activeNode = bestNode
}
}
func (c *MultiNode[CHAIN_ID, RPC]) checkLeaseLoop(ctx context.Context) {
c.leaseTicker = time.NewTicker(c.leaseDuration)
defer c.leaseTicker.Stop()
for {
select {
case <-c.leaseTicker.C:
c.checkLease()
case <-ctx.Done():
return
}
}
}
func (c *MultiNode[CHAIN_ID, RPC]) runLoop(ctx context.Context) {
nodeStates := make([]nodeWithState, len(c.primaryNodes))
for i, n := range c.primaryNodes {
nodeStates[i] = nodeWithState{
Node: n.String(),
State: n.State().String(),
DeadSince: nil,
}
}
c.report(nodeStates)
monitor := services.NewTicker(c.reportInterval)
defer monitor.Stop()
for {
select {
case <-monitor.C:
c.report(nodeStates)
case <-ctx.Done():
return
}
}
}
type nodeWithState struct {
Node string
State string
DeadSince *time.Time
}
func (c *MultiNode[CHAIN_ID, RPC]) report(nodesStateInfo []nodeWithState) {
start := time.Now()
var dead int
counts := make(map[nodeState]int)
for i, n := range c.primaryNodes {
state := n.State()
counts[state]++
nodesStateInfo[i].State = state.String()
if state == nodeStateAlive {
nodesStateInfo[i].DeadSince = nil
continue
}
if nodesStateInfo[i].DeadSince == nil {
nodesStateInfo[i].DeadSince = &start
}
if start.Sub(*nodesStateInfo[i].DeadSince) >= c.deathDeclarationDelay {
dead++
}
}
ctx, cancel := c.eng.NewCtx()
defer cancel()
for _, state := range allNodeStates {
count := int64(counts[state])
c.metrics.RecordNodeStates(ctx, state.String(), count)
}
total := len(c.primaryNodes)
live := total - dead
c.lggr.Tracew(fmt.Sprintf("MultiNode state: %d/%d nodes are alive", live, total), "nodeStates", nodesStateInfo)
if total == dead {
rerr := fmt.Errorf("no primary nodes available: 0/%d nodes are alive", total)
c.lggr.Criticalw(rerr.Error(), "nodeStates", nodesStateInfo)
c.eng.EmitHealthErr(rerr)
} else if dead > 0 {
c.lggr.Errorw(fmt.Sprintf("At least one primary node is dead: %d/%d nodes are alive", live, total), "nodeStates", nodesStateInfo)
}
}