v3_election_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. "context"
  17. "fmt"
  18. "testing"
  19. "time"
  20. "go.etcd.io/etcd/clientv3"
  21. "go.etcd.io/etcd/clientv3/concurrency"
  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.Errorf("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.Errorf("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.Errorf("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)
  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/etcd-io/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. }
  200. // TestElectionOnSessionRestart tests that a quick restart of leader (resulting
  201. // in a new session with the same lease id) does not result in loss of
  202. // leadership.
  203. func TestElectionOnSessionRestart(t *testing.T) {
  204. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  205. defer clus.Terminate(t)
  206. cli := clus.RandClient()
  207. session, err := concurrency.NewSession(cli)
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. e := concurrency.NewElection(session, "test-elect")
  212. if cerr := e.Campaign(context.TODO(), "abc"); cerr != nil {
  213. t.Fatal(cerr)
  214. }
  215. // ensure leader is not lost to waiter on fail-over
  216. waitSession, werr := concurrency.NewSession(cli)
  217. if werr != nil {
  218. t.Fatal(werr)
  219. }
  220. defer waitSession.Orphan()
  221. waitCtx, waitCancel := context.WithTimeout(context.TODO(), 5*time.Second)
  222. defer waitCancel()
  223. go concurrency.NewElection(waitSession, "test-elect").Campaign(waitCtx, "123")
  224. // simulate restart by reusing the lease from the old session
  225. newSession, nerr := concurrency.NewSession(cli, concurrency.WithLease(session.Lease()))
  226. if nerr != nil {
  227. t.Fatal(nerr)
  228. }
  229. defer newSession.Orphan()
  230. newElection := concurrency.NewElection(newSession, "test-elect")
  231. if ncerr := newElection.Campaign(context.TODO(), "def"); ncerr != nil {
  232. t.Fatal(ncerr)
  233. }
  234. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  235. defer cancel()
  236. if resp := <-newElection.Observe(ctx); len(resp.Kvs) == 0 || string(resp.Kvs[0].Value) != "def" {
  237. t.Errorf("expected value=%q, got response %v", "def", resp)
  238. }
  239. }
  240. // TestElectionObserveCompacted checks that observe can tolerate
  241. // a leader key with a modrev less than the compaction revision.
  242. func TestElectionObserveCompacted(t *testing.T) {
  243. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  244. defer clus.Terminate(t)
  245. cli := clus.Client(0)
  246. session, err := concurrency.NewSession(cli)
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. defer session.Orphan()
  251. e := concurrency.NewElection(session, "test-elect")
  252. if cerr := e.Campaign(context.TODO(), "abc"); cerr != nil {
  253. t.Fatal(cerr)
  254. }
  255. presp, perr := cli.Put(context.TODO(), "foo", "bar")
  256. if perr != nil {
  257. t.Fatal(perr)
  258. }
  259. if _, cerr := cli.Compact(context.TODO(), presp.Header.Revision); cerr != nil {
  260. t.Fatal(cerr)
  261. }
  262. v, ok := <-e.Observe(context.TODO())
  263. if !ok {
  264. t.Fatal("failed to observe on compacted revision")
  265. }
  266. if string(v.Kvs[0].Value) != "abc" {
  267. t.Fatalf(`expected leader value "abc", got %q`, string(v.Kvs[0].Value))
  268. }
  269. }