From 317c1bcdd0d369598b2ec340a105a50e5ed3db57 Mon Sep 17 00:00:00 2001 From: Bin Shi <39923490+binshi-bing@users.noreply.github.com> Date: Mon, 26 Jun 2023 20:34:05 -0700 Subject: [PATCH] Add split-range cmd and fix duplicate keyspaces (#6689) close tikv/pd#6687, close tikv/pd#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 --- pkg/keyspace/tso_keyspace_group.go | 10 ++++- tools/pd-ctl/pdctl/command/global.go | 2 +- .../pdctl/command/keyspace_group_command.go | 42 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pkg/keyspace/tso_keyspace_group.go b/pkg/keyspace/tso_keyspace_group.go index fe91443bb95..c4916f5f526 100644 --- a/pkg/keyspace/tso_keyspace_group.go +++ b/pkg/keyspace/tso_keyspace_group.go @@ -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 { diff --git a/tools/pd-ctl/pdctl/command/global.go b/tools/pd-ctl/pdctl/command/global.go index 623ab3edfba..8d888b60b1f 100644 --- a/tools/pd-ctl/pdctl/command/global.go +++ b/tools/pd-ctl/pdctl/command/global.go @@ -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!") diff --git a/tools/pd-ctl/pdctl/command/keyspace_group_command.go b/tools/pd-ctl/pdctl/command/keyspace_group_command.go index 3e46df39e63..b5ccaf01e2b 100644 --- a/tools/pd-ctl/pdctl/command/keyspace_group_command.go +++ b/tools/pd-ctl/pdctl/command/keyspace_group_command.go @@ -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()) @@ -50,6 +51,15 @@ func newSplitKeyspaceGroupCommand() *cobra.Command { return r } +func newSplitRangeKeyspaceGroupCommand() *cobra.Command { + r := &cobra.Command{ + Use: "split-range ", + 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 ", @@ -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()