Skip to content

Commit 7ddabdd

Browse files
committed
add show region by keyspace-id
Signed-off-by: Ryan Leung <rleungx@gmail.com>
1 parent 7cbc382 commit 7ddabdd

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

tools/pd-ctl/pdctl/command/region_command.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import (
3737

3838
"github.com/tikv/pd/client/clients/router"
3939
pd "github.com/tikv/pd/client/http"
40+
"github.com/tikv/pd/pkg/codec"
4041
"github.com/tikv/pd/pkg/utils/typeutil"
4142
"github.com/tikv/pd/tools/pd-ctl/helper/mok"
42-
"github.com/tikv/pd/tools/pd-ctl/helper/tidb/codec"
4343
)
4444

4545
var (
@@ -78,6 +78,7 @@ func NewRegionCommand() *cobra.Command {
7878
r.AddCommand(NewRegionsByKeysCommand())
7979
r.AddCommand(NewRangesWithRangeHolesCommand())
8080
r.AddCommand(NewInvalidTiFlashKeyCommand())
81+
r.AddCommand(NewRegionsByKeyspaceTableIDCommand())
8182

8283
topRead := &cobra.Command{
8384
Use: `topread [byte|query] <limit> [--jq="<query string>"]`,
@@ -418,6 +419,71 @@ func showRegionsByKeysCommandFunc(cmd *cobra.Command, args []string) {
418419
cmd.Println(r)
419420
}
420421

422+
// NewRegionsByKeyspaceTableIDCommand returns regions with keyspace and tableID subcommand of regionCmd.
423+
func NewRegionsByKeyspaceTableIDCommand() *cobra.Command {
424+
r := &cobra.Command{
425+
Use: `keyspace-id <keyspace_id> [table-id <table_id>] [<limit>] [--jq="<query string>"]`,
426+
Short: "show regions for keyspace or table",
427+
Run: showRegionsByKeyspaceTableIDCommandFunc,
428+
}
429+
430+
r.Flags().String("jq", "", "jq query")
431+
return r
432+
}
433+
434+
func showRegionsByKeyspaceTableIDCommandFunc(cmd *cobra.Command, args []string) {
435+
if len(args) < 1 || len(args) > 4 {
436+
cmd.Println(cmd.UsageString())
437+
return
438+
}
439+
keyspaceID, err := strconv.ParseInt(args[0], 10, 32)
440+
if err != nil {
441+
cmd.Println("Error: ", "keyspace-id should be a number")
442+
return
443+
}
444+
startKey := []byte{'x', byte(keyspaceID >> 16), byte(keyspaceID >> 8), byte(keyspaceID)}
445+
nextKeyspaceID := keyspaceID + 1
446+
endKey := []byte{'x', byte(nextKeyspaceID >> 16), byte(nextKeyspaceID >> 8), byte(nextKeyspaceID)}
447+
if len(args) >= 3 {
448+
if args[1] != "table-id" {
449+
cmd.Println("Error: ", "table-id is required")
450+
return
451+
}
452+
tableID, err := strconv.ParseInt(args[2], 10, 64)
453+
if err != nil {
454+
cmd.Println("Error: ", "table-id should be a number")
455+
return
456+
}
457+
nextTableID := tableID + 1
458+
startKey = append(startKey, 't')
459+
startKey = codec.EncodeInt(startKey, tableID)
460+
endKey = append(startKey, 't')
461+
endKey = codec.EncodeInt(endKey, nextTableID)
462+
}
463+
464+
encodedStartKey := codec.EncodeBytes(startKey)
465+
encodedEndKey := codec.EncodeBytes(endKey)
466+
prefix := regionsKeyPrefix + "?key=" + url.QueryEscape(string(encodedStartKey)) + "&end_key=" + url.QueryEscape(string(encodedEndKey))
467+
if len(args) == 2 || len(args) == 4 {
468+
limit, err := strconv.Atoi(args[len(args)-1])
469+
if err != nil {
470+
cmd.Println("Error: ", "limit should be a number")
471+
return
472+
}
473+
prefix += "&limit=" + strconv.Itoa(limit)
474+
}
475+
r, err := doRequest(cmd, prefix, http.MethodGet, http.Header{})
476+
if err != nil {
477+
cmd.Printf("Failed to get region: %s\n", err)
478+
return
479+
}
480+
if flag := cmd.Flag("jq"); flag != nil && flag.Value.String() != "" {
481+
printWithJQFilter(r, flag.Value.String())
482+
return
483+
}
484+
cmd.Println(r)
485+
}
486+
421487
// NewRegionWithCheckCommand returns a region with check subcommand of regionCmd
422488
func NewRegionWithCheckCommand() *cobra.Command {
423489
r := &cobra.Command{

0 commit comments

Comments
 (0)