From 96df9b6ab7abfe70f8e097c4741b4d82cc973f6e Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 7 Dec 2021 14:38:18 +0800 Subject: [PATCH 01/10] add helper functions Signed-off-by: CalvinNeo --- store/helper/helper.go | 234 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/store/helper/helper.go b/store/helper/helper.go index 125052d10cd75..879b94d6af84f 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -15,11 +15,13 @@ package helper import ( + "bufio" "bytes" "context" "encoding/hex" "encoding/json" "fmt" + "github.com/pingcap/tidb/ddl/placement" "io" "math" "net/http" @@ -904,3 +906,235 @@ func (h *Helper) GetPDRegionStats(tableID int64, stats *PDRegionStats) error { return dec.Decode(stats) } + +// DeletePlacementRule is to delete placement rule for certain group. +func (h *Helper) DeletePlacementRule(group string, ruleID string) error { + pdAddrs, err := h.GetPDAddr() + if err != nil { + return errors.Trace(err) + } + + deleteURL := fmt.Sprintf("%s://%s/pd/api/v1/config/rule/%v/%v", + util.InternalHTTPSchema(), + pdAddrs[0], + group, + ruleID, + ) + + req, err := http.NewRequest("DELETE", deleteURL, nil) + if err != nil { + return err + } + + resp, err := util.InternalHTTPClient().Do(req) + if err != nil { + return err + } + if resp.StatusCode != 200 { + return errors.New("DeletePlacementRule returns error") + } + defer func() { + if err = resp.Body.Close(); err != nil { + log.Error("err", zap.Error(err)) + } + }() + return nil +} + +// SetPlacementRule is a helper function to set placement rule. +func (h *Helper) SetPlacementRule(rule placement.Rule) error { + pdAddrs, err := h.GetPDAddr() + if err != nil { + return errors.Trace(err) + } + m, _ := json.Marshal(rule) + + postURL := fmt.Sprintf("%s://%s/pd/api/v1/config/rule", + util.InternalHTTPSchema(), + pdAddrs[0], + ) + buf := bytes.NewBuffer(m) + resp, err := util.InternalHTTPClient().Post(postURL, "application/json", buf) + if err != nil { + return err + } + if resp.StatusCode != 200 { + return errors.New("SetPlacementRule returns error") + } + defer func() { + if err = resp.Body.Close(); err != nil { + log.Error("err", zap.Error(err)) + } + }() + return nil +} + +// GetGroupRules to get all placement rule in a certain group. +func (h *Helper) GetGroupRules(group string) ([]placement.Rule, error) { + pdAddrs, err := h.GetPDAddr() + if err != nil { + return nil, errors.Trace(err) + } + + getURL := fmt.Sprintf("%s://%s/pd/api/v1/config/rules/group/%s", + util.InternalHTTPSchema(), + pdAddrs[0], + group, + ) + + resp, err := util.InternalHTTPClient().Get(getURL) + if err != nil { + return nil, err + } + + if resp.StatusCode != 200 { + return nil, errors.New("GetGroupRules returns error") + } + + defer func() { + if err = resp.Body.Close(); err != nil { + log.Error("err", zap.Error(err)) + } + }() + + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(resp.Body) + if err != nil { + return nil, errors.Trace(err) + } + + var rules []placement.Rule + err = json.Unmarshal(buf.Bytes(), &rules) + if err != nil { + return nil, errors.Trace(err) + } + + return rules, nil +} + +// PostAccelerateSchedule sends `regions/accelerate-schedule` request. +func (h *Helper) PostAccelerateSchedule(tableID int64) 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) + + postURL := fmt.Sprintf("%s://%s/pd/api/v1/regions/accelerate-schedule", + util.InternalHTTPSchema(), + pdAddrs[0]) + + if err != nil { + return err + } + input := map[string]string{ + "start_key": url.QueryEscape(string(startKey)), + "end_key": url.QueryEscape(string(endKey)), + } + v, err := json.Marshal(input) + if err != nil { + return err + } + resp, err := util.InternalHTTPClient().Post(postURL, "application/json", bytes.NewBuffer(v)) + defer func() { + if err = resp.Body.Close(); err != nil { + log.Error("err", zap.Error(err)) + } + }() + if err != nil { + return err + } + 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 err + } + + defer func() { + if err = resp.Body.Close(); err != nil { + log.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 { + endKey, _ = url.QueryUnescape(endKey) + _, decodedEndKey, _ := codec.DecodeBytes([]byte(endKey), []byte{}) + tableID := tablecodec.DecodeTableID(decodedEndKey) + tableID -= 1 + return tableID +} + +// ComputeTiFlashStatus is helper function for CollectTiFlashStatus. +func ComputeTiFlashStatus(reader *bufio.Reader, regionReplica *map[int64]int) error { + ns, _, _ := reader.ReadLine() + n, err := strconv.ParseInt(string(ns), 10, 64) + if err != nil { + return errors.Trace(err) + } + for i := int64(0); i < n; i++ { + rs, _, _ := reader.ReadLine() + // For (`table`, `store`), has region `r` + r, err := strconv.ParseInt(strings.Trim(string(rs), "\r\n \t"), 10, 32) + if err != nil { + return errors.Trace(err) + } + if i, ok := (*regionReplica)[r]; ok { + (*regionReplica)[r] = i + 1 + } else { + (*regionReplica)[r] = 1 + } + } + return nil +} + +// CollectTiFlashStatus query sync status of one table from TiFlash store. +func CollectTiFlashStatus(statusAddress string, tableID int64, regionReplica *map[int64]int) error { + statURL := fmt.Sprintf("%s://%s/tiflash/sync-status/%d", + util.InternalHTTPSchema(), + statusAddress, + tableID, + ) + resp, err := util.InternalHTTPClient().Get(statURL) + if err != nil { + return nil + } + + defer func() { + resp.Body.Close() + }() + + reader := bufio.NewReader(resp.Body) + if err = ComputeTiFlashStatus(reader, regionReplica); err != nil { + return errors.Trace(err) + } + return nil +} From a2a2101768da3ab04665d969a6ba830850254012 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 7 Dec 2021 14:45:32 +0800 Subject: [PATCH 02/10] add test Signed-off-by: CalvinNeo --- store/helper/helper_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/store/helper/helper_test.go b/store/helper/helper_test.go index 7237b0b815db5..4c7fc833b654c 100644 --- a/store/helper/helper_test.go +++ b/store/helper/helper_test.go @@ -15,10 +15,12 @@ package helper_test import ( + "bufio" "crypto/tls" "encoding/json" "net/http" "net/http/httptest" + "strings" "testing" "time" @@ -438,3 +440,16 @@ func mockStoreStatResponse(w http.ResponseWriter, _ *http.Request) { log.Panic("write http response failed", zap.Error(err)) } } + +func TestComputeTiFlashStatus(t *testing.T) { + regionReplica := make(map[int64]int) + resp1 := "0\n\n" + resp2 := "1\n1009\n" + br1 := bufio.NewReader(strings.NewReader(resp1)) + br2 := bufio.NewReader(strings.NewReader(resp2)) + err := helper.ComputeTiFlashStatus(br1, ®ionReplica) + require.NoError(t, err) + err = helper.ComputeTiFlashStatus(br2, ®ionReplica) + require.NoError(t, err) + require.Equal(t, len(regionReplica), 1) +} From 17267dd1e8c699bbc2462c22117ad99331215624 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 7 Dec 2021 14:48:43 +0800 Subject: [PATCH 03/10] add GetDropOrTruncateTableInfoFromJobsByStore Signed-off-by: CalvinNeo --- ddl/ddl.go | 37 +++++++++++++++++++++++++++++++++++++ executor/ddl.go | 39 ++++++--------------------------------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index ebec94019105b..bb6dd167e15aa 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -22,6 +22,7 @@ import ( "context" "flag" "fmt" + "github.com/pingcap/tidb/util/gcutil" "sync" "time" @@ -731,3 +732,39 @@ func init() { RunInGoTest = true } } + +// GetDropOrTruncateTableInfoFromJobsByStore implements GetDropOrTruncateTableInfoFromJobs +func GetDropOrTruncateTableInfoFromJobsByStore(jobs []*model.Job, gcSafePoint uint64, getTable func(uint64, int64, int64) (*model.TableInfo, error), fn func(*model.Job, *model.TableInfo) (bool, error)) (bool, error) { + for _, job := range jobs { + // Check GC safe point for getting snapshot infoSchema. + err := gcutil.ValidateSnapshotWithGCSafePoint(job.StartTS, gcSafePoint) + if err != nil { + return false, err + } + if job.Type != model.ActionDropTable && job.Type != model.ActionTruncateTable { + continue + } + + tbl, err := getTable(job.StartTS, job.SchemaID, job.TableID) + if err != nil { + if meta.ErrDBNotExists.Equal(err) { + // The dropped/truncated DDL maybe execute failed that caused by the parallel DDL execution, + // then can't find the table from the snapshot info-schema. Should just ignore error here, + // see more in TestParallelDropSchemaAndDropTable. + continue + } + return false, err + } + if tbl == nil { + // The dropped/truncated DDL maybe execute failed that caused by the parallel DDL execution, + // then can't find the table from the snapshot info-schema. Should just ignore error here, + // see more in TestParallelDropSchemaAndDropTable. + continue + } + finish, err := fn(job, tbl) + if err != nil || finish { + return finish, err + } + } + return false, nil +} diff --git a/executor/ddl.go b/executor/ddl.go index 4c2be5828b8a8..5bb39190c0a4d 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -672,42 +672,15 @@ func (e *DDLExec) getRecoverTableByJobID(s *ast.RecoverTableStmt, t *meta.Meta, // GetDropOrTruncateTableInfoFromJobs gets the dropped/truncated table information from DDL jobs, // it will use the `start_ts` of DDL job as snapshot to get the dropped/truncated table information. func GetDropOrTruncateTableInfoFromJobs(jobs []*model.Job, gcSafePoint uint64, dom *domain.Domain, fn func(*model.Job, *model.TableInfo) (bool, error)) (bool, error) { - for _, job := range jobs { - // Check GC safe point for getting snapshot infoSchema. - err := gcutil.ValidateSnapshotWithGCSafePoint(job.StartTS, gcSafePoint) + getTable := func(StartTS uint64, SchemaID int64, TableID int64) (*model.TableInfo, error) { + snapMeta, err := dom.GetSnapshotMeta(StartTS) if err != nil { - return false, err - } - if job.Type != model.ActionDropTable && job.Type != model.ActionTruncateTable { - continue - } - - snapMeta, err := dom.GetSnapshotMeta(job.StartTS) - if err != nil { - return false, err - } - tbl, err := snapMeta.GetTable(job.SchemaID, job.TableID) - if err != nil { - if meta.ErrDBNotExists.Equal(err) { - // The dropped/truncated DDL maybe execute failed that caused by the parallel DDL execution, - // then can't find the table from the snapshot info-schema. Should just ignore error here, - // see more in TestParallelDropSchemaAndDropTable. - continue - } - return false, err - } - if tbl == nil { - // The dropped/truncated DDL maybe execute failed that caused by the parallel DDL execution, - // then can't find the table from the snapshot info-schema. Should just ignore error here, - // see more in TestParallelDropSchemaAndDropTable. - continue - } - finish, err := fn(job, tbl) - if err != nil || finish { - return finish, err + return nil, err } + tbl, err := snapMeta.GetTable(SchemaID, TableID) + return tbl, err } - return false, nil + return ddl.GetDropOrTruncateTableInfoFromJobsByStore(jobs, gcSafePoint, getTable, fn) } func (e *DDLExec) getRecoverTableByTableName(tableName *ast.TableName) (*model.Job, *model.TableInfo, error) { From e89215642c4e5f955433d8e6a18f2f292cb4f7b0 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Tue, 7 Dec 2021 15:16:21 +0800 Subject: [PATCH 04/10] fmt Signed-off-by: CalvinNeo --- store/helper/helper.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/store/helper/helper.go b/store/helper/helper.go index 879b94d6af84f..d2a44821b1dcd 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -21,7 +21,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "github.com/pingcap/tidb/ddl/placement" "io" "math" "net/http" @@ -31,6 +30,8 @@ import ( "strings" "time" + "github.com/pingcap/tidb/ddl/placement" + "github.com/pingcap/errors" deadlockpb "github.com/pingcap/kvproto/pkg/deadlock" "github.com/pingcap/kvproto/pkg/kvrpcpb" From 56bf6a3354c8d4440668fe5bcd94eefa97b72b95 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Wed, 8 Dec 2021 11:05:44 +0800 Subject: [PATCH 05/10] fmt Signed-off-by: CalvinNeo --- ddl/ddl.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index bb6dd167e15aa..0a234f8c815bd 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -22,10 +22,11 @@ import ( "context" "flag" "fmt" - "github.com/pingcap/tidb/util/gcutil" "sync" "time" + "github.com/pingcap/tidb/util/gcutil" + "github.com/google/uuid" "github.com/ngaut/pools" "github.com/pingcap/errors" From 46c88bd57ff2417e9065d4fb1e6b3de2b5fb8af3 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Thu, 9 Dec 2021 09:57:01 +0800 Subject: [PATCH 06/10] fmt --- ddl/ddl.go | 3 +-- store/helper/helper.go | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index 0a234f8c815bd..2277e35ac4b0b 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -25,8 +25,6 @@ import ( "sync" "time" - "github.com/pingcap/tidb/util/gcutil" - "github.com/google/uuid" "github.com/ngaut/pools" "github.com/pingcap/errors" @@ -49,6 +47,7 @@ import ( "github.com/pingcap/tidb/table" goutil "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/admin" + "github.com/pingcap/tidb/util/gcutil" "github.com/pingcap/tidb/util/logutil" "go.etcd.io/etcd/clientv3" "go.uber.org/zap" diff --git a/store/helper/helper.go b/store/helper/helper.go index d2a44821b1dcd..ac76ed57f0c7d 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -30,12 +30,10 @@ import ( "strings" "time" - "github.com/pingcap/tidb/ddl/placement" - "github.com/pingcap/errors" deadlockpb "github.com/pingcap/kvproto/pkg/deadlock" "github.com/pingcap/kvproto/pkg/kvrpcpb" - "github.com/pingcap/log" + "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/metrics" "github.com/pingcap/tidb/parser/model" @@ -899,7 +897,7 @@ func (h *Helper) GetPDRegionStats(tableID int64, stats *PDRegionStats) error { defer func() { if err = resp.Body.Close(); err != nil { - log.Error("err", zap.Error(err)) + logutil.BgLogger().Error("err", zap.Error(err)) } }() @@ -931,12 +929,12 @@ func (h *Helper) DeletePlacementRule(group string, ruleID string) error { if err != nil { return err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return errors.New("DeletePlacementRule returns error") } defer func() { if err = resp.Body.Close(); err != nil { - log.Error("err", zap.Error(err)) + logutil.BgLogger().Error("err", zap.Error(err)) } }() return nil @@ -959,12 +957,12 @@ func (h *Helper) SetPlacementRule(rule placement.Rule) error { if err != nil { return err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return errors.New("SetPlacementRule returns error") } defer func() { if err = resp.Body.Close(); err != nil { - log.Error("err", zap.Error(err)) + logutil.BgLogger().Error("err", zap.Error(err)) } }() return nil @@ -988,13 +986,13 @@ func (h *Helper) GetGroupRules(group string) ([]placement.Rule, error) { return nil, err } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return nil, errors.New("GetGroupRules returns error") } defer func() { if err = resp.Body.Close(); err != nil { - log.Error("err", zap.Error(err)) + logutil.BgLogger().Error("err", zap.Error(err)) } }() @@ -1042,7 +1040,7 @@ func (h *Helper) PostAccelerateSchedule(tableID int64) error { resp, err := util.InternalHTTPClient().Post(postURL, "application/json", bytes.NewBuffer(v)) defer func() { if err = resp.Body.Close(); err != nil { - log.Error("err", zap.Error(err)) + logutil.BgLogger().Error("err", zap.Error(err)) } }() if err != nil { @@ -1076,7 +1074,7 @@ func (h *Helper) GetPDRegionRecordStats(tableID int64, stats *PDRegionStats) err defer func() { if err = resp.Body.Close(); err != nil { - log.Error("err", zap.Error(err)) + logutil.BgLogger().Error("err", zap.Error(err)) } }() From 99b4644a91b5743a77d7797a20295bb48aec48f3 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Fri, 17 Dec 2021 11:50:26 +0800 Subject: [PATCH 07/10] Modify according to requests Signed-off-by: CalvinNeo --- store/helper/helper.go | 35 +++++++++++++++-------------------- store/helper/helper_test.go | 2 ++ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/store/helper/helper.go b/store/helper/helper.go index ac76ed57f0c7d..b7035870f9401 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -894,7 +894,6 @@ func (h *Helper) GetPDRegionStats(tableID int64, stats *PDRegionStats) error { if err != nil { return err } - defer func() { if err = resp.Body.Close(); err != nil { logutil.BgLogger().Error("err", zap.Error(err)) @@ -929,14 +928,14 @@ func (h *Helper) DeletePlacementRule(group string, ruleID string) error { if err != nil { return err } - if resp.StatusCode != http.StatusOK { - return errors.New("DeletePlacementRule returns error") - } defer func() { if err = resp.Body.Close(); err != nil { logutil.BgLogger().Error("err", zap.Error(err)) } }() + if resp.StatusCode != http.StatusOK { + return errors.New("DeletePlacementRule returns error") + } return nil } @@ -957,14 +956,14 @@ func (h *Helper) SetPlacementRule(rule placement.Rule) error { if err != nil { return err } - if resp.StatusCode != http.StatusOK { - return errors.New("SetPlacementRule returns error") - } defer func() { if err = resp.Body.Close(); err != nil { logutil.BgLogger().Error("err", zap.Error(err)) } }() + if resp.StatusCode != http.StatusOK { + return errors.New("SetPlacementRule returns error") + } return nil } @@ -985,17 +984,16 @@ func (h *Helper) GetGroupRules(group string) ([]placement.Rule, error) { if err != nil { return nil, err } - - if resp.StatusCode != http.StatusOK { - return nil, errors.New("GetGroupRules returns error") - } - defer func() { if err = resp.Body.Close(); err != nil { logutil.BgLogger().Error("err", zap.Error(err)) } }() + if resp.StatusCode != http.StatusOK { + return nil, errors.New("GetGroupRules returns error") + } + buf := new(bytes.Buffer) _, err = buf.ReadFrom(resp.Body) if err != nil { @@ -1026,9 +1024,6 @@ func (h *Helper) PostAccelerateSchedule(tableID int64) error { util.InternalHTTPSchema(), pdAddrs[0]) - if err != nil { - return err - } input := map[string]string{ "start_key": url.QueryEscape(string(startKey)), "end_key": url.QueryEscape(string(endKey)), @@ -1038,14 +1033,14 @@ func (h *Helper) PostAccelerateSchedule(tableID int64) error { return err } resp, err := util.InternalHTTPClient().Post(postURL, "application/json", bytes.NewBuffer(v)) + if err != nil { + return err + } defer func() { if err = resp.Body.Close(); err != nil { logutil.BgLogger().Error("err", zap.Error(err)) } }() - if err != nil { - return err - } return nil } @@ -1071,7 +1066,6 @@ func (h *Helper) GetPDRegionRecordStats(tableID int64, stats *PDRegionStats) err if err != nil { return err } - defer func() { if err = resp.Body.Close(); err != nil { logutil.BgLogger().Error("err", zap.Error(err)) @@ -1116,6 +1110,7 @@ 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", util.InternalHTTPSchema(), @@ -1124,7 +1119,7 @@ func CollectTiFlashStatus(statusAddress string, tableID int64, regionReplica *ma ) resp, err := util.InternalHTTPClient().Get(statURL) if err != nil { - return nil + return errors.Trace(err) } defer func() { diff --git a/store/helper/helper_test.go b/store/helper/helper_test.go index 4c7fc833b654c..caaaa67106ea7 100644 --- a/store/helper/helper_test.go +++ b/store/helper/helper_test.go @@ -443,7 +443,9 @@ func mockStoreStatResponse(w http.ResponseWriter, _ *http.Request) { func TestComputeTiFlashStatus(t *testing.T) { regionReplica := make(map[int64]int) + // There are no region in this TiFlash store. resp1 := "0\n\n" + // There are one region 1009 in this TiFlash store. resp2 := "1\n1009\n" br1 := bufio.NewReader(strings.NewReader(resp1)) br2 := bufio.NewReader(strings.NewReader(resp2)) From e6fa5370012f88d3840f80dad8a0186eb3660128 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Fri, 17 Dec 2021 11:59:12 +0800 Subject: [PATCH 08/10] fix Signed-off-by: CalvinNeo --- store/helper/helper.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/store/helper/helper.go b/store/helper/helper.go index b7035870f9401..8b6493489a4a1 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -876,7 +876,7 @@ type PDRegionStats struct { func (h *Helper) GetPDRegionStats(tableID int64, stats *PDRegionStats) error { pdAddrs, err := h.GetPDAddr() if err != nil { - return err + return errors.Trace(err) } startKey := tablecodec.EncodeTablePrefix(tableID) @@ -892,7 +892,7 @@ func (h *Helper) GetPDRegionStats(tableID int64, stats *PDRegionStats) error { resp, err := util.InternalHTTPClient().Get(statURL) if err != nil { - return err + return errors.Trace(err) } defer func() { if err = resp.Body.Close(); err != nil { @@ -921,12 +921,12 @@ func (h *Helper) DeletePlacementRule(group string, ruleID string) error { req, err := http.NewRequest("DELETE", deleteURL, nil) if err != nil { - return err + return errors.Trace(err) } resp, err := util.InternalHTTPClient().Do(req) if err != nil { - return err + return errors.Trace(err) } defer func() { if err = resp.Body.Close(); err != nil { @@ -954,7 +954,7 @@ func (h *Helper) SetPlacementRule(rule placement.Rule) error { buf := bytes.NewBuffer(m) resp, err := util.InternalHTTPClient().Post(postURL, "application/json", buf) if err != nil { - return err + return errors.Trace(err) } defer func() { if err = resp.Body.Close(); err != nil { @@ -982,7 +982,7 @@ func (h *Helper) GetGroupRules(group string) ([]placement.Rule, error) { resp, err := util.InternalHTTPClient().Get(getURL) if err != nil { - return nil, err + return nil, errors.Trace(err) } defer func() { if err = resp.Body.Close(); err != nil { @@ -1030,11 +1030,11 @@ func (h *Helper) PostAccelerateSchedule(tableID int64) error { } v, err := json.Marshal(input) if err != nil { - return err + return errors.Trace(err) } resp, err := util.InternalHTTPClient().Post(postURL, "application/json", bytes.NewBuffer(v)) if err != nil { - return err + return errors.Trace(err) } defer func() { if err = resp.Body.Close(); err != nil { @@ -1064,7 +1064,7 @@ func (h *Helper) GetPDRegionRecordStats(tableID int64, stats *PDRegionStats) err resp, err := util.InternalHTTPClient().Get(statURL) if err != nil { - return err + return errors.Trace(err) } defer func() { if err = resp.Body.Close(); err != nil { From fe1a1902d70aaaa0a86c3df1a72bf717fbca0d28 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Fri, 17 Dec 2021 12:18:10 +0800 Subject: [PATCH 09/10] add check for contents Signed-off-by: CalvinNeo --- store/helper/helper_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/store/helper/helper_test.go b/store/helper/helper_test.go index 8bc3a190cab28..c29873fd81c74 100644 --- a/store/helper/helper_test.go +++ b/store/helper/helper_test.go @@ -446,4 +446,7 @@ func TestComputeTiFlashStatus(t *testing.T) { err = helper.ComputeTiFlashStatus(br2, ®ionReplica) require.NoError(t, err) require.Equal(t, len(regionReplica), 1) + v, ok := regionReplica[1009] + require.Equal(t, v, 1) + require.Equal(t, ok, true) } From 2d8b39f15b1171afada6525f80f8c59ca8317ee1 Mon Sep 17 00:00:00 2001 From: CalvinNeo Date: Fri, 17 Dec 2021 12:20:10 +0800 Subject: [PATCH 10/10] fix Signed-off-by: CalvinNeo --- store/helper/helper.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/store/helper/helper.go b/store/helper/helper.go index 8b6493489a4a1..00706047eb145 100644 --- a/store/helper/helper.go +++ b/store/helper/helper.go @@ -1123,7 +1123,10 @@ func CollectTiFlashStatus(statusAddress string, tableID int64, regionReplica *ma } defer func() { - resp.Body.Close() + err = resp.Body.Close() + if err != nil { + logutil.BgLogger().Error("close body failed", zap.Error(err)) + } }() reader := bufio.NewReader(resp.Body)