-
Notifications
You must be signed in to change notification settings - Fork 5
/
discovery.go
320 lines (273 loc) · 7.77 KB
/
discovery.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
package discovery
import (
"bufio"
"context"
"fmt"
"math"
"net"
"os"
"strings"
"sync"
"sync/atomic"
"github.com/ibalajiarun/go-consensus/cmd/master/masterpb"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
"github.com/ibalajiarun/go-consensus/pkg/logger"
"google.golang.org/grpc"
)
type Discovery struct {
Config
identifier int32
msgC chan packet
grpcServer *grpc.Server
peers map[string]peerpb.PeerInfo
peersByRegion map[string][]peerpb.PeerInfo
cIDGenByRegion sync.Map
ipMap map[string]string
retChans []chan<- bool
sInfo []peerpb.PeerInfo
leader peerpb.PeerID
secretKeys []string
publicKeys []string
ctx context.Context
ctxCancel func()
readyChan chan error
logger logger.Logger
}
type packetType int
const (
pktRegister packetType = iota + 1
pktRetrieve
)
type packet struct {
id peerpb.BasicPeerInfo
retC chan<- bool
pktType packetType
}
type retPacket struct {
id peerpb.BasicPeerInfo
retC chan<- bool
}
func NewDiscoveryServer(logger logger.Logger, config Config, ipMapping map[string]string) *Discovery {
ctx, cancel := context.WithCancel(context.Background())
d := &Discovery{
Config: config,
identifier: 0,
msgC: make(chan packet, config.PeerCount),
grpcServer: grpc.NewServer(),
peers: make(map[string]peerpb.PeerInfo),
peersByRegion: make(map[string][]peerpb.PeerInfo),
ipMap: ipMapping,
retChans: make([]chan<- bool, 0, config.PeerCount),
leader: math.MaxUint64,
ctx: ctx,
ctxCancel: cancel,
logger: logger,
readyChan: make(chan error),
}
d.readKeys()
return d
}
func (d *Discovery) Run(ctx context.Context) {
for {
select {
case pkt := <-d.msgC:
switch pkt.pktType {
case pktRegister:
d.processPacket(pkt)
case pktRetrieve:
d.retrieveServiceInfo(pkt)
}
case <-ctx.Done():
return
}
}
}
func (d *Discovery) Serve(lis net.Listener) error {
go d.Run(d.ctx)
masterpb.RegisterServiceDiscoveryServer(d.grpcServer, d)
return d.grpcServer.Serve(lis)
}
func (d *Discovery) Stop() {
d.grpcServer.Stop()
d.ctxCancel()
}
func (d *Discovery) processPacket(pkt packet) error {
if info, exists := d.peers[pkt.id.PodName]; exists {
d.logger.Infof("node already registered %v; received %v\n", info, pkt)
return nil
}
rID := peerpb.PeerID(d.identifier)
d.identifier++
if d.leader == math.MaxUint64 && strings.TrimSpace(pkt.id.Region) == d.LeaderRegion {
d.leader = rID
d.logger.Infof("Leader set to %d", rID)
}
if newIP, ok := d.ipMap[fmt.Sprintf("%s/%s", pkt.id.HostMachine, pkt.id.PodIP)]; ok {
pkt.id.PodIP = newIP
} else {
d.logger.Errorf("cannot find mapping IP for %v", pkt.id)
}
nodeInfo := peerpb.PeerInfo{
BasicPeerInfo: pkt.id,
PeerID: rID,
}
d.logger.Infof("registered node %v\n", nodeInfo)
d.peers[pkt.id.PodName] = nodeInfo
d.peersByRegion[pkt.id.Region] = append(d.peersByRegion[pkt.id.Region], nodeInfo)
d.retChans = append(d.retChans, pkt.retC)
if d.identifier == d.PeerCount {
d.logger.Infof("registered %v nodes...\n", d.PeerCount)
if d.leader == math.MaxUint64 {
d.readyChan <- fmt.Errorf("no leader set. target Region is %s", d.LeaderRegion)
} else {
d.readyChan <- nil
}
} else if d.identifier > d.PeerCount {
d.readyChan <- fmt.Errorf("more nodes registered than expected... %v > %v", d.identifier, d.PeerCount)
}
return nil
}
func (d *Discovery) WaitReady(ctx context.Context) error {
for {
select {
case err := <-d.readyChan:
return err
case <-ctx.Done():
return fmt.Errorf("timeout occured: %w", ctx.Err())
}
}
}
func (d *Discovery) TriggerClients() {
nodes := make([]peerpb.PeerInfo, 0, d.PeerCount)
for _, info := range d.peers {
nodes = append(nodes, info)
}
d.sInfo = nodes
for _, retC := range d.retChans {
retC <- true
}
d.retChans = nil
}
func (d *Discovery) retrieveServiceInfo(pkt packet) {
if d.identifier == d.PeerCount {
d.logger.Debugf("retrieving service info for %v nodes...\n", d.PeerCount)
pkt.retC <- true
} else {
if d.identifier > d.PeerCount {
d.logger.Errorf("more nodes registered than expected... %v > %v", d.identifier, d.PeerCount)
return
}
d.retChans = append(d.retChans, pkt.retC)
}
}
// Register is the grpc handler for registering a service
func (d *Discovery) Register(ctx context.Context, id *peerpb.BasicPeerInfo) (*masterpb.ServerResponse, error) {
ret := make(chan bool, 1)
d.msgC <- packet{
pktType: pktRegister,
id: *id,
retC: ret,
}
select {
case <-ret:
pod, ok := d.peers[id.PodName]
if !ok {
d.logger.Panicf("couldn't find peer id for %v", id)
}
res := &masterpb.ServerResponse{
PeerConfig: &peerpb.PeerConfig{
PeerDetails: d.sInfo,
ListenPort: 7000,
LogVerbose: d.ServerConfig.LogVerbose,
Algorithm: d.Algorithm,
MaxFailures: d.MaxFailures,
MaxFastFailures: d.MaxFastFailures,
LeaderID: d.leader,
SecretKeys: d.secretKeys,
PublicKeys: d.publicKeys,
EnclavePath: d.ServerConfig.EnclavePath,
EnclaveBatchSize: d.ServerConfig.EnclaveBatchSize,
DqOBatchSize: d.ServerConfig.DqOBatchSize,
DqOBatchTimeout: d.ServerConfig.DqOBatchTimeout,
Workers: d.ServerConfig.Workers,
WorkersQueueSizes: d.ServerConfig.WorkersQueueSizes,
CmdBatchSize: d.ServerConfig.CmdBatchSize,
CmdBatchTimeout: d.ServerConfig.CmdBatchTimeout,
ReqBufThreshold: d.ServerConfig.ReqBufThreshold,
ThreshsignFastLagrange: d.ServerConfig.ThreshsignFastLagrange,
RccAlgorithm: d.ServerConfig.RccAlgorithm,
DispelWaitForAllRb: d.ServerConfig.DispelWaitForAllRb,
MultiChainDuoBFT: d.ServerConfig.MultiChainDuoBFTConfig,
},
PeerID: pod.PeerID,
}
return res, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (d *Discovery) GetServiceInfo(ctx context.Context, info *peerpb.BasicPeerInfo) (*masterpb.ClientResponse, error) {
ret := make(chan bool, 1)
d.msgC <- packet{
pktType: pktRetrieve,
retC: ret,
}
select {
case <-ret:
cIdGenPtr, _ := d.cIDGenByRegion.LoadOrStore(info.Region, new(int32))
cid := nextClientID(cIdGenPtr.(*int32), d.peersByRegion[info.Region], d.PeerCount)
d.logger.Infof("registered client %v", info)
res := &masterpb.ClientResponse{
Config: &masterpb.ClientConfig{
BaseClientConfig: &d.ClientConfig,
Nodes: d.sInfo,
LeaderID: d.leader,
Algorithm: d.Algorithm,
MaxFailures: d.MaxFailures,
MaxFastFailures: d.MaxFastFailures,
},
ClientId: uint32(cid),
}
return res, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (d *Discovery) readKeys() error {
if len(d.KeyFile) <= 0 {
return fmt.Errorf("key file mising")
}
var skeys []string
var vkeys []string
file, err := os.Open(d.KeyFile)
if err != nil {
return fmt.Errorf("unable to open keyfile %v: %w", d.KeyFile, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
idx := 0
for idx < 4 && scanner.Scan() {
vkey := scanner.Text()
vkeys = append(vkeys, vkey)
idx++
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("unable to read keyfile %v: %w", d.KeyFile, err)
}
for scanner.Scan() {
skey := scanner.Text()
skeys = append(skeys, skey)
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("unable to read keyfile %v: %w", d.KeyFile, err)
}
d.logger.Infof("Read %d secret keys and %d public keys", len(skeys), len(vkeys))
d.secretKeys = skeys
d.publicKeys = vkeys
return nil
}
func nextClientID(idGen *int32, regionPeers []peerpb.PeerInfo, peerCount int32) int32 {
idx := atomic.AddInt32(idGen, 1)
cid := int32(regionPeers[int(idx-1)%len(regionPeers)].PeerID) + (int32(int(idx-1)/len(regionPeers)) * peerCount)
return cid
}