Skip to content

Commit

Permalink
etcdctl: find leader when promoting learner
Browse files Browse the repository at this point in the history
find leader endpoint in the given endpoints, send member promote request
to leader endpoint.
  • Loading branch information
jingyih committed Apr 26, 2019
1 parent c9644ae commit 285a96f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion etcdctl/ctlv3/command/member_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,37 @@ func memberPromoteCommandFunc(cmd *cobra.Command, args []string) {
}

ctx, cancel := commandCtx(cmd)
resp, err := mustClientFromCmd(cmd).MemberPromote(ctx, id)
eps, err := endpointsFromCmd(cmd)
if err != nil {
ExitWithError(ExitError, err)
}

var (
leaderEp string
leaderFound bool
)
for _, ep := range eps {
cfg := clientConfigFromCmd(cmd)
cfg.endpoints = []string{ep}
cli := cfg.mustClient()
resp, serr := cli.Status(ctx, ep)
if serr != nil {
ExitWithError(ExitError, serr)
}
if resp.Header.GetMemberId() == resp.Leader {
leaderFound = true
leaderEp = ep
break
}
cli.Close()
}
if !leaderFound {
ExitWithError(ExitBadArgs, fmt.Errorf("no leader endpoint found in %v", eps))
}

cfg := clientConfigFromCmd(cmd)
cfg.endpoints = []string{leaderEp}
resp, err := cfg.mustClient().MemberPromote(ctx, id)
cancel()
if err != nil {
ExitWithError(ExitError, err)
Expand Down

0 comments on commit 285a96f

Please sign in to comment.