node.go 3.3 KB

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