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) #17864

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -261,16 +261,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:*
nolouch marked this conversation as resolved.
Show resolved Hide resolved

**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:*
nolouch marked this conversation as resolved.
Show resolved Hide resolved

1. Get TiDB server settings

Expand Down
37 changes: 26 additions & 11 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,13 @@ func (t *tikvHandlerTool) getTable(dbName, tableName string) (table.PhysicalTabl
if err != nil {
return nil, errors.Trace(err)
}
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 nil, errors.New("work on partitioned table, please specify the table name like this: table(partition)")
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)
Expand All @@ -320,7 +324,7 @@ func (t *tikvHandlerTool) getTable(dbName, tableName string) (table.PhysicalTabl
return pt.GetPartition(pid), nil
}
if partitionName != "" {
return nil, fmt.Errorf("%s is not a partitionted table", tableName)
return nil, fmt.Errorf("%s is not a partitionted table", tableVal.Meta().Name)
}
return tableVal.(table.PhysicalTable), nil
}
Expand Down Expand Up @@ -934,22 +938,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(tableVal, w)
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 @@ -1108,13 +1123,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 @@ -1137,9 +1152,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