Skip to content

Commit

Permalink
status: add new http status API for get meta regions. (#4597)
Browse files Browse the repository at this point in the history
  • Loading branch information
hicqu authored and winoros committed Oct 10, 2017
1 parent e11c4fb commit e9ffb58
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 @@ -289,6 +289,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)

// 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

0 comments on commit e9ffb58

Please sign in to comment.