-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbully.go
538 lines (449 loc) · 11.8 KB
/
bully.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
package bullyelection
import (
"bytes"
"context"
"fmt"
"log"
"net"
"os"
"sort"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/hashicorp/memberlist"
"github.com/oklog/ulid/v2"
"github.com/pkg/errors"
)
const (
DefaultElectionTimeout = 10 * time.Second
DefaultUpdateNodeTimeout = 5 * time.Second
DefaultJoinNodeTimeout = DefaultElectionTimeout + (10 * time.Second)
DefaultLeaveNodeTimeout = 10 * time.Second
DefaultTransferLeaderTimeout = DefaultElectionTimeout + (10 * time.Second)
DefaultRetryNodeMsgTimeout = 5 * time.Second
DefaultRetryNodeEventTimeout = 5 * time.Second
)
var (
ErrBullyInitialize = errors.New("bully initialize")
ErrBullyBusy = errors.New("bully busy")
ErrJoinTimeout = errors.New("join timeout")
ErrLeaveTimeout = errors.New("leave timeout")
ErrTransferLeadershipTimeout = errors.New("transfer_leadership timeout")
ErrNodeNotFound = errors.New("node not found")
)
type (
ObserveFunc func(*Bully, NodeEvent, string, string)
ULIDGeneratorFunc func() string
OnErrorFunc func(error)
)
// impl check
var (
_ ObserveFunc = DefaultObserverFunc
_ ULIDGeneratorFunc = DefaultULIDGeneratorFunc
_ OnErrorFunc = DefaultOnErrorFunc
)
func DefaultObserverFunc(b *Bully, evt NodeEvent, id, addr string) {
// nop
}
func DefaultULIDGeneratorFunc() string {
return ulid.Make().String()
}
func DefaultOnErrorFunc(err error) {
log.Printf("error: %+v", err)
}
type BullyOptFunc func(*bullyOpt)
type bullyOpt struct {
observeFunc ObserveFunc
electionTimeout time.Duration
updateNodeTimeout time.Duration
joinNodeTimeout time.Duration
leaveNodeTimeout time.Duration
transferLeaderTimeout time.Duration
retryNodeMsgTimeout time.Duration
retryNodeEventTimeout time.Duration
ulidGeneratorFunc ULIDGeneratorFunc
onErrorFunc OnErrorFunc
logger *log.Logger
enableUniqNodeName bool
}
func WithObserveFunc(f ObserveFunc) BullyOptFunc {
return func(o *bullyOpt) {
o.observeFunc = f
}
}
func WithElectionTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.electionTimeout = d
}
}
func WithUpdateNodeTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.updateNodeTimeout = d
}
}
func WithJoinNodeTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.joinNodeTimeout = d
}
}
func WithLeaveNodeTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.leaveNodeTimeout = d
}
}
func WithTransferLeaderTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.transferLeaderTimeout = d
}
}
func WithRetryNodeMsgTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.retryNodeMsgTimeout = d
}
}
func WithRetryNodeEventTimeout(d time.Duration) BullyOptFunc {
return func(o *bullyOpt) {
o.retryNodeEventTimeout = d
}
}
func WithULIDGeneratorFunc(f ULIDGeneratorFunc) BullyOptFunc {
return func(o *bullyOpt) {
o.ulidGeneratorFunc = f
}
}
func WithOnErrorFunc(f OnErrorFunc) BullyOptFunc {
return func(o *bullyOpt) {
o.onErrorFunc = f
}
}
func WithLogger(logger *log.Logger) BullyOptFunc {
return func(o *bullyOpt) {
o.logger = logger
}
}
func WithEnableUniqNodeName(enable bool) BullyOptFunc {
return func(o *bullyOpt) {
o.enableUniqNodeName = enable
}
}
func newBullyOpt(opts []BullyOptFunc) *bullyOpt {
opt := &bullyOpt{
electionTimeout: DefaultElectionTimeout,
updateNodeTimeout: DefaultUpdateNodeTimeout,
joinNodeTimeout: DefaultJoinNodeTimeout,
leaveNodeTimeout: DefaultLeaveNodeTimeout,
transferLeaderTimeout: DefaultTransferLeaderTimeout,
retryNodeMsgTimeout: DefaultRetryNodeMsgTimeout,
retryNodeEventTimeout: DefaultRetryNodeEventTimeout,
observeFunc: DefaultObserverFunc,
ulidGeneratorFunc: DefaultULIDGeneratorFunc,
onErrorFunc: DefaultOnErrorFunc,
enableUniqNodeName: true,
}
for _, f := range opts {
f(opt)
}
return opt
}
type Bully struct {
mu *sync.RWMutex
wg *sync.WaitGroup
electionQueue chan *nodeEventMsg
waitElection chan struct{}
electionCancel context.CancelFunc
opt *bullyOpt
cancel context.CancelFunc
node Node
list *memberlist.Memberlist
}
func (b *Bully) IsVoter() bool {
b.mu.RLock()
defer b.mu.RUnlock()
return b.node.IsVoter()
}
func (b *Bully) ID() string {
b.mu.RLock()
defer b.mu.RUnlock()
return b.node.ID()
}
func (b *Bully) Address() string {
b.mu.RLock()
defer b.mu.RUnlock()
return b.fulladdress()
}
func (b *Bully) fulladdress() string {
return net.JoinHostPort(b.node.Addr(), strconv.Itoa(b.node.Port()))
}
func (b *Bully) IsLeader() bool {
b.mu.RLock()
defer b.mu.RUnlock()
return b.node.IsLeader()
}
func (b *Bully) Metadata() []byte {
b.mu.RLock()
defer b.mu.RUnlock()
return b.node.UserMetadata()
}
func (b *Bully) UpdateMetadata(data []byte) error {
b.mu.RLock()
defer b.mu.RUnlock()
b.node.setUserMetadata(data)
if err := b.list.UpdateNode(10 * time.Second); err != nil {
return errors.WithStack(err)
}
return nil
}
func (b *Bully) Members() []Node {
b.mu.RLock()
defer b.mu.RUnlock()
return b.listNodes()
}
func (b *Bully) listNodes() []Node {
members := b.list.Members()
sort.Slice(members, func(i, j int) bool {
return members[i].Name < members[j].Name
})
m := make([]Node, len(members))
for i, member := range members {
meta, err := fromJSON(bytes.NewReader(member.Meta))
if err != nil {
b.opt.logger.Printf("warn: fromJSON(%s):%+v", member.Meta, errors.WithStack(err))
continue
}
m[i] = nodeMetaToNode(meta)
}
return m
}
func (b *Bully) followerJoin(addr string) error {
b.opt.logger.Printf("info: join %s", addr)
if _, err := b.list.Join([]string{addr}); err != nil {
return errors.WithStack(err)
}
return nil
}
func (b *Bully) Join(addr string) error {
if addr == b.list.LocalNode().Address() {
return nil // skip self join
}
if b.IsVoter() != true {
return b.followerJoin(addr)
}
// clear leader
b.clearLeaderID()
if err := b.updateNode(); err != nil {
return errors.WithStack(err)
}
wait := make(chan struct{})
b.mu.Lock()
b.waitElection = wait
b.mu.Unlock()
defer func() {
b.mu.Lock()
b.waitElection = nil
b.mu.Unlock()
}()
b.opt.logger.Printf("info: join %s", addr)
if _, err := b.list.Join([]string{addr}); err != nil {
return errors.WithStack(err)
}
select {
case <-wait:
return nil
case <-time.After(b.opt.joinNodeTimeout):
return errors.Wrapf(ErrJoinTimeout, "timeout = %s", b.opt.joinNodeTimeout)
}
}
func (b *Bully) Leave() error {
if err := b.list.Leave(b.opt.leaveNodeTimeout); err != nil {
return errors.Wrapf(ErrLeaveTimeout, "timeout = %s: %+v", b.opt.leaveNodeTimeout, err)
}
return nil
}
func (b *Bully) LeadershipTransfer(ctx context.Context) error {
if b.IsLeader() != true {
return nil
}
wait := make(chan struct{})
b.mu.Lock()
b.waitElection = wait
b.mu.Unlock()
defer func() {
b.mu.Lock()
b.waitElection = nil
b.mu.Unlock()
}()
if err := b.startLeadershipTransfer(ctx); err != nil {
return errors.WithStack(err)
}
select {
case <-wait:
return nil
case <-time.After(b.opt.transferLeaderTimeout):
return errors.Wrapf(ErrTransferLeadershipTimeout, "timeout = %s", b.opt.transferLeaderTimeout)
}
}
func (b *Bully) Shutdown() error {
b.mu.Lock()
err := b.list.Shutdown()
b.mu.Unlock()
if err != nil {
return errors.WithStack(err)
}
b.cancel()
b.wg.Wait()
return nil
}
func (b *Bully) clearLeaderID() bool {
return b.setLeaderID("")
}
func (b *Bully) setLeaderID(id string) bool {
if vn, ok := b.node.(internalVoterNode); ok {
vn.setLeaderID(id)
return true
}
return false
}
func (b *Bully) getLeaderID() string {
if vn, ok := b.node.(internalVoterNode); ok {
return vn.getLeaderID()
}
return ""
}
func (b *Bully) setULID(ulid string) bool {
if vn, ok := b.node.(internalVoterNode); ok {
vn.setULID(ulid)
return true
}
return false
}
func (b *Bully) getULID() string {
if vn, ok := b.node.(internalVoterNode); ok {
return vn.getULID()
}
return ""
}
func (b *Bully) updateNode() error {
if err := b.list.UpdateNode(b.opt.updateNodeTimeout); err != nil {
return errors.WithStack(err)
}
return nil
}
func (b *Bully) findNode(targetNodeID string) (*memberlist.Node, error) {
for _, m := range b.list.Members() {
if m.Name == targetNodeID {
return m, nil
}
}
return nil, errors.Wrapf(ErrNodeNotFound, "target node-id=%s", targetNodeID)
}
func (b *Bully) send(targetNodeID string, data []byte) error {
targetNode, err := b.findNode(targetNodeID)
if err != nil {
return errors.WithStack(err)
}
if err := b.list.SendReliable(targetNode, data); err != nil {
return errors.WithStack(err)
}
return nil
}
func (b *Bully) checkVoterNode(targetNodeID string) bool {
for _, n := range b.listNodes() {
if n.ID() == targetNodeID {
if n.IsVoter() {
return true
}
}
}
return false
}
func newBully(opt *bullyOpt, node Node, list *memberlist.Memberlist, cancel context.CancelFunc) *Bully {
return &Bully{
mu: new(sync.RWMutex),
wg: new(sync.WaitGroup),
electionQueue: make(chan *nodeEventMsg, 1024),
waitElection: nil,
electionCancel: nopCancelFunc(),
opt: opt,
node: node,
list: list,
cancel: cancel,
}
}
type createNodeFunc func(ulid string) Node
func createBully(parent context.Context, conf *memberlist.Config, funcs []BullyOptFunc, createNode createNodeFunc) (*Bully, error) {
originalNodeName := conf.Name
ctx, cancel := context.WithCancel(parent)
opt := newBullyOpt(funcs)
if opt.logger == nil {
opt.logger = log.New(os.Stderr, conf.Name+" ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
}
ulid := opt.ulidGeneratorFunc()
if opt.enableUniqNodeName {
conf.Name = fmt.Sprintf("%s-%s", conf.Name, ulid)
}
if conf.Logger == nil {
conf.Logger = opt.logger
}
ready := newAtomicReadyStatus()
msgCh := make(chan []byte)
evtCh := make(chan *nodeEventMsg)
resolvLater := false
if conf.BindPort == 0 && conf.AdvertisePort == 0 {
resolvLater = true
}
node := createNode(ulid)
conf.Delegate = newObserveNodeMessage(ctx, opt, ready, msgCh, node)
conf.Events = newObserveNodeEvent(ctx, opt, ready, evtCh)
list, err := memberlist.Create(conf)
if err != nil {
cancel()
return nil, errors.WithStack(err)
}
if resolvLater {
node.setPort(int(list.LocalNode().Port))
opt.logger.SetPrefix(fmt.Sprintf("%s(%s:%d) ", originalNodeName, node.Addr(), node.Port()))
}
b := newBully(opt, node, list, cancel)
b.wg.Add(3)
go b.readNodeMessageLoop(ctx, msgCh, evtCh)
go b.readNodeEventLoop(ctx, evtCh)
go b.electionRunLoop(ctx)
if ready.setStatusOk() != true {
cancel()
return nil, errors.Wrapf(ErrBullyInitialize, "already initialized")
}
return b, nil
}
func CreateVoter(parent context.Context, conf *memberlist.Config, funcs ...BullyOptFunc) (*Bully, error) {
return createBully(parent, conf, funcs, func(ulid string) Node {
return newVoterNode(conf.Name, ulid, conf.AdvertiseAddr, conf.AdvertisePort)
})
}
func CreateNonVoter(parent context.Context, conf *memberlist.Config, funcs ...BullyOptFunc) (*Bully, error) {
return createBully(parent, conf, funcs, func(ulid string) Node {
return newNonvoterNode(conf.Name, ulid, conf.AdvertiseAddr, conf.AdvertisePort)
})
}
type readyStatus int32
const (
readyInit readyStatus = iota
readyOk
)
type atomicReadyStatus struct {
v int32
}
func (a *atomicReadyStatus) setStatusOk() bool {
return atomic.CompareAndSwapInt32(&a.v, int32(readyInit), int32(readyOk))
}
func (a *atomicReadyStatus) IsOk() bool {
return atomic.LoadInt32(&a.v) == int32(readyOk)
}
func newAtomicReadyStatus() *atomicReadyStatus {
return &atomicReadyStatus{int32(readyInit)}
}
func nopCancelFunc() context.CancelFunc {
return context.CancelFunc(func() {
// nop
})
}