forked from uber/ringpop-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
480 lines (381 loc) · 12.1 KB
/
node.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
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package swim
import (
"errors"
"sync"
"time"
"github.com/uber/ringpop-go/discovery"
"github.com/benbjohnson/clock"
"github.com/rcrowley/go-metrics"
log "github.com/uber-common/bark"
"github.com/uber/ringpop-go/logging"
"github.com/uber/ringpop-go/shared"
"github.com/uber/ringpop-go/util"
)
var (
// ErrNodeNotReady is returned when a remote request is being handled while the node is not yet ready
ErrNodeNotReady = errors.New("node is not ready to handle requests")
)
// Options is a configuration struct passed the NewNode constructor.
type Options struct {
StateTimeouts StateTimeouts
MinProtocolPeriod time.Duration
JoinTimeout, PingTimeout, PingRequestTimeout time.Duration
PingRequestSize int
RollupFlushInterval time.Duration
RollupMaxUpdates int
Clock clock.Clock
}
func defaultOptions() *Options {
opts := &Options{
StateTimeouts: StateTimeouts{
Suspect: 5000 * time.Millisecond,
},
MinProtocolPeriod: 200 * time.Millisecond,
JoinTimeout: 1000 * time.Millisecond,
PingTimeout: 1500 * time.Millisecond,
PingRequestTimeout: 5000 * time.Millisecond,
PingRequestSize: 3,
RollupFlushInterval: 5000 * time.Millisecond,
RollupMaxUpdates: 250,
Clock: clock.New(),
}
return opts
}
func mergeDefaultOptions(opts *Options) *Options {
def := defaultOptions()
if opts == nil {
return def
}
opts.StateTimeouts = mergeStateTimeouts(opts.StateTimeouts, def.StateTimeouts)
opts.MinProtocolPeriod = util.SelectDuration(opts.MinProtocolPeriod,
def.MinProtocolPeriod)
opts.RollupMaxUpdates = util.SelectInt(opts.RollupMaxUpdates,
def.RollupMaxUpdates)
opts.RollupFlushInterval = util.SelectDuration(opts.RollupFlushInterval,
def.RollupFlushInterval)
opts.JoinTimeout = util.SelectDuration(opts.JoinTimeout, def.JoinTimeout)
opts.PingTimeout = util.SelectDuration(opts.PingTimeout, def.PingTimeout)
opts.PingRequestTimeout = util.SelectDuration(opts.PingRequestTimeout,
def.PingRequestTimeout)
opts.PingRequestSize = util.SelectInt(opts.PingRequestSize,
def.PingRequestSize)
if opts.Clock == nil {
opts.Clock = def.Clock
}
return opts
}
// NodeInterface specifies the public-facing methods that a SWIM Node
// implements.
type NodeInterface interface {
Bootstrap(opts *BootstrapOptions) ([]string, error)
CountReachableMembers() int
Destroy()
GetChecksum() uint32
GetReachableMembers() []string
MemberStats() MemberStats
ProtocolStats() ProtocolStats
Ready() bool
RegisterListener(l EventListener)
}
// A Node is a SWIM member
type Node struct {
app string
service string
address string
state struct {
stopped, destroyed, pinging, ready bool
sync.RWMutex
}
channel shared.SubChannel
memberlist *memberlist
memberiter memberIter
disseminator *disseminator
stateTransitions *stateTransitions
gossip *gossip
rollup *updateRollup
joinTimeout, pingTimeout, pingRequestTimeout time.Duration
pingRequestSize int
listeners []EventListener
clientRate metrics.Meter
serverRate metrics.Meter
totalRate metrics.Meter
startTime time.Time
logger log.Logger
// clock is used to generate incarnation numbers; it is typically the
// system clock, wrapped via clock.New()
clock clock.Clock
}
// NewNode returns a new SWIM Node.
func NewNode(app, address string, channel shared.SubChannel, opts *Options) *Node {
// use defaults for options that are unspecified
opts = mergeDefaultOptions(opts)
node := &Node{
address: address,
app: app,
channel: channel,
logger: logging.Logger("node").WithField("local", address),
joinTimeout: opts.JoinTimeout,
pingTimeout: opts.PingTimeout,
pingRequestTimeout: opts.PingRequestTimeout,
pingRequestSize: opts.PingRequestSize,
clientRate: metrics.NewMeter(),
serverRate: metrics.NewMeter(),
totalRate: metrics.NewMeter(),
clock: opts.Clock,
}
node.memberlist = newMemberlist(node)
node.memberiter = newMemberlistIter(node.memberlist)
node.stateTransitions = newStateTransitions(node, opts.StateTimeouts)
node.gossip = newGossip(node, opts.MinProtocolPeriod)
node.disseminator = newDisseminator(node)
node.rollup = newUpdateRollup(node, opts.RollupFlushInterval,
opts.RollupMaxUpdates)
if node.channel != nil {
node.registerHandlers()
node.service = node.channel.ServiceName()
}
return node
}
// Address returns the address of the SWIM node.
func (n *Node) Address() string {
return n.address
}
// App returns the Node's application name.
func (n *Node) App() string {
return n.app
}
// HasChanges reports whether Node has changes to disseminate.
func (n *Node) HasChanges() bool {
return n.disseminator.HasChanges()
}
// Incarnation returns the incarnation number of the Node.
func (n *Node) Incarnation() int64 {
if n.memberlist != nil && n.memberlist.local != nil {
n.memberlist.local.RLock()
incarnation := n.memberlist.local.Incarnation
n.memberlist.local.RUnlock()
return incarnation
}
return -1
}
func (n *Node) emit(event interface{}) {
for _, listener := range n.listeners {
listener.HandleEvent(event)
}
}
// RegisterListener adds an EventListener to the node. When a swim event e is
// emitted, l.HandleEvent(e) is called for every registered listener l.
// Attention, all listeners are called synchronously. Be careful with
// registering blocking and other slow calls.
func (n *Node) RegisterListener(l EventListener) {
n.listeners = append(n.listeners, l)
}
// Start starts the SWIM protocol and all sub-protocols.
func (n *Node) Start() {
n.gossip.Start()
n.stateTransitions.Enable()
n.state.Lock()
n.state.stopped = false
n.state.Unlock()
}
// Stop stops the SWIM protocol and all sub-protocols.
func (n *Node) Stop() {
n.gossip.Stop()
n.stateTransitions.Disable()
n.state.Lock()
n.state.stopped = true
n.state.Unlock()
}
// Stopped returns whether or not the SWIM protocol is currently running.
func (n *Node) Stopped() bool {
n.state.RLock()
stopped := n.state.stopped
n.state.RUnlock()
return stopped
}
// Destroy stops the SWIM protocol and all sub-protocols.
func (n *Node) Destroy() {
n.Stop()
n.rollup.Destroy()
n.state.Lock()
n.state.destroyed = true
n.state.Unlock()
}
// Destroyed returns whether or not the node has been destroyed.
func (n *Node) Destroyed() bool {
n.state.RLock()
destroyed := n.state.destroyed
n.state.RUnlock()
return destroyed
}
// Ready returns whether or not the node has bootstrapped and is ready for use.
func (n *Node) Ready() bool {
n.state.RLock()
ready := n.state.ready
n.state.RUnlock()
return ready
}
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//
// Bootstrapping
//
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// BootstrapOptions is a configuration struct passed to Node.Bootstrap.
type BootstrapOptions struct {
// The DiscoverProvider resolves a list of bootstrap hosts.
DiscoverProvider discovery.DiscoverProvider
// Whether or not gossip should be started immediately after a successful
// bootstrap.
Stopped bool
// Amount of time before individual join requests time out.
JoinTimeout time.Duration
// Minimum number of nodes to join to satisfy a bootstrap.
JoinSize int
// Maximum time to attempt joins before the entire bootstrap process times
// out.
MaxJoinDuration time.Duration
// A higher ParallelismFactor increases the number of nodes that a
// bootstrapping node will attempt to reach out to in order to satisfy
// `JoinSize` (the number of nodes that will be contacted at a time is
// `ParallelismFactor * JoinSize`).
ParallelismFactor int
}
// Bootstrap joins a node to a cluster. The channel provided to the node must be
// listening for the bootstrap to complete.
func (n *Node) Bootstrap(opts *BootstrapOptions) ([]string, error) {
if n.channel == nil {
return nil, errors.New("channel required")
}
if opts == nil {
opts = &BootstrapOptions{}
}
n.memberlist.Reincarnate()
joinOpts := &joinOpts{
timeout: opts.JoinTimeout,
size: opts.JoinSize,
maxJoinDuration: opts.MaxJoinDuration,
parallelismFactor: opts.ParallelismFactor,
discoverProvider: opts.DiscoverProvider,
}
joined, err := sendJoin(n, joinOpts)
if err != nil {
n.logger.WithFields(log.Fields{
"err": err.Error(),
}).Error("bootstrap failed")
return nil, err
}
if !opts.Stopped {
n.gossip.Start()
}
n.state.Lock()
n.state.ready = true
n.state.Unlock()
n.startTime = time.Now()
return joined, nil
}
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//
// Change Handling
//
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
func (n *Node) handleChanges(changes []Change) {
for _, change := range changes {
n.disseminator.RecordChange(change)
switch change.Status {
case Alive:
n.stateTransitions.Cancel(change)
n.disseminator.AdjustMaxPropagations()
case Faulty:
n.stateTransitions.Cancel(change)
case Suspect:
n.stateTransitions.ScheduleSuspectToFaulty(change)
n.disseminator.AdjustMaxPropagations()
case Leave:
n.stateTransitions.Cancel(change)
n.disseminator.AdjustMaxPropagations()
}
}
}
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//
// Gossip
//
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
func (n *Node) pinging() bool {
n.state.RLock()
pinging := n.state.pinging
n.state.RUnlock()
return pinging
}
func (n *Node) setPinging(pinging bool) {
n.state.Lock()
n.state.pinging = pinging
n.state.Unlock()
}
// pingNextMember pings the next member in the memberlist
func (n *Node) pingNextMember() {
member, ok := n.memberiter.Next()
if !ok {
n.logger.Warn("no pingable members")
return
}
if n.pinging() {
n.logger.Warn("node already pinging")
return
}
n.setPinging(true)
defer n.setPinging(false)
// send ping
res, err := sendPing(n, member.Address, n.pingTimeout)
if err == nil {
n.memberlist.Update(res.Changes)
return
}
// ping failed, send ping requests
target := member.Address
targetReached, errs := indirectPing(n, target, n.pingRequestSize, n.pingRequestTimeout)
// if all helper nodes are unreachable, the indirectPing is inconclusive
if len(errs) == n.pingRequestSize {
n.logger.WithFields(log.Fields{
"target": target,
"errors": errs,
"numErrors": len(errs),
}).Warn("ping request inconclusive due to errors")
return
}
if !targetReached {
n.logger.WithField("target", target).Info("ping request target unreachable")
n.memberlist.MakeSuspect(member.Address, member.Incarnation)
return
}
n.logger.WithField("target", target).Info("ping request target reachable")
}
// GetReachableMembers returns a slice of members currently in this node's
// membership list that aren't faulty.
func (n *Node) GetReachableMembers() []string {
return n.memberlist.GetReachableMembers()
}
// CountReachableMembers returns the number of members currently in this node's
// membership list that aren't faulty.
func (n *Node) CountReachableMembers() int {
return n.memberlist.CountReachableMembers()
}