Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 59 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,64 @@ 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: ")
regionsIDSet := make(map[uint64]struct{})
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 := rc.ScanRegions(startKey, endKey, limit)
for _, region := range regions {
regionsIDSet[region.GetID()] = struct{}{}
}
msgBuilder.WriteString(fmt.Sprintf("[%s,%s), ", rawStartKey, rawEndKey))
}
if len(regionsIDSet) > 0 {
regionsIDList := make([]uint64, 0, len(regionsIDSet))
for id := range regionsIDSet {
regionsIDList = append(regionsIDList, id)
}
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