node.go 6.0 KB

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