-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
*: expose Leadership Transfer API to clients #8153
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d82f257
etcdserver/etcdserverpb: define 'MoveLeader' RPC
gyuho 265303c
*: regenerate proto with 'MoveLeader' RPC
gyuho 939bbd7
etcdserver/*: add 'ErrNotLeader'
gyuho b1a0ae3
etcdserver/api/v3rpc: add 'MoveLeader' to 'maintenanceServer'
gyuho 3e263d5
proxy/*: add 'MoveLeader' RPC
gyuho 403ba1d
etcdserver: expose 'transferLeadership' as 'MoveLeader'
gyuho c5532dd
integration: test 'MoveLeader' service
gyuho 581a83d
clientv3/*: add 'MoveLeader' method to 'Maintenance'
gyuho 6e9b776
etcdctl/ctlv3: add 'move-leader' command
gyuho d428958
e2e: test 'move-leader' command
gyuho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" | ||
"github.com/coreos/etcd/integration" | ||
"github.com/coreos/etcd/pkg/testutil" | ||
) | ||
|
||
func TestMaintenanceMoveLeader(t *testing.T) { | ||
defer testutil.AfterTest(t) | ||
|
||
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3}) | ||
defer clus.Terminate(t) | ||
|
||
oldLeadIdx := clus.WaitLeader(t) | ||
targetIdx := (oldLeadIdx + 1) % 3 | ||
target := uint64(clus.Members[targetIdx].ID()) | ||
|
||
cli := clus.Client(targetIdx) | ||
_, err := cli.MoveLeader(context.Background(), target) | ||
if err != rpctypes.ErrNotLeader { | ||
t.Fatalf("error expected %v, got %v", rpctypes.ErrNotLeader, err) | ||
} | ||
|
||
cli = clus.Client(oldLeadIdx) | ||
_, err = cli.MoveLeader(context.Background(), target) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
leadIdx := clus.WaitLeader(t) | ||
lead := uint64(clus.Members[leadIdx].ID()) | ||
if target != lead { | ||
t.Fatalf("new leader expected %d, got %d", target, lead) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/pkg/testutil" | ||
"github.com/coreos/etcd/pkg/types" | ||
) | ||
|
||
func TestCtlV3MoveLeader(t *testing.T) { | ||
defer testutil.AfterTest(t) | ||
|
||
epc := setupEtcdctlTest(t, &configNoTLS, true) | ||
defer func() { | ||
if errC := epc.Close(); errC != nil { | ||
t.Fatalf("error closing etcd processes (%v)", errC) | ||
} | ||
}() | ||
|
||
var leadIdx int | ||
var leaderID uint64 | ||
var transferee uint64 | ||
for i, ep := range epc.grpcEndpoints() { | ||
cli, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{ep}, | ||
DialTimeout: 3 * time.Second, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
resp, err := cli.Status(context.Background(), ep) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
cli.Close() | ||
|
||
if resp.Header.GetMemberId() == resp.Leader { | ||
leadIdx = i | ||
leaderID = resp.Leader | ||
} else { | ||
transferee = resp.Header.GetMemberId() | ||
} | ||
} | ||
|
||
os.Setenv("ETCDCTL_API", "3") | ||
defer os.Unsetenv("ETCDCTL_API") | ||
cx := ctlCtx{ | ||
t: t, | ||
cfg: configNoTLS, | ||
dialTimeout: 7 * time.Second, | ||
epc: epc, | ||
} | ||
|
||
tests := []struct { | ||
prefixes []string | ||
expect string | ||
}{ | ||
{ // request to non-leader | ||
cx.prefixArgs([]string{cx.epc.grpcEndpoints()[(leadIdx+1)%3]}), | ||
"no leader endpoint given at ", | ||
}, | ||
{ // request to leader | ||
cx.prefixArgs([]string{cx.epc.grpcEndpoints()[leadIdx]}), | ||
fmt.Sprintf("Leadership transferred from %s to %s", types.ID(leaderID), types.ID(transferee)), | ||
}, | ||
} | ||
for i, tc := range tests { | ||
cmdArgs := append(tc.prefixes, "move-leader", types.ID(transferee).String()) | ||
if err := spawnWithExpect(cmdArgs, tc.expect); err != nil { | ||
t.Fatalf("#%d: %v", i, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/coreos/etcd/clientv3" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewMoveLeaderCommand returns the cobra command for "move-leader". | ||
func NewMoveLeaderCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "move-leader <transferee-member-id>", | ||
Short: "Transfers leadership to another etcd cluster member.", | ||
Run: transferLeadershipCommandFunc, | ||
} | ||
return cmd | ||
} | ||
|
||
// transferLeadershipCommandFunc executes the "compaction" command. | ||
func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
ExitWithError(ExitBadArgs, fmt.Errorf("move-leader command needs 1 argument")) | ||
} | ||
target, err := strconv.ParseUint(args[0], 16, 64) | ||
if err != nil { | ||
ExitWithError(ExitBadArgs, err) | ||
} | ||
|
||
c := mustClientFromCmd(cmd) | ||
eps := c.Endpoints() | ||
c.Close() | ||
|
||
ctx, cancel := commandCtx(cmd) | ||
|
||
// find current leader | ||
var leaderCli *clientv3.Client | ||
var leaderID uint64 | ||
for _, ep := range eps { | ||
cli, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{ep}, | ||
DialTimeout: 3 * time.Second, | ||
}) | ||
if err != nil { | ||
ExitWithError(ExitError, err) | ||
} | ||
resp, err := cli.Status(ctx, ep) | ||
if err != nil { | ||
ExitWithError(ExitError, err) | ||
} | ||
|
||
if resp.Header.GetMemberId() == resp.Leader { | ||
leaderCli = cli | ||
leaderID = resp.Leader | ||
break | ||
} | ||
cli.Close() | ||
} | ||
if leaderCli == nil { | ||
ExitWithError(ExitBadArgs, fmt.Errorf("no leader endpoint given at %v", eps)) | ||
} | ||
|
||
var resp *clientv3.MoveLeaderResponse | ||
resp, err = leaderCli.MoveLeader(ctx, target) | ||
cancel() | ||
if err != nil { | ||
ExitWithError(ExitError, err) | ||
} | ||
|
||
display.MoveLeader(leaderID, target, *resp) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
break
?