-
Notifications
You must be signed in to change notification settings - Fork 726
/
pd_service_discovery.go
586 lines (524 loc) · 18.8 KB
/
pd_service_discovery.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2019 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 pd
import (
"context"
"reflect"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/grpcutil"
"github.com/tikv/pd/client/tlsutil"
"go.uber.org/zap"
"google.golang.org/grpc"
)
const (
globalDCLocation = "global"
memberUpdateInterval = time.Minute
serviceModeUpdateInterval = 3 * time.Second
updateMemberTimeout = time.Second // Use a shorter timeout to recover faster from network isolation.
)
// ServiceDiscovery defines the general interface for service discovery on a quorum-based cluster
// or a primary/secondary configured cluster.
type ServiceDiscovery interface {
// Init initialize the concrete client underlying
Init() error
// Close releases all resources
Close()
// GetClusterID returns the ID of the cluster
GetClusterID() uint64
// GetURLs returns the URLs of the servers.
GetURLs() []string
// GetServingEndpointClientConn returns the grpc client connection of the serving endpoint
// which is the leader in a quorum-based cluster or the primary in a primary/secondary
// configured cluster.
GetServingEndpointClientConn() *grpc.ClientConn
// GetClientConns returns the mapping {addr -> a gRPC connection}
GetClientConns() *sync.Map
// GetServingAddr returns the serving endpoint which is the leader in a quorum-based cluster
// or the primary in a primary/secondary configured cluster.
GetServingAddr() string
// GetBackupAddrs gets the addresses of the current reachable and healthy backup service
// endpoints randomly. Backup service endpoints are followers in a quorum-based cluster or
// secondaries in a primary/secondary configured cluster.
GetBackupAddrs() []string
// GetOrCreateGRPCConn returns the corresponding grpc client connection of the given addr
GetOrCreateGRPCConn(addr string) (*grpc.ClientConn, error)
// ScheduleCheckMemberChanged is used to trigger a check to see if there is any membership change
// among the leader/followers in a quorum-based cluster or among the primary/secondaries in a
// primary/secondary configured cluster.
ScheduleCheckMemberChanged()
// CheckMemberChanged immediately check if there is any membership change among the leader/followers
// in a quorum-based cluster or among the primary/secondaries in a primary/secondary configured cluster.
CheckMemberChanged() error
// AddServingAddrSwitchedCallback adds callbacks which will be called when the leader
// in a quorum-based cluster or the primary in a primary/secondary configured cluster
// is switched.
AddServingAddrSwitchedCallback(callbacks ...func())
// AddServiceAddrsSwitchedCallback adds callbacks which will be called when any leader/follower
// in a quorum-based cluster or any primary/secondary in a primary/secondary configured cluster
// is changed.
AddServiceAddrsSwitchedCallback(callbacks ...func())
}
type tsoLocalServAddrsUpdatedFunc func(map[string]string) error
type tsoGlobalServAddrUpdatedFunc func(string) error
type tsoAllocatorEventSource interface {
// SetTSOLocalServAddrsUpdatedCallback adds a callback which will be called when the local tso
// allocator leader list is updated.
SetTSOLocalServAddrsUpdatedCallback(callback tsoLocalServAddrsUpdatedFunc)
// SetTSOGlobalServAddrUpdatedCallback adds a callback which will be called when the global tso
// allocator leader is updated.
SetTSOGlobalServAddrUpdatedCallback(callback tsoGlobalServAddrUpdatedFunc)
}
var _ ServiceDiscovery = (*pdServiceDiscovery)(nil)
var _ tsoAllocatorEventSource = (*pdServiceDiscovery)(nil)
// pdServiceDiscovery is the service discovery client of PD/API service which is quorum based
type pdServiceDiscovery struct {
isInitialized bool
urls atomic.Value // Store as []string
// PD leader URL
leader atomic.Value // Store as string
// PD follower URLs
followers atomic.Value // Store as []string
clusterID uint64
// addr -> a gRPC connection
clientConns sync.Map // Store as map[string]*grpc.ClientConn
// serviceModeUpdateCb will be called when the service mode gets updated
serviceModeUpdateCb func(pdpb.ServiceMode)
// leaderSwitchedCbs will be called after the leader switched
leaderSwitchedCbs []func()
// membersChangedCbs will be called after there is any membership change in the
// leader and followers
membersChangedCbs []func()
// tsoLocalAllocLeadersUpdatedCb will be called when the local tso allocator
// leader list is updated. The input is a map {DC Location -> Leader Addr}
tsoLocalAllocLeadersUpdatedCb tsoLocalServAddrsUpdatedFunc
// tsoGlobalAllocLeaderUpdatedCb will be called when the global tso allocator
// leader is updated.
tsoGlobalAllocLeaderUpdatedCb tsoGlobalServAddrUpdatedFunc
checkMembershipCh chan struct{}
wg *sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
closeOnce sync.Once
tlsCfg *tlsutil.TLSConfig
// Client option.
option *option
}
// newPDServiceDiscovery returns a new PD service discovery-based client.
func newPDServiceDiscovery(
ctx context.Context, cancel context.CancelFunc,
wg *sync.WaitGroup,
serviceModeUpdateCb func(pdpb.ServiceMode),
urls []string, tlsCfg *tlsutil.TLSConfig, option *option,
) *pdServiceDiscovery {
pdsd := &pdServiceDiscovery{
checkMembershipCh: make(chan struct{}, 1),
ctx: ctx,
cancel: cancel,
wg: wg,
serviceModeUpdateCb: serviceModeUpdateCb,
tlsCfg: tlsCfg,
option: option,
}
pdsd.urls.Store(urls)
return pdsd
}
func (c *pdServiceDiscovery) Init() error {
if c.isInitialized {
return nil
}
if err := c.initRetry(c.initClusterID); err != nil {
c.cancel()
return err
}
if err := c.initRetry(c.updateMember); err != nil {
c.cancel()
return err
}
log.Info("[pd] init cluster id", zap.Uint64("cluster-id", c.clusterID))
c.updateServiceMode()
c.wg.Add(2)
go c.updateMemberLoop()
go c.updateServiceModeLoop()
c.isInitialized = true
return nil
}
func (c *pdServiceDiscovery) initRetry(f func() error) error {
var err error
for i := 0; i < c.option.maxRetryTimes; i++ {
if err = f(); err == nil {
return nil
}
select {
case <-c.ctx.Done():
return err
case <-time.After(time.Second):
}
}
return errors.WithStack(err)
}
func (c *pdServiceDiscovery) updateMemberLoop() {
defer c.wg.Done()
ctx, cancel := context.WithCancel(c.ctx)
defer cancel()
ticker := time.NewTicker(memberUpdateInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
case <-c.checkMembershipCh:
}
failpoint.Inject("skipUpdateMember", func() {
failpoint.Continue()
})
if err := c.updateMember(); err != nil {
log.Error("[pd] failed to update member", zap.Strings("urls", c.GetURLs()), errs.ZapError(err))
}
}
}
func (c *pdServiceDiscovery) updateServiceModeLoop() {
failpoint.Inject("skipUpdateServiceMode", func() {
failpoint.Return()
})
defer c.wg.Done()
ctx, cancel := context.WithCancel(c.ctx)
defer cancel()
ticker := time.NewTicker(serviceModeUpdateInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
c.updateServiceMode()
}
}
// Close releases all resources.
func (c *pdServiceDiscovery) Close() {
c.closeOnce.Do(func() {
log.Info("[pd] close pd service discovery client")
c.clientConns.Range(func(key, cc interface{}) bool {
if err := cc.(*grpc.ClientConn).Close(); err != nil {
log.Error("[pd] failed to close grpc clientConn", errs.ZapError(errs.ErrCloseGRPCConn, err))
}
c.clientConns.Delete(key)
return true
})
})
}
// GetClusterID returns the ClusterID.
func (c *pdServiceDiscovery) GetClusterID() uint64 {
return c.clusterID
}
// GetURLs returns the URLs of the servers.
// For testing use. It should only be called when the client is closed.
func (c *pdServiceDiscovery) GetURLs() []string {
return c.urls.Load().([]string)
}
// GetServingAddr returns the grpc client connection of the serving endpoint
// which is the leader in a quorum-based cluster or the primary in a primary/secondary
// configured cluster.
func (c *pdServiceDiscovery) GetServingEndpointClientConn() *grpc.ClientConn {
if cc, ok := c.clientConns.Load(c.getLeaderAddr()); ok {
return cc.(*grpc.ClientConn)
}
return nil
}
// GetClientConns returns the mapping {addr -> a gRPC connection}
func (c *pdServiceDiscovery) GetClientConns() *sync.Map {
return &c.clientConns
}
// GetServingAddr returns the leader address
func (c *pdServiceDiscovery) GetServingAddr() string {
return c.getLeaderAddr()
}
// GetBackupAddrs gets the addresses of the current reachable and healthy followers
// in a quorum-based cluster.
func (c *pdServiceDiscovery) GetBackupAddrs() []string {
return c.getFollowerAddrs()
}
// ScheduleCheckMemberChanged is used to check if there is any membership
// change among the leader and the followers.
func (c *pdServiceDiscovery) ScheduleCheckMemberChanged() {
select {
case c.checkMembershipCh <- struct{}{}:
default:
}
}
// Immediately check if there is any membership change among the leader/followers in a
// quorum-based cluster or among the primary/secondaries in a primary/secondary configured cluster.
func (c *pdServiceDiscovery) CheckMemberChanged() error {
return c.updateMember()
}
// AddServingAddrSwitchedCallback adds callbacks which will be called
// when the leader is switched.
func (c *pdServiceDiscovery) AddServingAddrSwitchedCallback(callbacks ...func()) {
c.leaderSwitchedCbs = append(c.leaderSwitchedCbs, callbacks...)
}
// AddServiceAddrsSwitchedCallback adds callbacks which will be called when
// any leader/follower is changed.
func (c *pdServiceDiscovery) AddServiceAddrsSwitchedCallback(callbacks ...func()) {
c.membersChangedCbs = append(c.membersChangedCbs, callbacks...)
}
// SetTSOLocalServAddrsUpdatedCallback adds a callback which will be called when the local tso
// allocator leader list is updated.
func (c *pdServiceDiscovery) SetTSOLocalServAddrsUpdatedCallback(callback tsoLocalServAddrsUpdatedFunc) {
c.tsoLocalAllocLeadersUpdatedCb = callback
}
// SetTSOGlobalServAddrUpdatedCallback adds a callback which will be called when the global tso
// allocator leader is updated.
func (c *pdServiceDiscovery) SetTSOGlobalServAddrUpdatedCallback(callback tsoGlobalServAddrUpdatedFunc) {
addr := c.getLeaderAddr()
if len(addr) > 0 {
callback(addr)
}
c.tsoGlobalAllocLeaderUpdatedCb = callback
}
// getLeaderAddr returns the leader address.
func (c *pdServiceDiscovery) getLeaderAddr() string {
leaderAddr := c.leader.Load()
if leaderAddr == nil {
return ""
}
return leaderAddr.(string)
}
// getFollowerAddrs returns the follower address.
func (c *pdServiceDiscovery) getFollowerAddrs() []string {
followerAddrs := c.followers.Load()
if followerAddrs == nil {
return []string{}
}
return followerAddrs.([]string)
}
func (c *pdServiceDiscovery) initClusterID() error {
ctx, cancel := context.WithCancel(c.ctx)
defer cancel()
clusterID := uint64(0)
for _, url := range c.GetURLs() {
members, err := c.getMembers(ctx, url, c.option.timeout)
if err != nil || members.GetHeader() == nil {
log.Warn("[pd] failed to get cluster id", zap.String("url", url), errs.ZapError(err))
continue
}
if clusterID == 0 {
clusterID = members.GetHeader().GetClusterId()
continue
}
failpoint.Inject("skipClusterIDCheck", func() {
failpoint.Continue()
})
// All URLs passed in should have the same cluster ID.
if members.GetHeader().GetClusterId() != clusterID {
return errors.WithStack(errUnmatchedClusterID)
}
}
// Failed to init the cluster ID.
if clusterID == 0 {
return errors.WithStack(errFailInitClusterID)
}
c.clusterID = clusterID
return nil
}
func (c *pdServiceDiscovery) updateServiceMode() {
leaderAddr := c.getLeaderAddr()
if len(leaderAddr) > 0 {
clusterInfo, err := c.getClusterInfo(c.ctx, leaderAddr, c.option.timeout)
// If the method is not supported, we set it to pd mode.
if err != nil {
// TODO: it's a hack way to solve the compatibility issue.
// we need to remove this after all maintained version supports the method.
if strings.Contains(err.Error(), "Unimplemented") {
c.serviceModeUpdateCb(pdpb.ServiceMode_PD_SVC_MODE)
} else {
log.Warn("[pd] failed to get cluster info for the leader", zap.String("leader-addr", leaderAddr), errs.ZapError(err))
}
return
}
c.serviceModeUpdateCb(clusterInfo.ServiceModes[0])
} else {
log.Warn("[pd] no leader found")
}
}
func (c *pdServiceDiscovery) updateMember() error {
for i, url := range c.GetURLs() {
failpoint.Inject("skipFirstUpdateMember", func() {
if i == 0 {
failpoint.Continue()
}
})
members, err := c.getMembers(c.ctx, url, updateMemberTimeout)
// Check the cluster ID.
if err == nil && members.GetHeader().GetClusterId() != c.clusterID {
err = errs.ErrClientUpdateMember.FastGenByArgs("cluster id does not match")
}
// Check the TSO Allocator Leader.
var errTSO error
if err == nil {
if members.GetLeader() == nil || len(members.GetLeader().GetClientUrls()) == 0 {
err = errs.ErrClientGetLeader.FastGenByArgs("leader address don't exist")
}
// Still need to update TsoAllocatorLeaders, even if there is no PD leader
errTSO = c.switchTSOAllocatorLeaders(members.GetTsoAllocatorLeaders())
}
// Failed to get members
if err != nil {
log.Info("[pd] cannot update member from this address",
zap.String("address", url),
errs.ZapError(err))
select {
case <-c.ctx.Done():
return errors.WithStack(err)
default:
continue
}
}
c.updateURLs(members.GetMembers())
c.updateFollowers(members.GetMembers(), members.GetLeader())
if err := c.switchLeader(members.GetLeader().GetClientUrls()); err != nil {
return err
}
// If `switchLeader` succeeds but `switchTSOAllocatorLeader` has an error,
// the error of `switchTSOAllocatorLeader` will be returned.
return errTSO
}
return errs.ErrClientGetMember.FastGenByArgs()
}
func (c *pdServiceDiscovery) getClusterInfo(ctx context.Context, url string, timeout time.Duration) (*pdpb.GetClusterInfoResponse, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
cc, err := c.GetOrCreateGRPCConn(url)
if err != nil {
return nil, err
}
clusterInfo, err := pdpb.NewPDClient(cc).GetClusterInfo(ctx, &pdpb.GetClusterInfoRequest{})
if err != nil {
attachErr := errors.Errorf("error:%s target:%s status:%s", err, cc.Target(), cc.GetState().String())
return nil, errs.ErrClientGetClusterInfo.Wrap(attachErr).GenWithStackByCause()
}
if clusterInfo.GetHeader().GetError() != nil {
attachErr := errors.Errorf("error:%s target:%s status:%s", clusterInfo.GetHeader().GetError().String(), cc.Target(), cc.GetState().String())
return nil, errs.ErrClientGetClusterInfo.Wrap(attachErr).GenWithStackByCause()
}
return clusterInfo, nil
}
func (c *pdServiceDiscovery) getMembers(ctx context.Context, url string, timeout time.Duration) (*pdpb.GetMembersResponse, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
cc, err := c.GetOrCreateGRPCConn(url)
if err != nil {
return nil, err
}
members, err := pdpb.NewPDClient(cc).GetMembers(ctx, &pdpb.GetMembersRequest{})
if err != nil {
attachErr := errors.Errorf("error:%s target:%s status:%s", err, cc.Target(), cc.GetState().String())
return nil, errs.ErrClientGetMember.Wrap(attachErr).GenWithStackByCause()
}
if members.GetHeader().GetError() != nil {
attachErr := errors.Errorf("error:%s target:%s status:%s", members.GetHeader().GetError().String(), cc.Target(), cc.GetState().String())
return nil, errs.ErrClientGetMember.Wrap(attachErr).GenWithStackByCause()
}
return members, nil
}
func (c *pdServiceDiscovery) updateURLs(members []*pdpb.Member) {
urls := make([]string, 0, len(members))
for _, m := range members {
urls = append(urls, m.GetClientUrls()...)
}
sort.Strings(urls)
oldURLs := c.GetURLs()
// the url list is same.
if reflect.DeepEqual(oldURLs, urls) {
return
}
c.urls.Store(urls)
// Update the connection contexts when member changes if TSO Follower Proxy is enabled.
if c.option.getEnableTSOFollowerProxy() {
// Run callbacks to reflect the membership changes in the leader and followers.
for _, cb := range c.membersChangedCbs {
cb()
}
}
log.Info("[pd] update member urls", zap.Strings("old-urls", oldURLs), zap.Strings("new-urls", urls))
}
func (c *pdServiceDiscovery) switchLeader(addrs []string) error {
// FIXME: How to safely compare leader urls? For now, only allows one client url.
addr := addrs[0]
oldLeader := c.getLeaderAddr()
if addr == oldLeader {
return nil
}
if _, err := c.GetOrCreateGRPCConn(addr); err != nil {
log.Warn("[pd] failed to connect leader", zap.String("leader", addr), errs.ZapError(err))
return err
}
// Set PD leader and Global TSO Allocator (which is also the PD leader)
c.leader.Store(addr)
// Run callbacks
if c.tsoGlobalAllocLeaderUpdatedCb != nil {
if err := c.tsoGlobalAllocLeaderUpdatedCb(addr); err != nil {
return err
}
}
for _, cb := range c.leaderSwitchedCbs {
cb()
}
log.Info("[pd] switch leader", zap.String("new-leader", addr), zap.String("old-leader", oldLeader))
return nil
}
func (c *pdServiceDiscovery) updateFollowers(members []*pdpb.Member, leader *pdpb.Member) {
var addrs []string
for _, member := range members {
if member.GetMemberId() != leader.GetMemberId() {
if len(member.GetClientUrls()) > 0 {
addrs = append(addrs, member.GetClientUrls()...)
}
}
}
c.followers.Store(addrs)
}
func (c *pdServiceDiscovery) switchTSOAllocatorLeaders(allocatorMap map[string]*pdpb.Member) error {
if len(allocatorMap) == 0 {
return nil
}
allocMap := make(map[string]string)
// Switch to the new one
for dcLocation, member := range allocatorMap {
if len(member.GetClientUrls()) == 0 {
continue
}
allocMap[dcLocation] = member.GetClientUrls()[0]
}
// Run the callback to reflect any possible change in the local tso allocators.
if c.tsoLocalAllocLeadersUpdatedCb != nil {
if err := c.tsoLocalAllocLeadersUpdatedCb(allocMap); err != nil {
return err
}
}
return nil
}
// GetOrCreateGRPCConn returns the corresponding grpc client connection of the given addr
func (c *pdServiceDiscovery) GetOrCreateGRPCConn(addr string) (*grpc.ClientConn, error) {
return grpcutil.GetOrCreateGRPCConn(c.ctx, &c.clientConns, addr, c.tlsCfg, c.option.gRPCDialOptions...)
}