v3_election_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. defer dropSessionLease(clus)
  28. leaders := 3
  29. followers := 3
  30. var clients []*clientv3.Client
  31. newClient := makeMultiNodeClients(t, clus.cluster, &clients)
  32. electedc := make(chan string)
  33. nextc := []chan struct{}{}
  34. // wait for all elections
  35. donec := make(chan struct{})
  36. for i := 0; i < followers; i++ {
  37. nextc = append(nextc, make(chan struct{}))
  38. go func(ch chan struct{}) {
  39. for j := 0; j < leaders; j++ {
  40. b := concurrency.NewElection(newClient(), "test-election")
  41. cctx, cancel := context.WithCancel(context.TODO())
  42. defer cancel()
  43. s, ok := <-b.Observe(cctx)
  44. if !ok {
  45. t.Fatalf("could not observe election; channel closed")
  46. }
  47. electedc <- string(s.Kvs[0].Value)
  48. // wait for next election round
  49. <-ch
  50. }
  51. donec <- struct{}{}
  52. }(nextc[i])
  53. }
  54. // elect some leaders
  55. for i := 0; i < leaders; i++ {
  56. go func() {
  57. e := concurrency.NewElection(newClient(), "test-election")
  58. ev := fmt.Sprintf("electval-%v", time.Now().UnixNano())
  59. if err := e.Campaign(context.TODO(), ev); err != nil {
  60. t.Fatalf("failed volunteer (%v)", err)
  61. }
  62. // wait for followers to accept leadership
  63. for j := 0; j < followers; j++ {
  64. s := <-electedc
  65. if s != ev {
  66. t.Errorf("wrong election value got %s, wanted %s", s, ev)
  67. }
  68. }
  69. // let next leader take over
  70. if err := e.Resign(context.TODO()); err != nil {
  71. t.Fatalf("failed resign (%v)", err)
  72. }
  73. // tell followers to start listening for next leader
  74. for j := 0; j < followers; j++ {
  75. nextc[j] <- struct{}{}
  76. }
  77. }()
  78. }
  79. // wait on followers
  80. for i := 0; i < followers; i++ {
  81. <-donec
  82. }
  83. closeClients(t, clients)
  84. }
  85. // TestElectionFailover tests that an election will
  86. func TestElectionFailover(t *testing.T) {
  87. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  88. defer clus.Terminate(t)
  89. defer dropSessionLease(clus)
  90. cctx, cancel := context.WithCancel(context.TODO())
  91. defer cancel()
  92. // first leader (elected)
  93. e := concurrency.NewElection(clus.clients[0], "test-election")
  94. if err := e.Campaign(context.TODO(), "foo"); err != nil {
  95. t.Fatalf("failed volunteer (%v)", err)
  96. }
  97. // check first leader
  98. resp, ok := <-e.Observe(cctx)
  99. if !ok {
  100. t.Fatalf("could not wait for first election; channel closed")
  101. }
  102. s := string(resp.Kvs[0].Value)
  103. if s != "foo" {
  104. t.Fatalf("wrong election result. got %s, wanted foo", s)
  105. }
  106. // next leader
  107. electedc := make(chan struct{})
  108. go func() {
  109. ee := concurrency.NewElection(clus.clients[1], "test-election")
  110. if eer := ee.Campaign(context.TODO(), "bar"); eer != nil {
  111. t.Fatal(eer)
  112. }
  113. electedc <- struct{}{}
  114. }()
  115. // invoke leader failover
  116. session, serr := concurrency.NewSession(clus.clients[0])
  117. if serr != nil {
  118. t.Fatal(serr)
  119. }
  120. if err := session.Close(); err != nil {
  121. t.Fatal(err)
  122. }
  123. // check new leader
  124. e = concurrency.NewElection(clus.clients[2], "test-election")
  125. resp, ok = <-e.Observe(cctx)
  126. if !ok {
  127. t.Fatalf("could not wait for second election; channel closed")
  128. }
  129. s = string(resp.Kvs[0].Value)
  130. if s != "bar" {
  131. t.Fatalf("wrong election result. got %s, wanted bar", s)
  132. }
  133. // leader must ack election (otherwise, Campaign may see closed conn)
  134. <-electedc
  135. }
  136. // TestElectionSessionRelock ensures that campaigning twice on the same election
  137. // with the same lock will Proclaim instead of deadlocking.
  138. func TestElectionSessionRecampaign(t *testing.T) {
  139. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  140. defer clus.Terminate(t)
  141. cli := clus.RandClient()
  142. e := concurrency.NewElection(cli, "test-elect")
  143. if err := e.Campaign(context.TODO(), "abc"); err != nil {
  144. t.Fatal(err)
  145. }
  146. e2 := concurrency.NewElection(cli, "test-elect")
  147. if err := e2.Campaign(context.TODO(), "def"); err != nil {
  148. t.Fatal(err)
  149. }
  150. ctx, cancel := context.WithCancel(context.TODO())
  151. defer cancel()
  152. if resp := <-e.Observe(ctx); len(resp.Kvs) == 0 || string(resp.Kvs[0].Value) != "def" {
  153. t.Fatalf("expected value=%q, got response %v", "def", resp)
  154. }
  155. }