v3_election_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/clientv3/concurrency"
  21. "golang.org/x/net/context"
  22. )
  23. // TestElectionWait tests if followers can correctly wait for elections.
  24. func TestElectionWait(t *testing.T) {
  25. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  26. defer clus.Terminate(t)
  27. leaders := 3
  28. followers := 3
  29. var clients []*clientv3.Client
  30. newClient := makeMultiNodeClients(t, clus.cluster, &clients)
  31. electedc := make(chan string)
  32. nextc := []chan struct{}{}
  33. // wait for all elections
  34. donec := make(chan struct{})
  35. for i := 0; i < followers; i++ {
  36. nextc = append(nextc, make(chan struct{}))
  37. go func(ch chan struct{}) {
  38. for j := 0; j < leaders; j++ {
  39. session, err := concurrency.NewSession(newClient())
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. b := concurrency.NewElection(session, "test-election")
  44. cctx, cancel := context.WithCancel(context.TODO())
  45. defer cancel()
  46. s, ok := <-b.Observe(cctx)
  47. if !ok {
  48. t.Fatalf("could not observe election; channel closed")
  49. }
  50. electedc <- string(s.Kvs[0].Value)
  51. // wait for next election round
  52. <-ch
  53. session.Orphan()
  54. }
  55. donec <- struct{}{}
  56. }(nextc[i])
  57. }
  58. // elect some leaders
  59. for i := 0; i < leaders; i++ {
  60. go func() {
  61. session, err := concurrency.NewSession(newClient())
  62. if err != nil {
  63. t.Error(err)
  64. }
  65. defer session.Orphan()
  66. e := concurrency.NewElection(session, "test-election")
  67. ev := fmt.Sprintf("electval-%v", time.Now().UnixNano())
  68. if err := e.Campaign(context.TODO(), ev); err != nil {
  69. t.Fatalf("failed volunteer (%v)", err)
  70. }
  71. // wait for followers to accept leadership
  72. for j := 0; j < followers; j++ {
  73. s := <-electedc
  74. if s != ev {
  75. t.Errorf("wrong election value got %s, wanted %s", s, ev)
  76. }
  77. }
  78. // let next leader take over
  79. if err := e.Resign(context.TODO()); err != nil {
  80. t.Fatalf("failed resign (%v)", err)
  81. }
  82. // tell followers to start listening for next leader
  83. for j := 0; j < followers; j++ {
  84. nextc[j] <- struct{}{}
  85. }
  86. }()
  87. }
  88. // wait on followers
  89. for i := 0; i < followers; i++ {
  90. <-donec
  91. }
  92. closeClients(t, clients)
  93. }
  94. // TestElectionFailover tests that an election will
  95. func TestElectionFailover(t *testing.T) {
  96. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  97. defer clus.Terminate(t)
  98. cctx, cancel := context.WithCancel(context.TODO())
  99. defer cancel()
  100. ss := make([]*concurrency.Session, 3, 3)
  101. for i := 0; i < 3; i++ {
  102. var err error
  103. ss[i], err = concurrency.NewSession(clus.clients[i])
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. defer ss[i].Orphan()
  108. }
  109. // first leader (elected)
  110. e := concurrency.NewElection(ss[0], "test-election")
  111. if err := e.Campaign(context.TODO(), "foo"); err != nil {
  112. t.Fatalf("failed volunteer (%v)", err)
  113. }
  114. // check first leader
  115. resp, ok := <-e.Observe(cctx)
  116. if !ok {
  117. t.Fatalf("could not wait for first election; channel closed")
  118. }
  119. s := string(resp.Kvs[0].Value)
  120. if s != "foo" {
  121. t.Fatalf("wrong election result. got %s, wanted foo", s)
  122. }
  123. // next leader
  124. electedc := make(chan struct{})
  125. go func() {
  126. ee := concurrency.NewElection(ss[1], "test-election")
  127. if eer := ee.Campaign(context.TODO(), "bar"); eer != nil {
  128. t.Fatal(eer)
  129. }
  130. electedc <- struct{}{}
  131. }()
  132. // invoke leader failover
  133. if err := ss[0].Close(); err != nil {
  134. t.Fatal(err)
  135. }
  136. // check new leader
  137. e = concurrency.NewElection(ss[2], "test-election")
  138. resp, ok = <-e.Observe(cctx)
  139. if !ok {
  140. t.Fatalf("could not wait for second election; channel closed")
  141. }
  142. s = string(resp.Kvs[0].Value)
  143. if s != "bar" {
  144. t.Fatalf("wrong election result. got %s, wanted bar", s)
  145. }
  146. // leader must ack election (otherwise, Campaign may see closed conn)
  147. <-electedc
  148. }
  149. // TestElectionSessionRelock ensures that campaigning twice on the same election
  150. // with the same lock will Proclaim instead of deadlocking.
  151. func TestElectionSessionRecampaign(t *testing.T) {
  152. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  153. defer clus.Terminate(t)
  154. cli := clus.RandClient()
  155. session, err := concurrency.NewSession(cli)
  156. if err != nil {
  157. t.Error(err)
  158. }
  159. defer session.Orphan()
  160. e := concurrency.NewElection(session, "test-elect")
  161. if err := e.Campaign(context.TODO(), "abc"); err != nil {
  162. t.Fatal(err)
  163. }
  164. e2 := concurrency.NewElection(session, "test-elect")
  165. if err := e2.Campaign(context.TODO(), "def"); err != nil {
  166. t.Fatal(err)
  167. }
  168. ctx, cancel := context.WithCancel(context.TODO())
  169. defer cancel()
  170. if resp := <-e.Observe(ctx); len(resp.Kvs) == 0 || string(resp.Kvs[0].Value) != "def" {
  171. t.Fatalf("expected value=%q, got response %v", "def", resp)
  172. }
  173. }
  174. // TestElectionOnPrefixOfExistingKey checks that a single
  175. // candidate can be elected on a new key that is a prefix
  176. // of an existing key. To wit, check for regression
  177. // of bug #6278. https://github.com/coreos/etcd/issues/6278
  178. //
  179. func TestElectionOnPrefixOfExistingKey(t *testing.T) {
  180. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  181. defer clus.Terminate(t)
  182. cli := clus.RandClient()
  183. if _, err := cli.Put(context.TODO(), "testa", "value"); err != nil {
  184. t.Fatal(err)
  185. }
  186. s, serr := concurrency.NewSession(cli)
  187. if serr != nil {
  188. t.Fatal(serr)
  189. }
  190. e := concurrency.NewElection(s, "test")
  191. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  192. err := e.Campaign(ctx, "abc")
  193. cancel()
  194. if err != nil {
  195. // after 5 seconds, deadlock results in
  196. // 'context deadline exceeded' here.
  197. t.Fatal(err)
  198. }
  199. }