Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/url"
"sort"
"strconv"
"strings"

"github.com/gorilla/mux"
"github.com/pingcap/failpoint"
Expand Down Expand Up @@ -836,6 +837,61 @@ func (h *regionsHandler) AccelerateRegionsScheduleInRange(w http.ResponseWriter,
h.rd.Text(w, http.StatusOK, fmt.Sprintf("Accelerate regions scheduling in a given range [%s,%s)", rawStartKey, rawEndKey))
}

// @Tags region
// @Summary Accelerate regions scheduling in given ranges, only receive hex format for keys
// @Accept json
// @Param body body object true "json params"
// @Param limit query integer false "Limit count" default(256)
// @Produce json
// @Success 200 {string} string "Accelerate regions scheduling in given ranges [startKey1, endKey1), [startKey2, endKey2), ..."
// @Failure 400 {string} string "The input is invalid."
// @Router /regions/accelerate-schedule/batch [post]
func (h *regionsHandler) AccelerateRegionsScheduleInRanges(w http.ResponseWriter, r *http.Request) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about exract the same code as function from AccelerateRegionsScheduleInRange?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AccelerateRegionsScheduleInRange is not used anymore, maybe we can remove it later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the old api can't be remove, you don't know the other compoment has depends on it .

rc := getCluster(r)
var input []map[string]interface{}
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
return
}
limit := 256
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
var err error
limit, err = strconv.Atoi(limitStr)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
}
if limit > maxRegionLimit {
limit = maxRegionLimit
}
var msgBuilder strings.Builder
msgBuilder.Grow(128)
msgBuilder.WriteString("Accelerate regions scheduling in given ranges: ")
var regions []*core.RegionInfo
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use regionsIDList?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

for _, rg := range input {
startKey, rawStartKey, err := apiutil.ParseKey("start_key", rg)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
endKey, rawEndKey, err := apiutil.ParseKey("end_key", rg)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
regions = append(regions, rc.ScanRegions(startKey, endKey, limit)...)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's happened if the regions exist this new region?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not possible, this api is used by partition table, the regions come from different partition tables.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this api has been expose by all people. unless you can ensure only the partition table use it .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only used by partition table now.
To avoid this, change to use set.

Copy link
Copy Markdown
Contributor

@bufferflies bufferflies Apr 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my mistake, not need to use set, cache TTL is map

pd/pkg/cache/ttl.go

Lines 65 to 68 in 5bdfa71

c.items[key] = ttlCacheItem{
value: value,
expire: time.Now().Add(ttl),
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

msgBuilder.WriteString(fmt.Sprintf("[%s,%s), ", rawStartKey, rawEndKey))
}
if len(regions) > 0 {
regionsIDList := make([]uint64, 0, len(regions))
for _, region := range regions {
regionsIDList = append(regionsIDList, region.GetID())
}
rc.AddSuspectRegions(regionsIDList...)
}
h.rd.Text(w, http.StatusOK, msgBuilder.String())
}

func (h *regionsHandler) GetTopNRegions(w http.ResponseWriter, r *http.Request, less func(a, b *core.RegionInfo) bool) {
rc := getCluster(r)
limit := defaultRegionLimit
Expand Down
20 changes: 20 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,26 @@ func (suite *regionTestSuite) TestAccelerateRegionsScheduleInRange() {
suite.Len(idList, 2)
}

func (suite *regionTestSuite) TestAccelerateRegionsScheduleInRanges() {
re := suite.Require()
r1 := newTestRegionInfo(557, 13, []byte("a1"), []byte("a2"))
r2 := newTestRegionInfo(558, 14, []byte("a2"), []byte("a3"))
r3 := newTestRegionInfo(559, 15, []byte("a3"), []byte("a4"))
r4 := newTestRegionInfo(560, 16, []byte("a4"), []byte("a5"))
r5 := newTestRegionInfo(561, 17, []byte("a5"), []byte("a6"))
mustRegionHeartbeat(re, suite.svr, r1)
mustRegionHeartbeat(re, suite.svr, r2)
mustRegionHeartbeat(re, suite.svr, r3)
mustRegionHeartbeat(re, suite.svr, r4)
mustRegionHeartbeat(re, suite.svr, r5)
body := fmt.Sprintf(`[{"start_key":"%s", "end_key": "%s"}, {"start_key":"%s", "end_key": "%s"}]`, hex.EncodeToString([]byte("a1")), hex.EncodeToString([]byte("a3")), hex.EncodeToString([]byte("a4")), hex.EncodeToString([]byte("a6")))

err := tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/regions/accelerate-schedule/batch", suite.urlPrefix), []byte(body), tu.StatusOK(re))
suite.NoError(err)
idList := suite.svr.GetRaftCluster().GetSuspectRegions()
suite.Len(idList, 4)
}

func (suite *regionTestSuite) TestScatterRegions() {
re := suite.Require()
r1 := newTestRegionInfo(601, 13, []byte("b1"), []byte("b2"))
Expand Down
1 change: 1 addition & 0 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
registerFunc(clusterRouter, "/regions/check/hist-keys", regionsHandler.GetKeysHistogram, setMethods(http.MethodGet), setAuditBackend(prometheus))
registerFunc(clusterRouter, "/regions/sibling/{id}", regionsHandler.GetRegionSiblings, setMethods(http.MethodGet), setAuditBackend(prometheus))
registerFunc(clusterRouter, "/regions/accelerate-schedule", regionsHandler.AccelerateRegionsScheduleInRange, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))
registerFunc(clusterRouter, "/regions/accelerate-schedule/batch", regionsHandler.AccelerateRegionsScheduleInRanges, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))
registerFunc(clusterRouter, "/regions/scatter", regionsHandler.ScatterRegions, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))
registerFunc(clusterRouter, "/regions/split", regionsHandler.SplitRegions, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))
registerFunc(clusterRouter, "/regions/range-holes", regionsHandler.GetRangeHoles, setMethods(http.MethodGet), setAuditBackend(prometheus))
Expand Down