From 125f9c2231bbff4d4e8212d77ba6233b0e3f7368 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:52:05 +0800 Subject: [PATCH] *: Auto refresh PD addrs for `PDPlacementManager`, `PDLabelManager`, `TiFlashPDPlacementManager` (#33909) (#33914) close pingcap/tidb#33908 --- domain/infosync/info.go | 21 ++++++++------------- domain/infosync/label_manager.go | 11 ++++++----- domain/infosync/placement_manager.go | 9 +++++---- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/domain/infosync/info.go b/domain/infosync/info.go index 8aec3c35275e8..dcaf12143126b 100644 --- a/domain/infosync/info.go +++ b/domain/infosync/info.go @@ -181,13 +181,8 @@ func GlobalInfoSyncerInit(ctx context.Context, id string, serverIDGetter func() if err != nil { return nil, err } - if etcdCli != nil { - is.labelRuleManager = initLabelRuleManager(etcdCli.Endpoints()) - is.placementManager = initPlacementManager(etcdCli.Endpoints()) - } else { - is.labelRuleManager = initLabelRuleManager([]string{}) - is.placementManager = initPlacementManager([]string{}) - } + is.labelRuleManager = initLabelRuleManager(etcdCli) + is.placementManager = initPlacementManager(etcdCli) setGlobalInfoSyncer(is) return is, nil } @@ -214,18 +209,18 @@ func (is *InfoSyncer) GetSessionManager() util2.SessionManager { return is.manager } -func initLabelRuleManager(addrs []string) LabelRuleManager { - if len(addrs) == 0 { +func initLabelRuleManager(etcdCli *clientv3.Client) LabelRuleManager { + if etcdCli == nil { return &mockLabelManager{labelRules: map[string][]byte{}} } - return &PDLabelManager{addrs: addrs} + return &PDLabelManager{etcdCli: etcdCli} } -func initPlacementManager(addrs []string) PlacementManager { - if len(addrs) == 0 { +func initPlacementManager(etcdCli *clientv3.Client) PlacementManager { + if etcdCli == nil { return &mockPlacementManager{} } - return &PDPlacementManager{addrs: addrs} + return &PDPlacementManager{etcdCli: etcdCli} } // GetServerInfo gets self server static information. diff --git a/domain/infosync/label_manager.go b/domain/infosync/label_manager.go index 663d3f01976fc..6cbc1683b604d 100644 --- a/domain/infosync/label_manager.go +++ b/domain/infosync/label_manager.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/ddl/label" "github.com/pingcap/tidb/util/pdapi" + "go.etcd.io/etcd/clientv3" ) // LabelRuleManager manages label rules @@ -35,7 +36,7 @@ type LabelRuleManager interface { // PDLabelManager manages rules with pd type PDLabelManager struct { - addrs []string + etcdCli *clientv3.Client } // PutLabelRule implements PutLabelRule @@ -44,7 +45,7 @@ func (lm *PDLabelManager) PutLabelRule(ctx context.Context, rule *label.Rule) er if err != nil { return err } - _, err = doRequest(ctx, lm.addrs, path.Join(pdapi.Config, "region-label", "rule"), "POST", bytes.NewReader(r)) + _, err = doRequest(ctx, lm.etcdCli.Endpoints(), path.Join(pdapi.Config, "region-label", "rule"), "POST", bytes.NewReader(r)) return err } @@ -55,14 +56,14 @@ func (lm *PDLabelManager) UpdateLabelRules(ctx context.Context, patch *label.Rul return err } - _, err = doRequest(ctx, lm.addrs, path.Join(pdapi.Config, "region-label", "rules"), "PATCH", bytes.NewReader(r)) + _, err = doRequest(ctx, lm.etcdCli.Endpoints(), path.Join(pdapi.Config, "region-label", "rules"), "PATCH", bytes.NewReader(r)) return err } // GetAllLabelRules implements GetAllLabelRules func (lm *PDLabelManager) GetAllLabelRules(ctx context.Context) ([]*label.Rule, error) { var rules []*label.Rule - res, err := doRequest(ctx, lm.addrs, path.Join(pdapi.Config, "region-label", "rules"), "GET", nil) + res, err := doRequest(ctx, lm.etcdCli.Endpoints(), path.Join(pdapi.Config, "region-label", "rules"), "GET", nil) if err == nil && res != nil { err = json.Unmarshal(res, &rules) @@ -78,7 +79,7 @@ func (lm *PDLabelManager) GetLabelRules(ctx context.Context, ruleIDs []string) ( } rules := []*label.Rule{} - res, err := doRequest(ctx, lm.addrs, path.Join(pdapi.Config, "region-label", "rules", "ids"), "GET", bytes.NewReader(ids)) + res, err := doRequest(ctx, lm.etcdCli.Endpoints(), path.Join(pdapi.Config, "region-label", "rules", "ids"), "GET", bytes.NewReader(ids)) if err == nil && res != nil { err = json.Unmarshal(res, &rules) diff --git a/domain/infosync/placement_manager.go b/domain/infosync/placement_manager.go index 7c4db7dcd61e3..f37f9e1eacd28 100644 --- a/domain/infosync/placement_manager.go +++ b/domain/infosync/placement_manager.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/util/pdapi" + "go.etcd.io/etcd/clientv3" ) // PlacementManager manages placement settings @@ -37,13 +38,13 @@ type PlacementManager interface { // PDPlacementManager manages placement with pd type PDPlacementManager struct { - addrs []string + etcdCli *clientv3.Client } // GetRuleBundle is used to get one specific rule bundle from PD. func (m *PDPlacementManager) GetRuleBundle(ctx context.Context, name string) (*placement.Bundle, error) { bundle := &placement.Bundle{ID: name} - res, err := doRequest(ctx, m.addrs, path.Join(pdapi.Config, "placement-rule", name), "GET", nil) + res, err := doRequest(ctx, m.etcdCli.Endpoints(), path.Join(pdapi.Config, "placement-rule", name), "GET", nil) if err == nil && res != nil { err = json.Unmarshal(res, bundle) } @@ -53,7 +54,7 @@ func (m *PDPlacementManager) GetRuleBundle(ctx context.Context, name string) (*p // GetAllRuleBundles is used to get all rule bundles from PD. It is used to load full rules from PD while fullload infoschema. func (m *PDPlacementManager) GetAllRuleBundles(ctx context.Context) ([]*placement.Bundle, error) { var bundles []*placement.Bundle - res, err := doRequest(ctx, m.addrs, path.Join(pdapi.Config, "placement-rule"), "GET", nil) + res, err := doRequest(ctx, m.etcdCli.Endpoints(), path.Join(pdapi.Config, "placement-rule"), "GET", nil) if err == nil && res != nil { err = json.Unmarshal(res, &bundles) } @@ -71,7 +72,7 @@ func (m *PDPlacementManager) PutRuleBundles(ctx context.Context, bundles []*plac return err } - _, err = doRequest(ctx, m.addrs, path.Join(pdapi.Config, "placement-rule")+"?partial=true", "POST", bytes.NewReader(b)) + _, err = doRequest(ctx, m.etcdCli.Endpoints(), path.Join(pdapi.Config, "placement-rule")+"?partial=true", "POST", bytes.NewReader(b)) return err }