Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdctl: support show keyspace group primary #6747

Merged
merged 7 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,15 @@

// 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

Check warning on line 1021 in pkg/keyspace/tso_keyspace_group.go

View check run for this annotation

Codecov / codecov/patch

pkg/keyspace/tso_keyspace_group.go#L1021

Added line #L1021 was not covered by tests
}
if kg == nil {
return "", ErrKeyspaceGroupNotExists(id)

Check warning on line 1024 in pkg/keyspace/tso_keyspace_group.go

View check run for this annotation

Codecov / codecov/patch

pkg/keyspace/tso_keyspace_group.go#L1024

Added line #L1024 was not covered by tests
}

// 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)
Expand All @@ -1024,10 +1033,10 @@
leader := &tsopb.Participant{}
ok, _, err := etcdutil.GetProtoMsgWithModRev(m.client, path, leader)
if err != nil {
return "", err

Check warning on line 1036 in pkg/keyspace/tso_keyspace_group.go

View check run for this annotation

Codecov / codecov/patch

pkg/keyspace/tso_keyspace_group.go#L1036

Added line #L1036 was not covered by tests
}
if !ok {
return "", ErrKeyspaceGroupPrimaryNotFound

Check warning on line 1039 in pkg/keyspace/tso_keyspace_group.go

View check run for this annotation

Codecov / codecov/patch

pkg/keyspace/tso_keyspace_group.go#L1039

Added line #L1039 was not covered by tests
}
// The format of leader name is address-groupID.
contents := strings.Split(leader.GetName(), "-")
Expand Down
41 changes: 11 additions & 30 deletions server/apiv2/handlers/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
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.
Expand Down Expand Up @@ -150,12 +149,22 @@
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

Check warning on line 157 in server/apiv2/handlers/tso_keyspace_group.go

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/tso_keyspace_group.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}
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)
}

Expand Down Expand Up @@ -486,34 +495,6 @@
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 {
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-ctl/pdctl/command/keyspace_group_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,18 @@

func showKeyspaceGroupPrimaryCommandFunc(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Usage()
return

Check warning on line 353 in tools/pd-ctl/pdctl/command/keyspace_group_command.go

View check run for this annotation

Codecov / codecov/patch

tools/pd-ctl/pdctl/command/keyspace_group_command.go#L352-L353

Added lines #L352 - L353 were not covered by tests
}
_, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
cmd.Printf("Failed to parse the keyspace group ID: %s\n", err)
return

Check warning on line 358 in tools/pd-ctl/pdctl/command/keyspace_group_command.go

View check run for this annotation

Codecov / codecov/patch

tools/pd-ctl/pdctl/command/keyspace_group_command.go#L357-L358

Added lines #L357 - L358 were not covered by tests
}
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{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be a bool, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"%s/%s?only_primary_member=" so?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can google it to find the best practice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
cmd.Printf("Failed to get the keyspace group primary information: %s\n", err)
return

Check warning on line 363 in tools/pd-ctl/pdctl/command/keyspace_group_command.go

View check run for this annotation

Codecov / codecov/patch

tools/pd-ctl/pdctl/command/keyspace_group_command.go#L362-L363

Added lines #L362 - L363 were not covered by tests
}
cmd.Println(r)
}
Expand Down