v3_leadership_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "context"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestMoveLeader(t *testing.T) { testMoveLeader(t, true) }
  24. func TestMoveLeaderService(t *testing.T) { testMoveLeader(t, false) }
  25. func testMoveLeader(t *testing.T, auto bool) {
  26. defer testutil.AfterTest(t)
  27. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  28. defer clus.Terminate(t)
  29. oldLeadIdx := clus.WaitLeader(t)
  30. oldLeadID := uint64(clus.Members[oldLeadIdx].s.ID())
  31. // ensure followers go through leader transition while learship transfer
  32. idc := make(chan uint64)
  33. for i := range clus.Members {
  34. if oldLeadIdx != i {
  35. go func(m *member) {
  36. idc <- checkLeaderTransition(t, m, oldLeadID)
  37. }(clus.Members[i])
  38. }
  39. }
  40. target := uint64(clus.Members[(oldLeadIdx+1)%3].s.ID())
  41. if auto {
  42. err := clus.Members[oldLeadIdx].s.TransferLeadership()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. } else {
  47. mvc := toGRPC(clus.Client(oldLeadIdx)).Maintenance
  48. _, err := mvc.MoveLeader(context.TODO(), &pb.MoveLeaderRequest{TargetID: target})
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. }
  53. // wait until leader transitions have happened
  54. var newLeadIDs [2]uint64
  55. for i := range newLeadIDs {
  56. select {
  57. case newLeadIDs[i] = <-idc:
  58. case <-time.After(time.Second):
  59. t.Fatal("timed out waiting for leader transition")
  60. }
  61. }
  62. // remaining members must agree on the same leader
  63. if newLeadIDs[0] != newLeadIDs[1] {
  64. t.Fatalf("expected same new leader %d == %d", newLeadIDs[0], newLeadIDs[1])
  65. }
  66. // new leader must be different than the old leader
  67. if oldLeadID == newLeadIDs[0] {
  68. t.Fatalf("expected old leader %d != new leader %d", oldLeadID, newLeadIDs[0])
  69. }
  70. // if move-leader were used, new leader must match transferee
  71. if !auto {
  72. if newLeadIDs[0] != target {
  73. t.Fatalf("expected new leader %d != target %d", newLeadIDs[0], target)
  74. }
  75. }
  76. }
  77. // TestMoveLeaderError ensures that request to non-leader fail.
  78. func TestMoveLeaderError(t *testing.T) {
  79. defer testutil.AfterTest(t)
  80. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  81. defer clus.Terminate(t)
  82. oldLeadIdx := clus.WaitLeader(t)
  83. followerIdx := (oldLeadIdx + 1) % 3
  84. target := uint64(clus.Members[(oldLeadIdx+2)%3].s.ID())
  85. mvc := toGRPC(clus.Client(followerIdx)).Maintenance
  86. _, err := mvc.MoveLeader(context.TODO(), &pb.MoveLeaderRequest{TargetID: target})
  87. if !eqErrGRPC(err, rpctypes.ErrGRPCNotLeader) {
  88. t.Errorf("err = %v, want %v", err, rpctypes.ErrGRPCNotLeader)
  89. }
  90. }