-
Notifications
You must be signed in to change notification settings - Fork 712
/
participant.go
357 lines (312 loc) · 11.4 KB
/
participant.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
// Copyright 2023 TiKV Project 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 member
import (
"context"
"fmt"
"path"
"strconv"
"sync/atomic"
"time"
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/election"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/utils/etcdutil"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
)
type leadershipCheckFunc func(*election.Leadership) bool
// Participant is used for the election related logic. Compared to its counterpart
// EmbeddedEtcdMember, Participant relies on etcd for election, but it's decoupled
// from the embedded etcd. It implements Member interface.
type Participant struct {
leadership *election.Leadership
// stored as member type
leader atomic.Value
client *clientv3.Client
rootPath string
leaderPath string
member *tsopb.Participant
// memberValue is the serialized string of `member`. It will be saved in the
// leader key when this participant is successfully elected as the leader of
// the group. Every write will use it to check the leadership.
memberValue string
// preCampaignChecker is called before the campaign. If it returns false, the
// campaign will be skipped.
preCampaignChecker leadershipCheckFunc
// lastLeaderUpdatedTime is the last time when the leader is updated.
lastLeaderUpdatedTime atomic.Value
}
// NewParticipant create a new Participant.
func NewParticipant(client *clientv3.Client) *Participant {
return &Participant{
client: client,
}
}
// InitInfo initializes the member info. The leader key is path.Join(rootPath, leaderName)
func (m *Participant) InitInfo(name string, id uint64, rootPath string, leaderName string, purpose string, advertiseListenAddr string) {
leader := &tsopb.Participant{
Name: name,
Id: id, // id is unique among all participants
ListenUrls: []string{advertiseListenAddr},
}
data, err := leader.Marshal()
if err != nil {
// can't fail, so panic here.
log.Fatal("marshal leader meet error", zap.Stringer("leader-name", leader), errs.ZapError(errs.ErrMarshalLeader, err))
}
m.member = leader
m.memberValue = string(data)
m.rootPath = rootPath
m.leaderPath = path.Join(rootPath, leaderName)
m.leadership = election.NewLeadership(m.client, m.GetLeaderPath(), purpose)
m.lastLeaderUpdatedTime.Store(time.Now())
log.Info("participant joining election", zap.Stringer("participant-info", m.member), zap.String("leader-path", m.leaderPath))
}
// ID returns the unique ID for this participant in the election group
func (m *Participant) ID() uint64 {
return m.member.Id
}
// Name returns the unique name in the election group.
func (m *Participant) Name() string {
return m.member.Name
}
// GetMember returns the member.
func (m *Participant) GetMember() interface{} {
return m.member
}
// MemberValue returns the member value.
func (m *Participant) MemberValue() string {
return m.memberValue
}
// Client returns the etcd client.
func (m *Participant) Client() *clientv3.Client {
return m.client
}
// IsLeader returns whether the participant is the leader or not by checking its leadership's
// lease and leader info.
func (m *Participant) IsLeader() bool {
return m.leadership.Check() && m.GetLeader().GetId() == m.member.GetId()
}
// IsLeaderElected returns true if the leader exists; otherwise false
func (m *Participant) IsLeaderElected() bool {
return m.GetLeader() != nil
}
// GetLeaderListenUrls returns current leader's listen urls
func (m *Participant) GetLeaderListenUrls() []string {
return m.GetLeader().GetListenUrls()
}
// GetLeaderID returns current leader's member ID.
func (m *Participant) GetLeaderID() uint64 {
return m.GetLeader().GetId()
}
// GetLeader returns current leader of the election group.
func (m *Participant) GetLeader() *tsopb.Participant {
leader := m.leader.Load()
if leader == nil {
return nil
}
member := leader.(*tsopb.Participant)
if member.GetId() == 0 {
return nil
}
return member
}
// setLeader sets the member's leader.
func (m *Participant) setLeader(member *tsopb.Participant) {
m.leader.Store(member)
m.lastLeaderUpdatedTime.Store(time.Now())
}
// unsetLeader unsets the member's leader.
func (m *Participant) unsetLeader() {
m.leader.Store(&tsopb.Participant{})
m.lastLeaderUpdatedTime.Store(time.Now())
}
// EnableLeader declares the member itself to be the leader.
func (m *Participant) EnableLeader() {
m.setLeader(m.member)
}
// GetLeaderPath returns the path of the leader.
func (m *Participant) GetLeaderPath() string {
return m.leaderPath
}
// GetLastLeaderUpdatedTime returns the last time when the leader is updated.
func (m *Participant) GetLastLeaderUpdatedTime() time.Time {
lastLeaderUpdatedTime := m.lastLeaderUpdatedTime.Load()
if lastLeaderUpdatedTime == nil {
return time.Time{}
}
return lastLeaderUpdatedTime.(time.Time)
}
// GetLeadership returns the leadership of the member.
func (m *Participant) GetLeadership() *election.Leadership {
return m.leadership
}
// CampaignLeader is used to campaign the leadership and make it become a leader.
func (m *Participant) CampaignLeader(leaseTimeout int64) error {
if m.preCampaignChecker != nil && !m.preCampaignChecker(m.leadership) {
return errs.ErrPreCheckCampaign
}
return m.leadership.Campaign(leaseTimeout, m.MemberValue())
}
// KeepLeader is used to keep the leader's leadership.
func (m *Participant) KeepLeader(ctx context.Context) {
m.leadership.Keep(ctx)
}
// PreCheckLeader does some pre-check before checking whether or not it's the leader.
// It returns true if it passes the pre-check, false otherwise.
func (m *Participant) PreCheckLeader() error {
// No specific thing to check. Returns no error.
return nil
}
// getPersistentLeader gets the corresponding leader from etcd by given leaderPath (as the key).
func (m *Participant) getPersistentLeader() (*tsopb.Participant, int64, error) {
leader := &tsopb.Participant{}
ok, rev, err := etcdutil.GetProtoMsgWithModRev(m.client, m.GetLeaderPath(), leader)
if err != nil {
return nil, 0, err
}
if !ok {
return nil, 0, nil
}
return leader, rev, nil
}
// CheckLeader checks if someone else is taking the leadership. If yes, returns the leader;
// otherwise returns a bool which indicates if it is needed to check later.
func (m *Participant) CheckLeader() (ElectionLeader, bool) {
if err := m.PreCheckLeader(); err != nil {
log.Error("failed to pass pre-check, check the leader later", errs.ZapError(errs.ErrEtcdLeaderNotFound))
time.Sleep(200 * time.Millisecond)
return nil, true
}
leader, revision, err := m.getPersistentLeader()
if err != nil {
log.Error("getting the leader meets error", errs.ZapError(err))
time.Sleep(200 * time.Millisecond)
return nil, true
}
if leader == nil {
// no leader yet
return nil, false
}
if m.IsSameLeader(leader) {
// oh, we are already the leader, which indicates we may meet something wrong
// in previous CampaignLeader. We should delete the leadership and campaign again.
log.Warn("the leader has not changed, delete and campaign again", zap.Stringer("old-leader", leader))
// Delete the leader itself and let others start a new election again.
if err = m.leadership.DeleteLeaderKey(); err != nil {
log.Error("deleting the leader key meets error", errs.ZapError(err))
time.Sleep(200 * time.Millisecond)
return nil, true
}
// Return nil and false to make sure the campaign will start immediately.
return nil, false
}
return &EtcdLeader{
wrapper: m,
pariticipant: leader,
revision: revision,
}, false
}
// WatchLeader is used to watch the changes of the leader.
func (m *Participant) WatchLeader(ctx context.Context, leader *tsopb.Participant, revision int64) {
m.setLeader(leader)
m.leadership.Watch(ctx, revision)
m.unsetLeader()
}
// ResetLeader is used to reset the member's current leadership.
// Basically it will reset the leader lease and unset leader info.
func (m *Participant) ResetLeader() {
m.leadership.Reset()
m.unsetLeader()
}
// IsSameLeader checks whether a server is the leader itself.
func (m *Participant) IsSameLeader(leader *tsopb.Participant) bool {
return leader.GetId() == m.ID()
}
// CheckPriority checks whether there is another participant has higher priority and resign it as the leader if so.
func (m *Participant) CheckPriority(ctx context.Context) {
// TODO: implement weighted-election when it's in need
}
func (m *Participant) getLeaderPriorityPath(id uint64) string {
return path.Join(m.rootPath, fmt.Sprintf("participant/%d/leader_priority", id))
}
// GetDCLocationPathPrefix returns the dc-location path prefix of the cluster.
func (m *Participant) GetDCLocationPathPrefix() string {
return path.Join(m.rootPath, dcLocationConfigEtcdPrefix)
}
// GetDCLocationPath returns the dc-location path of a member with the given member ID.
func (m *Participant) GetDCLocationPath(id uint64) string {
return path.Join(m.GetDCLocationPathPrefix(), fmt.Sprint(id))
}
// SetLeaderPriority saves the priority to be elected as the etcd leader.
func (m *Participant) SetLeaderPriority(id uint64, priority int) error {
key := m.getLeaderPriorityPath(id)
res, err := m.leadership.LeaderTxn().Then(clientv3.OpPut(key, strconv.Itoa(priority))).Commit()
if err != nil {
return errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
}
if !res.Succeeded {
log.Error("save etcd leader priority failed, maybe not the leader")
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return nil
}
// DeleteLeaderPriority removes the etcd leader priority config.
func (m *Participant) DeleteLeaderPriority(id uint64) error {
key := m.getLeaderPriorityPath(id)
res, err := m.leadership.LeaderTxn().Then(clientv3.OpDelete(key)).Commit()
if err != nil {
return errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
}
if !res.Succeeded {
log.Error("delete etcd leader priority failed, maybe not the leader")
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return nil
}
// DeleteDCLocationInfo removes the dc-location info.
func (m *Participant) DeleteDCLocationInfo(id uint64) error {
key := m.GetDCLocationPath(id)
res, err := m.leadership.LeaderTxn().Then(clientv3.OpDelete(key)).Commit()
if err != nil {
return errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
}
if !res.Succeeded {
log.Error("delete dc-location info failed, maybe not the leader")
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return nil
}
// GetLeaderPriority loads the priority to be elected as the etcd leader.
func (m *Participant) GetLeaderPriority(id uint64) (int, error) {
key := m.getLeaderPriorityPath(id)
res, err := etcdutil.EtcdKVGet(m.client, key)
if err != nil {
return 0, err
}
if len(res.Kvs) == 0 {
return 0, nil
}
priority, err := strconv.ParseInt(string(res.Kvs[0].Value), 10, 32)
if err != nil {
return 0, errs.ErrStrconvParseInt.Wrap(err).GenWithStackByCause()
}
return int(priority), nil
}
// SetPreCampaignChecker sets the pre-campaign checker.
func (m *Participant) SetPreCampaignChecker(checker leadershipCheckFunc) {
m.preCampaignChecker = checker
}