-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Changes from 2 commits
61c61db
8479bb7
6cc94e6
007d88d
5d20378
b6aecfb
eca760b
66ef8ce
1ed1548
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,11 +43,12 @@ import ( | |
|
||
const ( | ||
pDBName = "db" | ||
pTableName = "table" | ||
pHexKey = "hexKey" | ||
pRegionID = "regionID" | ||
pRecordID = "recordID" | ||
pStartTS = "startTS" | ||
pHexKey = "hexKey" | ||
pTableID = "table_id" | ||
pTableName = "table" | ||
) | ||
|
||
const ( | ||
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check dbs value? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't check There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(<) | ||
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) | ||
} |
There was a problem hiding this comment.
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
orregion_id
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.