-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
gossip.go
969 lines (870 loc) · 32.4 KB
/
gossip.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
/*
Each node attempts to contact peer nodes to gather all Infos in
the system with minimal total hops. The algorithm is as follows:
0 Node starts up gossip server to accept incoming gossip requests.
Continue to step #1 to join the gossip network.
1 Node selects random peer from bootstrap list, excluding its own
address for its first outgoing connection. Node starts client and
continues to step #2.
2 Node requests gossip from peer. Gossip requests (and responses)
contain a map from node ID to info about other nodes in the
network. Each node maintains its own map as well as the maps of
each of its peers. The info for each node includes the most recent
timestamp of any Info originating at that node, as well as the min
number of hops to reach that node. Requesting node times out at
checkInterval. On timeout, client is closed and GC'd. If node has
no outgoing connections, goto #1.
a. When gossip is received, infostore is augmented. If new Info was
received, the client in question is credited. If node has no
outgoing connections, goto #1.
b. If any gossip was received at > MaxHops and num connected peers
< maxPeers(), choose random peer from those originating Info >
MaxHops, start it, and goto #2.
c. If sentinel gossip keyed by KeySentinel is missing or expired,
node is considered partitioned; goto #1.
3 On connect, if node has too many connected clients, gossip requests
are returned immediately with an alternate address set to a random
selection from amongst already-connected clients.
*/
package gossip
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"net"
"sync"
"time"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/gossip/resolver"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/rpc"
"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/stop"
"github.com/gogo/protobuf/proto"
)
const (
// MaxHops is the maximum number of hops which any gossip info
// should require to transit between any two nodes in a gossip
// network.
MaxHops = 5
// minPeers is the minimum number of peers which the maxPeers()
// function will return. This is set higher than one to prevent
// excessive tightening of the network.
minPeers = 3
// ttlNodeDescriptorGossip is time-to-live for node ID -> address.
ttlNodeDescriptorGossip = 0 * time.Second
// defaultStallInterval is the default interval for checking whether
// the incoming and outgoing connections to the gossip network are
// insufficient to keep the network connected.
defaultStallInterval = 1 * time.Second
// defaultBootstrapInterval is the minimum time between successive
// bootstrapping attempts to avoid busy-looping trying to find the
// sentinel gossip info.
defaultBootstrapInterval = 1 * time.Second
// defaultCullInterval is the default interval for culling the least
// "useful" outgoing gossip connection to free up space for a more
// efficiently targeted connection to the most distant node.
defaultCullInterval = 60 * time.Second
)
var (
// TestBootstrap is the default gossip bootstrap used for running tests.
TestBootstrap = []resolver.Resolver{}
)
// Storage is an interface which allows the gossip instance
// to read and write bootstrapping data to persistent storage
// between instantiations.
type Storage interface {
// ReadBootstrapInfo fetches the bootstrap data from the persistent
// store into the provided bootstrap protobuf. Returns nil or an
// error on failure.
ReadBootstrapInfo(*BootstrapInfo) error
// WriteBootstrapInfo stores the provided bootstrap data to the
// persistent store. Returns nil or an error on failure.
WriteBootstrapInfo(*BootstrapInfo) error
}
// Gossip is an instance of a gossip node. It embeds a gossip server.
// During bootstrapping, the bootstrap list contains candidates for
// entry to the gossip network.
type Gossip struct {
Connected chan struct{} // Closed upon initial connection
hasConnected bool // Set first time network is connected
rpcContext *rpc.Context // The context required for RPC
*server // Embedded gossip RPC server
outgoing nodeSet // Set of outgoing client node IDs
storage Storage // Persistent storage interface
bootstrapInfo BootstrapInfo // BootstrapInfo proto for persistent storage
bootstrapping map[string]struct{} // Set of active bootstrap clients
clientsMu sync.Mutex // Mutex protects the clients slice
clients []*client // Slice of clients
disconnected chan *client // Channel of disconnected clients
stalled chan struct{} // Channel to wakeup stalled bootstrap
stallInterval time.Duration
bootstrapInterval time.Duration
cullInterval time.Duration
// The system config is treated unlike other info objects.
// It is used so often that we keep an unmarshalled version of it
// here and its own set of callbacks.
// We do not use the infostore to avoid unmarshalling under the
// main gossip lock.
systemConfig *config.SystemConfig
systemConfigMu sync.RWMutex
systemConfigChannels []chan<- struct{}
// resolvers is a list of resolvers used to determine
// bootstrap hosts for connecting to the gossip network.
resolverIdx int
resolvers []resolver.Resolver
resolversTried map[int]struct{} // Set of attempted resolver indexes
nodesSeen map[roachpb.NodeID]struct{} // Transitive set of all nodes we've seen gossip from
}
// New creates an instance of a gossip node.
func New(rpcContext *rpc.Context, resolvers []resolver.Resolver) *Gossip {
g := &Gossip{
Connected: make(chan struct{}),
server: newServer(),
outgoing: makeNodeSet(minPeers),
bootstrapping: map[string]struct{}{},
clients: []*client{},
disconnected: make(chan *client, 10),
stalled: make(chan struct{}, 1),
stallInterval: defaultStallInterval,
bootstrapInterval: defaultBootstrapInterval,
cullInterval: defaultCullInterval,
nodesSeen: map[roachpb.NodeID]struct{}{},
}
g.SetResolvers(resolvers)
// The gossip RPC context doesn't measure clock offsets, isn't
// shared with the other RPC clients which the node may be using,
// and disables reconnects to make it possible to know for certain
// which other nodes in the cluster are incoming via gossip.
if rpcContext != nil {
g.rpcContext = rpcContext.Copy()
g.rpcContext.DisableCache = true
g.rpcContext.DisableReconnects = true
g.rpcContext.RemoteClocks = nil
}
// Add ourselves as a SystemConfig watcher.
g.is.registerCallback(KeySystemConfig, g.updateSystemConfig)
// Add ourselves as a node descriptor watcher.
g.is.registerCallback(MakePrefixPattern(KeyNodeIDPrefix), g.updateNodeAddress)
return g
}
// GetNodeID returns the instance's saved node ID.
func (g *Gossip) GetNodeID() roachpb.NodeID {
g.mu.Lock()
defer g.mu.Unlock()
return g.is.NodeID
}
// SetNodeID sets the infostore's node ID.
func (g *Gossip) SetNodeID(nodeID roachpb.NodeID) {
g.mu.Lock()
defer g.mu.Unlock()
if g.is.NodeID != 0 && g.is.NodeID != nodeID {
// TODO(spencer): change this to a panic after fixing unittests
// which do invoke this with different node IDs.
log.Errorf("different node IDs were set for the same gossip instance (%d, %d)", g.is.NodeID, nodeID)
}
g.is.NodeID = nodeID
}
// SetNodeDescriptor adds the node descriptor to the gossip network
// and sets the infostore's node ID.
func (g *Gossip) SetNodeDescriptor(desc *roachpb.NodeDescriptor) error {
log.Infof("setting node descriptor %+v", desc)
if err := g.AddInfoProto(MakeNodeIDKey(desc.NodeID), desc, ttlNodeDescriptorGossip); err != nil {
return util.Errorf("couldn't gossip descriptor for node %d: %v", desc.NodeID, err)
}
return nil
}
// SetStallInterval sets the interval between periodic checks to
// determine whether the node is not fully connected to the gossip
// network.
func (g *Gossip) SetStallInterval(interval time.Duration) {
g.mu.Lock()
defer g.mu.Unlock()
g.stallInterval = interval
}
// SetBootstrapInterval sets a minimum interval between successive
// attempts to connect to new hosts in order to join the gossip
// network.
func (g *Gossip) SetBootstrapInterval(interval time.Duration) {
g.mu.Lock()
defer g.mu.Unlock()
g.bootstrapInterval = interval
}
// SetCullInterval sets the interval between periodic shutdown of
// outgoing gossip client connections in an effort to improve the
// fitness of the network.
func (g *Gossip) SetCullInterval(interval time.Duration) {
g.mu.Lock()
defer g.mu.Unlock()
g.cullInterval = interval
}
// SetStorage provides an instance of the Storage interface
// for reading and writing gossip bootstrap data from persistent
// storage. This should be invoked as early in the lifecycle of a
// gossip instance as possible, but can be called at any time.
func (g *Gossip) SetStorage(storage Storage) error {
g.mu.Lock()
defer g.mu.Unlock()
g.storage = storage
// Read the bootstrap info from the persistent store.
var storedBI BootstrapInfo
err := storage.ReadBootstrapInfo(&storedBI)
if err != nil {
log.Warningf("failed to read bootstrap info: %s", err)
}
log.Infof("read %d gossip host(s) for bootstrapping from persistent storage", len(storedBI.Addresses))
// Merge the stored bootstrap info addresses with any we've become
// aware of through the --gossip bootstrap hosts we've connected to.
if len(g.bootstrapInfo.Addresses) > 0 {
existing := map[string]struct{}{}
makeKey := func(a util.UnresolvedAddr) string { return fmt.Sprintf("%s,%s", a.Network(), a.String()) }
for _, addr := range g.bootstrapInfo.Addresses {
existing[makeKey(addr)] = struct{}{}
}
for _, addr := range storedBI.Addresses {
if _, ok := existing[makeKey(addr)]; !ok {
g.bootstrapInfo.Addresses = append(g.bootstrapInfo.Addresses, addr)
}
}
// Persist merged addresses.
if numAddrs := len(g.bootstrapInfo.Addresses); numAddrs > len(storedBI.Addresses) {
if err := g.storage.WriteBootstrapInfo(&g.bootstrapInfo); err != nil {
log.Error(err)
} else {
log.Infof("wrote %d merged gossip host(s) to persistent storage", numAddrs)
}
}
} else {
g.bootstrapInfo = storedBI
}
// Cycle through all persisted bootstrap hosts and add resolvers for
// any which haven't already been added.
newResolverFound := false
for _, addr := range g.bootstrapInfo.Addresses {
r, err := resolver.NewResolverFromUnresolvedAddr(addr)
if err != nil {
log.Warningf("bad bootstrap address %s: %s", addr, err)
continue
}
if g.haveResolver(r) {
continue
}
// If we find a new resolver, reset the resolver index so that the
// next resolver we try is the first of the new resolvers.
if !newResolverFound {
newResolverFound = true
g.resolverIdx = len(g.resolvers) - 1
}
g.resolvers = append(g.resolvers, r)
}
// If there are no resolvers after persistent storage has been queried, fatal error.
if len(g.resolvers) == 0 {
return fmt.Errorf("no resolvers specified for gossip network: try adding peers via --gossip")
}
// If a new resolver was found, immediately signal bootstrap.
if newResolverFound {
if log.V(1) {
log.Infof("found new resolvers from storage; signalling bootstrap")
}
g.signalStalled()
}
return nil
}
// SetResolvers initializes the set of gossip resolvers used to
// find nodes to bootstrap the gossip network.
func (g *Gossip) SetResolvers(resolvers []resolver.Resolver) {
g.mu.Lock()
defer g.mu.Unlock()
// Start index at end because get next address loop logic increments as first step.
g.resolverIdx = len(resolvers) - 1
g.resolvers = resolvers
g.resolversTried = map[int]struct{}{}
}
// GetNodeIDAddress looks up the address of the node by ID.
func (g *Gossip) GetNodeIDAddress(nodeID roachpb.NodeID) (net.Addr, error) {
g.mu.Lock()
defer g.mu.Unlock()
return g.getNodeIDAddressLocked(nodeID)
}
// GetNodeDescriptor looks up the descriptor of the node by ID.
func (g *Gossip) GetNodeDescriptor(nodeID roachpb.NodeID) (*roachpb.NodeDescriptor, error) {
nodeDescriptor := &roachpb.NodeDescriptor{}
if err := g.GetInfoProto(MakeNodeIDKey(nodeID), nodeDescriptor); err != nil {
return nil, util.Errorf("unable to lookup descriptor for node %d: %s", nodeID, err)
}
return nodeDescriptor, nil
}
// EnableSimulationCycler is for TESTING PURPOSES ONLY. It sets a
// condition variable which is signaled at each cycle of the
// simulation via SimulationCycle(). The gossip server makes each
// connecting client wait for the cycler to signal before responding.
func (g *Gossip) EnableSimulationCycler(enable bool) {
g.mu.Lock()
defer g.mu.Unlock()
if enable {
g.simulationCycler = sync.NewCond(&g.mu)
} else {
g.simulationCycler.Broadcast()
g.simulationCycler = nil
}
}
// SimulationCycle cycles this gossip node's server by allowing all
// connected clients to proceed one step.
func (g *Gossip) SimulationCycle() {
g.mu.Lock()
defer g.mu.Unlock()
g.simulationCycler.Broadcast()
}
// haveResolver returns whether the specified resolver is already in
// the gossip node's list of resolvers. The caller must hold the
// gossip mutex.
func (g *Gossip) haveResolver(r resolver.Resolver) bool {
for _, ex := range g.resolvers {
if ex.Type() == r.Type() && ex.Addr() == r.Addr() {
return true
}
}
return false
}
// maxPeers returns the maximum number of peers each gossip node
// may connect to. This is based on maxHops, which is a preset
// maximum for number of hops allowed before the gossip network
// will seek to "tighten" by creating new connections to distant
// nodes.
func (g *Gossip) maxPeers(nodeCount int) int {
// This formula uses MaxHops-1, instead of MaxHops, to provide a
// "fudge" factor for max connected peers, to account for the
// arbitrary, decentralized way in which gossip networks are created.
maxPeers := int(math.Ceil(math.Exp(math.Log(float64(nodeCount)) / float64(MaxHops-1))))
if maxPeers < minPeers {
return minPeers
}
return maxPeers
}
// updateNodeAddress is a gossip callback which fires with each
// update to the node address. This allows us to compute the
// total size of the gossip network (for determining max peers
// each gossip node is allowed to have), as well as to create
// new resolvers for each encountered host and to write the
// set of gossip node addresses to persistent storage when it
// changes.
func (g *Gossip) updateNodeAddress(_ string, content roachpb.Value) {
var desc roachpb.NodeDescriptor
if err := content.GetProto(&desc); err != nil {
log.Error(err)
return
}
g.mu.Lock()
defer g.mu.Unlock()
// If the node has already been seen, return immediately.
if _, ok := g.nodesSeen[desc.NodeID]; ok {
return
}
g.nodesSeen[desc.NodeID] = struct{}{} // add it!
// Recompute max peers based on size of network and set the max
// sizes for incoming and outgoing node sets.
maxPeers := g.maxPeers(len(g.nodesSeen))
g.incoming.setMaxSize(maxPeers)
g.outgoing.setMaxSize(maxPeers)
// Add this new node to our list of resolvers so we can keep
// connecting to gossip if the original resolvers go offline.
r, err := resolver.NewResolverFromUnresolvedAddr(desc.Address)
if err != nil {
log.Warningf("bad address from gossip node %s: %s", desc, err)
return
}
if g.haveResolver(r) {
return
}
g.resolvers = append(g.resolvers, r)
// Add new address to bootstrap info and persist if possible.
g.bootstrapInfo.Addresses = append(g.bootstrapInfo.Addresses, desc.Address)
if g.storage != nil {
// TODO(spencer): need to clean up ancient gossip nodes, which
// will otherwise stick around in the bootstrap info forever.
if err := g.storage.WriteBootstrapInfo(&g.bootstrapInfo); err != nil {
log.Error(err)
}
}
}
// getNodeDescriptorLocked looks up the descriptor of the node by ID. The mutex
// is assumed held by the caller. This method is called externally via
// GetNodeDescriptor and internally by getNodeIDAddressLocked.
func (g *Gossip) getNodeDescriptorLocked(nodeID roachpb.NodeID) (*roachpb.NodeDescriptor, error) {
nodeIDKey := MakeNodeIDKey(nodeID)
// We can't use GetInfoProto here because that method grabs the lock.
if i := g.is.getInfo(nodeIDKey); i != nil {
if err := i.Value.Verify([]byte(nodeIDKey)); err != nil {
return nil, err
}
nodeDescriptor := &roachpb.NodeDescriptor{}
if err := i.Value.GetProto(nodeDescriptor); err != nil {
return nil, err
}
return nodeDescriptor, nil
}
return nil, util.Errorf("unable to lookup descriptor for node %d", nodeID)
}
// getNodeIDAddressLocked looks up the address of the node by ID. The mutex is
// assumed held by the caller. This method is called externally via
// GetNodeIDAddress or internally when looking up a "distant" node address to
// connect directly to.
func (g *Gossip) getNodeIDAddressLocked(nodeID roachpb.NodeID) (net.Addr, error) {
nd, err := g.getNodeDescriptorLocked(nodeID)
if err != nil {
return nil, err
}
return nd.Address, nil
}
// AddInfo adds or updates an info object. Returns an error if info
// couldn't be added.
func (g *Gossip) AddInfo(key string, val []byte, ttl time.Duration) error {
g.mu.Lock()
defer g.mu.Unlock()
err := g.is.addInfo(key, g.is.newInfo(val, ttl))
if err == nil {
g.checkHasConnected()
}
return err
}
// AddInfoProto adds or updates an info object. Returns an error if info
// couldn't be added.
func (g *Gossip) AddInfoProto(key string, msg proto.Message, ttl time.Duration) error {
bytes, err := proto.Marshal(msg)
if err != nil {
return err
}
return g.AddInfo(key, bytes, ttl)
}
// GetInfo returns an info value by key or an error if specified
// key does not exist or has expired.
func (g *Gossip) GetInfo(key string) ([]byte, error) {
g.mu.Lock()
i := g.is.getInfo(key)
g.mu.Unlock()
if i != nil {
if err := i.Value.Verify([]byte(key)); err != nil {
return nil, err
}
return i.Value.GetBytes()
}
return nil, util.Errorf("key %q does not exist or has expired", key)
}
// GetInfoProto returns an info value by key or an error if specified
// key does not exist or has expired.
func (g *Gossip) GetInfoProto(key string, msg proto.Message) error {
bytes, err := g.GetInfo(key)
if err != nil {
return err
}
return proto.Unmarshal(bytes, msg)
}
// GetInfosAsJSON returns the contents of the infostore, marshalled to
// JSON.
func (g *Gossip) GetInfosAsJSON() ([]byte, error) {
g.mu.Lock()
defer g.mu.Unlock()
return json.MarshalIndent(g.is, "", " ")
}
// Callback is a callback method to be invoked on gossip update
// of info denoted by key.
type Callback func(string, roachpb.Value)
// RegisterCallback registers a callback for a key pattern to be
// invoked whenever new info for a gossip key matching pattern is
// received. The callback method is invoked with the info key which
// matched pattern. Returns a function to unregister the callback.
func (g *Gossip) RegisterCallback(pattern string, method Callback) func() {
if pattern == KeySystemConfig {
log.Warningf("raw gossip callback registered on %s, consider using RegisterSystemConfigChannel",
KeySystemConfig)
}
g.mu.Lock()
unregister := g.is.registerCallback(pattern, method)
g.mu.Unlock()
return func() {
g.mu.Lock()
unregister()
g.mu.Unlock()
}
}
// GetSystemConfig returns the local unmarshalled version of the
// system config. It may be nil if it was never gossiped.
func (g *Gossip) GetSystemConfig() *config.SystemConfig {
g.systemConfigMu.RLock()
defer g.systemConfigMu.RUnlock()
return g.systemConfig
}
// RegisterSystemConfigChannel registers a channel to signify updates for the
// system config. It is notified after registration, and whenever a new
// system config is successfully unmarshalled.
func (g *Gossip) RegisterSystemConfigChannel() <-chan struct{} {
g.systemConfigMu.Lock()
defer g.systemConfigMu.Unlock()
// Create channel that receives new system config notifications.
// The channel has a size of 1 to prevent gossip from blocking on it.
c := make(chan struct{}, 1)
g.systemConfigChannels = append(g.systemConfigChannels, c)
// Notify the channel right away if we have a config.
if g.systemConfig != nil {
c <- struct{}{}
}
return c
}
// updateSystemConfig is the raw gossip info callback.
// Unmarshal the system config, and if successfully, update out
// copy and run the callbacks.
func (g *Gossip) updateSystemConfig(key string, content roachpb.Value) {
if key != KeySystemConfig {
log.Fatalf("wrong key received on SystemConfig callback: %s", key)
return
}
cfg := &config.SystemConfig{}
if err := content.GetProto(cfg); err != nil {
log.Errorf("could not unmarshal system config on callback: %s", err)
return
}
g.systemConfigMu.Lock()
defer g.systemConfigMu.Unlock()
g.systemConfig = cfg
for _, c := range g.systemConfigChannels {
select {
case c <- struct{}{}:
default:
}
}
}
// Incoming returns a slice of incoming gossip client connection
// node IDs.
func (g *Gossip) Incoming() []roachpb.NodeID {
g.mu.Lock()
defer g.mu.Unlock()
return g.incoming.asSlice()
}
// Outgoing returns a slice of outgoing gossip client connection
// node IDs. Note that these outgoing client connections may not
// actually be legitimately connected. They may be in the process
// of trying, or may already have failed, but haven't yet been
// processed by the gossip instance.
func (g *Gossip) Outgoing() []roachpb.NodeID {
g.mu.Lock()
defer g.mu.Unlock()
return g.outgoing.asSlice()
}
// MaxHops returns the maximum number of hops to reach any other
// node in the system, according to the infos which have reached
// this node via gossip network.
func (g *Gossip) MaxHops() uint32 {
g.mu.Lock()
defer g.mu.Unlock()
_, maxHops := g.is.mostDistant()
return maxHops
}
// Start launches the gossip instance, which commences joining the
// gossip network using the supplied rpc server and the gossip
// bootstrap addresses specified via command-line flag: --gossip.
//
// The supplied address is used to identify the gossip instance in the
// gossip network; it will be used by other instances to connect to
// this instance.
//
// This method starts bootstrap loop, gossip server, and client
// management in separate goroutines and returns.
func (g *Gossip) Start(rpcServer *rpc.Server, addr net.Addr, stopper *stop.Stopper) {
g.server.start(rpcServer, addr, stopper) // serve gossip protocol
g.bootstrap(stopper) // bootstrap gossip client
g.manage(stopper) // manage gossip clients
}
// hasIncoming returns whether the server has an incoming gossip
// client matching the provided node ID. Mutex should be held by
// caller.
func (g *Gossip) hasIncoming(nodeID roachpb.NodeID) bool {
return g.incoming.hasNode(nodeID)
}
// hasOutgoing returns whether the server has an outgoing gossip
// client matching the provided node ID. Mutex should be held by
// caller.
func (g *Gossip) hasOutgoing(nodeID roachpb.NodeID) bool {
return g.outgoing.hasNode(nodeID)
}
// filterExtant removes any nodes from the supplied nodeSet which
// are already connected to this node, either via outgoing or incoming
// client connections.
func (g *Gossip) filterExtant(nodes nodeSet) nodeSet {
return nodes.filter(func(a roachpb.NodeID) bool {
return !g.outgoing.hasNode(a)
}).filter(func(a roachpb.NodeID) bool {
return !g.incoming.hasNode(a)
})
}
// getNextBootstrapAddress returns the next available bootstrap
// address by consulting the first non-exhausted resolver from the
// slice supplied to the constructor or set using setBootstrap().
// The lock is assumed held.
func (g *Gossip) getNextBootstrapAddress() net.Addr {
// Run through resolvers round robin starting at last resolved index.
for i := 0; i < len(g.resolvers); i++ {
g.resolverIdx = (g.resolverIdx + 1) % len(g.resolvers)
g.resolversTried[g.resolverIdx] = struct{}{}
resolver := g.resolvers[g.resolverIdx]
addr, err := resolver.GetAddress()
if err != nil {
log.Errorf("invalid bootstrap address: %+v, %v", resolver, err)
continue
} else if addr.String() == g.is.NodeAddr.String() {
// Skip our own node address.
continue
}
_, addrActive := g.bootstrapping[addr.String()]
if !resolver.IsExhausted() || !addrActive {
g.bootstrapping[addr.String()] = struct{}{}
return addr
}
}
return nil
}
// bootstrap connects the node to the gossip network. Bootstrapping
// commences in the event there are no connected clients or the
// sentinel gossip info is not available. After a successful bootstrap
// connection, this method will block on the stalled condvar, which
// receives notifications that gossip network connectivity has been
// lost and requires re-bootstrapping.
func (g *Gossip) bootstrap(stopper *stop.Stopper) {
stopper.RunWorker(func() {
for {
g.mu.Lock()
if g.closed {
g.mu.Unlock()
return
}
haveClients := g.outgoing.len() > 0
haveSentinel := g.is.getInfo(KeySentinel) != nil
if !haveClients || !haveSentinel {
// Try to get another bootstrap address from the resolvers.
if addr := g.getNextBootstrapAddress(); addr != nil {
g.startClient(addr, stopper)
}
}
g.mu.Unlock()
// Pause an interval before next possible bootstrap.
select {
case <-time.After(g.bootstrapInterval):
// continue
case <-stopper.ShouldStop():
return
}
// Block until we need bootstrapping again.
select {
case <-g.stalled:
// continue
case <-stopper.ShouldStop():
return
}
}
})
}
// manage manages outgoing clients. Periodically, the infostore is
// scanned for infos with hop count exceeding the MaxHops
// threshold. If the number of outgoing clients doesn't exceed
// maxPeers(), a new gossip client is connected to a randomly selected
// peer beyond MaxHops threshold. Otherwise, the least useful peer
// node is cut off to make room for a replacement. Disconnected
// clients are processed via the disconnected channel and taken out of
// the outgoing address set. If there are no longer any outgoing
// connections or the sentinel gossip is unavailable, the bootstrapper
// is notified via the stalled conditional variable.
func (g *Gossip) manage(stopper *stop.Stopper) {
stopper.RunWorker(func() {
cullTicker := time.NewTicker(g.jitteredInterval(g.cullInterval))
stallTicker := time.NewTicker(g.jitteredInterval(g.stallInterval))
defer cullTicker.Stop()
defer stallTicker.Stop()
for {
select {
case <-stopper.ShouldStop():
return
case c := <-g.disconnected:
g.doDisconnected(stopper, c)
case nodeID := <-g.tighten:
g.tightenNetwork(stopper, nodeID)
case <-cullTicker.C:
g.cullNetwork()
case <-stallTicker.C:
g.mu.Lock()
g.maybeSignalStalledLocked()
g.mu.Unlock()
}
}
})
}
// jitteredInterval returns a randomly jittered (+/-25%) duration
// from checkInterval.
func (g *Gossip) jitteredInterval(interval time.Duration) time.Duration {
return time.Duration(float64(interval) * (0.75 + 0.5*rand.Float64()))
}
// tightenNetwork "tightens" the network by starting a new gossip
// client to the most distant node as measured in required gossip hops
// to propagate info from the distant node to this node.
func (g *Gossip) tightenNetwork(stopper *stop.Stopper, distantNodeID roachpb.NodeID) {
g.mu.Lock()
defer g.mu.Unlock()
if g.outgoing.hasSpace() {
if nodeAddr, err := g.getNodeIDAddressLocked(distantNodeID); err != nil {
log.Errorf("node %d: %s", distantNodeID, err)
} else {
log.Infof("starting client to distant node %d to tighten network graph", distantNodeID)
g.startClient(nodeAddr, stopper)
}
}
}
// cullNetwork is called periodically to remove the least "useful"
// outgoing node to free up an outgoing spot for a more targeted
// tightening (via tightenNetwork).
func (g *Gossip) cullNetwork() {
g.mu.Lock()
defer g.mu.Unlock()
// If there's no space, find and remove least useful peer, if possible.
if g.outgoing.hasSpace() {
return
}
leastUsefulID := g.is.leastUseful(g.outgoing)
if leastUsefulID == 0 {
if log.V(1) {
log.Infof("couldn't find least useful client to close")
}
return
}
if log.V(1) {
log.Infof("closing least useful client to node %d to tighten network graph", leastUsefulID)
}
g.closeClient(leastUsefulID)
}
func (g *Gossip) doDisconnected(stopper *stop.Stopper, c *client) {
g.mu.Lock()
defer g.mu.Unlock()
g.removeClient(c)
// If the client was disconnected with a forwarding address, connect now.
if c.forwardAddr != nil {
g.startClient(c.forwardAddr, stopper)
}
g.maybeSignalStalledLocked()
}
func (g *Gossip) maybeSignalStalledLocked() {
// If there are no outgoing hosts or sentinel gossip is missing,
// and there are still unused bootstrap hosts, signal bootstrapper
// to try another.
if g.outgoing.len()+g.incoming.len() == 0 {
g.signalStalled()
} else if g.is.getInfo(KeySentinel) == nil {
g.warnAboutStall()
g.signalStalled()
}
}
func (g *Gossip) signalStalled() {
select {
case g.stalled <- struct{}{}:
default:
}
}
// warnAboutStall attempts to diagnose the cause of a gossip network
// not being connected to the sentinel. This could happen in a network
// partition, or because of misconfiguration. It's impossible to tell,
// but we can warn appropriately. If there are no incoming or outgoing
// connections, we warn about the --gossip flag being set. If we've
// connected, and all resolvers have been tried, we warn about either
// the first range not being available or else possible the cluster
// never having been initialized.
func (g *Gossip) warnAboutStall() {
if g.outgoing.len()+g.incoming.len() == 0 {
log.Warningf("not connected to gossip; check that gossip flag is set appropriately")
} else if len(g.resolversTried) == len(g.resolvers) {
log.Warningf("first range unavailable or cluster not initialized")
} else {
log.Warningf("partition in gossip network; attempting new connection")
}
}
// checkHasConnected checks whether this gossip instance is connected
// to enough of the gossip network that it has received the cluster ID
// gossip info. Once connected, the "Connected" channel is closed to
// signal to any waiters that the gossip instance is ready. The gossip
// mutex should be held by caller.
func (g *Gossip) checkHasConnected() {
// Check if we have the cluster ID gossip to start.
// If so, then mark ourselves as trivially connected to the gossip network.
if !g.hasConnected && g.is.getInfo(KeyClusterID) != nil {
g.hasConnected = true
close(g.Connected)
}
}
// startClient launches a new client connected to remote address.
// The client is added to the outgoing address set and launched in
// a goroutine.
func (g *Gossip) startClient(addr net.Addr, stopper *stop.Stopper) {
log.Infof("starting client to %s", addr)
c := newClient(addr)
g.clientsMu.Lock()
g.clients = append(g.clients, c)
g.clientsMu.Unlock()
c.start(g, g.disconnected, g.rpcContext, stopper)
}
// closeClient finds and removes a client from the clients slice.
func (g *Gossip) closeClient(nodeID roachpb.NodeID) {
c := g.removeMatchingClient(func(c *client) bool { return c.peerID == nodeID })
if c != nil {
c.close()
}
}
// removeClient removes the specified client. Called when a client
// disconnects.
func (g *Gossip) removeClient(c *client) {
g.removeMatchingClient(func(match *client) bool { return c == match })
}
// removeMatchingClient finds and removes a client which matches the
// provided match function from the clients slice. Returns the client
// if found and removed.
func (g *Gossip) removeMatchingClient(match func(*client) bool) *client {
g.clientsMu.Lock()
defer g.clientsMu.Unlock()
for i, c := range g.clients {
if match(c) {
g.clients = append(g.clients[:i], g.clients[i+1:]...)
delete(g.bootstrapping, c.addr.String())
g.outgoing.removeNode(c.peerID)
return c
}
}
return nil
}
func (g *Gossip) findClient(match func(*client) bool) *client {
g.clientsMu.Lock()
defer g.clientsMu.Unlock()
for _, c := range g.clients {
if match(c) {
return c
}
}
return nil
}
var _ security.RequestWithUser = &Request{}
// GetUser implements security.RequestWithUser.
// Gossip messages are always sent by the node user.
func (*Request) GetUser() string {
return security.NodeUser
}