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

status: add new http status API for get meta regions. #4597

Merged
merged 9 commits into from
Oct 10, 2017
1 change: 1 addition & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (s *Server) startHTTPServer() {
tikvHandler := s.newRegionHandler()
// HTTP path for regions
router.Handle("/tables/{db}/{table}/regions", tableRegionsHandler{tikvHandler})
router.Handle("/regions/meta", tikvHandler)
router.Handle("/regions/{regionID}", tikvHandler)
router.Handle("/mvcc/key/{db}/{table}/{recordID}", mvccTxnHandler{tikvHandler, opMvccGetByKey})
router.Handle("/mvcc/txn/{startTS}/{db}/{table}", mvccTxnHandler{tikvHandler, opMvccGetByTxn})
Expand Down
19 changes: 19 additions & 0 deletions server/region_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,25 @@ func (rh tableRegionsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
func (rh regionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// parse and check params
params := mux.Vars(req)
if _, ok := params[pRegionID]; !ok {
startKey := []byte{'m'}
endKey := []byte{'n'}

recordRegionIDs, err := rh.regionCache.ListRegionIDsInKeyRange(rh.bo, startKey, endKey)
if err != nil {
rh.writeError(w, err)
return
}

recordRegions, err := rh.getRegionsMeta(recordRegionIDs)
if err != nil {
rh.writeError(w, err)
return
}
rh.writeData(w, recordRegions)
return
}

regionIDInt, err := strconv.ParseInt(params[pRegionID], 0, 64)
if err != nil {
rh.writeError(w, err)
Expand Down
18 changes: 18 additions & 0 deletions server/region_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ func (ts *TidbRegionHandlerTestSuite) TestGetRegionByIDWithError(c *C) {
defer resp.Body.Close()
}

func (ts *TidbRegionHandlerTestSuite) TestRegionsFromMeta(c *C) {
ts.startServer(c)
defer ts.stopServer(c)
resp, err := http.Get("http://127.0.0.1:10090/regions/meta")
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
Copy link
Member

Choose a reason for hiding this comment

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

Why not verify the body?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now we do verify whether the body is a valid json array or not.

Copy link
Member

Choose a reason for hiding this comment

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

Why not verify if it is region meta?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. PTAL, thanks!


// Verify the resp body.
decoder := json.NewDecoder(resp.Body)
metas := make([]RegionMeta, 0)
err = decoder.Decode(&metas)
c.Assert(err, IsNil)
for _, meta := range metas {
c.Assert(meta.ID != 0, IsTrue)
}
}

func (ts *TidbRegionHandlerTestSuite) startServer(c *C) {
mvccStore := mocktikv.NewMvccStore()
store, err := tikv.NewMockTikvStore(tikv.WithMVCCStore(mvccStore))
Expand Down