network_partition_test.go 4.2 KB

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