node.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Package raft implements raft.
  2. package raft
  3. import (
  4. "errors"
  5. "log"
  6. pb "github.com/coreos/etcd/raft/raftpb"
  7. "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
  8. )
  9. var ErrStopped = errors.New("raft: stopped")
  10. // Ready encapsulates the entries and messages that are ready to be saved to
  11. // stable storage, committed or sent to other peers.
  12. type Ready struct {
  13. // The current state of a Node
  14. pb.State
  15. // Entries specifies entries to be saved to stable storage BEFORE
  16. // Messages are sent.
  17. Entries []pb.Entry
  18. // CommittedEntries specifies entries to be committed to a
  19. // store/state-machine. These have previously been committed to stable
  20. // store.
  21. CommittedEntries []pb.Entry
  22. // Messages specifies outbound messages to be sent AFTER Entries are
  23. // committed to stable storage.
  24. Messages []pb.Message
  25. }
  26. func isStateEqual(a, b pb.State) bool {
  27. return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex
  28. }
  29. func (rd Ready) containsUpdates(prev Ready) bool {
  30. return !isStateEqual(prev.State, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
  31. }
  32. type Node struct {
  33. ctx context.Context
  34. propc chan pb.Message
  35. recvc chan pb.Message
  36. readyc chan Ready
  37. tickc chan struct{}
  38. alwaysreadyc chan Ready
  39. done chan struct{}
  40. }
  41. func Start(id int64, peers []int64, election, heartbeat int) Node {
  42. n := Node{
  43. propc: make(chan pb.Message),
  44. recvc: make(chan pb.Message),
  45. readyc: make(chan Ready),
  46. tickc: make(chan struct{}),
  47. alwaysreadyc: make(chan Ready),
  48. done: make(chan struct{}),
  49. }
  50. r := newRaft(id, peers, election, heartbeat)
  51. go n.run(r)
  52. return n
  53. }
  54. func (n *Node) Stop() {
  55. close(n.done)
  56. }
  57. func (n *Node) run(r *raft) {
  58. propc := n.propc
  59. readyc := n.readyc
  60. var lead int64
  61. var prev Ready
  62. prev.Vote = none
  63. for {
  64. if lead != r.lead {
  65. log.Printf("raft: leader changed from %#x to %#x", lead, r.lead)
  66. lead = r.lead
  67. if r.hasLeader() {
  68. propc = n.propc
  69. } else {
  70. propc = nil
  71. }
  72. }
  73. rd := Ready{
  74. r.State,
  75. r.raftLog.unstableEnts(),
  76. r.raftLog.nextEnts(),
  77. r.msgs,
  78. }
  79. if rd.containsUpdates(prev) {
  80. readyc = n.readyc
  81. prev = rd
  82. } else {
  83. readyc = nil
  84. }
  85. select {
  86. case m := <-propc:
  87. m.From = r.id
  88. r.Step(m)
  89. case m := <-n.recvc:
  90. r.Step(m) // raft never returns an error
  91. case <-n.tickc:
  92. r.tick()
  93. case readyc <- rd:
  94. r.raftLog.resetNextEnts()
  95. r.raftLog.resetUnstable()
  96. r.msgs = nil
  97. case n.alwaysreadyc <- rd:
  98. // this is for testing only
  99. case <-n.done:
  100. return
  101. }
  102. }
  103. }
  104. func (n *Node) Tick() error {
  105. select {
  106. case n.tickc <- struct{}{}:
  107. return nil
  108. case <-n.done:
  109. return n.ctx.Err()
  110. }
  111. }
  112. func (n *Node) Campaign(ctx context.Context) error {
  113. return n.Step(ctx, pb.Message{Type: msgHup})
  114. }
  115. // Propose proposes data be appended to the log.
  116. func (n *Node) Propose(ctx context.Context, data []byte) error {
  117. return n.Step(ctx, pb.Message{Type: msgProp, Entries: []pb.Entry{{Data: data}}})
  118. }
  119. // Step advances the state machine using msgs. The ctx.Err() will be returned,
  120. // if any.
  121. func (n *Node) Step(ctx context.Context, m pb.Message) error {
  122. ch := n.recvc
  123. if m.Type == msgProp {
  124. ch = n.propc
  125. }
  126. select {
  127. case ch <- m:
  128. return nil
  129. case <-ctx.Done():
  130. return ctx.Err()
  131. case <-n.done:
  132. return ErrStopped
  133. }
  134. }
  135. // ReadState returns the current point-in-time state.
  136. func (n *Node) Ready() <-chan Ready {
  137. return n.readyc
  138. }
  139. // RecvReadyNow returns the state of n without blocking. It is primarly for
  140. // testing purposes only.
  141. func RecvReadyNow(n Node) Ready {
  142. return <-n.alwaysreadyc
  143. }