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

http: add http api for get db and table info that is related to the tableID (#8256) #9408

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions docs/tidb_http_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
curl http://{TiDBIP}:10080/schema?table_id={tableID}
```

1. Get database information, table information and tidb info schema version by tableID.

```shell
curl http://{TiDBIP}:10080/db-table/{tableID}
```

1. Get MVCC Information of the key with a specified handle ID

```shell
Expand Down
15 changes: 15 additions & 0 deletions infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type InfoSchema interface {
TableByName(schema, table model.CIStr) (table.Table, error)
TableExists(schema, table model.CIStr) bool
SchemaByID(id int64) (*model.DBInfo, bool)
SchemaByTable(tableInfo *model.TableInfo) (*model.DBInfo, bool)
TableByID(id int64) (table.Table, bool)
AllocByID(id int64) (autoid.Allocator, bool)
AllSchemaNames() []string
Expand Down Expand Up @@ -194,6 +195,20 @@ func (is *infoSchema) SchemaByID(id int64) (val *model.DBInfo, ok bool) {
return nil, false
}

func (is *infoSchema) SchemaByTable(tableInfo *model.TableInfo) (val *model.DBInfo, ok bool) {
if tableInfo == nil {
return nil, false
}
for _, v := range is.schemaMap {
if tbl, ok := v.tables[tableInfo.Name.L]; ok {
if tbl.Meta().ID == tableInfo.ID {
return v.dbInfo, true
}
}
}
return nil, false
}

func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) {
slice := is.sortedTablesBuckets[tableBucketIdx(id)]
idx := slice.searchTable(id)
Expand Down
47 changes: 47 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
pRegionID = "regionID"
pStartTS = "startTS"
pTableName = "table"
pTableID = "tableID"
pColumnID = "colID"
pColumnTp = "colTp"
pColumnFlag = "colFlag"
Expand Down Expand Up @@ -317,6 +318,10 @@ type schemaHandler struct {
*tikvHandlerTool
}

type dbTableHandler struct {
*tikvHandlerTool
}

// regionHandler is the common field for http handler. It contains
// some common functions for all handlers.
type regionHandler struct {
Expand Down Expand Up @@ -1362,3 +1367,45 @@ func (h allServerInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
}
writeData(w, clusterInfo)
}

// dbTableInfo is used to report the database, table information and the current schema version.
type dbTableInfo struct {
DBInfo *model.DBInfo `json:"db_info"`
TableInfo *model.TableInfo `json:"table_info"`
SchemaVersion int64 `json:"schema_version"`
}

//ServeHTTP handles request of database information and table information by tableID.
func (h dbTableHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
tableID := params[pTableID]
tblID, err := strconv.Atoi(tableID)
if err != nil {
writeError(w, errors.Errorf("Wrong tableID: %v", tableID))
return
}

schema, err := h.schema()
if err != nil {
writeError(w, err)
return
}

dbTblInfo := dbTableInfo{
SchemaVersion: schema.SchemaMetaVersion(),
}
tbl, ok := schema.TableByID(int64(tblID))
if !ok {
writeError(w, infoschema.ErrTableNotExists.GenWithStack("Table which ID = %s does not exist.", tableID))
return
}
dbTblInfo.TableInfo = tbl.Meta()
dbInfo, ok := schema.SchemaByTable(dbTblInfo.TableInfo)
if !ok {
log.Warnf("can not find the database of table id: %v, table name: %v", dbTblInfo.TableInfo.ID, dbTblInfo.TableInfo.Name)
writeData(w, dbTblInfo)
return
}
dbTblInfo.DBInfo = dbInfo
writeData(w, dbTblInfo)
}
12 changes: 12 additions & 0 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,18 @@ func (ts *HTTPHandlerTestSuite) TestGetSchema(c *C) {

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

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/db-table/5"))
c.Assert(err, IsNil)
var dbtbl *dbTableInfo
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&dbtbl)
c.Assert(err, IsNil)
c.Assert(dbtbl.TableInfo.Name.L, Equals, "user")
c.Assert(dbtbl.DBInfo.Name.L, Equals, "mysql")
se, err := session.CreateSession(ts.store.(kv.Storage))
c.Assert(err, IsNil)
c.Assert(dbtbl.SchemaVersion, Equals, domain.GetDomain(se.(sessionctx.Context)).InfoSchema().SchemaMetaVersion())
}

func (ts *HTTPHandlerTestSuite) TestAllHistory(c *C) {
Expand Down
3 changes: 3 additions & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (s *Server) startHTTPServer() {
// HTTP path for get server info.
router.Handle("/info", serverInfoHandler{tikvHandlerTool})
router.Handle("/info/all", allServerInfoHandler{tikvHandlerTool})
// HTTP path for get db and table info that is related to the tableID.
router.Handle("/db-table/{tableID}", dbTableHandler{tikvHandlerTool})

if s.cfg.Store == "tikv" {
// HTTP path for tikv.
router.Handle("/tables/{db}/{table}/regions", tableHandler{tikvHandlerTool, opTableRegions})
Expand Down