node.go 3.8 KB

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