Skip to content

Commit

Permalink
Fix the InfoSyncer test
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Nov 29, 2023
1 parent 72f3c32 commit 296a147
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 70 deletions.
3 changes: 1 addition & 2 deletions pkg/domain/infosync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
Expand Down
58 changes: 32 additions & 26 deletions pkg/domain/infosync/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down
41 changes: 0 additions & 41 deletions pkg/domain/infosync/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
1 change: 0 additions & 1 deletion pkg/domain/infosync/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 296a147

Please sign in to comment.