Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server/api: fix the issue about regions/check API #1311

Merged
merged 4 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 30 additions & 43 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,14 +317,7 @@ 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,
}
res := convertToAPIRegions(regions)
Copy link
Member

Choose a reason for hiding this comment

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

regionsInfo?

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

Expand Down
27 changes: 27 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"net/url"
"sort"

"github.com/pingcap/kvproto/pkg/pdpb"

Copy link
Contributor

Choose a reason for hiding this comment

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

Extra empty line?

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/pd/server"
Expand Down Expand Up @@ -84,6 +86,31 @@ 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)}})

Copy link
Member

Choose a reason for hiding this comment

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

blank line

}

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