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: schema info api of http status server #5256

Merged
merged 9 commits into from
Nov 30, 2017
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
3 changes: 3 additions & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (s *Server) startHTTPServer() {
router.Handle("/mvcc/txn/{startTS}/{db}/{table}", mvccTxnHandler{tikvHandler, opMvccGetByTxn})
router.Handle("/mvcc/txn/{startTS}", mvccTxnHandler{tikvHandler, opMvccGetByTxn})
router.Handle("/mvcc/hex/{hexKey}", mvccTxnHandler{tikvHandler, opMvccGetByHex})
router.Handle("/schema", schemaHandler{tikvHandler})
router.Handle("/schema/{db}", schemaHandler{tikvHandler})
router.Handle("/schema/{db}/{table}", schemaHandler{tikvHandler})
}
addr := fmt.Sprintf(":%d", s.cfg.Status.StatusPort)
if s.cfg.Status.StatusPort == 0 {
Expand Down
57 changes: 55 additions & 2 deletions server/region_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ import (

const (
pDBName = "db"
pTableName = "table"
pHexKey = "hexKey"
pRegionID = "regionID"
pRecordID = "recordID"
pStartTS = "startTS"
pHexKey = "hexKey"
pTableID = "table_id"
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we need to use the unified format? tableID or region_id

Copy link
Member Author

Choose a reason for hiding this comment

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

It's based on @AndreMouche 's document. I'll check it out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Has moved it to a standalone part.

pTableName = "table"
)

const (
Expand All @@ -72,6 +73,11 @@ type regionHandler struct {
*regionHandlerTool
}

// schemaHandler is the handler for list database or table schemas.
type schemaHandler struct {
*regionHandler
}

// tableRegionsHandler is the handler for list table's regions.
type tableRegionsHandler struct {
*regionHandler
Expand Down Expand Up @@ -227,6 +233,53 @@ func (t *regionHandlerTool) getRegionsMeta(regionIDs []uint64) ([]RegionMeta, er
return regions, nil
}

// ServeHTTP handles request of list a database or table's schemas.
func (rh schemaHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
schema, err := rh.schema()
if err != nil {
rh.writeError(w, err)
return
}

// parse params
params := mux.Vars(req)

if dbName, ok := params[pDBName]; ok {
cDBName := model.NewCIStr(dbName)
if tableName, ok := params[pTableName]; ok {
// table schema of a specified table name
cTableName := model.NewCIStr(tableName)
if data, err := schema.TableByName(cDBName, cTableName); err != nil {
rh.writeError(w, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we return after writeXXX, since we do not care about this logic anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

} else {
rh.writeData(w, data)
}
} else {
// all table schemas in a specified database
if schema.SchemaExists(cDBName) {
rh.writeData(w, schema.SchemaTables(cDBName))
} else {
rh.writeError(w, infoschema.ErrDatabaseNotExists.GenByArgs(dbName))
}
}
} else if tableID := req.FormValue(pTableID); len(tableID) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

WIth the return in line 266, we do not need the else. So as line 280.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

// table schema of a specified tableID
tid, err := strconv.Atoi(tableID)
if err != nil {
rh.writeError(w, err)
return
}
if data, ok := schema.TableByID(int64(tid)); ok {
rh.writeData(w, data)
} else {
rh.writeError(w, infoschema.ErrTableNotExists.Gen("Table which ID = %s does not exist.", tableID))
}
} else {
// all databases' schemas
rh.writeData(w, schema.AllSchemas())
}
}

// ServeHTTP handles request of list a table's regions.
func (rh tableRegionsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// parse params
Expand Down
46 changes: 46 additions & 0 deletions server/region_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/mock-tikv"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
)
Expand Down Expand Up @@ -302,3 +304,47 @@ func (ts *TidbRegionHandlerTestSuite) TestGetMvccNotFound(c *C) {
c.Assert(err, IsNil)
c.Assert(p.Info, IsNil)
}

func (ts *TidbRegionHandlerTestSuite) TestGetSchema(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)
var dbs []*model.DBInfo
err = decoder.Decode(&dbs)
c.Assert(err, IsNil)
Copy link
Contributor

Choose a reason for hiding this comment

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

check dbs value?

Copy link
Member Author

Choose a reason for hiding this comment

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

done


resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=5"))
c.Assert(err, IsNil)
var t *tables.MemoryTable
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&t)
c.Assert(err, IsNil)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Member Author

Choose a reason for hiding this comment

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

done


resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=a"))
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't check resp, using _ is better.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

c.Assert(err, IsNil)

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema?table_id=1"))
c.Assert(err, IsNil)

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/tidb"))
c.Assert(err, IsNil)
var lt []*tables.MemoryTable
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&lt)
c.Assert(err, IsNil)

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/abc"))
c.Assert(err, IsNil)

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/tidb/test"))
c.Assert(err, IsNil)
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&t)
c.Assert(err, IsNil)

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/mysql/abc"))
c.Assert(err, IsNil)
}