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 4 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.NewGetLeaderCmd(),
)
return cmd
}
Expand Down
56 changes: 56 additions & 0 deletions dm/ctl/master/get_leader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/dm/dm/ctl/common"
"github.com/pingcap/dm/dm/pb"

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

// NewGetLeaderCmd creates an GetLeader command
func NewGetLeaderCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-leader",
Short: "get master leader",
Run: getLeaderFunc,
}
return cmd
}

// getLeaderFunc does get leader request
func getLeaderFunc(cmd *cobra.Command, _ []string) {
if len(cmd.Flags().Args()) > 0 {
cmd.SetOut(os.Stdout)
cmd.Usage()
return
}

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

cli := common.MasterClient()
resp, err := cli.GetLeader(ctx, &pb.GetLeaderRequest{})
if err != nil {
common.PrintLines("get leader failed, error:\n%v", errors.ErrorStack(err))
return
}

common.PrettyPrintResponse(resp)
}
21 changes: 21 additions & 0 deletions dm/master/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,3 +1559,24 @@ func (s *Server) getSourceRespsAfterOperation(ctx context.Context, taskName stri
wg.Wait()
return sortCommonWorkerResults(sourceRespCh)
}

// GetLeader get master leader
func (s *Server) GetLeader(ctx context.Context, req *pb.GetLeaderRequest) (*pb.GetLeaderResponse, error) {
log.L().Info("", zap.Stringer("payload", req), zap.String("request", "GetLeader"))

_, leaderID, addr, err := s.election.LeaderInfo(ctx)

if err != nil {
return &pb.GetLeaderResponse{
Result: false,
Msg: errors.ErrorStack(err),
}, nil
}

log.L().Info("get leader successfully", zap.String("leaderId", leaderID))
return &pb.GetLeaderResponse{
Result: true,
Name: leaderID,
Addr: addr,
}, nil
}
83 changes: 83 additions & 0 deletions dm/master/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,3 +1141,86 @@ func mockRevelantWorkerClient(mockWorkerClient *pbmock.MockWorkerClient, taskNam
},
).Return(queryResp, nil).MaxTimes(maxRetryNum)
}

func (t *testMaster) TestGetLeader(c *check.C) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// create a new cluster
cfg1 := NewConfig()
c.Assert(cfg1.Parse([]string{"-config=./dm-master.toml"}), check.IsNil)
cfg1.Name = "dm-master-1"
cfg1.DataDir = c.MkDir()
cfg1.MasterAddr = tempurl.Alloc()[len("http://"):]
cfg1.PeerUrls = tempurl.Alloc()
cfg1.AdvertisePeerUrls = cfg1.PeerUrls
cfg1.InitialCluster = fmt.Sprintf("%s=%s", cfg1.Name, cfg1.AdvertisePeerUrls)

s1 := NewServer(cfg1)
c.Assert(s1.Start(ctx), check.IsNil)

// wait the first one become the leader
c.Assert(utils.WaitSomething(30, 100*time.Millisecond, func() bool {
return s1.election.IsLeader()
}), check.IsTrue)

{
res, err := s1.GetLeader(ctx, &pb.GetLeaderRequest{})
c.Assert(err, check.IsNil)
c.Assert(res.Name, check.Equals, cfg1.Name)
c.Assert(res.Addr, check.Equals, cfg1.AdvertiseAddr)
}

// join to an existing cluster
cfg2 := NewConfig()
c.Assert(cfg2.Parse([]string{"-config=./dm-master.toml"}), check.IsNil)
cfg2.Name = "dm-master-2"
cfg2.DataDir = c.MkDir()
cfg2.MasterAddr = tempurl.Alloc()[len("http://"):]
cfg2.PeerUrls = tempurl.Alloc()
cfg2.AdvertisePeerUrls = cfg2.PeerUrls
cfg2.Join = cfg1.MasterAddr // join to an existing cluster

s2 := NewServer(cfg2)
c.Assert(s2.Start(ctx), check.IsNil)
defer s2.Close()

{
res, err := s1.GetLeader(ctx, &pb.GetLeaderRequest{})
c.Assert(err, check.IsNil)
c.Assert(res.Name, check.Equals, cfg1.Name)
c.Assert(res.Addr, check.Equals, cfg1.AdvertiseAddr)
}
{
res, err := s2.GetLeader(ctx, &pb.GetLeaderRequest{})
c.Assert(err, check.IsNil)
c.Assert(res.Name, check.Equals, cfg1.Name)
c.Assert(res.Addr, check.Equals, cfg1.AdvertiseAddr)
}

s1.Close()
{
res, err := s2.GetLeader(ctx, &pb.GetLeaderRequest{})
c.Assert(err, check.IsNil)
c.Assert(res.Name, check.Equals, "")
c.Assert(res.Addr, check.Equals, "")
}

/*
It seems that if we kill leader in 2 masters cluster,
the second one wouldn't be the leader so follow cases would not be passed.
But I fail to create 3 masters cluster...

// wait the second one become the leader
c.Assert(utils.WaitSomething(30, 100*time.Millisecond, func() bool {
return s2.election.IsLeader()
}), check.IsTrue)

{
res, err := s2.GetLeader(ctx, &pb.GetLeaderRequest{})
c.Assert(err, check.IsNil)
c.Assert(res.Name, check.Equals, cfg2.Name)
c.Assert(res.Addr, check.Equals, cfg2.AdvertiseAddr)
}

*/
}
Loading