Skip to content

Commit

Permalink
add promote api
Browse files Browse the repository at this point in the history
lint by comment

address the comment

use better desc

fix desc

fix test

fix test

debug test

fix test

fix desc

revise code

fix duplicated tag

address the comments
  • Loading branch information
Yisaer committed Jul 2, 2020
1 parent af5511a commit ff05a45
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
66 changes: 66 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package api

import (
"container/heap"
"fmt"
"net/http"
"net/url"
"sort"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/kvproto/pkg/replication_modepb"
"github.com/pingcap/pd/v4/pkg/apiutil"
"github.com/pingcap/pd/v4/server"
"github.com/pingcap/pd/v4/server/core"
"github.com/unrolled/render"
Expand Down Expand Up @@ -557,6 +559,70 @@ func (h *regionsHandler) GetTopSize(w http.ResponseWriter, r *http.Request) {
})
}

// @Tags region
// @Summary Accelerate regions schedule in given range
// @Accept json
// @Param body body object true "json params"
// @Param limit query integer false "Limit count" default(256)
// @Produce json
// @Success 200 {string} string "The regions in given range have been improve schedule priority"
// @Failure 400 {string} string "The input is invalid."
// @Router /regions/accelerate-schedule [post]
func (h *regionsHandler) AccelerateRegionsScheduleInRange(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r.Context())
var input map[string]interface{}
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
return
}
parseKey := func(name string, input map[string]interface{}) (string, error) {
k, ok := input[name]
if !ok {
return "", fmt.Errorf("missing %s", name)
}
key, ok := k.(string)
if !ok {
return "", fmt.Errorf("bad format %s", name)
}
return key, nil
}

startKey, err := parseKey("start_key", input)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

endKey, err := parseKey("end_key", input)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
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
}

regions := rc.ScanRegions([]byte(startKey), []byte(endKey), limit)
if len(regions) > 0 {
regionsIDList := make([]uint64, 0, len(regions))
for _, region := range regions {
regionsIDList = append(regionsIDList, region.GetID())
}
rc.AddSuspectRegions(regionsIDList...)
}

h.rd.JSON(w, http.StatusOK, nil)
}

func (h *regionsHandler) GetTopNRegions(w http.ResponseWriter, r *http.Request, less func(a, b *core.RegionInfo) bool) {
rc := getCluster(r.Context())
limit := defaultRegionLimit
Expand Down
12 changes: 12 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ func (s *testRegionSuite) TestTopSize(c *C) {
s.checkTopRegions(c, fmt.Sprintf("%s/regions/size?limit=%d", s.urlPrefix, 2), []uint64{7, 8})
}

func (s *testRegionSuite) TestAccelerateRegionsScheduleInRange(c *C) {
r1 := newTestRegionInfo(557, 13, []byte("a1"), []byte("a2"))
r2 := newTestRegionInfo(558, 14, []byte("a2"), []byte("a3"))
r3 := newTestRegionInfo(559, 15, []byte("a3"), []byte("a4"))
mustRegionHeartbeat(c, s.svr, r1)
mustRegionHeartbeat(c, s.svr, r2)
mustRegionHeartbeat(c, s.svr, r3)

err := postJSON(testDialClient, fmt.Sprintf("%s/regions/accelerate-schedule", s.urlPrefix), []byte(`{"start_key":"a1", "end_key": "a4"}`))
c.Assert(err, IsNil)
}

func (s *testRegionSuite) checkTopRegions(c *C, url string, regionIDs []uint64) {
regions := &RegionsInfo{}
err := readJSON(testDialClient, url, regions)
Expand Down
1 change: 1 addition & 0 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func createRouter(ctx context.Context, prefix string, svr *server.Server) *mux.R
clusterRouter.HandleFunc("/regions/check/hist-size", regionsHandler.GetSizeHistogram).Methods("GET")
clusterRouter.HandleFunc("/regions/check/hist-keys", regionsHandler.GetKeysHistogram).Methods("GET")
clusterRouter.HandleFunc("/regions/sibling/{id}", regionsHandler.GetRegionSiblings).Methods("GET")
clusterRouter.HandleFunc("/regions/accelerate-schedule", regionsHandler.AccelerateRegionsScheduleInRange).Methods("POST")

apiRouter.Handle("/version", newVersionHandler(rd)).Methods("GET")
apiRouter.Handle("/status", newStatusHandler(svr, rd)).Methods("GET")
Expand Down

0 comments on commit ff05a45

Please sign in to comment.