node.go 3.4 KB

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