node.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. type Ready struct {
  11. // The current state of a Node
  12. pb.State
  13. // Entries specifies entries to be saved to stable storage BEFORE
  14. // Messages are sent.
  15. Entries []pb.Entry
  16. // CommittedEntries specifies entries to be committed to a
  17. // store/state-machine. These have previously been committed to stable
  18. // store.
  19. CommittedEntries []pb.Entry
  20. // Messages specifies outbound messages to be sent AFTER Entries are
  21. // committed to stable storage.
  22. Messages []pb.Message
  23. }
  24. func isStateEqual(a, b pb.State) bool {
  25. return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex
  26. }
  27. func (rd Ready) containsUpdates(prev Ready) bool {
  28. return !isStateEqual(prev.State, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
  29. }
  30. type Node struct {
  31. ctx context.Context
  32. propc chan pb.Message
  33. recvc chan pb.Message
  34. readyc chan Ready
  35. tickc chan struct{}
  36. alwaysreadyc chan Ready
  37. done chan struct{}
  38. }
  39. func Start(id int64, peers []int64, election, heartbeat int) Node {
  40. n := newNode()
  41. r := newRaft(id, peers, election, heartbeat)
  42. go n.run(r)
  43. return n
  44. }
  45. func Restart(id int64, peers []int64, election, heartbeat int, st pb.State, ents []pb.Entry) Node {
  46. n := newNode()
  47. r := newRaft(id, peers, election, heartbeat)
  48. r.loadState(st)
  49. r.loadEnts(ents)
  50. go n.run(r)
  51. return n
  52. }
  53. func newNode() Node {
  54. return Node{
  55. propc: make(chan pb.Message),
  56. recvc: make(chan pb.Message),
  57. readyc: make(chan Ready),
  58. tickc: make(chan struct{}),
  59. alwaysreadyc: make(chan Ready),
  60. done: make(chan struct{}),
  61. }
  62. }
  63. func (n *Node) Stop() {
  64. close(n.done)
  65. }
  66. func (n *Node) run(r *raft) {
  67. propc := n.propc
  68. readyc := n.readyc
  69. var lead int64
  70. var prev Ready
  71. prev.State = r.State
  72. for {
  73. if lead != r.lead {
  74. log.Printf("raft: leader changed from %#x to %#x", lead, r.lead)
  75. lead = r.lead
  76. if r.hasLeader() {
  77. propc = n.propc
  78. } else {
  79. propc = nil
  80. }
  81. }
  82. rd := Ready{
  83. r.State,
  84. r.raftLog.unstableEnts(),
  85. r.raftLog.nextEnts(),
  86. r.msgs,
  87. }
  88. if rd.containsUpdates(prev) {
  89. readyc = n.readyc
  90. prev = rd
  91. } else {
  92. readyc = nil
  93. }
  94. select {
  95. case m := <-propc:
  96. m.From = r.id
  97. r.Step(m)
  98. case m := <-n.recvc:
  99. r.Step(m) // raft never returns an error
  100. case <-n.tickc:
  101. r.tick()
  102. case readyc <- rd:
  103. r.raftLog.resetNextEnts()
  104. r.raftLog.resetUnstable()
  105. r.msgs = nil
  106. case n.alwaysreadyc <- rd:
  107. // this is for testing only
  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. // RecvReadyNow returns the state of n without blocking. It is primarly for
  149. // testing purposes only.
  150. func RecvReadyNow(n Node) Ready {
  151. return <-n.alwaysreadyc
  152. }