-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathleader_election.go
117 lines (102 loc) · 3.44 KB
/
leader_election.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
// Copyright (c) 2018 The Jaeger 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.
package leaderelection
import (
"sync"
"time"
"go.uber.org/atomic"
"go.uber.org/zap"
dl "github.com/jaegertracing/jaeger/pkg/distributedlock"
)
const (
acquireLockErrMsg = "Failed to acquire lock"
)
// ElectionParticipant partakes in leader election to become leader.
type ElectionParticipant interface {
IsLeader() bool
}
type electionParticipant struct {
ElectionParticipantOptions
lock dl.Lock
isLeader *atomic.Bool
resourceName string
closeChan chan struct{}
wg sync.WaitGroup
}
// ElectionParticipantOptions control behavior of the election participant. TODO func applyDefaults(), parameter error checking, etc.
type ElectionParticipantOptions struct {
LeaderLeaseRefreshInterval time.Duration
FollowerLeaseRefreshInterval time.Duration
Logger *zap.Logger
}
// NewElectionParticipant returns a ElectionParticipant which attempts to become leader.
func NewElectionParticipant(lock dl.Lock, resourceName string, options ElectionParticipantOptions) ElectionParticipant {
return &electionParticipant{
ElectionParticipantOptions: options,
lock: lock,
resourceName: resourceName,
isLeader: atomic.NewBool(false),
closeChan: make(chan struct{}),
}
}
// Start runs a background thread which attempts to acquire the leader lock.
func (p *electionParticipant) Start() error {
p.wg.Add(1)
go p.runAcquireLockLoop()
return nil
}
// Close implements io.Closer.
func (p *electionParticipant) Close() error {
close(p.closeChan)
p.wg.Wait()
return nil
}
// IsLeader returns true if this process is the leader.
func (p *electionParticipant) IsLeader() bool {
return p.isLeader.Load()
}
// runAcquireLockLoop attempts to acquire the leader lock. If it succeeds, it will attempt to retain it,
// otherwise it sleeps and attempts to gain the lock again.
func (p *electionParticipant) runAcquireLockLoop() {
defer p.wg.Done()
ticker := time.NewTicker(p.acquireLock())
for {
select {
case <-ticker.C:
ticker.Stop()
ticker = time.NewTicker(p.acquireLock())
case <-p.closeChan:
ticker.Stop()
return
}
}
}
// acquireLock attempts to acquire the lock and returns the interval to sleep before the next retry.
func (p *electionParticipant) acquireLock() time.Duration {
if acquiredLeaderLock, err := p.lock.Acquire(p.resourceName, p.FollowerLeaseRefreshInterval); err == nil {
p.setLeader(acquiredLeaderLock)
} else {
p.Logger.Error(acquireLockErrMsg, zap.Error(err))
}
if p.IsLeader() {
// If this process holds the leader lock, retry with a shorter cadence
// to retain the leader lease.
return p.LeaderLeaseRefreshInterval
}
// If this process failed to acquire the leader lock, retry with a longer cadence
return p.FollowerLeaseRefreshInterval
}
func (p *electionParticipant) setLeader(isLeader bool) {
p.isLeader.Store(isLeader)
}