From 94f24319ca11a61c41f03f4abcd143a44843e161 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Tue, 4 Jul 2023 19:36:26 +0800 Subject: [PATCH 1/5] pdctl: support show keyspace group primary Signed-off-by: lhy1024 --- pkg/keyspace/tso_keyspace_group.go | 22 +++++ pkg/keyspace/util.go | 3 + pkg/tso/keyspace_group_manager.go | 1 + server/apiv2/handlers/tso_keyspace_group.go | 28 ++++++ tests/pdctl/keyspace/keyspace_group_test.go | 95 +++++++++++++++++++ .../pdctl/command/keyspace_group_command.go | 28 ++++++ 6 files changed, 177 insertions(+) diff --git a/pkg/keyspace/tso_keyspace_group.go b/pkg/keyspace/tso_keyspace_group.go index dd9319e806f..457c11edcfb 100644 --- a/pkg/keyspace/tso_keyspace_group.go +++ b/pkg/keyspace/tso_keyspace_group.go @@ -17,6 +17,7 @@ package keyspace import ( "context" "encoding/json" + "fmt" "sort" "strconv" "strings" @@ -25,6 +26,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" + "github.com/pingcap/kvproto/pkg/tsopb" "github.com/pingcap/log" "github.com/tikv/pd/pkg/balancer" "github.com/tikv/pd/pkg/mcs/discovery" @@ -1010,3 +1012,23 @@ func (m *GroupManager) FinishMergeKeyspaceByID(mergeTargetID uint32) error { zap.Reflect("merge-list", mergeList)) return nil } + +func (m *GroupManager) GetKeyspaceGroupPrimaryByID(id uint32) (string, error) { + // default keyspace group: "/ms/{cluster_id}/tso/00000/primary". + // non-default keyspace group: "/ms/{cluster_id}/tso/keyspace_groups/election/{group}/primary". + path := fmt.Sprintf("/ms/%d/tso/00000/primary", m.clusterID) + if id != utils.DefaultKeyspaceGroupID { + path = fmt.Sprintf("/ms/%d/tso/keyspace_groups/election/%05d/primary", m.clusterID, id) + } + leader := &tsopb.Participant{} + ok, _, err := etcdutil.GetProtoMsgWithModRev(m.client, path, leader) + if err != nil { + return "", err + } + if !ok { + return "", ErrKeyspaceGroupPrimaryNotFound + } + // The format of leader name is address-groupID. + contents := strings.Split(leader.GetName(), "-") + return contents[0], err +} diff --git a/pkg/keyspace/util.go b/pkg/keyspace/util.go index 240306f8124..100b0eb6986 100644 --- a/pkg/keyspace/util.go +++ b/pkg/keyspace/util.go @@ -94,6 +94,9 @@ var ( } // Only keyspaces in the state specified by allowChangeConfig are allowed to change their config. allowChangeConfig = []keyspacepb.KeyspaceState{keyspacepb.KeyspaceState_ENABLED, keyspacepb.KeyspaceState_DISABLED} + + // ErrKeyspaceGroupPrimaryNotFound is used to indicate primary of target keyspace group does not exist. + ErrKeyspaceGroupPrimaryNotFound = errors.New("primary of keyspace group does not exist") ) // validateID check if keyspace falls within the acceptable range. diff --git a/pkg/tso/keyspace_group_manager.go b/pkg/tso/keyspace_group_manager.go index 529e9bcdd37..d74a3f417ca 100644 --- a/pkg/tso/keyspace_group_manager.go +++ b/pkg/tso/keyspace_group_manager.go @@ -683,6 +683,7 @@ func (kgm *KeyspaceGroupManager) updateKeyspaceGroup(group *endpoint.KeyspaceGro } // If the keyspace group is not initialized, initialize it. + // The format of leader name is address-groupID. uniqueName := fmt.Sprintf("%s-%05d", kgm.electionNamePrefix, group.ID) uniqueID := memberutil.GenerateUniqueID(uniqueName) log.Info("joining primary election", diff --git a/server/apiv2/handlers/tso_keyspace_group.go b/server/apiv2/handlers/tso_keyspace_group.go index 7030c332406..a5265ca7e02 100644 --- a/server/apiv2/handlers/tso_keyspace_group.go +++ b/server/apiv2/handlers/tso_keyspace_group.go @@ -47,6 +47,7 @@ func RegisterTSOKeyspaceGroup(r *gin.RouterGroup) { router.DELETE("/:id/split", FinishSplitKeyspaceByID) router.POST("/:id/merge", MergeKeyspaceGroups) router.DELETE("/:id/merge", FinishMergeKeyspaceByID) + router.GET("/:id/primary", GetKeyspaceGroupPrimaryByID) } // CreateKeyspaceGroupParams defines the params for creating keyspace groups. @@ -485,6 +486,33 @@ func SetPriorityForKeyspaceGroup(c *gin.Context) { c.JSON(http.StatusOK, nil) } +func GetKeyspaceGroupPrimaryByID(c *gin.Context) { + id, err := validateKeyspaceGroupID(c) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, "invalid keyspace group id") + return + } + svr := c.MustGet(middlewares.ServerContextKey).(*server.Server) + manager := svr.GetKeyspaceGroupManager() + if manager == nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, groupManagerUninitializedErr) + return + } + // check if keyspace group exists + kg, err := manager.GetKeyspaceGroupByID(id) + if err != nil || kg == nil { + c.AbortWithStatusJSON(http.StatusBadRequest, "keyspace group does not exist") + return + } + // get primary + primary, err := manager.GetKeyspaceGroupPrimaryByID(id) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) + return + } + c.JSON(http.StatusOK, primary) +} + func validateKeyspaceGroupID(c *gin.Context) (uint32, error) { id, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil { diff --git a/tests/pdctl/keyspace/keyspace_group_test.go b/tests/pdctl/keyspace/keyspace_group_test.go index c982dc140af..50da5a3fd77 100644 --- a/tests/pdctl/keyspace/keyspace_group_test.go +++ b/tests/pdctl/keyspace/keyspace_group_test.go @@ -450,3 +450,98 @@ func TestKeyspaceGroupState(t *testing.T) { re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes")) re.NoError(failpoint.Disable("github.com/tikv/pd/server/delayStartServerLoop")) } + +func TestShowKeyspaceGroupPrimary(t *testing.T) { + re := require.New(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes", `return(true)`)) + re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/tso/fastGroupSplitPatroller", `return(true)`)) + re.NoError(failpoint.Enable("github.com/tikv/pd/server/delayStartServerLoop", `return(true)`)) + keyspaces := make([]string, 0) + for i := 0; i < 10; i++ { + keyspaces = append(keyspaces, fmt.Sprintf("keyspace_%d", i)) + } + tc, err := tests.NewTestAPICluster(ctx, 3, func(conf *config.Config, serverName string) { + conf.Keyspace.PreAlloc = keyspaces + }) + re.NoError(err) + err = tc.RunInitialServers() + re.NoError(err) + pdAddr := tc.GetConfig().GetClientURL() + + s1, tsoServerCleanup1, err := tests.StartSingleTSOTestServer(ctx, re, pdAddr, tempurl.Alloc()) + defer tsoServerCleanup1() + re.NoError(err) + s2, tsoServerCleanup2, err := tests.StartSingleTSOTestServer(ctx, re, pdAddr, tempurl.Alloc()) + defer tsoServerCleanup2() + re.NoError(err) + cmd := pdctlCmd.GetRootCmd() + + tc.WaitLeader() + leaderServer := tc.GetServer(tc.GetLeader()) + re.NoError(leaderServer.BootstrapCluster()) + defaultKeyspaceGroupID := fmt.Sprintf("%d", utils.DefaultKeyspaceGroupID) + + // check keyspace group 0 information. + var keyspaceGroup endpoint.KeyspaceGroup + testutil.Eventually(re, func() bool { + args := []string{"-u", pdAddr, "keyspace-group"} + output, err := pdctl.ExecuteCommand(cmd, append(args, defaultKeyspaceGroupID)...) + re.NoError(err) + + err = json.Unmarshal(output, &keyspaceGroup) + re.NoError(err) + re.Equal(utils.DefaultKeyspaceGroupID, keyspaceGroup.ID) + return len(keyspaceGroup.Members) == 2 + }) + for _, member := range keyspaceGroup.Members { + re.Contains([]string{s1.GetAddr(), s2.GetAddr()}, member.Address) + } + + // get primary for keyspace group 0. + testutil.Eventually(re, func() bool { + args := []string{"-u", pdAddr, "keyspace-group", "primary", defaultKeyspaceGroupID} + output, err := pdctl.ExecuteCommand(cmd, args...) + re.NoError(err) + addr := strings.ReplaceAll(string(output), "\"", "") + addr = strings.ReplaceAll(addr, "\n", "") + return s1.GetAddr() == addr || s2.GetAddr() == addr + }) + + // split keyspace group. + testutil.Eventually(re, func() bool { + args := []string{"-u", pdAddr, "keyspace-group", "split", "0", "1", "2"} + output, err := pdctl.ExecuteCommand(cmd, args...) + re.NoError(err) + return strings.Contains(string(output), "Success") + }) + + // check keyspace group 1 information. + testutil.Eventually(re, func() bool { + args := []string{"-u", pdAddr, "keyspace-group"} + output, err := pdctl.ExecuteCommand(cmd, append(args, "1")...) + re.NoError(err) + + err = json.Unmarshal(output, &keyspaceGroup) + re.NoError(err) + return len(keyspaceGroup.Members) == 2 + }) + for _, member := range keyspaceGroup.Members { + re.Contains([]string{s1.GetAddr(), s2.GetAddr()}, member.Address) + } + + // get primary for keyspace group 1. + testutil.Eventually(re, func() bool { + args := []string{"-u", pdAddr, "keyspace-group", "primary", "1"} + output, err := pdctl.ExecuteCommand(cmd, args...) + re.NoError(err) + addr := strings.ReplaceAll(string(output), "\"", "") + addr = strings.ReplaceAll(addr, "\n", "") + return s1.GetAddr() == addr || s2.GetAddr() == addr + }) + + re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes")) + re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/tso/fastGroupSplitPatroller")) + re.NoError(failpoint.Disable("github.com/tikv/pd/server/delayStartServerLoop")) +} diff --git a/tools/pd-ctl/pdctl/command/keyspace_group_command.go b/tools/pd-ctl/pdctl/command/keyspace_group_command.go index b5acf0fa7e8..665be591e9f 100644 --- a/tools/pd-ctl/pdctl/command/keyspace_group_command.go +++ b/tools/pd-ctl/pdctl/command/keyspace_group_command.go @@ -42,6 +42,7 @@ func NewKeyspaceGroupCommand() *cobra.Command { cmd.AddCommand(newFinishMergeKeyspaceGroupCommand()) cmd.AddCommand(newSetNodesKeyspaceGroupCommand()) cmd.AddCommand(newSetPriorityKeyspaceGroupCommand()) + cmd.AddCommand(newShowKeyspaceGroupPrimaryCommand()) cmd.Flags().String("state", "", "state filter") return cmd } @@ -111,6 +112,15 @@ func newSetPriorityKeyspaceGroupCommand() *cobra.Command { return r } +func newShowKeyspaceGroupPrimaryCommand() *cobra.Command { + r := &cobra.Command{ + Use: "primary ", + Short: "show th primary of tso nodes for keyspace group with the given ID.", + Run: showKeyspaceGroupPrimaryCommandFunc, + } + return r +} + func showKeyspaceGroupsCommandFunc(cmd *cobra.Command, args []string) { prefix := keyspaceGroupsPrefix if len(args) > 1 { @@ -337,6 +347,24 @@ func setPriorityKeyspaceGroupCommandFunc(cmd *cobra.Command, args []string) { }) } +func showKeyspaceGroupPrimaryCommandFunc(cmd *cobra.Command, args []string) { + if len(args) < 1 { + cmd.Usage() + return + } + _, err := strconv.ParseUint(args[0], 10, 32) + if err != nil { + cmd.Printf("Failed to parse the keyspace group ID: %s\n", err) + return + } + r, err := doRequest(cmd, fmt.Sprintf("%s/%s/primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) + if err != nil { + cmd.Printf("Failed to get the keyspace group primary information: %s\n", err) + return + } + cmd.Println(r) +} + func convertToKeyspaceGroup(content string) string { kg := endpoint.KeyspaceGroup{} err := json.Unmarshal([]byte(content), &kg) From be30d79005f53a8fdc719ac382a6742356c8a541 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Tue, 4 Jul 2023 19:50:31 +0800 Subject: [PATCH 2/5] fix lint Signed-off-by: lhy1024 --- pkg/keyspace/tso_keyspace_group.go | 1 + server/apiv2/handlers/tso_keyspace_group.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pkg/keyspace/tso_keyspace_group.go b/pkg/keyspace/tso_keyspace_group.go index 457c11edcfb..54887c762cb 100644 --- a/pkg/keyspace/tso_keyspace_group.go +++ b/pkg/keyspace/tso_keyspace_group.go @@ -1013,6 +1013,7 @@ func (m *GroupManager) FinishMergeKeyspaceByID(mergeTargetID uint32) error { return nil } +// GetKeyspaceGroupPrimaryByID returns the primary node of the keyspace group by ID. func (m *GroupManager) GetKeyspaceGroupPrimaryByID(id uint32) (string, error) { // default keyspace group: "/ms/{cluster_id}/tso/00000/primary". // non-default keyspace group: "/ms/{cluster_id}/tso/keyspace_groups/election/{group}/primary". diff --git a/server/apiv2/handlers/tso_keyspace_group.go b/server/apiv2/handlers/tso_keyspace_group.go index a5265ca7e02..23a5528d2ce 100644 --- a/server/apiv2/handlers/tso_keyspace_group.go +++ b/server/apiv2/handlers/tso_keyspace_group.go @@ -486,6 +486,7 @@ func SetPriorityForKeyspaceGroup(c *gin.Context) { c.JSON(http.StatusOK, nil) } +// GetKeyspaceGroupPrimaryByID gets primary of keyspace group by ID. func GetKeyspaceGroupPrimaryByID(c *gin.Context) { id, err := validateKeyspaceGroupID(c) if err != nil { From 0ab36f465e330e2031eced8f46a56d6d2364ec54 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Wed, 5 Jul 2023 11:49:05 +0800 Subject: [PATCH 3/5] use query Signed-off-by: lhy1024 --- pkg/keyspace/tso_keyspace_group.go | 9 ++++ server/apiv2/handlers/tso_keyspace_group.go | 41 +++++-------------- .../pdctl/command/keyspace_group_command.go | 2 +- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/pkg/keyspace/tso_keyspace_group.go b/pkg/keyspace/tso_keyspace_group.go index 54887c762cb..d319798738b 100644 --- a/pkg/keyspace/tso_keyspace_group.go +++ b/pkg/keyspace/tso_keyspace_group.go @@ -1015,6 +1015,15 @@ func (m *GroupManager) FinishMergeKeyspaceByID(mergeTargetID uint32) error { // GetKeyspaceGroupPrimaryByID returns the primary node of the keyspace group by ID. func (m *GroupManager) GetKeyspaceGroupPrimaryByID(id uint32) (string, error) { + // check if the keyspace group exists + kg, err := m.GetKeyspaceGroupByID(id) + if err != nil { + return "", err + } + if kg == nil { + return "", ErrKeyspaceGroupNotExists(id) + } + // default keyspace group: "/ms/{cluster_id}/tso/00000/primary". // non-default keyspace group: "/ms/{cluster_id}/tso/keyspace_groups/election/{group}/primary". path := fmt.Sprintf("/ms/%d/tso/00000/primary", m.clusterID) diff --git a/server/apiv2/handlers/tso_keyspace_group.go b/server/apiv2/handlers/tso_keyspace_group.go index 23a5528d2ce..0e8c039d521 100644 --- a/server/apiv2/handlers/tso_keyspace_group.go +++ b/server/apiv2/handlers/tso_keyspace_group.go @@ -47,7 +47,6 @@ func RegisterTSOKeyspaceGroup(r *gin.RouterGroup) { router.DELETE("/:id/split", FinishSplitKeyspaceByID) router.POST("/:id/merge", MergeKeyspaceGroups) router.DELETE("/:id/merge", FinishMergeKeyspaceByID) - router.GET("/:id/primary", GetKeyspaceGroupPrimaryByID) } // CreateKeyspaceGroupParams defines the params for creating keyspace groups. @@ -150,12 +149,22 @@ func GetKeyspaceGroupByID(c *gin.Context) { c.AbortWithStatusJSON(http.StatusInternalServerError, groupManagerUninitializedErr) return } + + if c.Query("get_param") == "primary" { + primary, err := manager.GetKeyspaceGroupPrimaryByID(id) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) + return + } + c.JSON(http.StatusOK, primary) + return + } + kg, err := manager.GetKeyspaceGroupByID(id) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) return } - c.IndentedJSON(http.StatusOK, kg) } @@ -486,34 +495,6 @@ func SetPriorityForKeyspaceGroup(c *gin.Context) { c.JSON(http.StatusOK, nil) } -// GetKeyspaceGroupPrimaryByID gets primary of keyspace group by ID. -func GetKeyspaceGroupPrimaryByID(c *gin.Context) { - id, err := validateKeyspaceGroupID(c) - if err != nil { - c.AbortWithStatusJSON(http.StatusBadRequest, "invalid keyspace group id") - return - } - svr := c.MustGet(middlewares.ServerContextKey).(*server.Server) - manager := svr.GetKeyspaceGroupManager() - if manager == nil { - c.AbortWithStatusJSON(http.StatusInternalServerError, groupManagerUninitializedErr) - return - } - // check if keyspace group exists - kg, err := manager.GetKeyspaceGroupByID(id) - if err != nil || kg == nil { - c.AbortWithStatusJSON(http.StatusBadRequest, "keyspace group does not exist") - return - } - // get primary - primary, err := manager.GetKeyspaceGroupPrimaryByID(id) - if err != nil { - c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) - return - } - c.JSON(http.StatusOK, primary) -} - func validateKeyspaceGroupID(c *gin.Context) (uint32, error) { id, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil { diff --git a/tools/pd-ctl/pdctl/command/keyspace_group_command.go b/tools/pd-ctl/pdctl/command/keyspace_group_command.go index 665be591e9f..3b75bdc9636 100644 --- a/tools/pd-ctl/pdctl/command/keyspace_group_command.go +++ b/tools/pd-ctl/pdctl/command/keyspace_group_command.go @@ -357,7 +357,7 @@ func showKeyspaceGroupPrimaryCommandFunc(cmd *cobra.Command, args []string) { cmd.Printf("Failed to parse the keyspace group ID: %s\n", err) return } - r, err := doRequest(cmd, fmt.Sprintf("%s/%s/primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) + r, err := doRequest(cmd, fmt.Sprintf("%s/%s?get_param=primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) if err != nil { cmd.Printf("Failed to get the keyspace group primary information: %s\n", err) return From ddc2ae98a9dfa31687cb04ba08fb018b39ce8be5 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Wed, 5 Jul 2023 14:23:25 +0800 Subject: [PATCH 4/5] update api Signed-off-by: lhy1024 --- server/apiv2/handlers/tso_keyspace_group.go | 14 ++++++++++++-- tests/pdctl/keyspace/keyspace_group_test.go | 12 ++++++------ .../pd-ctl/pdctl/command/keyspace_group_command.go | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/server/apiv2/handlers/tso_keyspace_group.go b/server/apiv2/handlers/tso_keyspace_group.go index 0e8c039d521..f5df812e428 100644 --- a/server/apiv2/handlers/tso_keyspace_group.go +++ b/server/apiv2/handlers/tso_keyspace_group.go @@ -135,6 +135,12 @@ func GetKeyspaceGroups(c *gin.Context) { c.IndentedJSON(http.StatusOK, kgs) } +// GetKeyspaceGroupPrimaryResponse defines the response for getting primary node of keyspace group. +type GetKeyspaceGroupPrimaryResponse struct { + ID uint32 `json:"id"` + Primary string `json:"primary"` +} + // GetKeyspaceGroupByID gets keyspace group by ID. func GetKeyspaceGroupByID(c *gin.Context) { id, err := validateKeyspaceGroupID(c) @@ -150,13 +156,17 @@ func GetKeyspaceGroupByID(c *gin.Context) { return } - if c.Query("get_param") == "primary" { + fields := c.Query("fields") // split by comma if need to add more fields + if fields == "primary" { primary, err := manager.GetKeyspaceGroupPrimaryByID(id) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) return } - c.JSON(http.StatusOK, primary) + c.JSON(http.StatusOK, &GetKeyspaceGroupPrimaryResponse{ + ID: id, + Primary: primary, + }) return } diff --git a/tests/pdctl/keyspace/keyspace_group_test.go b/tests/pdctl/keyspace/keyspace_group_test.go index 50da5a3fd77..2ec200b0493 100644 --- a/tests/pdctl/keyspace/keyspace_group_test.go +++ b/tests/pdctl/keyspace/keyspace_group_test.go @@ -504,9 +504,9 @@ func TestShowKeyspaceGroupPrimary(t *testing.T) { args := []string{"-u", pdAddr, "keyspace-group", "primary", defaultKeyspaceGroupID} output, err := pdctl.ExecuteCommand(cmd, args...) re.NoError(err) - addr := strings.ReplaceAll(string(output), "\"", "") - addr = strings.ReplaceAll(addr, "\n", "") - return s1.GetAddr() == addr || s2.GetAddr() == addr + var resp handlers.GetKeyspaceGroupPrimaryResponse + json.Unmarshal(output, &resp) + return s1.GetAddr() == resp.Primary || s2.GetAddr() == resp.Primary }) // split keyspace group. @@ -536,9 +536,9 @@ func TestShowKeyspaceGroupPrimary(t *testing.T) { args := []string{"-u", pdAddr, "keyspace-group", "primary", "1"} output, err := pdctl.ExecuteCommand(cmd, args...) re.NoError(err) - addr := strings.ReplaceAll(string(output), "\"", "") - addr = strings.ReplaceAll(addr, "\n", "") - return s1.GetAddr() == addr || s2.GetAddr() == addr + var resp handlers.GetKeyspaceGroupPrimaryResponse + json.Unmarshal(output, &resp) + return s1.GetAddr() == resp.Primary || s2.GetAddr() == resp.Primary }) re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes")) diff --git a/tools/pd-ctl/pdctl/command/keyspace_group_command.go b/tools/pd-ctl/pdctl/command/keyspace_group_command.go index 3b75bdc9636..615251c31f3 100644 --- a/tools/pd-ctl/pdctl/command/keyspace_group_command.go +++ b/tools/pd-ctl/pdctl/command/keyspace_group_command.go @@ -357,7 +357,7 @@ func showKeyspaceGroupPrimaryCommandFunc(cmd *cobra.Command, args []string) { cmd.Printf("Failed to parse the keyspace group ID: %s\n", err) return } - r, err := doRequest(cmd, fmt.Sprintf("%s/%s?get_param=primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) + r, err := doRequest(cmd, fmt.Sprintf("%s/%s?fields=primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) if err != nil { cmd.Printf("Failed to get the keyspace group primary information: %s\n", err) return From 6f12b4cef9233396c9a4f1ec2345a5a8f39e2001 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Fri, 7 Jul 2023 12:25:23 +0800 Subject: [PATCH 5/5] use KeyspaceGroupPrimaryPath Signed-off-by: lhy1024 --- pkg/keyspace/tso_keyspace_group.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/keyspace/tso_keyspace_group.go b/pkg/keyspace/tso_keyspace_group.go index d319798738b..8ec473add79 100644 --- a/pkg/keyspace/tso_keyspace_group.go +++ b/pkg/keyspace/tso_keyspace_group.go @@ -17,7 +17,6 @@ package keyspace import ( "context" "encoding/json" - "fmt" "sort" "strconv" "strings" @@ -1024,14 +1023,10 @@ func (m *GroupManager) GetKeyspaceGroupPrimaryByID(id uint32) (string, error) { return "", ErrKeyspaceGroupNotExists(id) } - // default keyspace group: "/ms/{cluster_id}/tso/00000/primary". - // non-default keyspace group: "/ms/{cluster_id}/tso/keyspace_groups/election/{group}/primary". - path := fmt.Sprintf("/ms/%d/tso/00000/primary", m.clusterID) - if id != utils.DefaultKeyspaceGroupID { - path = fmt.Sprintf("/ms/%d/tso/keyspace_groups/election/%05d/primary", m.clusterID, id) - } + rootPath := endpoint.TSOSvcRootPath(m.clusterID) + primaryPath := endpoint.KeyspaceGroupPrimaryPath(rootPath, id) leader := &tsopb.Participant{} - ok, _, err := etcdutil.GetProtoMsgWithModRev(m.client, path, leader) + ok, _, err := etcdutil.GetProtoMsgWithModRev(m.client, primaryPath, leader) if err != nil { return "", err }