election.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 concurrency
  15. import (
  16. "errors"
  17. "fmt"
  18. v3 "github.com/coreos/etcd/clientv3"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/mvcc/mvccpb"
  21. "golang.org/x/net/context"
  22. )
  23. var (
  24. ErrElectionNotLeader = errors.New("election: not leader")
  25. ErrElectionNoLeader = errors.New("election: no leader")
  26. )
  27. type Election struct {
  28. session *Session
  29. keyPrefix string
  30. leaderKey string
  31. leaderRev int64
  32. leaderSession *Session
  33. hdr *pb.ResponseHeader
  34. }
  35. // NewElection returns a new election on a given key prefix.
  36. func NewElection(s *Session, pfx string) *Election {
  37. return &Election{session: s, keyPrefix: pfx + "/"}
  38. }
  39. // ResumeElection initializes an election with a known leader.
  40. func ResumeElection(s *Session, pfx string, leaderKey string, leaderRev int64) *Election {
  41. return &Election{
  42. session: s,
  43. leaderKey: leaderKey,
  44. leaderRev: leaderRev,
  45. leaderSession: s,
  46. }
  47. }
  48. // Campaign puts a value as eligible for the election. It blocks until
  49. // it is elected, an error occurs, or the context is cancelled.
  50. func (e *Election) Campaign(ctx context.Context, val string) error {
  51. s := e.session
  52. client := e.session.Client()
  53. k := fmt.Sprintf("%s%x", e.keyPrefix, s.Lease())
  54. txn := client.Txn(ctx).If(v3.Compare(v3.CreateRevision(k), "=", 0))
  55. txn = txn.Then(v3.OpPut(k, val, v3.WithLease(s.Lease())))
  56. txn = txn.Else(v3.OpGet(k))
  57. resp, err := txn.Commit()
  58. if err != nil {
  59. return err
  60. }
  61. e.leaderKey, e.leaderRev, e.leaderSession = k, resp.Header.Revision, s
  62. if !resp.Succeeded {
  63. kv := resp.Responses[0].GetResponseRange().Kvs[0]
  64. e.leaderRev = kv.CreateRevision
  65. if string(kv.Value) != val {
  66. if err = e.Proclaim(ctx, val); err != nil {
  67. e.Resign(ctx)
  68. return err
  69. }
  70. }
  71. }
  72. _, err = waitDeletes(ctx, client, e.keyPrefix, e.leaderRev-1)
  73. if err != nil {
  74. // clean up in case of context cancel
  75. select {
  76. case <-ctx.Done():
  77. e.Resign(client.Ctx())
  78. default:
  79. e.leaderSession = nil
  80. }
  81. return err
  82. }
  83. e.hdr = resp.Header
  84. return nil
  85. }
  86. // Proclaim lets the leader announce a new value without another election.
  87. func (e *Election) Proclaim(ctx context.Context, val string) error {
  88. if e.leaderSession == nil {
  89. return ErrElectionNotLeader
  90. }
  91. client := e.session.Client()
  92. cmp := v3.Compare(v3.CreateRevision(e.leaderKey), "=", e.leaderRev)
  93. txn := client.Txn(ctx).If(cmp)
  94. txn = txn.Then(v3.OpPut(e.leaderKey, val, v3.WithLease(e.leaderSession.Lease())))
  95. tresp, terr := txn.Commit()
  96. if terr != nil {
  97. return terr
  98. }
  99. if !tresp.Succeeded {
  100. e.leaderKey = ""
  101. return ErrElectionNotLeader
  102. }
  103. e.hdr = tresp.Header
  104. return nil
  105. }
  106. // Resign lets a leader start a new election.
  107. func (e *Election) Resign(ctx context.Context) (err error) {
  108. if e.leaderSession == nil {
  109. return nil
  110. }
  111. client := e.session.Client()
  112. cmp := v3.Compare(v3.CreateRevision(e.leaderKey), "=", e.leaderRev)
  113. resp, err := client.Txn(ctx).If(cmp).Then(v3.OpDelete(e.leaderKey)).Commit()
  114. if err == nil {
  115. e.hdr = resp.Header
  116. }
  117. e.leaderKey = ""
  118. e.leaderSession = nil
  119. return err
  120. }
  121. // Leader returns the leader value for the current election.
  122. func (e *Election) Leader(ctx context.Context) (*v3.GetResponse, error) {
  123. client := e.session.Client()
  124. resp, err := client.Get(ctx, e.keyPrefix, v3.WithFirstCreate()...)
  125. if err != nil {
  126. return nil, err
  127. } else if len(resp.Kvs) == 0 {
  128. // no leader currently elected
  129. return nil, ErrElectionNoLeader
  130. }
  131. return resp, nil
  132. }
  133. // Observe returns a channel that reliably observes ordered leader proposals
  134. // as GetResponse values on every current elected leader key. It will not
  135. // necessarily fetch all historical leader updates, but will always post the
  136. // most recent leader value.
  137. //
  138. // The channel closes when the context is canceled or the underlying watcher
  139. // is otherwise disrupted.
  140. func (e *Election) Observe(ctx context.Context) <-chan v3.GetResponse {
  141. retc := make(chan v3.GetResponse)
  142. go e.observe(ctx, retc)
  143. return retc
  144. }
  145. func (e *Election) observe(ctx context.Context, ch chan<- v3.GetResponse) {
  146. client := e.session.Client()
  147. defer close(ch)
  148. for {
  149. resp, err := client.Get(ctx, e.keyPrefix, v3.WithFirstCreate()...)
  150. if err != nil {
  151. return
  152. }
  153. var kv *mvccpb.KeyValue
  154. var hdr *pb.ResponseHeader
  155. if len(resp.Kvs) == 0 {
  156. cctx, cancel := context.WithCancel(ctx)
  157. // wait for first key put on prefix
  158. opts := []v3.OpOption{v3.WithRev(resp.Header.Revision), v3.WithPrefix()}
  159. wch := client.Watch(cctx, e.keyPrefix, opts...)
  160. for kv == nil {
  161. wr, ok := <-wch
  162. if !ok || wr.Err() != nil {
  163. cancel()
  164. return
  165. }
  166. // only accept puts; a delete will make observe() spin
  167. for _, ev := range wr.Events {
  168. if ev.Type == mvccpb.PUT {
  169. hdr, kv = &wr.Header, ev.Kv
  170. // may have multiple revs; hdr.rev = the last rev
  171. // set to kv's rev in case batch has multiple Puts
  172. hdr.Revision = kv.ModRevision
  173. break
  174. }
  175. }
  176. }
  177. cancel()
  178. } else {
  179. hdr, kv = resp.Header, resp.Kvs[0]
  180. }
  181. select {
  182. case ch <- v3.GetResponse{Header: hdr, Kvs: []*mvccpb.KeyValue{kv}}:
  183. case <-ctx.Done():
  184. return
  185. }
  186. cctx, cancel := context.WithCancel(ctx)
  187. wch := client.Watch(cctx, string(kv.Key), v3.WithRev(hdr.Revision+1))
  188. keyDeleted := false
  189. for !keyDeleted {
  190. wr, ok := <-wch
  191. if !ok {
  192. cancel()
  193. return
  194. }
  195. for _, ev := range wr.Events {
  196. if ev.Type == mvccpb.DELETE {
  197. keyDeleted = true
  198. break
  199. }
  200. resp.Header = &wr.Header
  201. resp.Kvs = []*mvccpb.KeyValue{ev.Kv}
  202. select {
  203. case ch <- *resp:
  204. case <-cctx.Done():
  205. cancel()
  206. return
  207. }
  208. }
  209. }
  210. cancel()
  211. }
  212. }
  213. // Key returns the leader key if elected, empty string otherwise.
  214. func (e *Election) Key() string { return e.leaderKey }
  215. // Rev returns the leader key's creation revision, if elected.
  216. func (e *Election) Rev() int64 { return e.leaderRev }
  217. // Header is the response header from the last successful election proposal.
  218. func (e *Election) Header() *pb.ResponseHeader { return e.hdr }