Skip to content

Commit

Permalink
Add split-range cmd and fix duplicate keyspaces (tikv#6689)
Browse files Browse the repository at this point in the history
close tikv#6687, close tikv#6688

Add split-range cmd and fix duplicate keyspaces
    
    1. Add split-range cmd to support StartKeyspaceID and EndKeyspaceID parameters.
    2. Fix "split 0 2 2 2" generate duplicate keyspaces in the keyspace list of the group"

Signed-off-by: Bin Shi <binshi.bing@gmail.com>
  • Loading branch information
binshi-bing authored and rleungx committed Aug 2, 2023
1 parent 45f697d commit 3628a5c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
10 changes: 9 additions & 1 deletion pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,15 @@ func buildSplitKeyspaces(
oldSplit = append(oldSplit, keyspace)
}
}
return oldSplit, new, nil
// Dedup new keyspaces if it's necessary.
if newNum == len(newKeyspaceMap) {
return oldSplit, new, nil
}
newSplit := make([]uint32, 0, len(newKeyspaceMap))
for keyspace := range newKeyspaceMap {
newSplit = append(newSplit, keyspace)
}
return oldSplit, newSplit, nil
}
// Split according to the start and end keyspace ID.
if startKeyspaceID == 0 && endKeyspaceID == 0 {
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) {
return nil
})
if err != nil {
cmd.Printf("Failed! %s", err)
cmd.Printf("Failed! %s\n", err)
return
}
cmd.Println("Success!")
Expand Down
42 changes: 42 additions & 0 deletions tools/pd-ctl/pdctl/command/keyspace_group_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewKeyspaceGroupCommand() *cobra.Command {
Run: showKeyspaceGroupCommandFunc,
}
cmd.AddCommand(newSplitKeyspaceGroupCommand())
cmd.AddCommand(newSplitRangeKeyspaceGroupCommand())
cmd.AddCommand(newFinishSplitKeyspaceGroupCommand())
cmd.AddCommand(newMergeKeyspaceGroupCommand())
cmd.AddCommand(newFinishMergeKeyspaceGroupCommand())
Expand All @@ -50,6 +51,15 @@ func newSplitKeyspaceGroupCommand() *cobra.Command {
return r
}

func newSplitRangeKeyspaceGroupCommand() *cobra.Command {
r := &cobra.Command{
Use: "split-range <keyspace_group_id> <new_keyspace_group_id> <start_keyspace_id> <end_keyspace_id>",
Short: "split the keyspace group with the given ID and transfer the keyspaces in the given range (both ends inclusive) into the newly split one",
Run: splitRangeKeyspaceGroupCommandFunc,
}
return r
}

func newFinishSplitKeyspaceGroupCommand() *cobra.Command {
r := &cobra.Command{
Use: "finish-split <keyspace_group_id>",
Expand Down Expand Up @@ -140,6 +150,38 @@ func splitKeyspaceGroupCommandFunc(cmd *cobra.Command, args []string) {
})
}

func splitRangeKeyspaceGroupCommandFunc(cmd *cobra.Command, args []string) {
if len(args) < 4 {
cmd.Usage()
return
}
_, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
cmd.Printf("Failed to parse the old keyspace group ID: %s\n", err)
return
}
newID, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
cmd.Printf("Failed to parse the new keyspace group ID: %s\n", err)
return
}
startKeyspaceID, err := strconv.ParseUint(args[2], 10, 32)
if err != nil {
cmd.Printf("Failed to parse the start keyspace ID: %s\n", err)
return
}
endKeyspaceID, err := strconv.ParseUint(args[3], 10, 32)
if err != nil {
cmd.Printf("Failed to parse the end keyspace ID: %s\n", err)
return
}
postJSON(cmd, fmt.Sprintf("%s/%s/split", keyspaceGroupsPrefix, args[0]), map[string]interface{}{
"new-id": uint32(newID),
"start-keyspace-id": uint32(startKeyspaceID),
"end-keyspace-id": uint32(endKeyspaceID),
})
}

func finishSplitKeyspaceGroupCommandFunc(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Usage()
Expand Down

0 comments on commit 3628a5c

Please sign in to comment.