-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathjoin_sender.go
488 lines (397 loc) · 13.4 KB
/
join_sender.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
// 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"
"fmt"
"math/rand"
"sync"
"time"
log "github.com/uber-common/bark"
"github.com/uber/ringpop-go/logging"
"github.com/uber/ringpop-go/shared"
"github.com/uber/ringpop-go/util"
"github.com/uber/tchannel-go/json"
)
const (
// If a node cannot complete a join within defaultMaxJoinDuration
// there is likely something very wrong. The aim is for the join
// operation to take no more than 1s, under normal conditions.
//
// The duration assigned below is very high for the following
// purposes:
// - Gives an application developer some time to diagnose
// what could be wrong.
// - Gives an operator some time to bootstrap a newly
// provisioned cluster
// - Trying forever is futile
defaultMaxJoinDuration = 120 * time.Second
defaultJoinTimeout = time.Second
defaultJoinSize = 3
defaultParallelismFactor = 2
)
var errJoinTimeout = errors.New("join timed out")
// A joinRequest is used to request a join to a remote node
type joinRequest struct {
App string `json:"app"`
Source string `json:"source"`
Incarnation int64 `json:"incarnationNumber"`
Timeout time.Duration `json:"timeout"`
Labels map[string]string `json:"labels,omitempty"`
}
// joinOpts are opts to perform a join with
type joinOpts struct {
timeout time.Duration
size int
maxJoinDuration time.Duration
parallelismFactor int
// delayer delays repeated join attempts.
delayer joinDelayer
}
// A joinSender is used to join an existing cluster of nodes defined in a node's
// bootstrap list
type joinSender struct {
node *Node
timeout time.Duration
// bootstrapHostsMap is a map of unique hosts each containing a slice of
// the instances (hostsports) on that particular host.
bootstrapHostsMap map[string][]string
// This is used as a multiple of the required nodes left to satisfy
// `joinSize`. Additional parallelism can be applied in order for
// `joinSize` to be satisfied faster.
parallelismFactor int
maxJoinDuration time.Duration
potentialNodes []string
preferredNodes []string
nonPreferredNodes []string
// We either join the number of nodes defined by size or limit it to the number
// of `potentialNodes` as we can't join more than there are to join in the first place.
size int
// A round is a complete cycle through all potential join targets. When a round
// is completed we start all over again, though full cycles should be very rare.
// We try to join nodes until `joinSize` is reached or `maxJoinDuration` is exceeded.
roundPotentialNodes []string
roundPreferredNodes []string
roundNonPreferredNodes []string
numTries int
// delayer delays repeated join attempts.
delayer joinDelayer
logger log.Logger
}
// newJoinSender returns a new JoinSender to join a cluster with
func newJoinSender(node *Node, opts *joinOpts) (*joinSender, error) {
if opts == nil {
opts = &joinOpts{}
}
if node.discoverProvider == nil {
return nil, errors.New("no discover provider")
}
// Resolve/retrieve bootstrap hosts from the provider specified in the
// join options.
bootstrapHosts, err := node.discoverProvider.Hosts()
if err != nil {
return nil, err
}
// Check we're in the bootstrap host list and add ourselves if we're not
// there. If the host list is empty, this will create a single-node
// cluster.
if !util.StringInSlice(bootstrapHosts, node.Address()) {
bootstrapHosts = append(bootstrapHosts, node.Address())
}
js := &joinSender{
node: node,
logger: logging.Logger("join").WithField("local", node.Address()),
}
// Parse bootstrap hosts into a map
js.parseHosts(bootstrapHosts)
js.potentialNodes = js.CollectPotentialNodes(nil)
js.timeout = util.SelectDuration(opts.timeout, defaultJoinTimeout)
js.maxJoinDuration = util.SelectDuration(opts.maxJoinDuration, defaultMaxJoinDuration)
js.parallelismFactor = util.SelectInt(opts.parallelismFactor, defaultParallelismFactor)
js.size = util.SelectInt(opts.size, defaultJoinSize)
js.size = util.Min(js.size, len(js.potentialNodes))
js.delayer = opts.delayer
if js.delayer == nil {
// Create and use exponential delayer as the delay mechanism. Create it
// with nil opts which uses default delayOpts.
js.delayer, err = newExponentialDelayer(js.node.address, nil)
if err != nil {
return nil, err
}
}
return js, nil
}
// parseHosts populates the bootstrap hosts map from the provided slice of
// hostports.
func (j *joinSender) parseHosts(hostports []string) {
// Parse bootstrap hosts into a map
j.bootstrapHostsMap = util.HostPortsByHost(hostports)
// Perform some sanity checks on the bootstrap hosts
err := util.CheckLocalMissing(j.node.address, j.bootstrapHostsMap[util.CaptureHost(j.node.address)])
if err != nil {
j.logger.Warn(err.Error())
}
mismatched, err := util.CheckHostnameIPMismatch(j.node.address, j.bootstrapHostsMap)
if err != nil {
j.logger.WithField("mismatched", mismatched).Warn(err.Error())
}
}
// potential nodes are nodes that can be joined that are not the local node
func (j *joinSender) CollectPotentialNodes(nodesJoined []string) []string {
if nodesJoined == nil {
nodesJoined = make([]string, 0)
}
var potentialNodes []string
for _, hostports := range j.bootstrapHostsMap {
for _, hostport := range hostports {
if j.node.address != hostport && !util.StringInSlice(nodesJoined, hostport) {
potentialNodes = append(potentialNodes, hostport)
}
}
}
return potentialNodes
}
// preferred nodes are nodes that are not on the same host as the local node
func (j *joinSender) CollectPreferredNodes() []string {
var preferredNodes []string
for host, hostports := range j.bootstrapHostsMap {
if host != util.CaptureHost(j.node.address) {
preferredNodes = append(preferredNodes, hostports...)
}
}
return preferredNodes
}
// non-preferred nodes are everyone else
func (j *joinSender) CollectNonPreferredNodes() []string {
if len(j.preferredNodes) == 0 {
return j.potentialNodes
}
var nonPreferredNodes []string
for _, host := range j.bootstrapHostsMap[util.CaptureHost(j.node.address)] {
if host != j.node.address {
nonPreferredNodes = append(nonPreferredNodes, host)
}
}
return nonPreferredNodes
}
func (j *joinSender) Init(nodesJoined []string) {
rand.Seed(time.Now().UnixNano())
j.potentialNodes = j.CollectPotentialNodes(nodesJoined)
j.preferredNodes = j.CollectPreferredNodes()
j.nonPreferredNodes = j.CollectNonPreferredNodes()
j.roundPotentialNodes = append(j.roundPotentialNodes, j.potentialNodes...)
j.roundPreferredNodes = append(j.roundPreferredNodes, j.preferredNodes...)
j.roundNonPreferredNodes = append(j.roundNonPreferredNodes, j.nonPreferredNodes...)
}
// selects a group of nodes
func (j *joinSender) SelectGroup(nodesJoined []string) []string {
var group []string
// if fully exhausted or first round, initialize this round's nodes
if len(j.roundPreferredNodes) == 0 && len(j.roundNonPreferredNodes) == 0 {
j.Init(nodesJoined)
}
numNodesLeft := j.size - len(nodesJoined)
cont := func() bool {
if len(group) == numNodesLeft*j.parallelismFactor {
return false
}
nodesAvailable := len(j.roundPreferredNodes) + len(j.roundNonPreferredNodes)
if nodesAvailable == 0 {
return false
}
return true
}
for cont() {
if len(j.roundPreferredNodes) > 0 {
group = append(group, util.TakeNode(&j.roundPreferredNodes, -1))
} else if len(j.roundNonPreferredNodes) > 0 {
group = append(group, util.TakeNode(&j.roundNonPreferredNodes, -1))
}
}
return group
}
func (j *joinSender) JoinCluster() ([]string, error) {
var nodesJoined []string
var numGroups = 0
var numJoined = 0
var numFailed = 0
var startTime = time.Now()
if util.SingleNodeCluster(j.node.address, j.bootstrapHostsMap) {
j.logger.Info("got single node cluster to join")
return nodesJoined, nil
}
for {
if j.node.Destroyed() {
j.node.EmitEvent(JoinFailedEvent{
Reason: Destroyed,
Error: nil,
})
return nil, errors.New("node destroyed while attempting to join cluster")
}
// join group of nodes
successes, failures := j.JoinGroup(nodesJoined)
nodesJoined = append(nodesJoined, successes...)
numJoined += len(successes)
numFailed += len(failures)
numGroups++
if numJoined >= j.size {
j.logger.WithFields(log.Fields{
"joinSize": j.size,
"joinTime": time.Now().Sub(startTime),
"numJoined": numJoined,
"numFailed": numFailed,
"numGroups": numGroups,
}).Debug("join complete")
break
}
joinDuration := time.Now().Sub(startTime)
if joinDuration > j.maxJoinDuration {
j.logger.WithFields(log.Fields{
"joinDuration": joinDuration,
"maxJoinDuration": j.maxJoinDuration,
"numJoined": numJoined,
"numFailed": numFailed,
"startTime": startTime,
}).Warn("max join duration exceeded")
err := fmt.Errorf("join duration of %v exceeded max %v",
joinDuration, j.maxJoinDuration)
j.node.EmitEvent(JoinFailedEvent{
Reason: Error,
Error: err,
})
return nodesJoined, err
}
j.logger.WithFields(log.Fields{
"joinSize": j.size,
"numJoined": numJoined,
"numFailed": numFailed,
"startTime": startTime,
}).Debug("join not yet complete")
j.delayer.delay()
}
j.node.EmitEvent(JoinCompleteEvent{
Duration: time.Now().Sub(startTime),
NumJoined: numJoined,
Joined: nodesJoined,
})
return nodesJoined, nil
}
// JoinGroup collects a number of nodes to join and sends join requests to them.
// nodesJoined contains the nodes that are already joined. The method returns
// the nodes that are succesfully joined, and the nodes that failed respond.
func (j *joinSender) JoinGroup(nodesJoined []string) ([]string, []string) {
group := j.SelectGroup(nodesJoined)
var responses struct {
successes []string
failures []string
sync.Mutex
}
var numNodesLeft = j.size - len(nodesJoined)
var startTime = time.Now()
var wg sync.WaitGroup
j.numTries++
j.node.EmitEvent(JoinTriesUpdateEvent{j.numTries})
for _, target := range group {
wg.Add(1)
go func(target string) {
defer wg.Done()
res, err := sendJoinRequest(j.node, target, j.timeout)
if err != nil {
msg := "attempt to join node failed"
if err == errJoinTimeout {
msg = "attempt to join node timed out"
}
j.logger.WithFields(log.Fields{
"remote": target,
"timeout": j.timeout,
}).Debug(msg)
responses.Lock()
responses.failures = append(responses.failures, target)
responses.Unlock()
return
}
responses.Lock()
responses.successes = append(responses.successes, target)
responses.Unlock()
start := time.Now()
j.node.memberlist.AddJoinList(res.Membership)
j.node.EmitEvent(AddJoinListEvent{
Duration: time.Now().Sub(start),
})
}(target)
}
// wait for joins to complete
wg.Wait()
// don't need to lock successes/failures since we're finished writing to them
j.logger.WithFields(log.Fields{
"groupSize": len(group),
"joinSize": j.size,
"joinTime": time.Now().Sub(startTime),
"numNodesLeft": numNodesLeft,
"numFailures": len(responses.failures),
"failures": responses.failures,
"numSuccesses": len(responses.successes),
"successes": responses.successes,
}).Debug("join group complete")
return responses.successes, responses.failures
}
// sendJoinRequest sends a join request to the specified target.
func sendJoinRequest(node *Node, target string, timeout time.Duration) (*joinResponse, error) {
ctx, cancel := shared.NewTChannelContext(timeout)
defer cancel()
peer := node.channel.Peers().GetOrAdd(target)
req := joinRequest{
App: node.app,
Source: node.address,
Incarnation: node.Incarnation(),
Timeout: timeout,
Labels: node.Labels().AsMap(),
}
res := &joinResponse{}
// make request
errC := make(chan error, 1)
go func() {
errC <- json.CallPeer(ctx, peer, node.service, "/protocol/join", req, res)
}()
// wait for result or timeout
var err error
select {
case err = <-errC:
case <-ctx.Done():
err = errJoinTimeout
}
if err != nil {
logging.Logger("join").WithFields(log.Fields{
"local": node.Address(),
"error": err,
}).Debug("could not complete join")
return nil, err
}
return res, err
}
// SendJoin creates a new JoinSender and attempts to join the cluster defined by
// the nodes bootstrap hosts
func sendJoin(node *Node, opts *joinOpts) ([]string, error) {
joiner, err := newJoinSender(node, opts)
if err != nil {
return nil, err
}
return joiner.JoinCluster()
}