node.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Package raft implements raft.
  2. package raft
  3. import (
  4. "errors"
  5. "log"
  6. "code.google.com/p/go.net/context"
  7. pb "github.com/coreos/etcd/raft/raftpb"
  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 := Node{
  41. propc: make(chan pb.Message),
  42. recvc: make(chan pb.Message),
  43. readyc: make(chan Ready),
  44. tickc: make(chan struct{}),
  45. alwaysreadyc: make(chan Ready),
  46. done: make(chan struct{}),
  47. }
  48. r := newRaft(id, peers, election, heartbeat)
  49. go n.run(r)
  50. return n
  51. }
  52. func (n *Node) Stop() {
  53. close(n.done)
  54. }
  55. func (n *Node) run(r *raft) {
  56. propc := n.propc
  57. readyc := n.readyc
  58. var prev Ready
  59. for {
  60. if r.hasLeader() {
  61. log.Printf("raft: found leader %#x - unblocking proposals", r.lead)
  62. propc = n.propc
  63. } else {
  64. // We cannot accept proposals because we don't know who
  65. // to send them to, so we'll apply back-pressure and
  66. // block senders.
  67. log.Println("raft: no leader - blocking proposals")
  68. propc = nil
  69. }
  70. rd := Ready{
  71. r.State,
  72. r.raftLog.unstableEnts(),
  73. r.raftLog.nextEnts(),
  74. r.msgs,
  75. }
  76. if rd.containsUpdates(prev) {
  77. readyc = n.readyc
  78. prev = rd
  79. } else {
  80. readyc = nil
  81. }
  82. select {
  83. case m := <-propc:
  84. m.From = r.id
  85. r.Step(m)
  86. case m := <-n.recvc:
  87. r.Step(m) // raft never returns an error
  88. case <-n.tickc:
  89. r.tick()
  90. case readyc <- rd:
  91. r.raftLog.resetNextEnts()
  92. r.raftLog.resetUnstable()
  93. r.msgs = nil
  94. case n.alwaysreadyc <- rd:
  95. // this is for testing only
  96. case <-n.done:
  97. return
  98. }
  99. }
  100. }
  101. func (n *Node) Tick() error {
  102. select {
  103. case n.tickc <- struct{}{}:
  104. return nil
  105. case <-n.done:
  106. return n.ctx.Err()
  107. }
  108. }
  109. func (n *Node) Campaign(ctx context.Context) error {
  110. return n.Step(ctx, pb.Message{Type: msgHup})
  111. }
  112. // Propose proposes data be appended to the log.
  113. func (n *Node) Propose(ctx context.Context, data []byte) error {
  114. return n.Step(ctx, pb.Message{Type: msgProp, Entries: []pb.Entry{{Data: data}}})
  115. }
  116. // Step advances the state machine using msgs. The ctx.Err() will be returned,
  117. // if any.
  118. func (n *Node) Step(ctx context.Context, m pb.Message) error {
  119. ch := n.recvc
  120. if m.Type == msgProp {
  121. ch = n.propc
  122. }
  123. select {
  124. case ch <- m:
  125. return nil
  126. case <-ctx.Done():
  127. return ctx.Err()
  128. case <-n.done:
  129. return ErrStopped
  130. }
  131. }
  132. // ReadState returns the current point-in-time state.
  133. func (n *Node) Ready() <-chan Ready {
  134. return n.readyc
  135. }
  136. // RecvReadyNow returns the state of n without blocking. It is primarly for
  137. // testing purposes only.
  138. func RecvReadyNow(n Node) Ready {
  139. return <-n.alwaysreadyc
  140. }