-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscuttlebutt.go
674 lines (575 loc) · 15.8 KB
/
scuttlebutt.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
scuttlebutt is a library for distributed cluster membership and failure
detection via an efficient reconciliation and flow-control anti-entropy
gossip protocol.
The failure detection mechanism is based on the phi accrual failure detector
used to mark failing nodes and remove them from the membership.
The speed of convergence can be tuned via the phi accrual failure detector.
*/
package scuttlebutt
import (
"container/list"
"crypto/aes"
"errors"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
metrics "github.com/armon/go-metrics"
"golang.org/x/exp/slices"
)
const (
defaultGossipNodes = 3
heartbeatKey = "heartbeat"
packetQueueDepth = 4096
protocolVersion = 1
udpTxBufferSize = 65507
)
var (
errRemotePacketEncryption = errors.New("Remote packet is encrypted and encryption is not configured")
errSecretKey = errors.New("Secret key must be 16, 24 or 32 bytes")
)
//State represents the current cluster state
type State struct {
state *clusterStateSnapshot
liveNodes []Node
deadNodes []Node
}
//Scuttlebutt is a scuttlebutt instance returned by NewScuttlebutt
type Scuttlebutt struct {
config *Config
clusterState *clusterState
heartbeat uint64
failureDetector *failureDetector
done chan struct{}
mutex sync.Mutex
wg sync.WaitGroup
logger *log.Logger
net *netTransport
queueCh chan struct{}
packetQueue *list.List
queueLock sync.Mutex
notifyCh chan struct{}
notifyQueue *list.List
notifyLock sync.Mutex
shutdownLock sync.Mutex
shutdown int32
encryptionEnabled bool
}
//NewScuttlebutt creates a Scuttlebutt instance based on a provided config
func NewScuttlebutt(config *Config, metadata map[string]string) (*Scuttlebutt, error) {
logger := config.Logger
if logger == nil {
logger = log.New(os.Stderr, "", log.LstdFlags)
}
if config.GossipNodes == 0 {
config.GossipNodes = defaultGossipNodes
}
nt, err := newNetTransport(config, logger)
if err != nil {
return nil, err
}
sb := &Scuttlebutt{config: config,
clusterState: newClusterState(),
heartbeat: 0,
failureDetector: newFailureDetector(config.FailureDetectorConfig),
done: make(chan struct{}),
queueCh: make(chan struct{}),
notifyCh: make(chan struct{}),
logger: logger,
packetQueue: list.New(),
notifyQueue: list.New(),
net: nt}
if config.SecretKey != nil && len(config.SecretKey) > 0 {
if keyLen := len(config.SecretKey); keyLen != 16 && keyLen != 24 && keyLen != 32 {
return nil, errSecretKey
}
sb.encryptionEnabled = true
}
nodeState := sb.getSelfNodeState()
nodeState.set(heartbeatKey, strconv.FormatUint(0, 10))
for key, value := range metadata {
nodeState.set(key, value)
}
sb.wg.Add(4)
go sb.packetListen()
go sb.notifyHandler()
go sb.packetHandler()
go sb.gossipHandler()
return sb, nil
}
func (sb *Scuttlebutt) ingestPacket(pkt *udpPacket) {
sb.mutex.Lock()
defer sb.mutex.Unlock()
msgType, buf, err := sb.processPacket(pkt.Buf)
if err != nil {
sb.logger.Printf("[ERR] failed to process packet: %v", err)
return
}
switch msgType {
case synMsg:
fallthrough
case ackMsg:
sb.queuePacket(&udpPacket{Buf: buf, From: pkt.From})
}
return
}
func (sb *Scuttlebutt) queuePacket(pkt *udpPacket) {
sb.queueLock.Lock()
if sb.packetQueue.Len() >= packetQueueDepth {
sb.logger.Printf("[WARN] packet queue full, dropping message from %s", pkt.From.String())
} else {
sb.packetQueue.PushBack(pkt)
}
sb.queueLock.Unlock()
select {
case sb.queueCh <- struct{}{}:
default:
}
return
}
func (sb *Scuttlebutt) getQueuePacket() (*udpPacket, bool) {
sb.queueLock.Lock()
defer sb.queueLock.Unlock()
if q := sb.packetQueue.Back(); q != nil {
sb.packetQueue.Remove(q)
packet := q.Value.(*udpPacket)
return packet, true
}
return &udpPacket{}, false
}
func (sb *Scuttlebutt) packetHandler() {
defer sb.wg.Done()
for {
select {
case <-sb.done:
return
case <-sb.queueCh:
for {
packet, ok := sb.getQueuePacket()
if !ok {
break
}
n, err := sb.net.writeToUDP(packet.Buf, packet.From)
if err != nil {
if s := sb.net.isClosing(); s == 0 {
sb.logger.Printf("[ERR] error writing UDP packet: %v", err)
}
continue
}
if n != len(packet.Buf) {
sb.logger.Printf("[ERR] write error, expected %d, written %d", len(packet.Buf), n)
}
}
}
}
}
func (sb *Scuttlebutt) getQueueEvent() (*NodeEvent, bool) {
sb.notifyLock.Lock()
defer sb.notifyLock.Unlock()
if n := sb.notifyQueue.Len(); n > 128 {
sb.logger.Printf("[WARN] notify queue has exceeded 128 pending items (%d)", n)
}
if q := sb.notifyQueue.Back(); q != nil {
sb.notifyQueue.Remove(q)
event := q.Value.(*NodeEvent)
return event, true
}
return &NodeEvent{}, false
}
func (sb *Scuttlebutt) notifyHandler() {
defer sb.wg.Done()
for {
select {
case <-sb.done:
return
case <-sb.notifyCh:
for {
event, ok := sb.getQueueEvent()
if !ok {
break
}
switch event.Event {
case NodeLive:
sb.config.Events.NotifyLive(event.Node)
case NodeDead:
sb.config.Events.NotifyDead(event.Node)
case NodeUpdate:
sb.config.Events.NotifyUpdate(event.Node)
}
}
}
}
}
func (sb *Scuttlebutt) packetListen() {
defer sb.wg.Done()
for {
select {
case <-sb.done:
return
case pkt := <-sb.net.packetCh():
sb.ingestPacket(pkt)
}
}
}
func (sb *Scuttlebutt) gossipHandler() {
defer sb.wg.Done()
ticker := time.NewTicker(sb.config.GossipInterval)
for {
select {
case <-ticker.C:
sb.gossip()
case <-sb.done:
ticker.Stop()
return
}
}
}
func (sb *Scuttlebutt) gossipOne(addr string) error {
sb.mutex.Lock()
defer sb.mutex.Unlock()
synPacket, err := sb.createSynPacket()
if err != nil {
return err
}
s, err := net.ResolveUDPAddr("udp4", addr)
if err != nil {
return err
}
sb.queueLock.Lock()
if sb.packetQueue.Len() >= packetQueueDepth {
sb.logger.Printf("[WARN] packet queue full, dropping SYN message to %s", addr)
} else {
sb.packetQueue.PushBack(&udpPacket{Buf: synPacket, From: s})
}
sb.queueLock.Unlock()
select {
case sb.queueCh <- struct{}{}:
default:
}
return nil
}
func (sb *Scuttlebutt) gossip() {
defer metrics.MeasureSince([]string{"scuttlebutt", "gossip"}, time.Now())
sb.mutex.Lock()
liveNodesCount := sb.failureDetector.getLiveNodesCount()
deadNodes := make([]string, 0)
for _, node := range sb.failureDetector.getDeadNodes() {
deadNodes = append(deadNodes, node.GossipPublicAddress)
}
seedNodes := make([]string, len(sb.clusterState.SeedNodes))
copy(seedNodes, sb.clusterState.SeedNodes)
var peerNodes []Node
if sb.failureDetector.getLiveNodesCount() != 0 {
peerNodes = sb.failureDetector.getLiveNodes()
} else {
peerNodes = sb.clusterState.getNodes()
}
sb.updateHeartbeat()
sb.mutex.Unlock()
nodes := getRandomNodes(sb.config.GossipNodes, peerNodes, sb.config.Node)
hasGossipedSeedNode := false
for _, node := range nodes {
if slices.Contains(seedNodes, node.GossipPublicAddress) {
hasGossipedSeedNode = true
break
}
}
var deadNode string
selectionProbability := float64(len(deadNodes)) / float64(liveNodesCount+1)
if selectionProbability > rand.Float64() {
if len(deadNodes) > 0 {
rand.Seed(time.Now().UnixNano())
randoms := rand.Perm(len(deadNodes))
deadNode = deadNodes[randoms[0]]
}
}
var seedNode string
if !hasGossipedSeedNode || liveNodesCount < len(seedNodes) {
selectionProbability := float64(len(seedNodes)) / float64(liveNodesCount+len(deadNodes))
if liveNodesCount == 0 || rand.Float64() <= selectionProbability {
if len(seedNodes) > 0 {
rand.Seed(time.Now().UnixNano())
randoms := rand.Perm(len(seedNodes))
seedNode = seedNodes[randoms[0]]
}
}
}
if len(nodes) > sb.config.GossipNodes {
nodes = nodes[:sb.config.GossipNodes]
}
for _, node := range nodes {
if err := sb.gossipOne(node.GossipPublicAddress); err != nil {
sb.logger.Printf("[ERR] Gossip error with a live node %v\n", err)
}
}
if deadNode != "" {
if err := sb.gossipOne(deadNode); err != nil {
sb.logger.Printf("[ERR] Gossip error with a dead node %v\n", err)
}
}
if seedNode != "" {
if err := sb.gossipOne(seedNode); err != nil {
sb.logger.Printf("[ERR] Gossip error with a seed node %v\n", err)
}
}
sb.mutex.Lock()
defer sb.mutex.Unlock()
sb.updateNodesState()
}
//Shutdown will stop all network activity
func (sb *Scuttlebutt) Shutdown() error {
sb.shutdownLock.Lock()
defer sb.shutdownLock.Unlock()
if s := atomic.LoadInt32(&sb.shutdown); s == 1 {
return nil
}
sb.net.shutdown()
atomic.StoreInt32(&sb.shutdown, 1)
close(sb.done)
sb.wg.Wait()
return nil
}
func (sb *Scuttlebutt) createSynPacket() ([]byte, error) {
deadNodes := sb.failureDetector.getDeadNodes()
digestMTU := udpTxBufferSize - 2 - len(sb.config.ClusterID) - 2 - sb.packetOverhead()
digest := sb.clusterState.computeRandomDigest(digestMTU, deadNodes)
buf, err := serializeSynPacket(sb.config.ClusterID, digest)
if err != nil {
return nil, err
}
if sb.config.EnableCompression {
buf, err = compressSerializedPacket(buf)
}
if sb.encryptionEnabled {
buf, err = encryptSerializedPacket(sb.config.SecretKey, buf)
}
return buf, err
}
func (sb *Scuttlebutt) computeDigest() (*digest, []Node) {
deadNodes := sb.failureDetector.getDeadNodes()
return sb.clusterState.computeRandomDigest(udpTxBufferSize/2, deadNodes), deadNodes
}
func (sb *Scuttlebutt) packetOverhead() int {
size := 1
if sb.config.EnableCompression {
size++
}
if sb.encryptionEnabled {
size += 1 + aes.BlockSize + aesNonceSize
}
return size
}
func (sb *Scuttlebutt) serializeAckPacket(digest *digest, delta *delta) ([]byte, error) {
buf, err := serializeAckPacket(digest, delta)
if sb.config.EnableCompression {
buf, err = compressSerializedPacket(buf)
}
if sb.encryptionEnabled {
buf, err = encryptSerializedPacket(sb.config.SecretKey, buf)
}
return buf, err
}
func (sb *Scuttlebutt) serializeAck2Packet(delta *delta) ([]byte, error) {
buf, err := serializeAck2Packet(delta)
if sb.config.EnableCompression {
buf, err = compressSerializedPacket(buf)
}
if sb.encryptionEnabled {
buf, err = encryptSerializedPacket(sb.config.SecretKey, buf)
}
return buf, err
}
func (sb *Scuttlebutt) deserializePacket(pktBuf []byte) (messageType, interface{}, error) {
var err error
msgType := messageType(pktBuf[0])
if msgType == encryptedMsg {
if !sb.encryptionEnabled {
return msgType, nil, errRemotePacketEncryption
}
pktBuf, err = decryptSerializedPacket(sb.config.SecretKey, pktBuf[1:])
if err != nil {
return msgType, nil, err
}
}
msgType, pkt, err := deserializePacket(pktBuf)
if err != nil {
return msgType, nil, err
}
return msgType, pkt, nil
}
func (sb *Scuttlebutt) processPacket(pktBuf []byte) (messageType, []byte, error) {
msgType, pkt, err := sb.deserializePacket(pktBuf)
if err != nil {
return msgType, nil, err
}
switch msgType {
case synMsg:
msg := pkt.(synMessage)
if msg.ClusterID != sb.config.ClusterID {
buf, err := serializeWrongClusterPacket()
return msgType, buf, err
}
digest, deadNodes := sb.computeDigest()
deltaMTU := udpTxBufferSize - digest.serializedSize() - ackHeaderSize - sb.packetOverhead()
delta := sb.clusterState.computeDelta(deltaMTU, msg.Digest, deadNodes)
sb.reportToFailureDetector(delta)
buf, err := sb.serializeAckPacket(digest, delta)
return msgType, buf, err
case ackMsg:
msg := pkt.(ackMessage)
sb.reportToFailureDetector(msg.Delta)
nodeUpdates := sb.clusterState.applyDelta(msg.Delta)
sb.handleNodeEventUpdate(nodeUpdates)
deadNodes := sb.failureDetector.getDeadNodes()
deltaMtu := udpTxBufferSize - sb.packetOverhead()
delta := sb.clusterState.computeDelta(deltaMtu, msg.Digest, deadNodes)
buf, err := sb.serializeAck2Packet(delta)
return msgType, buf, err
case ack2Msg:
msg := pkt.(ack2Message)
sb.reportToFailureDetector(msg.Delta)
nodeUpdates := sb.clusterState.applyDelta(msg.Delta)
sb.handleNodeEventUpdate(nodeUpdates)
return msgType, nil, nil
case wrongClusterMsg:
sb.logger.Printf("[WARN] message rejected by peer: cluster ID mismatch")
return msgType, nil, nil
}
return msgType, nil, fmt.Errorf("unknown message type")
}
func (sb *Scuttlebutt) reportToFailureDetector(delta *delta) {
for node, nodedelta := range delta.NodeDeltas {
nodestate := sb.clusterState.getNodeStateDefault(node)
localMaxVersion := nodestate.getMaxVersion()
if localMaxVersion < nodedelta.maxVersion() {
sb.failureDetector.reportHeartbeat(node)
}
}
}
func (sb *Scuttlebutt) updateNodesState() {
liveNodesBefore := sb.failureDetector.getLiveNodes()
clusterNodes := sb.clusterState.getNodes()
for _, node := range clusterNodes {
if node != sb.config.Node {
sb.failureDetector.updateNodeState(node)
}
}
liveNodesAfter := sb.failureDetector.getLiveNodes()
if !nodesMatch(liveNodesAfter, liveNodesBefore) {
sb.handleNodeEventState(liveNodesBefore, liveNodesAfter)
}
evNodes := sb.failureDetector.evictDeadNodes()
for _, node := range evNodes {
sb.clusterState.removeNode(node)
}
}
func (sb *Scuttlebutt) updateHeartbeat() {
sb.heartbeat++
hb := sb.heartbeat
nodeState := sb.clusterState.getNodeStateDefault(sb.config.Node)
nodeState.set(heartbeatKey, strconv.FormatUint(hb, 10))
}
func (sb *Scuttlebutt) stateSnapshot() *clusterStateSnapshot {
return sb.clusterState.snapshot()
}
func (sb *Scuttlebutt) getSelfNodeState() *nodeState {
return sb.clusterState.getNodeStateDefault(sb.config.Node)
}
func (sb *Scuttlebutt) handleNodeEventUpdate(nodeUpdates map[Node]struct{}) {
if sb.config.Events != nil {
sb.notifyLock.Lock()
for k := range nodeUpdates {
sb.notifyQueue.PushBack(&NodeEvent{Event: NodeUpdate, Node: k})
}
sb.notifyLock.Unlock()
if len(nodeUpdates) > 0 {
select {
case sb.notifyCh <- struct{}{}:
default:
}
}
}
}
func (sb *Scuttlebutt) handleNodeEventState(before, after []Node) {
if sb.config.Events != nil {
sb.notifyLock.Lock()
for _, node := range after {
if !slices.Contains(before, node) {
sb.notifyQueue.PushBack(&NodeEvent{Event: NodeLive, Node: node})
}
}
for _, node := range before {
if !slices.Contains(after, node) {
sb.notifyQueue.PushBack(&NodeEvent{Event: NodeDead, Node: node})
}
}
sb.notifyLock.Unlock()
select {
case sb.notifyCh <- struct{}{}:
default:
}
}
}
//Join is used to join a cluster by contacting all given hosts
func (sb *Scuttlebutt) Join(seedNodes []string) error {
sb.mutex.Lock()
defer sb.mutex.Unlock()
for _, node := range seedNodes {
sb.clusterState.SeedNodes = append(sb.clusterState.SeedNodes, node)
}
return nil
}
//Set key-value metadata
func (sb *Scuttlebutt) Set(key, value string) error {
sb.mutex.Lock()
defer sb.mutex.Unlock()
return sb.getSelfNodeState().set(key, value)
}
//State returns cluster state
func (sb *Scuttlebutt) State() *State {
sb.mutex.Lock()
defer sb.mutex.Unlock()
state := sb.stateSnapshot()
liveNodes := sb.failureDetector.getLiveNodes()
deadNodes := sb.failureDetector.getDeadNodes()
return &State{state: state, liveNodes: liveNodes, deadNodes: deadNodes}
}
//GetLiveNodes returns live nodes
func (s *State) GetLiveNodes() []Node {
return s.liveNodes
}
//GetDeadNodes returns dead nodes
func (s *State) GetDeadNodes() []Node {
return s.deadNodes
}
//GetAll returns node metadata
func (s *State) GetAll(node Node) map[string]string {
keys := make(map[string]string, 0)
if nodestate, ok := s.state.NodeStates[node]; ok {
for k, v := range nodestate.KV {
if k == heartbeatKey {
continue
}
keys[k] = v.Value
}
}
return keys
}
//SeedNodes returns seed nodes
func (s *State) SeedNodes() []string {
return s.state.SeedNodes
}
//Get node-key value
func (s *State) Get(node Node, key string) (string, bool) {
nodestate, ok := s.state.NodeStates[node]
if ok {
return nodestate.get(key)
}
return "", false
}