network_partition_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 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. "fmt"
  17. "testing"
  18. "time"
  19. "go.etcd.io/etcd/pkg/testutil"
  20. )
  21. func TestNetworkPartition5MembersLeaderInMinority(t *testing.T) {
  22. defer testutil.AfterTest(t)
  23. clus := NewClusterV3(t, &ClusterConfig{Size: 5})
  24. defer clus.Terminate(t)
  25. leadIndex := clus.WaitLeader(t)
  26. // minority: leader, follower / majority: follower, follower, follower
  27. minority := []int{leadIndex, (leadIndex + 1) % 5}
  28. majority := []int{(leadIndex + 2) % 5, (leadIndex + 3) % 5, (leadIndex + 4) % 5}
  29. minorityMembers := getMembersByIndexSlice(clus.cluster, minority)
  30. majorityMembers := getMembersByIndexSlice(clus.cluster, majority)
  31. // network partition (bi-directional)
  32. injectPartition(t, minorityMembers, majorityMembers)
  33. // minority leader must be lost
  34. clus.waitNoLeader(minorityMembers)
  35. // wait extra election timeout
  36. time.Sleep(2 * majorityMembers[0].ElectionTimeout())
  37. // new leader must be from majority
  38. clus.waitLeader(t, majorityMembers)
  39. // recover network partition (bi-directional)
  40. recoverPartition(t, minorityMembers, majorityMembers)
  41. // write to majority first
  42. clusterMustProgress(t, append(majorityMembers, minorityMembers...))
  43. }
  44. func TestNetworkPartition5MembersLeaderInMajority(t *testing.T) {
  45. // retry up to 3 times, in case of leader election on majority partition due to slow hardware
  46. var err error
  47. for i := 0; i < 3; i++ {
  48. if err = testNetworkPartition5MembersLeaderInMajority(t); err == nil {
  49. break
  50. }
  51. t.Logf("[%d] got %v", i, err)
  52. }
  53. if err != nil {
  54. t.Fatalf("failed after 3 tries (%v)", err)
  55. }
  56. }
  57. func testNetworkPartition5MembersLeaderInMajority(t *testing.T) error {
  58. defer testutil.AfterTest(t)
  59. clus := NewClusterV3(t, &ClusterConfig{Size: 5})
  60. defer clus.Terminate(t)
  61. leadIndex := clus.WaitLeader(t)
  62. // majority: leader, follower, follower / minority: follower, follower
  63. majority := []int{leadIndex, (leadIndex + 1) % 5, (leadIndex + 2) % 5}
  64. minority := []int{(leadIndex + 3) % 5, (leadIndex + 4) % 5}
  65. majorityMembers := getMembersByIndexSlice(clus.cluster, majority)
  66. minorityMembers := getMembersByIndexSlice(clus.cluster, minority)
  67. // network partition (bi-directional)
  68. injectPartition(t, majorityMembers, minorityMembers)
  69. // minority leader must be lost
  70. clus.waitNoLeader(minorityMembers)
  71. // wait extra election timeout
  72. time.Sleep(2 * majorityMembers[0].ElectionTimeout())
  73. // leader must be hold in majority
  74. leadIndex2 := clus.waitLeader(t, majorityMembers)
  75. leadID, leadID2 := clus.Members[leadIndex].s.ID(), majorityMembers[leadIndex2].s.ID()
  76. if leadID != leadID2 {
  77. return fmt.Errorf("unexpected leader change from %s, got %s", leadID, leadID2)
  78. }
  79. // recover network partition (bi-directional)
  80. recoverPartition(t, majorityMembers, minorityMembers)
  81. // write to majority first
  82. clusterMustProgress(t, append(majorityMembers, minorityMembers...))
  83. return nil
  84. }
  85. func TestNetworkPartition4Members(t *testing.T) {
  86. defer testutil.AfterTest(t)
  87. clus := NewClusterV3(t, &ClusterConfig{Size: 4})
  88. defer clus.Terminate(t)
  89. leadIndex := clus.WaitLeader(t)
  90. // groupA: leader, follower / groupB: follower, follower
  91. groupA := []int{leadIndex, (leadIndex + 1) % 4}
  92. groupB := []int{(leadIndex + 2) % 4, (leadIndex + 3) % 4}
  93. leaderPartition := getMembersByIndexSlice(clus.cluster, groupA)
  94. followerPartition := getMembersByIndexSlice(clus.cluster, groupB)
  95. // network partition (bi-directional)
  96. injectPartition(t, leaderPartition, followerPartition)
  97. // no group has quorum, so leader must be lost in all members
  98. clus.WaitNoLeader()
  99. // recover network partition (bi-directional)
  100. recoverPartition(t, leaderPartition, followerPartition)
  101. // need to wait since it recovered with no leader
  102. clus.WaitLeader(t)
  103. clusterMustProgress(t, clus.Members)
  104. }
  105. func getMembersByIndexSlice(clus *cluster, idxs []int) []*member {
  106. ms := make([]*member, len(idxs))
  107. for i, idx := range idxs {
  108. ms[i] = clus.Members[idx]
  109. }
  110. return ms
  111. }
  112. func injectPartition(t *testing.T, src, others []*member) {
  113. for _, m := range src {
  114. m.InjectPartition(t, others...)
  115. }
  116. }
  117. func recoverPartition(t *testing.T, src, others []*member) {
  118. for _, m := range src {
  119. m.RecoverPartition(t, others...)
  120. }
  121. }