From 56fc6512692e3e1a5b07d8ab91527f6237b4a9c8 Mon Sep 17 00:00:00 2001 From: hehechen Date: Fri, 28 Oct 2022 18:17:57 +0800 Subject: [PATCH] This is an automated cherry-pick of #38571 Signed-off-by: ti-chi-bot --- ddl/ddl_tiflash_api.go | 6 +++--- domain/infosync/info.go | 6 +++--- domain/infosync/tiflash_manager.go | 33 ++++++++++++++---------------- store/helper/helper.go | 33 ------------------------------ 4 files changed, 21 insertions(+), 57 deletions(-) diff --git a/ddl/ddl_tiflash_api.go b/ddl/ddl_tiflash_api.go index 199f11d366ed5..cb149f238a81c 100644 --- a/ddl/ddl_tiflash_api.go +++ b/ddl/ddl_tiflash_api.go @@ -419,11 +419,11 @@ func (d *ddl) pollTiFlashReplicaStatus(ctx sessionctx.Context, pollTiFlashContex logutil.BgLogger().Debug("CollectTiFlashStatus", zap.Any("regionReplica", regionReplica), zap.Int64("tableID", tb.ID)) - var stats helper.PDRegionStats - if err := infosync.GetTiFlashPDRegionRecordStats(context.Background(), tb.ID, &stats); err != nil { + var regionCount int + if err := infosync.GetTiFlashRegionCountFromPD(context.Background(), tb.ID, ®ionCount); err != nil { + logutil.BgLogger().Error("Fail to get regionCount from PD.", zap.Int64("tableID", tb.ID)) return allReplicaReady, err } - regionCount := stats.Count flashRegionCount := len(regionReplica) avail := regionCount == flashRegionCount failpoint.Inject("PollTiFlashReplicaStatusReplaceCurAvailableValue", func(val failpoint.Value) { diff --git a/domain/infosync/info.go b/domain/infosync/info.go index e236524d404bc..cb2b844e440d6 100644 --- a/domain/infosync/info.go +++ b/domain/infosync/info.go @@ -1024,13 +1024,13 @@ func PostTiFlashAccelerateSchedule(ctx context.Context, tableID int64) error { return is.tiflashPlacementManager.PostAccelerateSchedule(ctx, tableID) } -// GetTiFlashPDRegionRecordStats is a helper function calling `/stats/region`. -func GetTiFlashPDRegionRecordStats(ctx context.Context, tableID int64, stats *helper.PDRegionStats) error { +// GetTiFlashRegionCountFromPD is a helper function calling `/stats/region`. +func GetTiFlashRegionCountFromPD(ctx context.Context, tableID int64, regionCount *int) error { is, err := getGlobalInfoSyncer() if err != nil { return errors.Trace(err) } - return is.tiflashPlacementManager.GetPDRegionRecordStats(ctx, tableID, stats) + return is.tiflashPlacementManager.GetRegionCountFromPD(ctx, tableID, regionCount) } // GetTiFlashStoresStat gets the TiKV store information by accessing PD's api. diff --git a/domain/infosync/tiflash_manager.go b/domain/infosync/tiflash_manager.go index 84a2345c0d572..521328ef61985 100644 --- a/domain/infosync/tiflash_manager.go +++ b/domain/infosync/tiflash_manager.go @@ -53,8 +53,8 @@ type TiFlashPlacementManager interface { GetGroupRules(ctx context.Context, group string) ([]placement.TiFlashRule, error) // PostAccelerateSchedule sends `regions/accelerate-schedule` request. PostAccelerateSchedule(ctx context.Context, tableID int64) error - // GetPDRegionRecordStats is a helper function calling `/stats/region`. - GetPDRegionRecordStats(ctx context.Context, tableID int64, stats *helper.PDRegionStats) error + // GetRegionCountFromPD is a helper function calling `/stats/region`. + GetRegionCountFromPD(ctx context.Context, tableID int64, regionCount *int) error // GetStoresStat gets the TiKV store information by accessing PD's api. GetStoresStat(ctx context.Context) (*helper.StoresStat, error) // Close is to close TiFlashPlacementManager @@ -200,14 +200,14 @@ func (m *TiFlashPDPlacementManager) PostAccelerateSchedule(ctx context.Context, return nil } -// GetPDRegionRecordStats is a helper function calling `/stats/region`. -func (m *TiFlashPDPlacementManager) GetPDRegionRecordStats(ctx context.Context, tableID int64, stats *helper.PDRegionStats) error { +// GetRegionCountFromPD is a helper function calling `/stats/region`. +func (m *TiFlashPDPlacementManager) 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) - p := fmt.Sprintf("/pd/api/v1/stats/region?start_key=%s&end_key=%s", + p := fmt.Sprintf("/pd/api/v1/stats/region?start_key=%s&end_key=%s&count", url.QueryEscape(string(startKey)), url.QueryEscape(string(endKey))) res, err := doRequest(ctx, "GetPDRegionStats", m.etcdCli.Endpoints(), p, "GET", nil) @@ -215,13 +215,14 @@ func (m *TiFlashPDPlacementManager) GetPDRegionRecordStats(ctx context.Context, return errors.Trace(err) } if res == nil { - return fmt.Errorf("TiFlashPDPlacementManager returns error in GetPDRegionRecordStats") + return fmt.Errorf("TiFlashPDPlacementManager returns error in GetRegionCountFromPD") } - - err = json.Unmarshal(res, stats) + var stats helper.PDRegionStats + err = json.Unmarshal(res, &stats) if err != nil { return errors.Trace(err) } + *regionCount = stats.Count return nil } @@ -457,16 +458,11 @@ func (tiflash *MockTiFlash) HandlePostAccelerateSchedule(endKey string) error { return nil } -// HandleGetPDRegionRecordStats is mock function for GetPDRegionRecordStats. +// HandleGetPDRegionRecordStats is mock function for GetRegionCountFromPD. // It currently always returns 1 Region for convenience. func (tiflash *MockTiFlash) HandleGetPDRegionRecordStats(_ int64) helper.PDRegionStats { return helper.PDRegionStats{ - Count: 1, - EmptyCount: 1, - StorageSize: 1, - StorageKeys: 1, - StoreLeaderCount: map[uint64]int{1: 1}, - StorePeerCount: map[uint64]int{1: 1}, + Count: 1, } } @@ -645,14 +641,15 @@ func (m *mockTiFlashPlacementManager) PostAccelerateSchedule(ctx context.Context return m.tiflash.HandlePostAccelerateSchedule(hex.EncodeToString(endKey)) } -// GetPDRegionRecordStats is a helper function calling `/stats/region`. -func (m *mockTiFlashPlacementManager) GetPDRegionRecordStats(ctx context.Context, tableID int64, stats *helper.PDRegionStats) error { +// GetRegionCountFromPD is a helper function calling `/stats/region`. +func (m *mockTiFlashPlacementManager) GetRegionCountFromPD(ctx context.Context, tableID int64, regionCount *int) error { m.Lock() defer m.Unlock() if m.tiflash == nil { return nil } - *stats = m.tiflash.HandleGetPDRegionRecordStats(tableID) + stats := m.tiflash.HandleGetPDRegionRecordStats(tableID) + *regionCount = stats.Count return nil } diff --git a/store/helper/helper.go b/store/helper/helper.go index 6c1edd228c9e8..4719b11b7dec8 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -1142,39 +1142,6 @@ func (h *Helper) PostAccelerateSchedule(tableID int64) error { return nil } -// GetPDRegionRecordStats is a helper function calling `/stats/region`. -func (h *Helper) GetPDRegionRecordStats(tableID int64, stats *PDRegionStats) error { - pdAddrs, err := h.GetPDAddr() - if err != nil { - return errors.Trace(err) - } - - startKey := tablecodec.GenTableRecordPrefix(tableID) - endKey := tablecodec.EncodeTablePrefix(tableID + 1) - startKey = codec.EncodeBytes([]byte{}, startKey) - endKey = codec.EncodeBytes([]byte{}, endKey) - - statURL := fmt.Sprintf("%s://%s/pd/api/v1/stats/region?start_key=%s&end_key=%s", - util.InternalHTTPSchema(), - pdAddrs[0], - url.QueryEscape(string(startKey)), - url.QueryEscape(string(endKey))) - - resp, err := util.InternalHTTPClient().Get(statURL) - if err != nil { - return errors.Trace(err) - } - defer func() { - if err = resp.Body.Close(); err != nil { - logutil.BgLogger().Error("err", zap.Error(err)) - } - }() - - dec := json.NewDecoder(resp.Body) - - return dec.Decode(stats) -} - // GetTiFlashTableIDFromEndKey computes tableID from pd rule's endKey. func GetTiFlashTableIDFromEndKey(endKey string) int64 { e, _ := hex.DecodeString(endKey)