-
Notifications
You must be signed in to change notification settings - Fork 5
/
pool.go
191 lines (151 loc) · 4.16 KB
/
pool.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
package consensus
import (
"context"
"fmt"
"sync"
"time"
v1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/sirupsen/logrus"
)
type SchedulerMode uint8
var (
RoundRobinScheduler SchedulerMode = 1
)
type PoolConfig struct {
FollowDistance uint64 `yaml:"followDistance" envconfig:"CONSENSUS_POOL_FOLLOW_DISTANCE"`
ForkDistance uint64 `yaml:"forkDistance" envconfig:"CONSENSUS_POOL_FORK_DISTANCE"`
SchedulerMode string `yaml:"schedulerMode" envconfig:"CONSENSUS_POOL_SCHEDULER_MODE"`
}
type Pool struct {
config *PoolConfig
ctx context.Context
logger logrus.FieldLogger
clientCounter uint16
clients []*Client
blockCache *BlockCache
forkCacheMutex sync.Mutex
forkCache map[int64][]*HeadFork
schedulerMode SchedulerMode
schedulerMutex sync.Mutex
rrLastIndexes map[ClientType]uint16
}
func NewPool(ctx context.Context, config *PoolConfig, logger logrus.FieldLogger) (*Pool, error) {
var err error
pool := Pool{
config: config,
ctx: ctx,
logger: logger,
clients: make([]*Client, 0),
forkCache: map[int64][]*HeadFork{},
rrLastIndexes: map[ClientType]uint16{},
}
switch config.SchedulerMode {
case "", "rr", "roundrobin":
pool.schedulerMode = RoundRobinScheduler
default:
return nil, fmt.Errorf("unknown pool schedulerMode: %v", config.SchedulerMode)
}
pool.blockCache, err = NewBlockCache(ctx, logger, config.FollowDistance)
if err != nil {
return nil, err
}
return &pool, nil
}
func (pool *Pool) GetBlockCache() *BlockCache {
return pool.blockCache
}
func (pool *Pool) GetValidatorSet() map[phase0.ValidatorIndex]*v1.Validator {
return pool.blockCache.getCachedValidatorSet(func() map[phase0.ValidatorIndex]*v1.Validator {
client := pool.GetReadyEndpoint(AnyClient)
if client == nil {
pool.logger.Errorf("could not load validator set: no ready client")
return nil
}
valset, err := client.GetRPCClient().GetStateValidators(client.clientCtx, "head")
if err != nil {
pool.logger.Errorf("could not load validator set: %v", err)
return nil
}
return valset
})
}
func (pool *Pool) AddEndpoint(endpoint *ClientConfig) (*Client, error) {
clientIdx := pool.clientCounter
pool.clientCounter++
client, err := pool.newPoolClient(clientIdx, endpoint)
if err != nil {
return nil, err
}
pool.clients = append(pool.clients, client)
return client, nil
}
func (pool *Pool) GetAllEndpoints() []*Client {
return pool.clients
}
func (pool *Pool) GetReadyEndpoint(clientType ClientType) *Client {
canonicalFork := pool.GetCanonicalFork(-1)
if canonicalFork == nil {
return nil
}
readyClients := canonicalFork.ReadyClients
if len(readyClients) == 0 {
return nil
}
selectedClient := pool.runClientScheduler(readyClients, clientType)
return selectedClient
}
func (pool *Pool) AwaitReadyEndpoint(ctx context.Context, clientType ClientType) *Client {
for {
client := pool.GetReadyEndpoint(clientType)
if client != nil {
return client
}
select {
case <-ctx.Done():
return nil
case <-time.After(1 * time.Second):
}
}
}
func (pool *Pool) IsClientReady(client *Client) bool {
if client == nil {
return false
}
canonicalFork := pool.GetCanonicalFork(-1)
if canonicalFork == nil {
return false
}
readyClients := canonicalFork.ReadyClients
for _, readyClient := range readyClients {
if readyClient == client {
return true
}
}
return false
}
func (pool *Pool) runClientScheduler(readyClients []*Client, clientType ClientType) *Client {
pool.schedulerMutex.Lock()
defer pool.schedulerMutex.Unlock()
if pool.schedulerMode == RoundRobinScheduler {
var firstReadyClient *Client
for _, client := range readyClients {
if clientType != AnyClient && clientType != client.clientType {
continue
}
if firstReadyClient == nil {
firstReadyClient = client
}
if client.clientIdx > pool.rrLastIndexes[clientType] {
pool.rrLastIndexes[clientType] = client.clientIdx
return client
}
}
if firstReadyClient == nil {
return nil
}
pool.rrLastIndexes[clientType] = firstReadyClient.clientIdx
return firstReadyClient
}
return readyClients[0]
}