node.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. done chan struct{}
  39. }
  40. func Start(id int64, peers []int64, election, heartbeat int) Node {
  41. n := newNode()
  42. r := newRaft(id, peers, election, heartbeat)
  43. go n.run(r)
  44. return n
  45. }
  46. func Restart(id int64, peers []int64, election, heartbeat int, st pb.State, ents []pb.Entry) Node {
  47. n := newNode()
  48. r := newRaft(id, peers, election, heartbeat)
  49. r.loadState(st)
  50. r.loadEnts(ents)
  51. go n.run(r)
  52. return n
  53. }
  54. func newNode() Node {
  55. return Node{
  56. propc: make(chan pb.Message),
  57. recvc: make(chan pb.Message),
  58. readyc: make(chan Ready),
  59. tickc: make(chan struct{}),
  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. } 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. prev = rd
  105. r.msgs = nil
  106. case <-n.done:
  107. return
  108. }
  109. }
  110. }
  111. func (n *Node) Tick() error {
  112. select {
  113. case n.tickc <- struct{}{}:
  114. return nil
  115. case <-n.done:
  116. return n.ctx.Err()
  117. }
  118. }
  119. func (n *Node) Campaign(ctx context.Context) error {
  120. return n.Step(ctx, pb.Message{Type: msgHup})
  121. }
  122. // Propose proposes data be appended to the log.
  123. func (n *Node) Propose(ctx context.Context, data []byte) error {
  124. return n.Step(ctx, pb.Message{Type: msgProp, Entries: []pb.Entry{{Data: data}}})
  125. }
  126. // Step advances the state machine using msgs. The ctx.Err() will be returned,
  127. // if any.
  128. func (n *Node) Step(ctx context.Context, m pb.Message) error {
  129. ch := n.recvc
  130. if m.Type == msgProp {
  131. ch = n.propc
  132. }
  133. select {
  134. case ch <- m:
  135. return nil
  136. case <-ctx.Done():
  137. return ctx.Err()
  138. case <-n.done:
  139. return ErrStopped
  140. }
  141. }
  142. // ReadState returns the current point-in-time state.
  143. func (n *Node) Ready() <-chan Ready {
  144. return n.readyc
  145. }