Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

.*: add list-member command #654

Merged
merged 24 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fabfc8f
add get-leader command
GMHDBJD May 8, 2020
3c6afe7
change help_cnt
GMHDBJD May 9, 2020
f1428ef
add get-leader tool
GMHDBJD May 9, 2020
42f4e76
add get-leader integration test
GMHDBJD May 9, 2020
6a57c43
add list-member command
GMHDBJD May 12, 2020
76cb180
Merge remote-tracking branch 'upstream/master' into addGetLeaderCommand
GMHDBJD May 12, 2020
e79f07c
add list-member command
GMHDBJD May 12, 2020
3a1fd03
fix useless change
GMHDBJD May 12, 2020
e2a64d9
Merge branch 'master' into addGetLeaderCommand
GMHDBJD May 12, 2020
5fe7d6b
change inferface
GMHDBJD May 13, 2020
f88a59d
Merge branch 'addGetLeaderCommand' of https://github.com/gmhdbjd/dm i…
GMHDBJD May 13, 2020
4d9b343
Merge branch 'master' into addGetLeaderCommand
GMHDBJD May 13, 2020
608c304
add sleep
GMHDBJD May 13, 2020
51ba9f2
Merge branch 'addGetLeaderCommand' of https://github.com/gmhdbjd/dm i…
GMHDBJD May 13, 2020
b0feea5
remove alive in listMemberLeader
GMHDBJD May 14, 2020
e59f7c4
add err for scheduler not started
GMHDBJD May 14, 2020
da734a8
add type and name flags
GMHDBJD May 15, 2020
2bfe4b9
Merge remote-tracking branch 'upstream/master' into addGetLeaderCommand
GMHDBJD May 15, 2020
63e96f6
add retry in test
GMHDBJD May 15, 2020
a8cf11d
Update dm/ctl/master/list_member.go
GMHDBJD May 15, 2020
5cc9634
address comment
GMHDBJD May 15, 2020
2dacb9a
remove type
GMHDBJD May 18, 2020
b559f33
Merge remote-tracking branch 'upstream/master' into addGetLeaderCommand
GMHDBJD May 18, 2020
866d8c9
remove reset function
GMHDBJD May 19, 2020
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
1 change: 1 addition & 0 deletions dm/ctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func NewRootCmd() *cobra.Command {
master.NewMigrateRelayCmd(),
master.NewOperateSourceCmd(),
master.NewOfflineWorkerCmd(),
master.NewListMemberCmd(),
)
return cmd
}
Expand Down
102 changes: 102 additions & 0 deletions dm/ctl/master/list_member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package master

import (
"context"
"os"

"github.com/pingcap/errors"
"github.com/spf13/cobra"

"github.com/pingcap/dm/dm/ctl/common"
"github.com/pingcap/dm/dm/pb"
)

var (
listMemberFlags = ListMemberFlags{}
)

// ListMemberFlags are flags that used in ListMember command
type ListMemberFlags struct {
names []string // specify names to list information
}

// Reset clears cache of ListMemberFlags
func (c ListMemberFlags) Reset() {
c.names = c.names[:0]
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? and where do we call this Reset?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not necessary. I have removed it.


// NewListMemberCmd creates an ListMember command
func NewListMemberCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-member [--type leader/master/worker] [--name master-name/worker-name ...]",
Short: "list member information",
Run: listMemberFunc,
}
cmd.Flags().StringP("type", "t", "", "specify a member type from master, worker, and leader")
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().StringSliceVarP(&listMemberFlags.names, "name", "n", []string{}, "specify member names in choosing type")
return cmd
}

func convertListMemberType(t string) pb.MemberType {
switch t {
case "master":
return pb.MemberType_MasterType
case "worker":
return pb.MemberType_WorkerType
case "leader":
return pb.MemberType_LeaderType
case "":
return pb.MemberType_AllType
default:
return pb.MemberType_InvalidType
}
}

// listMemberFunc does list member request
func listMemberFunc(cmd *cobra.Command, _ []string) {
if len(cmd.Flags().Args()) != 0 {
cmd.SetOut(os.Stdout)
cmd.Usage()
return
}

member, err := cmd.Flags().GetString("type")
if err != nil {
common.PrintLines("%s", errors.ErrorStack(err))
return
}

memType := convertListMemberType(member)
lichunzhu marked this conversation as resolved.
Show resolved Hide resolved
if memType == pb.MemberType_InvalidType {
common.PrintLines("invalid type '%s', choose from master, worker, and leader", member)
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cli := common.MasterClient()
resp, err := cli.ListMember(ctx, &pb.ListMemberRequest{
MemType: memType,
Names: listMemberFlags.names,
})

if err != nil {
common.PrintLines("list member failed, error:\n%v", errors.ErrorStack(err))
return
}
common.PrettyPrintResponse(resp)
}
16 changes: 16 additions & 0 deletions dm/master/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,22 @@ func (s *Scheduler) RemoveWorker(name string) error {
return nil
}

// GetAllWorkers gets all worker agent.
func (s *Scheduler) GetAllWorkers() ([]*Worker, error) {
s.mu.RLock()
defer s.mu.RUnlock()

if !s.started {
return nil, terror.ErrSchedulerNotStarted.Generate()
}

workers := make([]*Worker, 0, len(s.workers))
for _, value := range s.workers {
workers = append(workers, value)
}
return workers, nil
}

// GetWorkerByName gets worker agent by worker name.
func (s *Scheduler) GetWorkerByName(name string) *Worker {
s.mu.RLock()
Expand Down
163 changes: 163 additions & 0 deletions dm/master/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,3 +1559,166 @@ func (s *Server) getSourceRespsAfterOperation(ctx context.Context, taskName stri
wg.Wait()
return sortCommonWorkerResults(sourceRespCh)
}

func (s *Server) listMemberMaster(ctx context.Context, names []string) (*pb.Members_Master, error) {

resp := &pb.Members_Master{
Master: &pb.ListMasterMember{},
}

memberList, err := s.etcdClient.MemberList(ctx)
if err != nil {
resp.Master.Msg = errors.ErrorStack(err)
return resp, nil
}

all := len(names) == 0
set := make(map[string]bool)
for _, name := range names {
set[name] = true
}

etcdMembers := memberList.Members
masters := make([]*pb.MasterInfo, 0, len(etcdMembers))
client := http.Client{
Timeout: 1 * time.Second,
}

for _, etcdMember := range etcdMembers {
if !all && !set[etcdMember.Name] {
continue
}

alive := true
_, err := client.Get(etcdMember.ClientURLs[0] + "/health")
if err != nil {
alive = false
}

masters = append(masters, &pb.MasterInfo{
Name: etcdMember.Name,
MemberID: etcdMember.ID,
Alive: alive,
ClientURLs: etcdMember.ClientURLs,
PeerURLs: etcdMember.PeerURLs,
})
}

resp.Master.Masters = masters
return resp, nil
}

func (s *Server) listMemberWorker(ctx context.Context, names []string) (*pb.Members_Worker, error) {
resp := &pb.Members_Worker{
Worker: &pb.ListWorkerMember{},
}

workerAgents, err := s.scheduler.GetAllWorkers()
if err != nil {
resp.Worker.Msg = errors.ErrorStack(err)
return resp, nil
}

all := len(names) == 0
set := make(map[string]bool)
for _, name := range names {
set[name] = true
}

workers := make([]*pb.WorkerInfo, 0, len(workerAgents))

for _, workerAgent := range workerAgents {
if !all && !set[workerAgent.BaseInfo().Name] {
continue
}

workers = append(workers, &pb.WorkerInfo{
Name: workerAgent.BaseInfo().Name,
Addr: workerAgent.BaseInfo().Addr,
Stage: string(workerAgent.Stage()),
Source: workerAgent.Bound().Source,
})
}

resp.Worker.Workers = workers
return resp, nil
}

func (s *Server) listMemberLeader(ctx context.Context, names []string) (*pb.Members_Leader, error) {
resp := &pb.Members_Leader{
Leader: &pb.ListLeaderMember{},
}

all := len(names) == 0
set := make(map[string]bool)
for _, name := range names {
set[name] = true
}

_, name, addr, err := s.election.LeaderInfo(ctx)
if err != nil {
resp.Leader.Msg = errors.ErrorStack(err)
return resp, nil
}

if !all && !set[name] {
return resp, nil
}

resp.Leader.Name = name
resp.Leader.Addr = addr
return resp, nil
}

// ListMember list member information
func (s *Server) ListMember(ctx context.Context, req *pb.ListMemberRequest) (*pb.ListMemberResponse, error) {
log.L().Info("", zap.Stringer("payload", req), zap.String("request", "ListMember"))

isLeader, needForward := s.isLeaderAndNeedForward()
if !isLeader {
if needForward {
return s.leaderClient.ListMember(ctx, req)
}
return nil, terror.ErrMasterRequestIsNotForwardToLeader
}

resp := &pb.ListMemberResponse{}
members := make([]*pb.Members, 0)

if req.MemType == pb.MemberType_AllType || req.MemType == pb.MemberType_MasterType {
res, err := s.listMemberMaster(ctx, req.Names)
if err != nil {
resp.Msg = errors.ErrorStack(err)
return resp, nil
}
members = append(members, &pb.Members{
Member: res,
})
}

if req.MemType == pb.MemberType_AllType || req.MemType == pb.MemberType_WorkerType {
res, err := s.listMemberWorker(ctx, req.Names)
if err != nil {
resp.Msg = errors.ErrorStack(err)
return resp, nil
}
members = append(members, &pb.Members{
Member: res,
})
}

if req.MemType == pb.MemberType_AllType || req.MemType == pb.MemberType_LeaderType {
res, err := s.listMemberLeader(ctx, req.Names)
if err != nil {
resp.Msg = errors.ErrorStack(err)
return resp, nil
}
members = append(members, &pb.Members{
Member: res,
})
}

resp.Result = true
resp.Members = members
return resp, nil
}
Loading