-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathbase_client.go
456 lines (413 loc) · 12.5 KB
/
base_client.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
// 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"
"fmt"
"reflect"
"sort"
"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
)
// baseClient is a basic client for all other complex client.
type baseClient struct {
urls atomic.Value // Store as []string
clusterID uint64
// PD leader URL
leader atomic.Value // Store as string
// PD follower URLs
followers atomic.Value // Store as []string
// addr -> TSO gRPC connection
clientConns sync.Map // Store as map[string]*grpc.ClientConn
// dc-location -> TSO allocator leader URL
allocators sync.Map // Store as map[string]string
checkLeaderCh chan struct{}
checkTSODispatcherCh chan struct{}
updateConnectionCtxsCh chan struct{}
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
security SecurityOption
// Client option.
option *option
}
// SecurityOption records options about tls
type SecurityOption struct {
CAPath string
CertPath string
KeyPath string
SSLCABytes []byte
SSLCertBytes []byte
SSLKEYBytes []byte
}
// newBaseClient returns a new baseClient.
func newBaseClient(ctx context.Context, urls []string, security SecurityOption) *baseClient {
clientCtx, clientCancel := context.WithCancel(ctx)
bc := &baseClient{
checkLeaderCh: make(chan struct{}, 1),
checkTSODispatcherCh: make(chan struct{}, 1),
updateConnectionCtxsCh: make(chan struct{}, 1),
ctx: clientCtx,
cancel: clientCancel,
security: security,
option: newOption(),
}
bc.urls.Store(urls)
return bc
}
func (c *baseClient) init() error {
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.wg.Add(1)
go c.memberLoop()
return nil
}
func (c *baseClient) 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 *baseClient) memberLoop() {
defer c.wg.Done()
ctx, cancel := context.WithCancel(c.ctx)
defer cancel()
for {
select {
case <-c.checkLeaderCh:
case <-time.After(memberUpdateInterval):
case <-ctx.Done():
return
}
failpoint.Inject("skipUpdateMember", func() {
failpoint.Continue()
})
if err := c.updateMember(); err != nil {
log.Error("[pd] failed updateMember", errs.ZapError(err))
}
}
}
// ScheduleCheckLeader is used to check leader.
func (c *baseClient) ScheduleCheckLeader() {
select {
case c.checkLeaderCh <- struct{}{}:
default:
}
}
func (c *baseClient) scheduleCheckTSODispatcher() {
select {
case c.checkTSODispatcherCh <- struct{}{}:
default:
}
}
func (c *baseClient) scheduleUpdateConnectionCtxs() {
select {
case c.updateConnectionCtxsCh <- struct{}{}:
default:
}
}
// GetClusterID returns the ClusterID.
func (c *baseClient) GetClusterID(context.Context) uint64 {
return c.clusterID
}
// GetLeaderAddr returns the leader address.
func (c *baseClient) GetLeaderAddr() string {
leaderAddr := c.leader.Load()
if leaderAddr == nil {
return ""
}
return leaderAddr.(string)
}
// GetFollowerAddrs returns the follower address.
func (c *baseClient) GetFollowerAddrs() []string {
followerAddrs := c.followers.Load()
if followerAddrs == nil {
return []string{}
}
return followerAddrs.([]string)
}
// GetURLs returns the URLs.
// For testing use. It should only be called when the client is closed.
func (c *baseClient) GetURLs() []string {
return c.urls.Load().([]string)
}
func (c *baseClient) GetAllocatorLeaderURLs() map[string]string {
allocatorLeader := make(map[string]string)
c.allocators.Range(func(dcLocation, url interface{}) bool {
allocatorLeader[dcLocation.(string)] = url.(string)
return true
})
return allocatorLeader
}
func (c *baseClient) getAllocatorLeaderAddrByDCLocation(dcLocation string) (string, bool) {
url, exist := c.allocators.Load(dcLocation)
if !exist {
return "", false
}
return url.(string), true
}
func (c *baseClient) getAllocatorClientConnByDCLocation(dcLocation string) (*grpc.ClientConn, string) {
url, ok := c.allocators.Load(dcLocation)
if !ok {
panic(fmt.Sprintf("the allocator leader in %s should exist", dcLocation))
}
cc, ok := c.clientConns.Load(url)
if !ok {
panic(fmt.Sprintf("the client connection of %s in %s should exist", url, dcLocation))
}
return cc.(*grpc.ClientConn), url.(string)
}
func (c *baseClient) gcAllocatorLeaderAddr(curAllocatorMap map[string]*pdpb.Member) {
// Clean up the old TSO allocators
c.allocators.Range(func(dcLocationKey, _ interface{}) bool {
dcLocation := dcLocationKey.(string)
// Skip the Global TSO Allocator
if dcLocation == globalDCLocation {
return true
}
if _, exist := curAllocatorMap[dcLocation]; !exist {
log.Info("[pd] delete unused tso allocator", zap.String("dc-location", dcLocation))
c.allocators.Delete(dcLocation)
}
return true
})
}
func (c *baseClient) initClusterID() error {
ctx, cancel := context.WithCancel(c.ctx)
defer cancel()
var clusterID uint64
for _, u := range c.GetURLs() {
members, err := c.getMembers(ctx, u, c.option.timeout)
if err != nil || members.GetHeader() == nil {
log.Warn("[pd] failed to get cluster id", zap.String("url", u), 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 *baseClient) updateMember() error {
for i, u := range c.GetURLs() {
failpoint.Inject("skipFirstUpdateMember", func() {
if i == 0 {
failpoint.Continue()
}
})
members, err := c.getMembers(c.ctx, u, 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.switchTSOAllocatorLeader(members.GetTsoAllocatorLeaders())
}
// Failed to get PD leader
if err != nil {
log.Info("[pd] cannot update member from this address",
zap.String("address", u),
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
}
c.scheduleCheckTSODispatcher()
// If `switchLeader` succeeds but `switchTSOAllocatorLeader` has an error,
// the error of `switchTSOAllocatorLeader` will be returned.
return errTSO
}
return errs.ErrClientGetLeader.FastGenByArgs(c.GetURLs())
}
func (c *baseClient) 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 *baseClient) 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() {
c.scheduleUpdateConnectionCtxs()
}
log.Info("[pd] update member urls", zap.Strings("old-urls", oldURLs), zap.Strings("new-urls", urls))
}
func (c *baseClient) 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)
c.allocators.Store(globalDCLocation, addr)
log.Info("[pd] switch leader", zap.String("new-leader", addr), zap.String("old-leader", oldLeader))
return nil
}
func (c *baseClient) 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 *baseClient) switchTSOAllocatorLeader(allocatorMap map[string]*pdpb.Member) error {
if len(allocatorMap) == 0 {
return nil
}
// Switch to the new one
for dcLocation, member := range allocatorMap {
if len(member.GetClientUrls()) == 0 {
continue
}
addr := member.GetClientUrls()[0]
oldAddr, exist := c.getAllocatorLeaderAddrByDCLocation(dcLocation)
if exist && addr == oldAddr {
continue
}
if _, err := c.getOrCreateGRPCConn(addr); err != nil {
log.Warn("[pd] failed to connect dc tso allocator leader",
zap.String("dc-location", dcLocation),
zap.String("leader", addr),
errs.ZapError(err))
return err
}
c.allocators.Store(dcLocation, addr)
log.Info("[pd] switch dc tso allocator leader",
zap.String("dc-location", dcLocation),
zap.String("new-leader", addr),
zap.String("old-leader", oldAddr))
}
// Garbage collection of the old TSO allocator leaders
c.gcAllocatorLeaderAddr(allocatorMap)
return nil
}
func (c *baseClient) getOrCreateGRPCConn(addr string) (*grpc.ClientConn, error) {
conn, ok := c.clientConns.Load(addr)
if ok {
return conn.(*grpc.ClientConn), nil
}
tlsCfg, err := tlsutil.TLSConfig{
CAPath: c.security.CAPath,
CertPath: c.security.CertPath,
KeyPath: c.security.KeyPath,
SSLCABytes: c.security.SSLCABytes,
SSLCertBytes: c.security.SSLCertBytes,
SSLKEYBytes: c.security.SSLKEYBytes,
}.ToTLSConfig()
if err != nil {
return nil, err
}
dCtx, cancel := context.WithTimeout(c.ctx, dialTimeout)
defer cancel()
cc, err := grpcutil.GetClientConn(dCtx, addr, tlsCfg, c.option.gRPCDialOptions...)
if err != nil {
return nil, err
}
if old, ok := c.clientConns.Load(addr); ok {
cc.Close()
log.Debug("use old connection", zap.String("target", cc.Target()), zap.String("state", cc.GetState().String()))
return old.(*grpc.ClientConn), nil
}
c.clientConns.Store(addr, cc)
return cc, nil
}