/
election.go
183 lines (144 loc) · 4.5 KB
/
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
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
/*
Copyright 2019 The Vitess 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 memorytopo
import (
"context"
"path"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
)
// NewLeaderParticipation is part of the topo.Conn interface.
func (c *Conn) NewLeaderParticipation(name, id string) (topo.LeaderParticipation, error) {
c.factory.callstats.Add([]string{"NewLeaderParticipation"}, 1)
if c.closed.Load() {
return nil, ErrConnectionClosed
}
c.factory.mu.Lock()
defer c.factory.mu.Unlock()
// Make sure the global path exists.
electionPath := path.Join(electionsPath, name)
if n := c.factory.getOrCreatePath(c.cell, electionPath); n == nil {
return nil, topo.NewError(topo.NoNode, electionPath)
}
return &cLeaderParticipation{
c: c,
name: name,
id: id,
stop: make(chan struct{}),
done: make(chan struct{}),
}, nil
}
// cLeaderParticipation implements topo.LeaderParticipation.
//
// We use a directory (in global election path, with the name) with
// ephemeral files in it, that contains the id. The oldest revision
// wins the election.
type cLeaderParticipation struct {
// c is our memorytopo connection
c *Conn
// name is the name of this LeaderParticipation
name string
// id is the process's current id.
id string
// stop is a channel closed when Stop is called.
stop chan struct{}
// done is a channel closed when we're done processing the Stop
done chan struct{}
}
// WaitForLeadership is part of the topo.LeaderParticipation interface.
func (mp *cLeaderParticipation) WaitForLeadership() (context.Context, error) {
if mp.c.closed.Load() {
return nil, ErrConnectionClosed
}
// If Stop was already called, mp.done is closed, so we are interrupted.
select {
case <-mp.done:
return nil, topo.NewError(topo.Interrupted, "Leadership")
default:
}
electionPath := path.Join(electionsPath, mp.name)
// We use a cancelable context here. If stop is closed,
// we just cancel that context.
lockCtx, lockCancel := context.WithCancel(context.Background())
// Try to get the primaryship, by getting a lock.
ld, err := mp.c.Lock(lockCtx, electionPath, mp.id)
if err != nil {
lockCancel()
close(mp.done)
// It can be that we were interrupted.
return nil, err
}
go func() {
<-mp.stop
if err := ld.Unlock(context.Background()); err != nil {
log.Errorf("failed to unlock LockDescriptor %v: %v", electionPath, err)
}
lockCancel()
close(mp.done)
}()
// We got the lock. Return the lockContext. If Stop() is called,
// it will cancel the lockCtx, and cancel the returned context.
return lockCtx, nil
}
// Stop is part of the topo.LeaderParticipation interface
func (mp *cLeaderParticipation) Stop() {
close(mp.stop)
<-mp.done
}
// GetCurrentLeaderID is part of the topo.LeaderParticipation interface
func (mp *cLeaderParticipation) GetCurrentLeaderID(ctx context.Context) (string, error) {
if mp.c.closed.Load() {
return "", ErrConnectionClosed
}
electionPath := path.Join(electionsPath, mp.name)
mp.c.factory.mu.Lock()
defer mp.c.factory.mu.Unlock()
n := mp.c.factory.nodeByPath(mp.c.cell, electionPath)
if n == nil {
return "", nil
}
return n.lockContents, nil
}
// WaitForNewLeader is part of the topo.LeaderParticipation interface
func (mp *cLeaderParticipation) WaitForNewLeader(ctx context.Context) (<-chan string, error) {
if mp.c.closed.Load() {
return nil, ErrConnectionClosed
}
mp.c.factory.mu.Lock()
defer mp.c.factory.mu.Unlock()
electionPath := path.Join(electionsPath, mp.name)
n := mp.c.factory.nodeByPath(mp.c.cell, electionPath)
if n == nil {
return nil, topo.NewError(topo.NoNode, electionPath)
}
notifications := make(chan string, 8)
watchIndex := n.addWatch(watch{lock: notifications})
if n.lock != nil {
notifications <- n.lockContents
}
go func() {
defer close(notifications)
select {
case <-mp.stop:
case <-ctx.Done():
}
mp.c.factory.mu.Lock()
defer mp.c.factory.mu.Unlock()
n := mp.c.factory.nodeByPath(mp.c.cell, electionPath)
if n == nil {
return
}
delete(n.watches, watchIndex)
}()
return notifications, nil
}