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: support partitioned table for table scatter (#17624) #17862

Closed
Closed
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: 4 additions & 2 deletions docs/tidb_http_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,18 @@ timezone.*
1. Scatter regions of the specified table, add a `scatter-range` scheduler for the PD and the range is same as the table range.

```shell
curl -X POST http://{TiDBIP}:10080/tables/{db}/{table}/scatter
curl http://{TiDBIP}:10080/tables/{db}/{table}/scatter
```
*Hint: On a partitioned table, use the `table(partition)` pattern as the table name, `test(p1)` for example:*

**Note**: The `scatter-range` scheduler may conflict with the global scheduler, do not use it for long periods on the larger table.

1. Stop scatter the regions, disable the `scatter-range` scheduler for the specified table.

```shell
curl -X POST http://{TiDBIP}:10080/tables/{db}/{table}/stop-scatter
curl http://{TiDBIP}:10080/tables/{db}/{table}/stop-scatter
```
*Hint: On a partitioned table, use the `table(partition)` pattern as the table name, `test(p1)` for example:*

1. Get TiDB server settings

Expand Down
51 changes: 42 additions & 9 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,29 @@ func (t *tikvHandlerTool) getTable(dbName, tableName string) (*model.TableInfo,
if err != nil {
return nil, errors.Trace(err)
}
<<<<<<< HEAD
return tableVal.Meta(), nil
=======
return t.getPartition(tableVal, partitionName)
}

func (t *tikvHandlerTool) getPartition(tableVal table.Table, partitionName string) (table.PhysicalTable, error) {
if pt, ok := tableVal.(table.PartitionedTable); ok {
if partitionName == "" {
return tableVal.(table.PhysicalTable), errors.New("work on partitioned table, please specify the table name like this: table(partition)")
}
tblInfo := pt.Meta()
pid, err := tables.FindPartitionByName(tblInfo, partitionName)
if err != nil {
return nil, errors.Trace(err)
}
return pt.GetPartition(pid), nil
}
if partitionName != "" {
return nil, fmt.Errorf("%s is not a partitionted table", tableVal.Meta().Name)
}
return tableVal.(table.PhysicalTable), nil
>>>>>>> 68d9273... server: support partitioned table for table scatter (#17624)
}

func (t *tikvHandlerTool) schema() (infoschema.InfoSchema, error) {
Expand Down Expand Up @@ -933,22 +955,33 @@ func (h tableHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
writeError(w, err)
return
}
// get table's schema.

tableName, partitionName := extractTableAndPartitionName(tableName)
tableVal, err := schema.TableByName(model.NewCIStr(dbName), model.NewCIStr(tableName))
if err != nil {
writeError(w, err)
return
}

switch h.op {
case opTableRegions:
h.handleRegionRequest(schema, tableVal, w, req)
case opTableDiskUsage:
h.handleDiskUsageRequest(schema, tableVal, w, req)
case opTableScatter:
h.handleScatterTableRequest(schema, tableVal, w, req)
// supports partition table, only get one physical table, prevent too many scatter schedulers.
ptbl, err := h.getPartition(tableVal, partitionName)
if err != nil {
writeError(w, err)
return
}
h.handleScatterTableRequest(schema, ptbl, w, req)
case opStopTableScatter:
h.handleStopScatterTableRequest(schema, tableVal, w, req)
ptbl, err := h.getPartition(tableVal, partitionName)
if err != nil {
writeError(w, err)
return
}
h.handleStopScatterTableRequest(schema, ptbl, w, req)
default:
writeError(w, errors.New("method not found"))
}
Expand Down Expand Up @@ -1107,13 +1140,13 @@ func (h tableHandler) deleteScatterSchedule(name string) error {
return nil
}

func (h tableHandler) handleScatterTableRequest(schema infoschema.InfoSchema, tbl table.Table, w http.ResponseWriter, req *http.Request) {
func (h tableHandler) handleScatterTableRequest(schema infoschema.InfoSchema, tbl table.PhysicalTable, w http.ResponseWriter, req *http.Request) {
// for record
tableID := tbl.Meta().ID
tableID := tbl.GetPhysicalID()
startKey, endKey := tablecodec.GetTableHandleKeyRange(tableID)
startKey = codec.EncodeBytes([]byte{}, startKey)
endKey = codec.EncodeBytes([]byte{}, endKey)
tableName := tbl.Meta().Name.String()
tableName := fmt.Sprintf("%s-%d", tbl.Meta().Name.String(), tableID)
err := h.addScatterSchedule(startKey, endKey, tableName)
if err != nil {
writeError(w, errors.Annotate(err, "scatter record error"))
Expand All @@ -1136,9 +1169,9 @@ func (h tableHandler) handleScatterTableRequest(schema infoschema.InfoSchema, tb
writeData(w, "success!")
}

func (h tableHandler) handleStopScatterTableRequest(schema infoschema.InfoSchema, tbl table.Table, w http.ResponseWriter, req *http.Request) {
func (h tableHandler) handleStopScatterTableRequest(schema infoschema.InfoSchema, tbl table.PhysicalTable, w http.ResponseWriter, req *http.Request) {
// for record
tableName := tbl.Meta().Name.String()
tableName := fmt.Sprintf("%s-%d", tbl.Meta().Name.String(), tbl.GetPhysicalID())
err := h.deleteScatterSchedule(tableName)
if err != nil {
writeError(w, errors.Annotate(err, "stop scatter record error"))
Expand Down