etcd_functional_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright 2014 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "math/rand"
  16. "net/http/httptest"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/config"
  20. )
  21. func TestKillLeader(t *testing.T) {
  22. tests := []int{3, 5, 9, 11}
  23. for i, tt := range tests {
  24. es, hs := buildCluster(tt, false)
  25. waitCluster(t, es)
  26. waitLeader(es)
  27. lead := es[0].p.node.Leader()
  28. es[lead].Stop()
  29. time.Sleep(es[0].tickDuration * defaultElection * 2)
  30. waitLeader(es)
  31. if es[1].p.node.Leader() == 0 {
  32. t.Errorf("#%d: lead = %d, want not 0", i, es[1].p.node.Leader())
  33. }
  34. for i := range es {
  35. es[len(es)-i-1].Stop()
  36. }
  37. for i := range hs {
  38. hs[len(hs)-i-1].Close()
  39. }
  40. }
  41. afterTest(t)
  42. }
  43. func TestRandomKill(t *testing.T) {
  44. tests := []int{3, 5, 9, 11}
  45. for _, tt := range tests {
  46. es, hs := buildCluster(tt, false)
  47. waitCluster(t, es)
  48. waitLeader(es)
  49. toKill := make(map[int64]struct{})
  50. for len(toKill) != tt/2-1 {
  51. toKill[rand.Int63n(int64(tt))] = struct{}{}
  52. }
  53. for k := range toKill {
  54. es[k].Stop()
  55. }
  56. time.Sleep(es[0].tickDuration * defaultElection * 2)
  57. waitLeader(es)
  58. for i := range es {
  59. es[len(es)-i-1].Stop()
  60. }
  61. for i := range hs {
  62. hs[len(hs)-i-1].Close()
  63. }
  64. }
  65. afterTest(t)
  66. }
  67. func TestJoinThroughFollower(t *testing.T) {
  68. tests := []int{3, 4, 5, 6}
  69. for _, tt := range tests {
  70. es := make([]*Server, tt)
  71. hs := make([]*httptest.Server, tt)
  72. for i := 0; i < tt; i++ {
  73. c := config.New()
  74. if i > 0 {
  75. c.Peers = []string{hs[i-1].URL}
  76. }
  77. es[i], hs[i] = initTestServer(c, int64(i), false)
  78. }
  79. go es[0].Run()
  80. for i := 1; i < tt; i++ {
  81. go es[i].Run()
  82. waitLeader(es[:i])
  83. }
  84. waitCluster(t, es)
  85. for i := range hs {
  86. es[len(hs)-i-1].Stop()
  87. }
  88. for i := range hs {
  89. hs[len(hs)-i-1].Close()
  90. }
  91. }
  92. afterTest(t)
  93. }
  94. type leadterm struct {
  95. lead int64
  96. term int64
  97. }
  98. func waitActiveLeader(es []*Server) (lead, term int64) {
  99. for {
  100. if l, t := waitLeader(es); l >= 0 && es[l].mode.Get() == participantMode {
  101. return l, t
  102. }
  103. }
  104. }
  105. // waitLeader waits until all alive servers are checked to have the same leader.
  106. // WARNING: The lead returned is not guaranteed to be actual leader.
  107. func waitLeader(es []*Server) (lead, term int64) {
  108. for {
  109. ls := make([]leadterm, 0, len(es))
  110. for i := range es {
  111. switch es[i].mode.Get() {
  112. case participantMode:
  113. ls = append(ls, getLead(es[i]))
  114. case standbyMode:
  115. //TODO(xiangli) add standby support
  116. case stopMode:
  117. }
  118. }
  119. if isSameLead(ls) {
  120. return ls[0].lead, ls[0].term
  121. }
  122. time.Sleep(es[0].tickDuration * defaultElection)
  123. }
  124. }
  125. func getLead(s *Server) leadterm {
  126. return leadterm{s.p.node.Leader(), s.p.node.Term()}
  127. }
  128. func isSameLead(ls []leadterm) bool {
  129. m := make(map[leadterm]int)
  130. for i := range ls {
  131. m[ls[i]] = m[ls[i]] + 1
  132. }
  133. if len(m) == 1 {
  134. if ls[0].lead == -1 {
  135. return false
  136. }
  137. return true
  138. }
  139. // todo(xiangli): printout the current cluster status for debugging....
  140. return false
  141. }