-
Notifications
You must be signed in to change notification settings - Fork 312
/
pdapi.go
864 lines (742 loc) · 22.9 KB
/
pdapi.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/jeremywohl/flatten"
perrs "github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/utils"
"golang.org/x/mod/semver"
)
// PDClient is an HTTP client of the PD server
type PDClient struct {
version string
addrs []string
tlsEnabled bool
httpClient *utils.HTTPClient
ctx context.Context
}
// LabelInfo represents an instance label info
type LabelInfo struct {
Machine string `json:"machine"`
Port string `json:"port"`
Store uint64 `json:"store"`
Status string `json:"status"`
Leaders int `json:"leaders"`
Regions int `json:"regions"`
Capacity string `json:"capacity"`
Available string `json:"available"`
Labels string `json:"labels"`
}
// NewPDClient returns a new PDClient, the context must have
// a *logprinter.Logger as value of "logger"
func NewPDClient(
ctx context.Context,
addrs []string,
timeout time.Duration,
tlsConfig *tls.Config,
) *PDClient {
enableTLS := false
if tlsConfig != nil {
enableTLS = true
}
if _, ok := ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger); !ok {
panic("the context must have logger inside")
}
cli := &PDClient{
addrs: addrs,
tlsEnabled: enableTLS,
httpClient: utils.NewHTTPClient(timeout, tlsConfig),
ctx: ctx,
}
cli.tryIdentifyVersion()
return cli
}
func (pc *PDClient) l() *logprinter.Logger {
return pc.ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger)
}
func (pc *PDClient) tryIdentifyVersion() {
endpoints := pc.getEndpoints(pdVersionURI)
response := map[string]string{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, &response)
})
if err == nil {
pc.version = response["version"]
}
}
// GetURL builds the the client URL of PDClient
func (pc *PDClient) GetURL(addr string) string {
httpPrefix := "http"
if pc.tlsEnabled {
httpPrefix = "https"
}
return fmt.Sprintf("%s://%s", httpPrefix, addr)
}
const (
// pdEvictLeaderName is evict leader scheduler name.
pdEvictLeaderName = "evict-leader-scheduler"
)
// nolint (some is unused now)
var (
pdPingURI = "pd/ping"
pdVersionURI = "pd/api/v1/version"
pdConfigURI = "pd/api/v1/config"
pdClusterIDURI = "pd/api/v1/cluster"
pdConfigReplicate = "pd/api/v1/config/replicate"
pdReplicationModeURI = "pd/api/v1/config/replication-mode"
pdRulesURI = "pd/api/v1/config/rules"
pdConfigSchedule = "pd/api/v1/config/schedule"
pdLeaderURI = "pd/api/v1/leader"
pdLeaderTransferURI = "pd/api/v1/leader/transfer"
pdMembersURI = "pd/api/v1/members"
pdSchedulersURI = "pd/api/v1/schedulers"
pdStoreURI = "pd/api/v1/store"
pdStoresURI = "pd/api/v1/stores"
pdStoresLimitURI = "pd/api/v1/stores/limit"
pdRegionsCheckURI = "pd/api/v1/regions/check"
)
func tryURLs(endpoints []string, f func(endpoint string) ([]byte, error)) ([]byte, error) {
if len(endpoints) == 0 {
return nil, errors.New("no endpoint available")
}
var err error
var bytes []byte
for _, endpoint := range endpoints {
var u *url.URL
u, err = url.Parse(endpoint)
if err != nil {
return bytes, perrs.AddStack(err)
}
endpoint = u.String()
bytes, err = f(endpoint)
if err != nil {
continue
}
return bytes, nil
}
if len(endpoints) > 1 && err != nil {
err = perrs.Errorf("no endpoint available, the last err was: %s", err)
}
return bytes, err
}
func (pc *PDClient) getEndpoints(uri string) (endpoints []string) {
for _, addr := range pc.addrs {
endpoint := fmt.Sprintf("%s/%s", pc.GetURL(addr), uri)
endpoints = append(endpoints, endpoint)
}
return
}
// CheckHealth checks the health of PD node
func (pc *PDClient) CheckHealth() error {
endpoints := pc.getEndpoints(pdPingURI)
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, nil
})
if err != nil {
return err
}
return nil
}
// GetStores queries the stores info from PD server
func (pc *PDClient) GetStores() (*StoresInfo, error) {
// Return all stores
query := "?state=0&state=1&state=2"
endpoints := pc.getEndpoints(pdStoresURI + query)
storesInfo := StoresInfo{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, &storesInfo)
})
if err != nil {
return nil, err
}
// Desc sorting the store list, we assume the store with largest ID is the
// latest one.
// Not necessary when we implement the workaround pd-3303 in GetCurrentStore()
// sort.Slice(storesInfo.Stores, func(i int, j int) bool {
// return storesInfo.Stores[i].Store.Id > storesInfo.Stores[j].Store.Id
// })
return &storesInfo, nil
}
// GetCurrentStore gets the current store info of a given host
func (pc *PDClient) GetCurrentStore(addr string) (*StoreInfo, error) {
stores, err := pc.GetStores()
if err != nil {
return nil, err
}
// Find the store with largest ID
var latestStore *StoreInfo
for _, store := range stores.Stores {
if store.Store.Address == addr {
// Workaround of pd-3303:
// If the PD leader has been switched multiple times, the store IDs
// may be not monitonically assigned. To workaround this, we iterate
// over the whole store list to see if any of the store's state is
// not marked as "tombstone", then use that as the result.
// See: https://github.com/tikv/pd/issues/3303
//
// It's logically not necessary to find the store with largest ID
// number anymore in this process, but we're keeping the behavior
// as the reasonable approach would still be using the state from
// latest store, and this is only a workaround.
if store.Store.State != metapb.StoreState_Tombstone {
return store, nil
}
if latestStore == nil {
latestStore = store
continue
}
if store.Store.Id > latestStore.Store.Id {
latestStore = store
}
}
}
if latestStore != nil {
return latestStore, nil
}
return nil, &NoStoreErr{addr: addr}
}
// WaitLeader wait until there's a leader or timeout.
func (pc *PDClient) WaitLeader(retryOpt *utils.RetryOption) error {
if retryOpt == nil {
retryOpt = &utils.RetryOption{
Delay: time.Second * 1,
Timeout: time.Second * 30,
}
}
if err := utils.Retry(func() error {
_, err := pc.GetLeader()
if err == nil {
return nil
}
// return error by default, to make the retry work
pc.l().Debugf("Still waitting for the PD leader to be elected")
return perrs.New("still waitting for the PD leader to be elected")
}, *retryOpt); err != nil {
return fmt.Errorf("error getting PD leader, %v", err)
}
return nil
}
// GetLeader queries the leader node of PD cluster
func (pc *PDClient) GetLeader() (*pdpb.Member, error) {
endpoints := pc.getEndpoints(pdLeaderURI)
leader := pdpb.Member{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, &leader)
})
if err != nil {
return nil, err
}
return &leader, nil
}
// GetMembers queries for member list from the PD server
func (pc *PDClient) GetMembers() (*pdpb.GetMembersResponse, error) {
endpoints := pc.getEndpoints(pdMembersURI)
members := pdpb.GetMembersResponse{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, &members)
})
if err != nil {
return nil, err
}
return &members, nil
}
// GetConfig returns all PD configs
func (pc *PDClient) GetConfig() (map[string]interface{}, error) {
endpoints := pc.getEndpoints(pdConfigURI)
// We don't use the `github.com/tikv/pd/server/config` directly because
// there is compatible issue: https://github.com/pingcap/tiup/issues/637
pdConfig := map[string]interface{}{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, &pdConfig)
})
if err != nil {
return nil, err
}
return flatten.Flatten(pdConfig, "", flatten.DotStyle)
}
// GetClusterID return cluster ID
func (pc *PDClient) GetClusterID() (int64, error) {
endpoints := pc.getEndpoints(pdClusterIDURI)
clusterID := map[string]interface{}{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, &clusterID)
})
if err != nil {
return 0, err
}
return int64(clusterID["id"].(float64)), nil
}
// GetDashboardAddress get the PD node address which runs dashboard
func (pc *PDClient) GetDashboardAddress() (string, error) {
cfg, err := pc.GetConfig()
if err != nil {
return "", perrs.AddStack(err)
}
addr, ok := cfg["pd-server.dashboard-address"].(string)
if !ok {
return "", perrs.New("cannot found dashboard address")
}
return addr, nil
}
// EvictPDLeader evicts the PD leader
func (pc *PDClient) EvictPDLeader(retryOpt *utils.RetryOption) error {
// get current members
members, err := pc.GetMembers()
if err != nil {
return err
}
if len(members.Members) == 1 {
pc.l().Warnf("Only 1 member in the PD cluster, skip leader evicting")
return nil
}
// try to evict the leader
cmd := fmt.Sprintf("%s/resign", pdLeaderURI)
endpoints := pc.getEndpoints(cmd)
_, err = tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Post(pc.ctx, endpoint, nil)
if err != nil {
return body, err
}
return body, nil
})
if err != nil {
return err
}
// wait for the transfer to complete
if retryOpt == nil {
retryOpt = &utils.RetryOption{
Delay: time.Second * 5,
Timeout: time.Second * 300,
}
}
if err := utils.Retry(func() error {
currLeader, err := pc.GetLeader()
if err != nil {
return err
}
// check if current leader is the leader to evict
if currLeader.Name != members.Leader.Name {
return nil
}
// return error by default, to make the retry work
pc.l().Debugf("Still waitting for the PD leader to transfer")
return perrs.New("still waitting for the PD leader to transfer")
}, *retryOpt); err != nil {
return fmt.Errorf("error evicting PD leader, %v", err)
}
return nil
}
// pdSchedulerRequest is the request body when evicting store leader
type pdSchedulerRequest struct {
Name string `json:"name"`
StoreID uint64 `json:"store_id"`
}
// EvictStoreLeader evicts the store leaders
// The host parameter should be in format of IP:Port, that matches store's address
func (pc *PDClient) EvictStoreLeader(host string, retryOpt *utils.RetryOption, countLeader func(string) (int, error)) error {
// get info of current stores
latestStore, err := pc.GetCurrentStore(host)
if err != nil {
if errors.Is(err, ErrNoStore) {
return nil
}
return err
}
// XXX: the status address in store will be something like 0.0.0.0:20180
var leaderCount int
if leaderCount, err = countLeader(latestStore.Store.Address); err != nil {
return err
}
if leaderCount == 0 {
// no store leader on the host, just skip
return nil
}
pc.l().Infof("\tEvicting %d leaders from store %s...", leaderCount, latestStore.Store.Address)
// set scheduler for stores
scheduler, err := json.Marshal(pdSchedulerRequest{
Name: pdEvictLeaderName,
StoreID: latestStore.Store.Id,
})
if err != nil {
return nil
}
endpoints := pc.getEndpoints(pdSchedulersURI)
_, err = tryURLs(endpoints, func(endpoint string) ([]byte, error) {
return pc.httpClient.Post(pc.ctx, endpoint, bytes.NewBuffer(scheduler))
})
if err != nil {
return err
}
// wait for the transfer to complete
if retryOpt == nil {
retryOpt = &utils.RetryOption{
Delay: time.Second * 5,
Timeout: time.Second * 600,
}
}
if err := utils.Retry(func() error {
currStore, err := pc.GetCurrentStore(host)
if err != nil {
if errors.Is(err, ErrNoStore) {
return nil
}
return err
}
// check if all leaders are evicted
if leaderCount, err = countLeader(currStore.Store.Address); err != nil {
return err
}
if leaderCount == 0 {
return nil
}
pc.l().Infof(
"\t Still waitting for %d store leaders to transfer...",
leaderCount,
)
// return error by default, to make the retry work
return perrs.New("still waiting for the store leaders to transfer")
}, *retryOpt); err != nil {
return fmt.Errorf("error evicting store leader from %s, %v", host, err)
}
return nil
}
// RemoveStoreEvict removes a store leader evict scheduler, which allows following
// leaders to be transffered to it again.
func (pc *PDClient) RemoveStoreEvict(host string) error {
// get info of current stores
latestStore, err := pc.GetCurrentStore(host)
if err != nil {
return err
}
// remove scheduler for the store
cmd := fmt.Sprintf(
"%s/%s",
pdSchedulersURI,
fmt.Sprintf("%s-%d", pdEvictLeaderName, latestStore.Store.Id),
)
endpoints := pc.getEndpoints(cmd)
logger := pc.l()
_, err = tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, statusCode, err := pc.httpClient.Delete(pc.ctx, endpoint, nil)
if err != nil {
if statusCode == http.StatusNotFound || bytes.Contains(body, []byte("scheduler not found")) {
logger.Debugf("Store leader evicting scheduler does not exist, ignore.")
return body, nil
}
return body, err
}
logger.Debugf("Delete leader evicting scheduler of store %d success", latestStore.Store.Id)
return body, nil
})
if err != nil {
return err
}
logger.Debugf("Removed store leader evicting scheduler from %s.", latestStore.Store.Address)
return nil
}
// DelPD deletes a PD node from the cluster, name is the Name of the PD member
func (pc *PDClient) DelPD(name string, retryOpt *utils.RetryOption) error {
// get current members
members, err := pc.GetMembers()
if err != nil {
return err
}
if len(members.Members) == 1 {
return perrs.New("at least 1 PD node must be online, can not delete")
}
// try to delete the node
cmd := fmt.Sprintf("%s/name/%s", pdMembersURI, name)
endpoints := pc.getEndpoints(cmd)
logger := pc.l()
_, err = tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, statusCode, err := pc.httpClient.Delete(pc.ctx, endpoint, nil)
if err != nil {
if statusCode == http.StatusNotFound || bytes.Contains(body, []byte("not found, pd")) {
logger.Debugf("PD node does not exist, ignore: %s", body)
return body, nil
}
return body, err
}
logger.Debugf("Delete PD %s from the cluster success", name)
return body, nil
})
if err != nil {
return err
}
// wait for the deletion to complete
if retryOpt == nil {
retryOpt = &utils.RetryOption{
Delay: time.Second * 2,
Timeout: time.Second * 60,
}
}
if err := utils.Retry(func() error {
currMembers, err := pc.GetMembers()
if err != nil {
return err
}
// check if the deleted member still present
for _, member := range currMembers.Members {
if member.Name == name {
return perrs.New("still waitting for the PD node to be deleted")
}
}
return nil
}, *retryOpt); err != nil {
return fmt.Errorf("error deleting PD node, %v", err)
}
return nil
}
func (pc *PDClient) isSameState(host string, state metapb.StoreState) (bool, error) {
// get info of current stores
storeInfo, err := pc.GetCurrentStore(host)
if err != nil {
return false, err
}
if storeInfo.Store.State == state {
return true, nil
}
return false, nil
}
// IsTombStone check if the node is Tombstone.
// The host parameter should be in format of IP:Port, that matches store's address
func (pc *PDClient) IsTombStone(host string) (bool, error) {
return pc.isSameState(host, metapb.StoreState_Tombstone)
}
// IsUp check if the node is Up state.
// The host parameter should be in format of IP:Port, that matches store's address
func (pc *PDClient) IsUp(host string) (bool, error) {
return pc.isSameState(host, metapb.StoreState_Up)
}
// DelStore deletes stores from a (TiKV) host
// The host parameter should be in format of IP:Port, that matches store's address
func (pc *PDClient) DelStore(host string, retryOpt *utils.RetryOption) error {
// get info of current stores
storeInfo, err := pc.GetCurrentStore(host)
if err != nil {
if errors.Is(err, ErrNoStore) {
return nil
}
return err
}
// get store ID of host
storeID := storeInfo.Store.Id
cmd := fmt.Sprintf("%s/%d", pdStoreURI, storeID)
endpoints := pc.getEndpoints(cmd)
logger := pc.l()
_, err = tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, statusCode, err := pc.httpClient.Delete(pc.ctx, endpoint, nil)
if err != nil {
if statusCode == http.StatusNotFound || bytes.Contains(body, []byte("not found")) {
logger.Debugf("store %d %s does not exist, ignore: %s", storeID, host, body)
return body, nil
}
return body, err
}
logger.Debugf("Delete store %d %s from the cluster success", storeID, host)
return body, nil
})
if err != nil {
return err
}
// wait for the deletion to complete
if retryOpt == nil {
retryOpt = &utils.RetryOption{
Delay: time.Second * 2,
Timeout: time.Second * 60,
}
}
if err := utils.Retry(func() error {
currStore, err := pc.GetCurrentStore(host)
if err != nil {
// the store does not exist anymore, just ignore and skip
if errors.Is(err, ErrNoStore) {
return nil
}
return err
}
if currStore.Store.Id == storeID {
// deleting a store may take long time to transfer data, so we
// return success once it get to "Offline" status and not waiting
// for the whole process to complete.
// When finished, the store's state will be "Tombstone".
if currStore.Store.State != metapb.StoreState_Up {
return nil
}
return perrs.New("still waiting for the store to be deleted")
}
return nil
}, *retryOpt); err != nil {
return fmt.Errorf("error deleting store, %v", err)
}
return nil
}
func (pc *PDClient) updateConfig(url string, body io.Reader) error {
endpoints := pc.getEndpoints(url)
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
return pc.httpClient.Post(pc.ctx, endpoint, body)
})
return err
}
// UpdateReplicateConfig updates the PD replication config
func (pc *PDClient) UpdateReplicateConfig(body io.Reader) error {
return pc.updateConfig(pdConfigReplicate, body)
}
// GetReplicateConfig gets the PD replication config
func (pc *PDClient) GetReplicateConfig() ([]byte, error) {
endpoints := pc.getEndpoints(pdConfigReplicate)
return tryURLs(endpoints, func(endpoint string) ([]byte, error) {
return pc.httpClient.Get(pc.ctx, endpoint)
})
}
// GetLocationLabels gets the replication.location-labels config from pd server
func (pc *PDClient) GetLocationLabels() ([]string, bool, error) {
config, err := pc.GetReplicateConfig()
if err != nil {
return nil, false, err
}
rc := PDReplicationConfig{}
if err := json.Unmarshal(config, &rc); err != nil {
return nil, false, perrs.Annotatef(err, "unmarshal replication config: %s", string(config))
}
return rc.LocationLabels, rc.EnablePlacementRules, nil
}
// GetTiKVLabels implements TiKVLabelProvider
func (pc *PDClient) GetTiKVLabels() (map[string]map[string]string, []map[string]LabelInfo, error) {
r, err := pc.GetStores()
if err != nil {
return nil, nil, err
}
var storeInfo []map[string]LabelInfo
locationLabels := map[string]map[string]string{}
for _, s := range r.Stores {
if s.Store.State == metapb.StoreState_Up {
lbs := s.Store.GetLabels()
labelsMap := map[string]string{}
var labelsArr []string
for _, lb := range lbs {
// Skip tiflash
if lb.GetKey() != "tiflash" {
labelsArr = append(labelsArr, fmt.Sprintf("%s: %s", lb.GetKey(), lb.GetValue()))
labelsMap[lb.GetKey()] = lb.GetValue()
}
}
locationLabels[s.Store.GetAddress()] = labelsMap
label := fmt.Sprintf("%s%s%s", "{", strings.Join(labelsArr, ","), "}")
storeInfo = append(storeInfo, map[string]LabelInfo{
strings.Split(s.Store.GetAddress(), ":")[0]: {
Machine: strings.Split(s.Store.GetAddress(), ":")[0],
Port: strings.Split(s.Store.GetAddress(), ":")[1],
Store: s.Store.GetId(),
Status: s.Store.State.String(),
Leaders: s.Status.LeaderCount,
Regions: s.Status.RegionCount,
Capacity: s.Status.Capacity.MarshalString(),
Available: s.Status.Available.MarshalString(),
Labels: label,
},
})
}
}
return locationLabels, storeInfo, nil
}
// UpdateScheduleConfig updates the PD schedule config
func (pc *PDClient) UpdateScheduleConfig(body io.Reader) error {
return pc.updateConfig(pdConfigSchedule, body)
}
// CheckRegion queries for the region with specific status
func (pc *PDClient) CheckRegion(state string) (*RegionsInfo, error) {
uri := pdRegionsCheckURI + "/" + state
endpoints := pc.getEndpoints(uri)
regionsInfo := RegionsInfo{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(pc.ctx, endpoint)
if err != nil {
return body, err
}
return body, json.Unmarshal(body, ®ionsInfo)
})
return ®ionsInfo, err
}
// SetReplicationConfig sets a config key value of PD replication, it has the
// same effect as `pd-ctl config set key value`
func (pc *PDClient) SetReplicationConfig(key string, value int) error {
// Only support for pd version >= v4.0.0
if pc.version == "" || semver.Compare(pc.version, "v4.0.0") < 0 {
return nil
}
data := map[string]interface{}{"set": map[string]interface{}{key: value}}
body, err := json.Marshal(data)
if err != nil {
return err
}
pc.l().Debugf("setting replication config: %s=%d", key, value)
return pc.updateConfig(pdReplicationModeURI, bytes.NewBuffer(body))
}
// SetAllStoreLimits sets store for all stores and types, it has the same effect
// as `pd-ctl store limit all value`
func (pc *PDClient) SetAllStoreLimits(value int) error {
// Only support for pd version >= v4.0.0
if pc.version == "" || semver.Compare(pc.version, "v4.0.0") < 0 {
return nil
}
data := map[string]interface{}{"rate": value}
body, err := json.Marshal(data)
if err != nil {
return err
}
pc.l().Debugf("setting store limit: %d", value)
return pc.updateConfig(pdStoresLimitURI, bytes.NewBuffer(body))
}