Skip to content

Commit

Permalink
server/api: fix the issue about regions/check API (#1311)
Browse files Browse the repository at this point in the history
* server/api: fix region check API interface
  • Loading branch information
nolouch authored and disksing committed Nov 7, 2018
1 parent baf3d4c commit aa57c78
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
75 changes: 31 additions & 44 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,25 @@ func newRegionsHandler(svr *server.Server, rd *render.Render) *regionsHandler {
}
}

func (h *regionsHandler) GetAll(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}
regions := cluster.GetRegions()
func convertToAPIRegions(regions []*core.RegionInfo) *regionsInfo {
regionInfos := make([]*regionInfo, len(regions))
for i, r := range regions {
regionInfos[i] = newRegionInfo(r)
}
regionsInfo := &regionsInfo{
return &regionsInfo{
Count: len(regions),
Regions: regionInfos,
}
}

func (h *regionsHandler) GetAll(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}
regions := cluster.GetRegions()
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

Expand All @@ -162,15 +166,7 @@ func (h *regionsHandler) ScanRegionsByKey(w http.ResponseWriter, r *http.Request
limit = maxRegionLimit
}
regions := cluster.ScanRegionsByKey([]byte(startKey), limit)

regionInfos := make([]*regionInfo, 0, len(regions))
for _, region := range regions {
regionInfos = append(regionInfos, newRegionInfo(region))
}
regionsInfo := &regionsInfo{
Count: len(regionInfos),
Regions: regionInfos,
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

Expand All @@ -188,65 +184,63 @@ func (h *regionsHandler) GetStoreRegions(w http.ResponseWriter, r *http.Request)
return
}
regions := cluster.GetStoreRegions(uint64(id))
regionInfos := make([]*regionInfo, len(regions))
for i, r := range regions {
regionInfos[i] = newRegionInfo(r)
}
regionsInfo := &regionsInfo{
Count: len(regions),
Regions: regionInfos,
}
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

func (h *regionsHandler) GetMissPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
res, err := handler.GetMissPeerRegions()
regions, err := handler.GetMissPeerRegions()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, res)
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

func (h *regionsHandler) GetExtraPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
res, err := handler.GetExtraPeerRegions()
regions, err := handler.GetExtraPeerRegions()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, res)
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

func (h *regionsHandler) GetPendingPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
res, err := handler.GetPendingPeerRegions()
regions, err := handler.GetPendingPeerRegions()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, res)
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

func (h *regionsHandler) GetDownPeerRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
res, err := handler.GetDownPeerRegions()
regions, err := handler.GetDownPeerRegions()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, res)
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

func (h *regionsHandler) GetIncorrectNamespaceRegions(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
res, err := handler.GetIncorrectNamespaceRegions()
regions, err := handler.GetIncorrectNamespaceRegions()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, res)
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

func (h *regionsHandler) GetRegionSiblings(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -323,15 +317,8 @@ func (h *regionsHandler) GetTopNRegions(w http.ResponseWriter, r *http.Request,
limit = maxRegionLimit
}
regions := topNRegions(cluster.GetRegions(), less, limit)
regionInfos := make([]*regionInfo, len(regions))
for i, r := range regions {
regionInfos[i] = newRegionInfo(r)
}
res := &regionsInfo{
Count: len(regions),
Regions: regionInfos,
}
h.rd.JSON(w, http.StatusOK, res)
regionsInfo := convertToAPIRegions(regions)
h.rd.JSON(w, http.StatusOK, regionsInfo)
}

// RegionHeap implements heap.Interface, used for selecting top n regions.
Expand Down
25 changes: 25 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/core"
)
Expand Down Expand Up @@ -84,6 +85,30 @@ func (s *testRegionSuite) TestRegion(c *C) {
c.Assert(r2, DeepEquals, newRegionInfo(r))
}

func (s *testRegionSuite) TestRegionCheck(c *C) {
r := newTestRegionInfo(2, 1, []byte("a"), []byte("b"))
downPeer := &metapb.Peer{Id: 13, StoreId: 2}
r = r.Clone(core.WithAddPeer(downPeer), core.WithDownPeers([]*pdpb.PeerStats{{Peer: downPeer, DownSeconds: 3600}}), core.WithPendingPeers([]*metapb.Peer{downPeer}))
mustRegionHeartbeat(c, s.svr, r)
url := fmt.Sprintf("%s/region/id/%d", s.urlPrefix, r.GetID())
r1 := &regionInfo{}
err := readJSONWithURL(url, r1)
c.Assert(err, IsNil)
c.Assert(r1, DeepEquals, newRegionInfo(r))

url = fmt.Sprintf("%s/regions/check/%s", s.urlPrefix, "down-peer")
r2 := &regionsInfo{}
err = readJSONWithURL(url, r2)
c.Assert(err, IsNil)
c.Assert(r2, DeepEquals, &regionsInfo{Count: 1, Regions: []*regionInfo{newRegionInfo(r)}})

url = fmt.Sprintf("%s/regions/check/%s", s.urlPrefix, "pending-peer")
r3 := &regionsInfo{}
err = readJSONWithURL(url, r3)
c.Assert(err, IsNil)
c.Assert(r3, DeepEquals, &regionsInfo{Count: 1, Regions: []*regionInfo{newRegionInfo(r)}})
}

func (s *testRegionSuite) TestRegions(c *C) {
rs := []*core.RegionInfo{
newTestRegionInfo(2, 1, []byte("a"), []byte("b")),
Expand Down

0 comments on commit aa57c78

Please sign in to comment.