diff --git a/.gitignore b/.gitignore index 9a29cf478f7c7..d65e28d5799f1 100644 --- a/.gitignore +++ b/.gitignore @@ -30,8 +30,8 @@ export-20*/ # Files generated when testing out /_bazel -bazel-bin -bazel-out -bazel-testlogs -bazel-tidb-cse +/bazel-bin +/bazel-out +/bazel-testlogs +/bazel-tidb* .ijwb/ diff --git a/ddl/BUILD.bazel b/ddl/BUILD.bazel index 803b066378a56..83f62db8e256e 100644 --- a/ddl/BUILD.bazel +++ b/ddl/BUILD.bazel @@ -207,6 +207,7 @@ go_test( "//errno", "//executor", "//infoschema", + "//keyspace", "//kv", "//meta", "//meta/autoid", diff --git a/ddl/attributes_sql_test.go b/ddl/attributes_sql_test.go index de00a63a7a661..f9ae008122627 100644 --- a/ddl/attributes_sql_test.go +++ b/ddl/attributes_sql_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/domain/infosync" + "github.com/pingcap/tidb/keyspace" "github.com/pingcap/tidb/store/gcworker" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/util/gcutil" @@ -269,7 +270,7 @@ PARTITION BY RANGE (c) ( func TestFlashbackTable(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") @@ -327,7 +328,7 @@ PARTITION BY RANGE (c) ( func TestDropTable(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") @@ -380,7 +381,7 @@ PARTITION BY RANGE (c) ( func TestCreateWithSameName(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") @@ -444,7 +445,7 @@ PARTITION BY RANGE (c) ( func TestPartition(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") @@ -504,7 +505,7 @@ PARTITION BY RANGE (c) ( func TestDropSchema(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") @@ -530,7 +531,7 @@ PARTITION BY RANGE (c) ( func TestDefaultKeyword(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") diff --git a/ddl/ddl_tiflash_api.go b/ddl/ddl_tiflash_api.go index 7e456815acd8c..bf3d21d040c27 100644 --- a/ddl/ddl_tiflash_api.go +++ b/ddl/ddl_tiflash_api.go @@ -355,22 +355,6 @@ func updateTiFlashStores(pollTiFlashContext *TiFlashManagementContext) error { return nil } -func getTiFlashPeerWithoutLagCount(pollTiFlashContext *TiFlashManagementContext, tableID int64) (int, error) { - // storeIDs -> regionID, PD will not create two peer on the same store - var flashPeerCount int - for _, store := range pollTiFlashContext.TiFlashStores { - regionReplica := make(map[int64]int) - err := helper.CollectTiFlashStatus(store.Store.StatusAddress, tableID, ®ionReplica) - if err != nil { - logutil.BgLogger().Error("Fail to get peer status from TiFlash.", - zap.Int64("tableID", tableID)) - return 0, err - } - flashPeerCount += len(regionReplica) - } - return flashPeerCount, nil -} - func pollAvailableTableProgress(schemas infoschema.InfoSchema, ctx sessionctx.Context, pollTiFlashContext *TiFlashManagementContext) { pollMaxCount := RefreshProgressMaxTableCount failpoint.Inject("PollAvailableTableProgressMaxCount", func(val failpoint.Value) { diff --git a/ddl/main_test.go b/ddl/main_test.go index 3418d16a23ece..6d16fcd8dce54 100644 --- a/ddl/main_test.go +++ b/ddl/main_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/domain/infosync" + "github.com/pingcap/tidb/keyspace" "github.com/pingcap/tidb/meta/autoid" "github.com/pingcap/tidb/testkit/testsetup" "github.com/tikv/client-go/v2/tikv" @@ -52,7 +53,7 @@ func TestMain(m *testing.M) { conf.Experimental.AllowsExpressionIndex = true }) - _, err := infosync.GlobalInfoSyncerInit(context.Background(), "t", func() uint64 { return 1 }, nil, true) + _, err := infosync.GlobalInfoSyncerInit(context.Background(), "t", func() uint64 { return 1 }, nil, keyspace.CodecV1, true) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "ddl: infosync.GlobalInfoSyncerInit: %v\n", err) os.Exit(1) diff --git a/domain/BUILD.bazel b/domain/BUILD.bazel index f0f72530fe1ad..a523777d781de 100644 --- a/domain/BUILD.bazel +++ b/domain/BUILD.bazel @@ -103,6 +103,7 @@ go_test( "//ddl", "//domain/infosync", "//errno", + "//keyspace", "//kv", "//metrics", "//parser/ast", diff --git a/domain/db_test.go b/domain/db_test.go index ff6e6b625788a..2eb714849f7ce 100644 --- a/domain/db_test.go +++ b/domain/db_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain/infosync" + "github.com/pingcap/tidb/keyspace" "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore" @@ -73,7 +74,7 @@ func TestNormalSessionPool(t *testing.T) { domain, err := session.BootstrapSession(store) require.NoError(t, err) defer domain.Close() - info, err1 := infosync.GlobalInfoSyncerInit(context.Background(), "t", func() uint64 { return 1 }, nil, true) + info, err1 := infosync.GlobalInfoSyncerInit(context.Background(), "t", func() uint64 { return 1 }, nil, keyspace.CodecV1, true) require.NoError(t, err1) conf := config.GetGlobalConfig() conf.Socket = "" @@ -107,7 +108,7 @@ func TestAbnormalSessionPool(t *testing.T) { domain, err := session.BootstrapSession(store) require.NoError(t, err) defer domain.Close() - info, err1 := infosync.GlobalInfoSyncerInit(context.Background(), "t", func() uint64 { return 1 }, nil, true) + info, err1 := infosync.GlobalInfoSyncerInit(context.Background(), "t", func() uint64 { return 1 }, nil, keyspace.CodecV1, true) require.NoError(t, err1) conf := config.GetGlobalConfig() conf.Socket = "" diff --git a/domain/domain.go b/domain/domain.go index 55ee015b1fb26..f896e1179fa65 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -1013,7 +1013,13 @@ func (do *Domain) Init( // step 1: prepare the info/schema syncer which domain reload needed. skipRegisterToDashboard := config.GetGlobalConfig().SkipRegisterToDashboard - do.info, err = infosync.GlobalInfoSyncerInit(ctx, do.ddl.GetID(), do.ServerID, do.etcdClient, skipRegisterToDashboard) + do.info, err = infosync.GlobalInfoSyncerInit(ctx, + do.ddl.GetID(), + do.ServerID, + do.etcdClient, + do.Store().GetCodec(), + skipRegisterToDashboard, + ) if err != nil { return err } diff --git a/domain/infosync/BUILD.bazel b/domain/infosync/BUILD.bazel index c0936390ff016..10310f360899d 100644 --- a/domain/infosync/BUILD.bazel +++ b/domain/infosync/BUILD.bazel @@ -19,6 +19,7 @@ go_library( "//ddl/placement", "//ddl/util", "//errno", + "//keyspace", "//kv", "//metrics", "//parser/model", @@ -40,8 +41,10 @@ go_library( "@com_github_gorilla_mux//:mux", "@com_github_pingcap_errors//:errors", "@com_github_pingcap_failpoint//:failpoint", + "@com_github_pingcap_kvproto//pkg/kvrpcpb", "@com_github_pingcap_kvproto//pkg/metapb", "@com_github_tikv_client_go_v2//oracle", + "@com_github_tikv_client_go_v2//tikv", "@com_github_tikv_pd_client//:client", "@io_etcd_go_etcd_client_v3//:client", "@io_etcd_go_etcd_client_v3//concurrency", @@ -59,6 +62,7 @@ go_test( deps = [ "//ddl/placement", "//ddl/util", + "//keyspace", "//parser/model", "//testkit/testsetup", "//util", diff --git a/domain/infosync/info.go b/domain/infosync/info.go index ca66f691892fc..9b440bc8eecf4 100644 --- a/domain/infosync/info.go +++ b/domain/infosync/info.go @@ -54,6 +54,7 @@ import ( "github.com/pingcap/tidb/util/pdapi" "github.com/pingcap/tidb/util/versioninfo" "github.com/tikv/client-go/v2/oracle" + "github.com/tikv/client-go/v2/tikv" pd "github.com/tikv/pd/client" clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/client/v3/concurrency" @@ -178,7 +179,12 @@ func setGlobalInfoSyncer(is *InfoSyncer) { } // GlobalInfoSyncerInit return a new InfoSyncer. It is exported for testing. -func GlobalInfoSyncerInit(ctx context.Context, id string, serverIDGetter func() uint64, etcdCli *clientv3.Client, skipRegisterToDashBoard bool) (*InfoSyncer, error) { +func GlobalInfoSyncerInit(ctx context.Context, + id string, + serverIDGetter func() uint64, + etcdCli *clientv3.Client, + codec tikv.Codec, + skipRegisterToDashBoard bool) (*InfoSyncer, error) { is := &InfoSyncer{ etcdCli: etcdCli, info: getServerInfo(id, serverIDGetter), @@ -192,7 +198,7 @@ func GlobalInfoSyncerInit(ctx context.Context, id string, serverIDGetter func() is.labelRuleManager = initLabelRuleManager(etcdCli) is.placementManager = initPlacementManager(etcdCli) is.scheduleManager = initScheduleManager(etcdCli) - is.tiflashReplicaManager = initTiFlashReplicaManager(etcdCli) + is.tiflashReplicaManager = initTiFlashReplicaManager(etcdCli, codec) setGlobalInfoSyncer(is) return is, nil } @@ -237,13 +243,17 @@ func initPlacementManager(etcdCli *clientv3.Client) PlacementManager { return &PDPlacementManager{etcdCli: etcdCli} } -func initTiFlashReplicaManager(etcdCli *clientv3.Client) TiFlashReplicaManager { +func initTiFlashReplicaManager(etcdCli *clientv3.Client, codec tikv.Codec) TiFlashReplicaManager { if etcdCli == nil { m := mockTiFlashReplicaManagerCtx{tiflashProgressCache: make(map[int64]float64)} return &m } logutil.BgLogger().Warn("init TiFlashReplicaManager", zap.Strings("pd addrs", etcdCli.Endpoints())) - return &TiFlashReplicaManagerCtx{etcdCli: etcdCli, tiflashProgressCache: make(map[int64]float64)} + return &TiFlashReplicaManagerCtx{ + etcdCli: etcdCli, + tiflashProgressCache: make(map[int64]float64), + codec: codec, + } } func initScheduleManager(etcdCli *clientv3.Client) ScheduleManager { diff --git a/domain/infosync/info_test.go b/domain/infosync/info_test.go index 90a30d8f1f161..55346ebcb1541 100644 --- a/domain/infosync/info_test.go +++ b/domain/infosync/info_test.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/ddl/util" + "github.com/pingcap/tidb/keyspace" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/testkit/testsetup" util2 "github.com/pingcap/tidb/util" @@ -67,7 +68,7 @@ func TestTopology(t *testing.T) { require.NoError(t, err) }() - info, err := GlobalInfoSyncerInit(ctx, currentID, func() uint64 { return 1 }, client, false) + info, err := GlobalInfoSyncerInit(ctx, currentID, func() uint64 { return 1 }, client, keyspace.CodecV1, false) require.NoError(t, err) err = info.newTopologySessionAndStoreServerInfo(ctx, util2.NewSessionDefaultRetryCnt) @@ -152,7 +153,7 @@ func (is *InfoSyncer) ttlKeyExists(ctx context.Context) (bool, error) { } func TestPutBundlesRetry(t *testing.T) { - _, err := GlobalInfoSyncerInit(context.TODO(), "test", func() uint64 { return 1 }, nil, false) + _, err := GlobalInfoSyncerInit(context.TODO(), "test", func() uint64 { return 1 }, nil, keyspace.CodecV1, false) require.NoError(t, err) bundle, err := placement.NewBundleFromOptions(&model.PlacementSettings{PrimaryRegion: "r1", Regions: "r1,r2"}) @@ -216,7 +217,7 @@ func TestPutBundlesRetry(t *testing.T) { func TestTiFlashManager(t *testing.T) { ctx := context.Background() - _, err := GlobalInfoSyncerInit(ctx, "test", func() uint64 { return 1 }, nil, false) + _, err := GlobalInfoSyncerInit(ctx, "test", func() uint64 { return 1 }, nil, keyspace.CodecV1, false) tiflash := NewMockTiFlash() SetMockTiFlash(tiflash) diff --git a/domain/infosync/tiflash_manager.go b/domain/infosync/tiflash_manager.go index 4d01c64de002d..8143c70531a34 100644 --- a/domain/infosync/tiflash_manager.go +++ b/domain/infosync/tiflash_manager.go @@ -31,16 +31,24 @@ import ( "github.com/gorilla/mux" "github.com/pingcap/errors" + "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/ddl/placement" + "github.com/pingcap/tidb/keyspace" "github.com/pingcap/tidb/store/helper" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/util/codec" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/pdapi" + "github.com/tikv/client-go/v2/tikv" clientv3 "go.etcd.io/etcd/client/v3" "go.uber.org/zap" ) +const ( + keyspaceIDLabel = "keyspace-id" + tableIDLabel = "table" +) + // TiFlashReplicaManager manages placement settings and replica progress for TiFlash. type TiFlashReplicaManager interface { // SetTiFlashGroupConfig sets the group index of the tiflash placement rule @@ -76,6 +84,8 @@ type TiFlashReplicaManagerCtx struct { etcdCli *clientv3.Client sync.RWMutex // protect tiflashProgressCache tiflashProgressCache map[int64]float64 + // keyspace meta + codec tikv.Codec } // Close is called to close TiFlashReplicaManagerCtx. @@ -83,12 +93,12 @@ func (m *TiFlashReplicaManagerCtx) Close(ctx context.Context) { } -func getTiFlashPeerWithoutLagCount(tiFlashStores map[int64]helper.StoreStat, tableID int64) (int, error) { +func getTiFlashPeerWithoutLagCount(tiFlashStores map[int64]helper.StoreStat, keyspaceID uint32, tableID int64) (int, error) { // storeIDs -> regionID, PD will not create two peer on the same store var flashPeerCount int for _, store := range tiFlashStores { regionReplica := make(map[int64]int) - err := helper.CollectTiFlashStatus(store.Store.StatusAddress, tableID, ®ionReplica) + err := helper.CollectTiFlashStatus(store.Store.StatusAddress, keyspaceID, tableID, ®ionReplica) if err != nil { logutil.BgLogger().Error("Fail to get peer status from TiFlash.", zap.Int64("tableID", tableID)) @@ -100,7 +110,7 @@ func getTiFlashPeerWithoutLagCount(tiFlashStores map[int64]helper.StoreStat, tab } // calculateTiFlashProgress calculates progress based on the region status from PD and TiFlash. -func calculateTiFlashProgress(tableID int64, replicaCount uint64, tiFlashStores map[int64]helper.StoreStat) (float64, error) { +func calculateTiFlashProgress(keyspaceID uint32, tableID int64, replicaCount uint64, tiFlashStores map[int64]helper.StoreStat) (float64, error) { var regionCount int if err := GetTiFlashRegionCountFromPD(context.Background(), tableID, ®ionCount); err != nil { logutil.BgLogger().Error("Fail to get regionCount from PD.", @@ -114,7 +124,7 @@ func calculateTiFlashProgress(tableID int64, replicaCount uint64, tiFlashStores return 0, fmt.Errorf("region count getting from PD is 0") } - tiflashPeerCount, err := getTiFlashPeerWithoutLagCount(tiFlashStores, tableID) + tiflashPeerCount, err := getTiFlashPeerWithoutLagCount(tiFlashStores, keyspaceID, tableID) if err != nil { logutil.BgLogger().Error("Fail to get peer count from TiFlash.", zap.Int64("tableID", tableID)) @@ -135,7 +145,7 @@ func calculateTiFlashProgress(tableID int64, replicaCount uint64, tiFlashStores // CalculateTiFlashProgress calculates TiFlash replica progress. func (m *TiFlashReplicaManagerCtx) CalculateTiFlashProgress(tableID int64, replicaCount uint64, tiFlashStores map[int64]helper.StoreStat) (float64, error) { - return calculateTiFlashProgress(tableID, replicaCount, tiFlashStores) + return calculateTiFlashProgress(keyspace.GetID(m.codec.GetKeyspace()), tableID, replicaCount, tiFlashStores) } // UpdateTiFlashProgressCache updates tiflashProgressCache @@ -218,8 +228,60 @@ func (m *TiFlashReplicaManagerCtx) SetTiFlashGroupConfig(ctx context.Context) er return nil } +func makeRuleID(c tikv.Codec, ruleID string) string { + if c.GetAPIVersion() == kvrpcpb.APIVersion_V2 && !strings.HasPrefix(ruleID, keyspaceIDLabel) { + keyspaceID := keyspace.GetID(c.GetKeyspace()) + ruleID = fmt.Sprintf("%s-%v-%s", keyspaceIDLabel, keyspaceID, ruleID) + } + return ruleID +} + +func getRulePrefix(c tikv.Codec) string { + if c.GetAPIVersion() == kvrpcpb.APIVersion_V2 { + return fmt.Sprintf("%s-%v", keyspaceIDLabel, keyspace.GetID(c.GetKeyspace())) + } + return tableIDLabel +} + +func encodeTiFlashPlacementRules(c tikv.Codec, rule *placement.TiFlashRule) error { + if c.GetAPIVersion() != kvrpcpb.APIVersion_V2 { + return nil + } + + startKey, err := hex.DecodeString(rule.StartKeyHex) + if err != nil { + return err + } + endKey, err := hex.DecodeString(rule.EndKeyHex) + if err != nil { + return err + } + + _, startKey, err = codec.DecodeBytes(startKey, nil) + if err != nil { + return err + } + _, endKey, err = codec.DecodeBytes(endKey, nil) + if err != nil { + return err + } + + startKey, endKey = c.EncodeRegionRange(startKey, endKey) + + rule.StartKeyHex = hex.EncodeToString(startKey) + rule.EndKeyHex = hex.EncodeToString(endKey) + + rule.ID = makeRuleID(c, rule.ID) + return nil +} + // SetPlacementRule is a helper function to set placement rule. func (m *TiFlashReplicaManagerCtx) SetPlacementRule(ctx context.Context, rule placement.TiFlashRule) error { + err := encodeTiFlashPlacementRules(m.codec, &rule) + if err != nil { + return errors.Trace(err) + } + if err := m.SetTiFlashGroupConfig(ctx); err != nil { return err } @@ -241,6 +303,7 @@ func (m *TiFlashReplicaManagerCtx) SetPlacementRule(ctx context.Context, rule pl // DeletePlacementRule is to delete placement rule for certain group. func (m *TiFlashReplicaManagerCtx) DeletePlacementRule(ctx context.Context, group string, ruleID string) error { + ruleID = makeRuleID(m.codec, ruleID) res, err := doRequest(ctx, "DeletePlacementRule", m.etcdCli.Endpoints(), path.Join(pdapi.Config, "rule", group, ruleID), "DELETE", nil) if err != nil { return errors.Trace(err) @@ -251,6 +314,16 @@ func (m *TiFlashReplicaManagerCtx) DeletePlacementRule(ctx context.Context, grou return nil } +func filterTiFlashRules(c tikv.Codec, rules []placement.TiFlashRule) []placement.TiFlashRule { + var ret []placement.TiFlashRule + for _, rule := range rules { + if strings.HasPrefix(rule.ID, getRulePrefix(c)) { + ret = append(ret, rule) + } + } + return ret +} + // GetGroupRules to get all placement rule in a certain group. func (m *TiFlashReplicaManagerCtx) GetGroupRules(ctx context.Context, group string) ([]placement.TiFlashRule, error) { res, err := doRequest(ctx, "GetGroupRules", m.etcdCli.Endpoints(), path.Join(pdapi.Config, "rules", "group", group), "GET", nil) @@ -266,6 +339,7 @@ func (m *TiFlashReplicaManagerCtx) GetGroupRules(ctx context.Context, group stri if err != nil { return nil, errors.Trace(err) } + rules = filterTiFlashRules(m.codec, rules) return rules, nil } @@ -274,8 +348,7 @@ func (m *TiFlashReplicaManagerCtx) GetGroupRules(ctx context.Context, group stri func (m *TiFlashReplicaManagerCtx) PostAccelerateSchedule(ctx context.Context, tableID int64) error { startKey := tablecodec.GenTableRecordPrefix(tableID) endKey := tablecodec.EncodeTablePrefix(tableID + 1) - startKey = codec.EncodeBytes([]byte{}, startKey) - endKey = codec.EncodeBytes([]byte{}, endKey) + startKey, endKey = m.codec.EncodeRegionRange(startKey, endKey) input := map[string]string{ "start_key": hex.EncodeToString(startKey), @@ -300,8 +373,7 @@ func (m *TiFlashReplicaManagerCtx) PostAccelerateSchedule(ctx context.Context, t func (m *TiFlashReplicaManagerCtx) GetRegionCountFromPD(ctx context.Context, tableID int64, regionCount *int) error { startKey := tablecodec.GenTableRecordPrefix(tableID) endKey := tablecodec.EncodeTablePrefix(tableID + 1) - startKey = codec.EncodeBytes([]byte{}, startKey) - endKey = codec.EncodeBytes([]byte{}, endKey) + startKey, endKey = m.codec.EncodeRegionRange(startKey, endKey) p := fmt.Sprintf("/pd/api/v1/stats/region?start_key=%s&end_key=%s&count", url.QueryEscape(string(startKey)), @@ -749,7 +821,7 @@ func (tiflash *MockTiFlash) PdSwitch(enabled bool) { // CalculateTiFlashProgress return truncated string to avoid float64 comparison. func (m *mockTiFlashReplicaManagerCtx) CalculateTiFlashProgress(tableID int64, replicaCount uint64, tiFlashStores map[int64]helper.StoreStat) (float64, error) { - return calculateTiFlashProgress(tableID, replicaCount, tiFlashStores) + return calculateTiFlashProgress(keyspace.NulSpaceID, tableID, replicaCount, tiFlashStores) } // UpdateTiFlashProgressCache updates tiflashProgressCache diff --git a/keyspace/keyspace.go b/keyspace/keyspace.go index 2e97d0c87cf5c..03819eb5fd8e0 100644 --- a/keyspace/keyspace.go +++ b/keyspace/keyspace.go @@ -14,6 +14,8 @@ const ( // EnvVarKeyspaceName is the system env name for keyspace name. EnvVarKeyspaceName = "KEYSPACE_NAME" + // NulSpaceID represents the API V1 data space. + NulSpaceID = 0xffffffff // tidbKeyspaceEtcdPathPrefix is the keyspace prefix for etcd namespace tidbKeyspaceEtcdPathPrefix = "/keyspaces/tidb/" @@ -61,7 +63,7 @@ func toUint32(b []byte) uint32 { // GetID is used to get keyspace id bytes from keyspace prefix func GetID(b []byte) uint32 { if len(b) < 4 { - return 0 + return NulSpaceID } return toUint32(b[1:4]) } diff --git a/server/BUILD.bazel b/server/BUILD.bazel index 334daa55f6c42..4135695cfc93b 100644 --- a/server/BUILD.bazel +++ b/server/BUILD.bazel @@ -159,6 +159,7 @@ go_test( "//expression", "//extension", "//infoschema", + "//keyspace", "//kv", "//meta", "//metrics", diff --git a/server/stat_test.go b/server/stat_test.go index 66c974a3deeea..5c115c7a0a4e8 100644 --- a/server/stat_test.go +++ b/server/stat_test.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/domain/infosync" + "github.com/pingcap/tidb/keyspace" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore" "github.com/stretchr/testify/require" @@ -46,7 +47,7 @@ func TestUptime(t *testing.T) { }() require.NoError(t, err) - _, err = infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), true) + _, err = infosync.GlobalInfoSyncerInit(context.Background(), dom.DDL().GetID(), dom.ServerID, dom.GetEtcdClient(), keyspace.CodecV1, true) require.NoError(t, err) tidbdrv := NewTiDBDriver(store) diff --git a/store/helper/helper.go b/store/helper/helper.go index b8ef9ae85bc51..05108d8789bff 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -1206,10 +1206,11 @@ func ComputeTiFlashStatus(reader *bufio.Reader, regionReplica *map[int64]int) er // CollectTiFlashStatus query sync status of one table from TiFlash store. // `regionReplica` is a map from RegionID to count of TiFlash Replicas in this region. -func CollectTiFlashStatus(statusAddress string, tableID int64, regionReplica *map[int64]int) error { - statURL := fmt.Sprintf("%s://%s/tiflash/sync-status/%d", +func CollectTiFlashStatus(statusAddress string, keyspaceID uint32, tableID int64, regionReplica *map[int64]int) error { + statURL := fmt.Sprintf("%s://%s/tiflash/sync-status/keyspace/%d/table/%d", util.InternalHTTPSchema(), statusAddress, + keyspaceID, tableID, ) resp, err := util.InternalHTTPClient().Get(statURL)