From 296a14730cdd0fe55e95096616f396d319808a0d Mon Sep 17 00:00:00 2001 From: JmPotato Date: Wed, 29 Nov 2023 16:54:43 +0800 Subject: [PATCH] Fix the InfoSyncer test Signed-off-by: JmPotato --- pkg/domain/infosync/BUILD.bazel | 3 +- pkg/domain/infosync/info.go | 58 ++++++++++++++++++-------------- pkg/domain/infosync/info_test.go | 41 ---------------------- pkg/domain/infosync/region.go | 1 - 4 files changed, 33 insertions(+), 70 deletions(-) diff --git a/pkg/domain/infosync/BUILD.bazel b/pkg/domain/infosync/BUILD.bazel index 8bb7f3fc036d2..1b20551cf539a 100644 --- a/pkg/domain/infosync/BUILD.bazel +++ b/pkg/domain/infosync/BUILD.bazel @@ -62,7 +62,7 @@ go_test( srcs = ["info_test.go"], embed = [":infosync"], flaky = True, - shard_count = 4, + shard_count = 3, deps = [ "//pkg/ddl/placement", "//pkg/ddl/util", @@ -72,7 +72,6 @@ go_test( "//pkg/util", "@com_github_pingcap_failpoint//:failpoint", "@com_github_stretchr_testify//require", - "@com_github_tikv_pd_client//http", "@io_etcd_go_etcd_tests_v3//integration", "@org_uber_go_goleak//:goleak", ], diff --git a/pkg/domain/infosync/info.go b/pkg/domain/infosync/info.go index 06b0475b2510b..55324a0073880 100644 --- a/pkg/domain/infosync/info.go +++ b/pkg/domain/infosync/info.go @@ -199,10 +199,13 @@ func GlobalInfoSyncerInit( codec tikv.Codec, skipRegisterToDashBoard bool, ) (*InfoSyncer, error) { + if pdHTTPCli != nil { + pdHTTPCli = pdHTTPCli.WithRespHandler(pdResponseHandler) + } is := &InfoSyncer{ etcdCli: etcdCli, - pdHTTPCli: pdHTTPCli.WithRespHandler(pdResponseHandler), unprefixedEtcdCli: unprefixedEtcdCli, + pdHTTPCli: pdHTTPCli, info: getServerInfo(id, serverIDGetter), serverInfoPath: fmt.Sprintf("%s/%s", ServerInformationPath, id), minStartTSPath: fmt.Sprintf("%s/%s", ServerMinStartTSPath, id), @@ -211,11 +214,11 @@ func GlobalInfoSyncerInit( if err != nil { return nil, err } - is.labelRuleManager = initLabelRuleManager(is.pdHTTPCli) - is.placementManager = initPlacementManager(is.pdHTTPCli) - is.scheduleManager = initScheduleManager(is.pdHTTPCli) - is.tiflashReplicaManager = initTiFlashReplicaManager(is.pdHTTPCli, codec) - is.resourceManagerClient = initResourceManagerClient(pdCli) + is.initLabelRuleManager() + is.initPlacementManager() + is.initScheduleManager() + is.initTiFlashReplicaManager(codec) + is.initResourceManagerClient(pdCli) setGlobalInfoSyncer(is) return is, nil } @@ -246,22 +249,24 @@ func (is *InfoSyncer) GetSessionManager() util2.SessionManager { return is.managerMu.SessionManager } -func initLabelRuleManager(pdHTTPCli pdhttp.Client) LabelRuleManager { - if pdHTTPCli == nil { - return &mockLabelManager{labelRules: map[string][]byte{}} +func (is *InfoSyncer) initLabelRuleManager() { + if is.pdHTTPCli == nil { + is.labelRuleManager = &mockLabelManager{labelRules: map[string][]byte{}} + return } - return &PDLabelManager{pdHTTPCli} + is.labelRuleManager = &PDLabelManager{is.pdHTTPCli} } -func initPlacementManager(pdHTTPCli pdhttp.Client) PlacementManager { - if pdHTTPCli == nil { - return &mockPlacementManager{} +func (is *InfoSyncer) initPlacementManager() { + if is.pdHTTPCli == nil { + is.placementManager = &mockPlacementManager{} + return } - return &PDPlacementManager{pdHTTPCli} + is.placementManager = &PDPlacementManager{is.pdHTTPCli} } -func initResourceManagerClient(pdCli pd.Client) (cli pd.ResourceManagerClient) { - cli = pdCli +func (is *InfoSyncer) initResourceManagerClient(pdCli pd.Client) { + var cli pd.ResourceManagerClient = pdCli if pdCli == nil { cli = NewMockResourceManagerClient() } @@ -295,23 +300,24 @@ func initResourceManagerClient(pdCli pd.Client) (cli pd.ResourceManagerClient) { } } }) - return + is.resourceManagerClient = cli } -func initTiFlashReplicaManager(pdHTTPCli pdhttp.Client, codec tikv.Codec) TiFlashReplicaManager { - if pdHTTPCli == nil { - m := mockTiFlashReplicaManagerCtx{tiflashProgressCache: make(map[int64]float64)} - return &m +func (is *InfoSyncer) initTiFlashReplicaManager(codec tikv.Codec) { + if is.pdHTTPCli == nil { + is.tiflashReplicaManager = &mockTiFlashReplicaManagerCtx{tiflashProgressCache: make(map[int64]float64)} + return } logutil.BgLogger().Warn("init TiFlashReplicaManager") - return &TiFlashReplicaManagerCtx{pdHTTPCli: pdHTTPCli, tiflashProgressCache: make(map[int64]float64), codec: codec} + is.tiflashReplicaManager = &TiFlashReplicaManagerCtx{pdHTTPCli: is.pdHTTPCli, tiflashProgressCache: make(map[int64]float64), codec: codec} } -func initScheduleManager(pdHTTPCli pdhttp.Client) ScheduleManager { - if pdHTTPCli == nil { - return &mockScheduleManager{} +func (is *InfoSyncer) initScheduleManager() { + if is.pdHTTPCli == nil { + is.scheduleManager = &mockScheduleManager{} + return } - return &PDScheduleManager{pdHTTPCli} + is.scheduleManager = &PDScheduleManager{is.pdHTTPCli} } // GetMockTiFlash can only be used in tests to get MockTiFlash diff --git a/pkg/domain/infosync/info_test.go b/pkg/domain/infosync/info_test.go index 83d7d8be39de2..957ac2986a018 100644 --- a/pkg/domain/infosync/info_test.go +++ b/pkg/domain/infosync/info_test.go @@ -33,7 +33,6 @@ import ( "github.com/pingcap/tidb/pkg/testkit/testsetup" util2 "github.com/pingcap/tidb/pkg/util" "github.com/stretchr/testify/require" - pd "github.com/tikv/pd/client/http" "go.etcd.io/etcd/tests/v3/integration" "go.uber.org/goleak" ) @@ -280,43 +279,3 @@ func TestTiFlashManager(t *testing.T) { CloseTiFlashManager(ctx) } - -func TestRuleOp(t *testing.T) { - rule := MakeNewRule(1, 2, []string{"a"}) - ruleOp := pd.RuleOp{ - Rule: &rule, - Action: pd.RuleOpAdd, - DeleteByIDPrefix: false, - } - j, err := json.Marshal(&ruleOp) - require.NoError(t, err) - ruleOpExpect := &pd.RuleOp{} - json.Unmarshal(j, ruleOpExpect) - require.Equal(t, ruleOp.Action, ruleOpExpect.Action) - require.Equal(t, *ruleOp.Rule, *ruleOpExpect.Rule) - ruleOps := make([]pd.RuleOp, 0, 2) - for i := 0; i < 10; i += 2 { - rule := MakeNewRule(int64(i), 2, []string{"a"}) - ruleOps = append(ruleOps, pd.RuleOp{ - Rule: &rule, - Action: pd.RuleOpAdd, - DeleteByIDPrefix: false, - }) - } - for i := 1; i < 10; i += 2 { - rule := MakeNewRule(int64(i), 2, []string{"b"}) - ruleOps = append(ruleOps, pd.RuleOp{ - Rule: &rule, - Action: pd.RuleOpDel, - DeleteByIDPrefix: false, - }) - } - j, err = json.Marshal(ruleOps) - require.NoError(t, err) - var ruleOpsExpect []pd.RuleOp - json.Unmarshal(j, &ruleOpsExpect) - for i := 0; i < len(ruleOps); i++ { - require.Equal(t, ruleOps[i].Action, ruleOpsExpect[i].Action) - require.Equal(t, *ruleOps[i].Rule, *ruleOpsExpect[i].Rule) - } -} diff --git a/pkg/domain/infosync/region.go b/pkg/domain/infosync/region.go index 4eb3863d4e059..d31b8e415d1ad 100644 --- a/pkg/domain/infosync/region.go +++ b/pkg/domain/infosync/region.go @@ -59,7 +59,6 @@ func GetReplicationState(ctx context.Context, startKey []byte, endKey []byte) (P return PlacementScheduleStatePending, err } st := PlacementScheduleStatePending - // it should not fail switch state { case "REPLICATED": st = PlacementScheduleStateScheduled