v3_leadership_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "strings"
  18. "testing"
  19. "time"
  20. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  21. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  22. "go.etcd.io/etcd/pkg/testutil"
  23. )
  24. func TestMoveLeader(t *testing.T) { testMoveLeader(t, true) }
  25. func TestMoveLeaderService(t *testing.T) { testMoveLeader(t, false) }
  26. func testMoveLeader(t *testing.T, auto bool) {
  27. defer testutil.AfterTest(t)
  28. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  29. defer clus.Terminate(t)
  30. oldLeadIdx := clus.WaitLeader(t)
  31. oldLeadID := uint64(clus.Members[oldLeadIdx].s.ID())
  32. // ensure followers go through leader transition while learship transfer
  33. idc := make(chan uint64)
  34. for i := range clus.Members {
  35. if oldLeadIdx != i {
  36. go func(m *member) {
  37. idc <- checkLeaderTransition(m, oldLeadID)
  38. }(clus.Members[i])
  39. }
  40. }
  41. target := uint64(clus.Members[(oldLeadIdx+1)%3].s.ID())
  42. if auto {
  43. err := clus.Members[oldLeadIdx].s.TransferLeadership()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. } else {
  48. mvc := toGRPC(clus.Client(oldLeadIdx)).Maintenance
  49. _, err := mvc.MoveLeader(context.TODO(), &pb.MoveLeaderRequest{TargetID: target})
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. }
  54. // wait until leader transitions have happened
  55. var newLeadIDs [2]uint64
  56. for i := range newLeadIDs {
  57. select {
  58. case newLeadIDs[i] = <-idc:
  59. case <-time.After(time.Second):
  60. t.Fatal("timed out waiting for leader transition")
  61. }
  62. }
  63. // remaining members must agree on the same leader
  64. if newLeadIDs[0] != newLeadIDs[1] {
  65. t.Fatalf("expected same new leader %d == %d", newLeadIDs[0], newLeadIDs[1])
  66. }
  67. // new leader must be different than the old leader
  68. if oldLeadID == newLeadIDs[0] {
  69. t.Fatalf("expected old leader %d != new leader %d", oldLeadID, newLeadIDs[0])
  70. }
  71. // if move-leader were used, new leader must match transferee
  72. if !auto {
  73. if newLeadIDs[0] != target {
  74. t.Fatalf("expected new leader %d != target %d", newLeadIDs[0], target)
  75. }
  76. }
  77. }
  78. // TestMoveLeaderError ensures that request to non-leader fail.
  79. func TestMoveLeaderError(t *testing.T) {
  80. defer testutil.AfterTest(t)
  81. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  82. defer clus.Terminate(t)
  83. oldLeadIdx := clus.WaitLeader(t)
  84. followerIdx := (oldLeadIdx + 1) % 3
  85. target := uint64(clus.Members[(oldLeadIdx+2)%3].s.ID())
  86. mvc := toGRPC(clus.Client(followerIdx)).Maintenance
  87. _, err := mvc.MoveLeader(context.TODO(), &pb.MoveLeaderRequest{TargetID: target})
  88. if !eqErrGRPC(err, rpctypes.ErrGRPCNotLeader) {
  89. t.Errorf("err = %v, want %v", err, rpctypes.ErrGRPCNotLeader)
  90. }
  91. }
  92. // TestMoveLeaderToLearnerError ensures that leader transfer to learner member will fail.
  93. func TestMoveLeaderToLearnerError(t *testing.T) {
  94. defer testutil.AfterTest(t)
  95. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  96. defer clus.Terminate(t)
  97. // we have to add and launch learner member after initial cluster was created, because
  98. // bootstrapping a cluster with learner member is not supported.
  99. clus.AddAndLaunchLearnerMember(t)
  100. learners, err := clus.GetLearnerMembers()
  101. if err != nil {
  102. t.Fatalf("failed to get the learner members in cluster: %v", err)
  103. }
  104. if len(learners) != 1 {
  105. t.Fatalf("added 1 learner to cluster, got %d", len(learners))
  106. }
  107. learnerID := learners[0].ID
  108. leaderIdx := clus.WaitLeader(t)
  109. cli := clus.Client(leaderIdx)
  110. _, err = cli.MoveLeader(context.Background(), learnerID)
  111. if err == nil {
  112. t.Fatalf("expecting leader transfer to learner to fail, got no error")
  113. }
  114. expectedErrKeywords := "bad leader transferee"
  115. if !strings.Contains(err.Error(), expectedErrKeywords) {
  116. t.Errorf("expecting error to contain %s, got %s", expectedErrKeywords, err.Error())
  117. }
  118. }
  119. // TestTransferLeadershipWithLearner ensures TransferLeadership does not timeout due to learner is
  120. // automatically picked by leader as transferee.
  121. func TestTransferLeadershipWithLearner(t *testing.T) {
  122. defer testutil.AfterTest(t)
  123. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  124. defer clus.Terminate(t)
  125. clus.AddAndLaunchLearnerMember(t)
  126. learners, err := clus.GetLearnerMembers()
  127. if err != nil {
  128. t.Fatalf("failed to get the learner members in cluster: %v", err)
  129. }
  130. if len(learners) != 1 {
  131. t.Fatalf("added 1 learner to cluster, got %d", len(learners))
  132. }
  133. leaderIdx := clus.WaitLeader(t)
  134. errCh := make(chan error, 1)
  135. go func() {
  136. // note that this cluster has 1 leader and 1 learner. TransferLeadership should return nil.
  137. // Leadership transfer is skipped in cluster with 1 voting member.
  138. errCh <- clus.Members[leaderIdx].s.TransferLeadership()
  139. }()
  140. select {
  141. case err := <-errCh:
  142. if err != nil {
  143. t.Errorf("got error during leadership transfer: %v", err)
  144. }
  145. case <-time.After(5 * time.Second):
  146. t.Error("timed out waiting for leader transition")
  147. }
  148. }