Skip to content

Commit

Permalink
api: support add region and hot region scheduler. (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing authored and siddontang committed Nov 23, 2017
1 parent 9ad56b4 commit d79ca66
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
5 changes: 1 addition & 4 deletions server/api/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ func (s *testOperatorSuite) TestAddRemovePeer(c *C) {
operator = mustReadURL(c, regionURL)
c.Assert(strings.Contains(operator, "add peer 1 on store 3"), IsTrue)

req, err := http.NewRequest("DELETE", regionURL, nil)
err = doDelete(regionURL)
c.Assert(err, IsNil)
res, err := http.DefaultClient.Do(req)
c.Assert(err, IsNil)
res.Body.Close()

err = postJSON(&http.Client{}, fmt.Sprintf("%s/operators", s.urlPrefix), []byte(`{"name":"remove-peer", "region_id": 1, "store_id": 2}`))
c.Assert(err, IsNil)
Expand Down
11 changes: 11 additions & 0 deletions server/api/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func (h *schedulerHandler) Post(w http.ResponseWriter, r *http.Request) {
h.r.JSON(w, http.StatusInternalServerError, err.Error())
return
}
case "balance-hot-region-scheduler":
if err := h.AddBalanceHotRegionScheduler(); err != nil {
h.r.JSON(w, http.StatusInternalServerError, err.Error())
return
}

case "balance-region-scheduler":
if err := h.AddBalanceRegionScheduler(); err != nil {
h.r.JSON(w, http.StatusInternalServerError, err.Error())
return
}
case "grant-leader-scheduler":
storeID, ok := input["store_id"].(float64)
if !ok {
Expand Down
103 changes: 103 additions & 0 deletions server/api/scheduler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"encoding/json"
"fmt"
"net/http"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server"
_ "github.com/pingcap/pd/server/schedulers"
)

var _ = Suite(&testScheduleSuite{})

type testScheduleSuite struct {
svr *server.Server
cleanup cleanUpFunc
urlPrefix string
}

func (s *testScheduleSuite) SetUpSuite(c *C) {
s.svr, s.cleanup = mustNewServer(c)
mustWaitLeader(c, []*server.Server{s.svr})

addr := s.svr.GetAddr()
s.urlPrefix = fmt.Sprintf("%s%s/api/v1/schedulers", addr, apiPrefix)

mustBootstrapCluster(c, s.svr)
mustPutStore(c, s.svr, 1, metapb.StoreState_Up, nil)
}

func (s *testScheduleSuite) TearDownSuite(c *C) {
s.cleanup()
}

func (s *testScheduleSuite) TestAPI(c *C) {
type arg struct {
opt string
value interface{}
}
cases := []struct {
name string
createdName string
args []arg
}{
{name: "balance-leader-scheduler"},
{name: "balance-hot-region-scheduler"},
{name: "balance-region-scheduler"},
{name: "shuffle-leader-scheduler"},
{name: "shuffle-region-scheduler"},
{
name: "grant-leader-scheduler",
createdName: "grant-leader-scheduler-1",
args: []arg{{"store_id", 1}},
},
{
name: "evict-leader-scheduler",
createdName: "evict-leader-scheduler-1",
args: []arg{{"store_id", 1}},
},
}
for _, ca := range cases {
input := make(map[string]interface{})
input["name"] = ca.name
for _, a := range ca.args {
input[a.opt] = a.value
}
body, err := json.Marshal(input)
c.Assert(err, IsNil)
s.testAddAndRemoveScheduler(ca.name, ca.createdName, body, c)
}

}

func (s *testScheduleSuite) testAddAndRemoveScheduler(name, createdName string, body []byte, c *C) {
if createdName == "" {
createdName = name
}
err := postJSON(&http.Client{}, s.urlPrefix, body)
c.Assert(err, IsNil)
handler := s.svr.GetHandler()
sches, err := handler.GetSchedulers()
c.Assert(err, IsNil)
c.Assert(sches[0], Equals, createdName)

deleteURL := fmt.Sprintf("%s/%s", s.urlPrefix, createdName)
err = doDelete(deleteURL)
c.Assert(err, IsNil)
}
13 changes: 13 additions & 0 deletions server/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ func postJSON(cli *http.Client, url string, data []byte) error {
}
return nil
}

func doDelete(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
res.Body.Close()
return nil
}
20 changes: 20 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ func (h *Handler) AddBalanceLeaderScheduler() error {
return h.AddScheduler(s)
}

// AddBalanceRegionScheduler adds a balance-region-scheduler.
func (h *Handler) AddBalanceRegionScheduler() error {
s, err := schedule.CreateScheduler("balance-region", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}

// AddBalanceHotRegionScheduler adds a balance-hot-region-scheduler.
func (h *Handler) AddBalanceHotRegionScheduler() error {
s, err := schedule.CreateScheduler("hot-region", h.opt)
if err != nil {
return errors.Trace(err)
}
log.Infof("create scheduler %s", s.GetName())
return h.AddScheduler(s)
}

// AddGrantLeaderScheduler adds a grant-leader-scheduler.
func (h *Handler) AddGrantLeaderScheduler(storeID uint64) error {
s, err := schedule.CreateScheduler("grant-leader", h.opt, strconv.FormatUint(storeID, 10))
Expand Down

0 comments on commit d79ca66

Please sign in to comment.