Skip to content

Commit

Permalink
Enhance split cmd and fix bug
Browse files Browse the repository at this point in the history
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 committed Jun 27, 2023
1 parent c07dd19 commit 1ac1067
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,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
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 1ac1067

Please sign in to comment.