node.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package raft
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "log"
  6. "math/rand"
  7. "time"
  8. )
  9. type Interface interface {
  10. Step(m Message) bool
  11. Msgs() []Message
  12. }
  13. type tick int64
  14. type Config struct {
  15. NodeId int64
  16. Addr string
  17. Context []byte
  18. }
  19. type Node struct {
  20. sm *stateMachine
  21. elapsed tick
  22. electionRand tick
  23. election tick
  24. heartbeat tick
  25. // TODO: it needs garbage collection later
  26. rmNodes map[int64]struct{}
  27. removed bool
  28. }
  29. func New(id int64, heartbeat, election tick) *Node {
  30. if election < heartbeat*3 {
  31. panic("election is least three times as heartbeat [election: %d, heartbeat: %d]")
  32. }
  33. rand.Seed(time.Now().UnixNano())
  34. n := &Node{
  35. heartbeat: heartbeat,
  36. election: election,
  37. electionRand: election + tick(rand.Int31())%election,
  38. sm: newStateMachine(id, []int64{id}),
  39. rmNodes: make(map[int64]struct{}),
  40. }
  41. return n
  42. }
  43. func (n *Node) Id() int64 { return n.sm.id }
  44. func (n *Node) ClusterId() int64 { return n.sm.clusterId }
  45. func (n *Node) Index() int64 { return n.sm.index.Get() }
  46. func (n *Node) Term() int64 { return n.sm.term.Get() }
  47. func (n *Node) Applied() int64 { return n.sm.raftLog.applied }
  48. func (n *Node) HasLeader() bool { return n.Leader() != none }
  49. func (n *Node) IsLeader() bool { return n.Leader() == n.Id() }
  50. func (n *Node) Leader() int64 { return n.sm.lead.Get() }
  51. func (n *Node) IsRemoved() bool { return n.removed }
  52. // Propose asynchronously proposes data be applied to the underlying state machine.
  53. func (n *Node) Propose(data []byte) { n.propose(Normal, data) }
  54. func (n *Node) propose(t int64, data []byte) {
  55. n.Step(Message{From: n.sm.id, ClusterId: n.ClusterId(), Type: msgProp, Entries: []Entry{{Type: t, Data: data}}})
  56. }
  57. func (n *Node) Campaign() { n.Step(Message{From: n.sm.id, ClusterId: n.ClusterId(), Type: msgHup}) }
  58. func (n *Node) InitCluster(clusterId int64) {
  59. d := make([]byte, 10)
  60. wn := binary.PutVarint(d, clusterId)
  61. n.propose(ClusterInit, d[:wn])
  62. }
  63. func (n *Node) Add(id int64, addr string, context []byte) {
  64. n.UpdateConf(AddNode, &Config{NodeId: id, Addr: addr, Context: context})
  65. }
  66. func (n *Node) Remove(id int64) {
  67. n.UpdateConf(RemoveNode, &Config{NodeId: id})
  68. }
  69. func (n *Node) Msgs() []Message { return n.sm.Msgs() }
  70. func (n *Node) Step(m Message) bool {
  71. if m.Type == msgDenied {
  72. n.removed = true
  73. return false
  74. }
  75. if n.ClusterId() != none && m.ClusterId != none && m.ClusterId != n.ClusterId() {
  76. log.Printf("denied a message from node %d, cluster %d. accept cluster: %d\n", m.From, m.ClusterId, n.ClusterId())
  77. n.sm.send(Message{To: m.From, ClusterId: n.ClusterId(), Type: msgDenied})
  78. return true
  79. }
  80. if _, ok := n.rmNodes[m.From]; ok {
  81. if m.From != n.sm.id {
  82. n.sm.send(Message{To: m.From, ClusterId: n.ClusterId(), Type: msgDenied})
  83. }
  84. return true
  85. }
  86. l := len(n.sm.msgs)
  87. if !n.sm.Step(m) {
  88. return false
  89. }
  90. for _, m := range n.sm.msgs[l:] {
  91. switch m.Type {
  92. case msgAppResp:
  93. // We just heard from the leader of the same term.
  94. n.elapsed = 0
  95. case msgVoteResp:
  96. // We just heard from the candidate the node voted for.
  97. if m.Index >= 0 {
  98. n.elapsed = 0
  99. }
  100. }
  101. }
  102. return true
  103. }
  104. // Next returns all the appliable entries
  105. func (n *Node) Next() []Entry {
  106. ents := n.sm.nextEnts()
  107. for i := range ents {
  108. switch ents[i].Type {
  109. case Normal:
  110. case ClusterInit:
  111. cid, nr := binary.Varint(ents[i].Data)
  112. if nr <= 0 {
  113. panic("init cluster failed: cannot read clusterId")
  114. }
  115. if n.ClusterId() != -1 {
  116. panic("cannot init a started cluster")
  117. }
  118. n.sm.clusterId = cid
  119. case AddNode:
  120. c := new(Config)
  121. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  122. log.Println(err)
  123. continue
  124. }
  125. n.sm.addNode(c.NodeId)
  126. delete(n.rmNodes, c.NodeId)
  127. case RemoveNode:
  128. c := new(Config)
  129. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  130. log.Println(err)
  131. continue
  132. }
  133. n.sm.removeNode(c.NodeId)
  134. n.rmNodes[c.NodeId] = struct{}{}
  135. if c.NodeId == n.sm.id {
  136. n.removed = true
  137. }
  138. default:
  139. panic("unexpected entry type")
  140. }
  141. }
  142. return ents
  143. }
  144. // Tick triggers the node to do a tick.
  145. // If the current elapsed is greater or equal than the timeout,
  146. // node will send corresponding message to the statemachine.
  147. func (n *Node) Tick() {
  148. if !n.sm.promotable() {
  149. return
  150. }
  151. timeout, msgType := n.electionRand, msgHup
  152. if n.sm.state == stateLeader {
  153. timeout, msgType = n.heartbeat, msgBeat
  154. }
  155. if n.elapsed >= timeout {
  156. n.Step(Message{From: n.sm.id, ClusterId: n.ClusterId(), Type: msgType})
  157. n.elapsed = 0
  158. if n.sm.state != stateLeader {
  159. n.electionRand = n.election + tick(rand.Int31())%n.election
  160. }
  161. } else {
  162. n.elapsed++
  163. }
  164. }
  165. func (n *Node) UpdateConf(t int64, c *Config) {
  166. data, err := json.Marshal(c)
  167. if err != nil {
  168. panic(err)
  169. }
  170. n.propose(t, data)
  171. }
  172. // UnstableEnts retuens all the entries that need to be persistent.
  173. func (n *Node) UnstableEnts() []Entry {
  174. return n.sm.raftLog.unstableEnts()
  175. }
  176. func (n *Node) UnstableState() State {
  177. if n.sm.unstableState == emptyState {
  178. return emptyState
  179. }
  180. s := n.sm.unstableState
  181. n.sm.clearState()
  182. return s
  183. }