-
Notifications
You must be signed in to change notification settings - Fork 719
/
member.go
526 lines (466 loc) · 16.3 KB
/
member.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
// Copyright 2016 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"
"math/rand"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/election"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/etcdutil"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/embed"
"go.uber.org/zap"
)
const (
// The timeout to wait transfer etcd leader to complete.
moveLeaderTimeout = 5 * time.Second
dcLocationConfigEtcdPrefix = "dc-location"
)
// EmbeddedEtcdMember is used for the election related logic. It implements Member interface.
type EmbeddedEtcdMember struct {
leadership *election.Leadership
leader atomic.Value // stored as *pdpb.Member
// etcd and cluster information.
etcd *embed.Etcd
client *clientv3.Client
id uint64 // etcd server id.
member *pdpb.Member // current PD's info.
rootPath string
// memberValue is the serialized string of `member`. It will be save in
// etcd leader key when the PD node is successfully elected as the PD leader
// of the cluster. Every write will use it to check PD leadership.
memberValue string
}
// NewMember create a new Member.
func NewMember(etcd *embed.Etcd, client *clientv3.Client, id uint64) *EmbeddedEtcdMember {
return &EmbeddedEtcdMember{
etcd: etcd,
client: client,
id: id,
}
}
// ID returns the unique etcd ID for this server in etcd cluster.
func (m *EmbeddedEtcdMember) ID() uint64 {
return m.id
}
// Name returns the unique etcd Name for this server in etcd cluster.
func (m *EmbeddedEtcdMember) Name() string {
return m.member.Name
}
// GetMember returns the member.
func (m *EmbeddedEtcdMember) GetMember() interface{} {
return m.member
}
// MemberValue returns the member value.
func (m *EmbeddedEtcdMember) MemberValue() string {
return m.memberValue
}
// Member returns the member.
func (m *EmbeddedEtcdMember) Member() *pdpb.Member {
return m.member
}
// Etcd returns etcd related information.
func (m *EmbeddedEtcdMember) Etcd() *embed.Etcd {
return m.etcd
}
// Client returns the etcd client.
func (m *EmbeddedEtcdMember) Client() *clientv3.Client {
return m.client
}
// IsLeader returns whether the server is PD leader or not by checking its leadership's lease and leader info.
func (m *EmbeddedEtcdMember) IsLeader() bool {
return m.leadership.Check() && m.GetLeader().GetMemberId() == m.member.GetMemberId()
}
// IsLeaderElected returns true if the leader exists; otherwise false
func (m *EmbeddedEtcdMember) IsLeaderElected() bool {
return m.GetLeader() != nil
}
// GetLeaderListenUrls returns current leader's listen urls
func (m *EmbeddedEtcdMember) GetLeaderListenUrls() []string {
return m.GetLeader().GetClientUrls()
}
// GetLeaderID returns current PD leader's member ID.
func (m *EmbeddedEtcdMember) GetLeaderID() uint64 {
return m.GetLeader().GetMemberId()
}
// GetLeader returns current PD leader of PD cluster.
func (m *EmbeddedEtcdMember) GetLeader() *pdpb.Member {
leader := m.leader.Load()
if leader == nil {
return nil
}
member := leader.(*pdpb.Member)
if member.GetMemberId() == 0 {
return nil
}
return member
}
// setLeader sets the member's PD leader.
func (m *EmbeddedEtcdMember) setLeader(member *pdpb.Member) {
m.leader.Store(member)
}
// unsetLeader unsets the member's PD leader.
func (m *EmbeddedEtcdMember) unsetLeader() {
m.leader.Store(&pdpb.Member{})
}
// EnableLeader sets the member itself to a PD leader.
func (m *EmbeddedEtcdMember) EnableLeader() {
m.setLeader(m.member)
}
// GetLeaderPath returns the path of the PD leader.
func (m *EmbeddedEtcdMember) GetLeaderPath() string {
return path.Join(m.rootPath, "leader")
}
// GetLeadership returns the leadership of the PD member.
func (m *EmbeddedEtcdMember) GetLeadership() *election.Leadership {
return m.leadership
}
// CampaignLeader is used to campaign a PD member's leadership
// and make it become a PD leader.
func (m *EmbeddedEtcdMember) CampaignLeader(leaseTimeout int64) error {
return m.leadership.Campaign(leaseTimeout, m.MemberValue())
}
// KeepLeader is used to keep the PD leader's leadership.
func (m *EmbeddedEtcdMember) KeepLeader(ctx context.Context) {
m.leadership.Keep(ctx)
}
// PreCheckLeader does some pre-check before checking whether or not it's the leader.
func (m *EmbeddedEtcdMember) PreCheckLeader() error {
if m.GetEtcdLeader() == 0 {
return errs.ErrEtcdLeaderNotFound
}
return nil
}
// getPersistentLeader gets the corresponding leader from etcd by given leaderPath (as the key).
func (m *EmbeddedEtcdMember) getPersistentLeader() (*pdpb.Member, int64, error) {
leader := &pdpb.Member{}
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 *EmbeddedEtcdMember) CheckLeader() (ElectionLeader, bool) {
if err := m.PreCheckLeader(); err != nil {
log.Error("failed to pass pre-check, check pd leader later", errs.ZapError(err))
time.Sleep(200 * time.Millisecond)
return nil, true
}
leader, revision, err := m.getPersistentLeader()
if err != nil {
log.Error("getting pd 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 a PD leader, which indicates we may meet something wrong
// in previous CampaignLeader. We should delete the leadership and campaign again.
log.Warn("the pd leader has not changed, delete and campaign again", zap.Stringer("old-pd-leader", leader))
// Delete the leader itself and let others start a new election again.
if err = m.leadership.DeleteLeaderKey(); err != nil {
log.Error("deleting pd 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 &EmbeddedEtcdLeader{
wrapper: m,
member: leader,
revision: revision,
}, false
}
// WatchLeader is used to watch the changes of the leader.
func (m *EmbeddedEtcdMember) WatchLeader(ctx context.Context, leader *pdpb.Member, revision int64) {
m.setLeader(leader)
m.leadership.Watch(ctx, revision)
m.unsetLeader()
}
// ResetLeader is used to reset the PD member's current leadership.
// Basically it will reset the leader lease and unset leader info.
func (m *EmbeddedEtcdMember) ResetLeader() {
m.leadership.Reset()
m.unsetLeader()
}
// CheckPriority checks whether the etcd leader should be moved according to the priority.
func (m *EmbeddedEtcdMember) CheckPriority(ctx context.Context) {
etcdLeader := m.GetEtcdLeader()
if etcdLeader == m.ID() || etcdLeader == 0 {
return
}
myPriority, err := m.GetMemberLeaderPriority(m.ID())
if err != nil {
log.Error("failed to load leader priority", errs.ZapError(err))
return
}
leaderPriority, err := m.GetMemberLeaderPriority(etcdLeader)
if err != nil {
log.Error("failed to load etcd leader priority", errs.ZapError(err))
return
}
if myPriority > leaderPriority {
err := m.MoveEtcdLeader(ctx, etcdLeader, m.ID())
if err != nil {
log.Error("failed to transfer etcd leader", errs.ZapError(err))
} else {
log.Info("transfer etcd leader",
zap.Uint64("from", etcdLeader),
zap.Uint64("to", m.ID()))
}
}
}
// MoveEtcdLeader tries to transfer etcd leader.
func (m *EmbeddedEtcdMember) MoveEtcdLeader(ctx context.Context, old, new uint64) error {
moveCtx, cancel := context.WithTimeout(ctx, moveLeaderTimeout)
defer cancel()
err := m.etcd.Server.MoveLeader(moveCtx, old, new)
if err != nil {
return errs.ErrEtcdMoveLeader.Wrap(err).GenWithStackByCause()
}
return nil
}
// GetEtcdLeader returns the etcd leader ID.
func (m *EmbeddedEtcdMember) GetEtcdLeader() uint64 {
return m.etcd.Server.Lead()
}
// IsSameLeader checks whether a server is the leader itself.
func (m *EmbeddedEtcdMember) IsSameLeader(leader *pdpb.Member) bool {
return leader.GetMemberId() == m.ID()
}
// InitMemberInfo initializes the member info.
func (m *EmbeddedEtcdMember) InitMemberInfo(advertiseClientUrls, advertisePeerUrls, name string, rootPath string) {
leader := &pdpb.Member{
Name: name,
MemberId: m.ID(),
ClientUrls: strings.Split(advertiseClientUrls, ","),
PeerUrls: strings.Split(advertisePeerUrls, ","),
}
data, err := leader.Marshal()
if err != nil {
// can't fail, so panic here.
log.Fatal("marshal pd leader meet error", zap.Stringer("pd-leader", leader), errs.ZapError(errs.ErrMarshalLeader, err))
}
m.member = leader
m.memberValue = string(data)
m.rootPath = rootPath
m.leadership = election.NewLeadership(m.client, m.GetLeaderPath(), "leader election")
log.Info("member joining election", zap.Stringer("member-info", m.member), zap.String("root-path", m.rootPath))
}
// ResignEtcdLeader resigns current PD's etcd leadership. If nextLeader is empty, all
// other pd-servers can campaign.
func (m *EmbeddedEtcdMember) ResignEtcdLeader(ctx context.Context, from string, nextEtcdLeader string) error {
log.Info("try to resign etcd leader to next pd-server", zap.String("from", from), zap.String("to", nextEtcdLeader))
// Determine next etcd leader candidates.
var etcdLeaderIDs []uint64
res, err := etcdutil.ListEtcdMembers(m.client)
if err != nil {
return err
}
// Do nothing when I am the only member of cluster.
if len(res.Members) == 1 && res.Members[0].ID == m.id && nextEtcdLeader == "" {
return nil
}
for _, member := range res.Members {
if (nextEtcdLeader == "" && member.ID != m.id) || (nextEtcdLeader != "" && member.Name == nextEtcdLeader) {
etcdLeaderIDs = append(etcdLeaderIDs, member.GetID())
}
}
if len(etcdLeaderIDs) == 0 {
return errors.New("no valid pd to transfer etcd leader")
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
nextEtcdLeaderID := etcdLeaderIDs[r.Intn(len(etcdLeaderIDs))]
return m.MoveEtcdLeader(ctx, m.ID(), nextEtcdLeaderID)
}
func (m *EmbeddedEtcdMember) getMemberLeaderPriorityPath(id uint64) string {
return path.Join(m.rootPath, fmt.Sprintf("member/%d/leader_priority", id))
}
// GetDCLocationPathPrefix returns the dc-location path prefix of the cluster.
func (m *EmbeddedEtcdMember) GetDCLocationPathPrefix() string {
return path.Join(m.rootPath, dcLocationConfigEtcdPrefix)
}
// GetDCLocationPath returns the dc-location path of a member with the given member ID.
func (m *EmbeddedEtcdMember) GetDCLocationPath(id uint64) string {
return path.Join(m.GetDCLocationPathPrefix(), fmt.Sprint(id))
}
// SetMemberLeaderPriority saves a member's priority to be elected as the etcd leader.
func (m *EmbeddedEtcdMember) SetMemberLeaderPriority(id uint64, priority int) error {
key := m.getMemberLeaderPriorityPath(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 pd leader")
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return nil
}
// DeleteMemberLeaderPriority removes a member's etcd leader priority config.
func (m *EmbeddedEtcdMember) DeleteMemberLeaderPriority(id uint64) error {
key := m.getMemberLeaderPriorityPath(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 pd leader")
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return nil
}
// DeleteMemberDCLocationInfo removes a member's dc-location info.
func (m *EmbeddedEtcdMember) DeleteMemberDCLocationInfo(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 pd leader")
return errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return nil
}
// GetMemberLeaderPriority loads a member's priority to be elected as the etcd leader.
func (m *EmbeddedEtcdMember) GetMemberLeaderPriority(id uint64) (int, error) {
key := m.getMemberLeaderPriorityPath(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
}
func (m *EmbeddedEtcdMember) getMemberBinaryDeployPath(id uint64) string {
return path.Join(m.rootPath, fmt.Sprintf("member/%d/deploy_path", id))
}
// GetMemberDeployPath loads a member's binary deploy path.
func (m *EmbeddedEtcdMember) GetMemberDeployPath(id uint64) (string, error) {
key := m.getMemberBinaryDeployPath(id)
res, err := etcdutil.EtcdKVGet(m.client, key)
if err != nil {
return "", err
}
if len(res.Kvs) == 0 {
return "", errs.ErrEtcdKVGetResponse.FastGenByArgs("no value")
}
return string(res.Kvs[0].Value), nil
}
// SetMemberDeployPath saves a member's binary deploy path.
func (m *EmbeddedEtcdMember) SetMemberDeployPath(id uint64) error {
key := m.getMemberBinaryDeployPath(id)
txn := kv.NewSlowLogTxn(m.client)
execPath, err := os.Executable()
deployPath := filepath.Dir(execPath)
if err != nil {
return errors.WithStack(err)
}
res, err := txn.Then(clientv3.OpPut(key, deployPath)).Commit()
if err != nil {
return errors.WithStack(err)
}
if !res.Succeeded {
return errors.New("failed to save deploy path")
}
return nil
}
func (m *EmbeddedEtcdMember) getMemberGitHashPath(id uint64) string {
return path.Join(m.rootPath, fmt.Sprintf("member/%d/git_hash", id))
}
func (m *EmbeddedEtcdMember) getMemberBinaryVersionPath(id uint64) string {
return path.Join(m.rootPath, fmt.Sprintf("member/%d/binary_version", id))
}
// GetMemberBinaryVersion loads a member's binary version.
func (m *EmbeddedEtcdMember) GetMemberBinaryVersion(id uint64) (string, error) {
key := m.getMemberBinaryVersionPath(id)
res, err := etcdutil.EtcdKVGet(m.client, key)
if err != nil {
return "", err
}
if len(res.Kvs) == 0 {
return "", errs.ErrEtcdKVGetResponse.FastGenByArgs("no value")
}
return string(res.Kvs[0].Value), nil
}
// GetMemberGitHash loads a member's git hash.
func (m *EmbeddedEtcdMember) GetMemberGitHash(id uint64) (string, error) {
key := m.getMemberGitHashPath(id)
res, err := etcdutil.EtcdKVGet(m.client, key)
if err != nil {
return "", err
}
if len(res.Kvs) == 0 {
return "", errs.ErrEtcdKVGetResponse.FastGenByArgs("no value")
}
return string(res.Kvs[0].Value), nil
}
// SetMemberBinaryVersion saves a member's binary version.
func (m *EmbeddedEtcdMember) SetMemberBinaryVersion(id uint64, releaseVersion string) error {
key := m.getMemberBinaryVersionPath(id)
txn := kv.NewSlowLogTxn(m.client)
res, err := txn.Then(clientv3.OpPut(key, releaseVersion)).Commit()
if err != nil {
return errors.WithStack(err)
}
if !res.Succeeded {
return errors.New("failed to save binary version")
}
return nil
}
// SetMemberGitHash saves a member's git hash.
func (m *EmbeddedEtcdMember) SetMemberGitHash(id uint64, gitHash string) error {
key := m.getMemberGitHashPath(id)
txn := kv.NewSlowLogTxn(m.client)
res, err := txn.Then(clientv3.OpPut(key, gitHash)).Commit()
if err != nil {
return errors.WithStack(err)
}
if !res.Succeeded {
return errors.New("failed to save git hash")
}
return nil
}
// Close gracefully shuts down all servers/listeners.
func (m *EmbeddedEtcdMember) Close() {
m.Etcd().Close()
}