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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import (
"context"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
Expand All @@ -25,6 +26,7 @@

"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"
Expand Down Expand Up @@ -1010,3 +1012,24 @@
zap.Reflect("merge-list", mergeList))
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".
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)
Copy link
Member

Choose a reason for hiding this comment

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

This code is duplicated in many places

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will move them to mcsutil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
leader := &tsopb.Participant{}
ok, _, err := etcdutil.GetProtoMsgWithModRev(m.client, path, leader)
if err != nil {
return "", err

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

View check run for this annotation

Codecov / codecov/patch

pkg/keyspace/tso_keyspace_group.go#L1027

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/keyspace/tso_keyspace_group.go#L1030

Added line #L1030 was not covered by tests
}
// The format of leader name is address-groupID.
contents := strings.Split(leader.GetName(), "-")
return contents[0], err
}
3 changes: 3 additions & 0 deletions pkg/keyspace/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions pkg/tso/keyspace_group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions server/apiv2/handlers/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
router.DELETE("/:id/split", FinishSplitKeyspaceByID)
router.POST("/:id/merge", MergeKeyspaceGroups)
router.DELETE("/:id/merge", FinishMergeKeyspaceByID)
router.GET("/:id/primary", GetKeyspaceGroupPrimaryByID)
lhy1024 marked this conversation as resolved.
Show resolved Hide resolved
}

// CreateKeyspaceGroupParams defines the params for creating keyspace groups.
Expand Down Expand Up @@ -485,6 +486,34 @@
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

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

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/tso_keyspace_group.go#L493-L494

Added lines #L493 - L494 were not covered by tests
}
svr := c.MustGet(middlewares.ServerContextKey).(*server.Server)
manager := svr.GetKeyspaceGroupManager()
if manager == nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, groupManagerUninitializedErr)
return

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

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/tso_keyspace_group.go#L499-L500

Added lines #L499 - L500 were not covered by tests
}
// 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

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

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/tso_keyspace_group.go#L505-L506

Added lines #L505 - L506 were not covered by tests
}
// get primary
primary, err := manager.GetKeyspaceGroupPrimaryByID(id)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
return

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

View check run for this annotation

Codecov / codecov/patch

server/apiv2/handlers/tso_keyspace_group.go#L511-L512

Added lines #L511 - L512 were not covered by tests
}
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
95 changes: 95 additions & 0 deletions tests/pdctl/keyspace/keyspace_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
28 changes: 28 additions & 0 deletions tools/pd-ctl/pdctl/command/keyspace_group_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
cmd.AddCommand(newFinishMergeKeyspaceGroupCommand())
cmd.AddCommand(newSetNodesKeyspaceGroupCommand())
cmd.AddCommand(newSetPriorityKeyspaceGroupCommand())
cmd.AddCommand(newShowKeyspaceGroupPrimaryCommand())
cmd.Flags().String("state", "", "state filter")
return cmd
}
Expand Down Expand Up @@ -111,6 +112,15 @@
return r
}

func newShowKeyspaceGroupPrimaryCommand() *cobra.Command {
r := &cobra.Command{
Use: "primary <keyspace_group_id>",
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 {
Expand Down Expand Up @@ -337,6 +347,24 @@
})
}

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{})
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)
}

func convertToKeyspaceGroup(content string) string {
kg := endpoint.KeyspaceGroup{}
err := json.Unmarshal([]byte(content), &kg)
Expand Down