Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <admin@liudos.us>
  • Loading branch information
lhy1024 committed Jul 5, 2023
1 parent 0ab36f4 commit ddc2ae9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions server/apiv2/handlers/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

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

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/tso_keyspace_group.go#L163-L164

Added lines #L163 - L164 were not covered by tests
}
c.JSON(http.StatusOK, primary)
c.JSON(http.StatusOK, &GetKeyspaceGroupPrimaryResponse{
ID: id,
Primary: primary,
})
return
}

Expand Down
12 changes: 6 additions & 6 deletions tests/pdctl/keyspace/keyspace_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"))
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 @@ -357,7 +357,7 @@ func showKeyspaceGroupPrimaryCommandFunc(cmd *cobra.Command, args []string) {
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?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

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
Expand Down

0 comments on commit ddc2ae9

Please sign in to comment.