/
xatu.go
168 lines (134 loc) · 3.69 KB
/
xatu.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
package xatu
import (
"context"
"errors"
"sync"
"time"
"github.com/ethpandaops/xatu/pkg/mimicry/coordinator/cache"
xatuCoordinator "github.com/ethpandaops/xatu/pkg/mimicry/coordinator/xatu/coordinator"
xatuPeer "github.com/ethpandaops/xatu/pkg/mimicry/coordinator/xatu/peer"
"github.com/ethpandaops/xatu/pkg/mimicry/p2p/handler"
xatupb "github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/go-co-op/gocron"
"github.com/sirupsen/logrus"
)
const Type = "xatu"
type Xatu struct {
handlers *handler.Peer
captureDelay time.Duration
log logrus.FieldLogger
cache *cache.SharedCache
coordinator *xatuCoordinator.Coordinator
mu sync.Mutex
peers map[string]*xatuPeer.Peer
metrics *Metrics
}
func New(name string, config *xatuCoordinator.Config, handlers *handler.Peer, captureDelay time.Duration, log logrus.FieldLogger) (*Xatu, error) {
if config == nil {
return nil, errors.New("config is required")
}
if err := config.Validate(); err != nil {
return nil, err
}
coordinator, err := xatuCoordinator.NewCoordinator(name, config, log)
if err != nil {
return nil, err
}
handlers.ExecutionStatus = coordinator.HandleExecutionNodeRecordStatus
return &Xatu{
handlers: handlers,
captureDelay: captureDelay,
log: log,
cache: cache.NewSharedCache(),
coordinator: coordinator,
mu: sync.Mutex{},
peers: make(map[string]*xatuPeer.Peer),
metrics: NewMetrics("xatu_mimicry_coordinator_xatu"),
}, nil
}
func (x *Xatu) Type() string {
return Type
}
func (x *Xatu) Start(ctx context.Context) error {
if err := x.coordinator.Start(ctx); err != nil {
return err
}
if err := x.cache.Start(ctx); err != nil {
return err
}
if err := x.startCrons(ctx); err != nil {
return err
}
return nil
}
func (x *Xatu) Stop(ctx context.Context) error {
return nil
}
func (x *Xatu) startCrons(ctx context.Context) error {
c := gocron.NewScheduler(time.Local)
if _, err := c.Every("5s").Do(func() {
x.mu.Lock()
defer x.mu.Unlock()
connectedPeers := 0
connectionAttempts := 0
for _, peer := range x.peers {
if peer.Record.Connected {
connectedPeers++
}
connectionAttempts += int(peer.Record.ConnectionAttempts)
}
x.metrics.SetPeers(connectedPeers, "connected")
x.metrics.SetPeers(len(x.peers)-connectedPeers, "disconnected")
x.metrics.SetPeerConnectionAttempts(connectionAttempts)
}); err != nil {
return err
}
if _, err := c.Every("5m").Do(func() {
x.mu.Lock()
defer x.mu.Unlock()
var records []*xatupb.CoordinatedNodeRecord
for _, peer := range x.peers {
records = append(records, peer.Record)
}
res, err := x.coordinator.CoordinateExecutionNodeRecords(ctx, records)
if err != nil {
x.log.WithError(err).Error("failed to coordinate execution node records")
return
}
if res == nil {
x.log.Error("failed to coordinate execution node records: nil response")
return
}
retryDelay := time.Duration(res.RetryDelay) * time.Second
for i, peer := range x.peers {
found := false
for _, record := range res.NodeRecords {
if record == i {
found = true
peer.RetryDelay(retryDelay)
break
}
}
// remove peer
if !found {
if err := peer.Stop(); err != nil {
x.log.WithError(err).Error("failed to stop peer")
}
delete(x.peers, i)
}
}
for _, record := range res.NodeRecords {
if _, ok := x.peers[record]; !ok {
x.peers[record] = xatuPeer.NewPeer(x.log, x.handlers, x.cache, record, retryDelay, x.captureDelay)
if err := x.peers[record].Start(ctx); err != nil {
x.log.WithError(err).Error("failed to start peer")
delete(x.peers, record)
}
}
}
}); err != nil {
return err
}
c.StartAsync()
return nil
}