node.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package raft
  2. import (
  3. "encoding/json"
  4. )
  5. type Interface interface {
  6. Step(m Message)
  7. Msgs() []Message
  8. }
  9. type tick int
  10. type ConfigCmd struct {
  11. Type string
  12. Addr int
  13. }
  14. type Node struct {
  15. // election timeout and heartbeat timeout in tick
  16. election tick
  17. heartbeat tick
  18. // elapsed ticks after the last reset
  19. elapsed tick
  20. sm *stateMachine
  21. addr int
  22. }
  23. func New(addr int, heartbeat, election tick) *Node {
  24. if election < heartbeat*3 {
  25. panic("election is least three times as heartbeat [election: %d, heartbeat: %d]")
  26. }
  27. n := &Node{
  28. heartbeat: heartbeat,
  29. election: election,
  30. addr: addr,
  31. }
  32. return n
  33. }
  34. // Propose asynchronously proposes data be applied to the underlying state machine.
  35. func (n *Node) Propose(data []byte) {
  36. m := Message{Type: msgProp, Entries: []Entry{{Data: data}}}
  37. n.Step(m)
  38. }
  39. func (n *Node) StartCluster() {
  40. if n.sm != nil {
  41. panic("node is started")
  42. }
  43. n.sm = newStateMachine(n.addr, []int{n.addr})
  44. n.Step(Message{Type: msgHup})
  45. n.Step(n.confMessage(&ConfigCmd{Type: "add", Addr: n.addr}))
  46. n.Next()
  47. }
  48. func (n *Node) Start() {
  49. if n.sm != nil {
  50. panic("node is started")
  51. }
  52. n.sm = newStateMachine(n.addr, nil)
  53. }
  54. func (n *Node) Add(addr int) {
  55. n.Step(n.confMessage(&ConfigCmd{Type: "add", Addr: addr}))
  56. }
  57. func (n *Node) Remove(addr int) {
  58. n.Step(n.confMessage(&ConfigCmd{Type: "remove", Addr: addr}))
  59. }
  60. func (n *Node) Msgs() []Message {
  61. return n.sm.Msgs()
  62. }
  63. func (n *Node) Step(m Message) {
  64. l := len(n.sm.msgs)
  65. n.sm.Step(m)
  66. for _, m := range n.sm.msgs[l:] {
  67. // reset elapsed in two cases:
  68. // msgAppResp -> heard from the leader of the same term
  69. // msgVoteResp with grant -> heard from the candidate the node voted for
  70. switch m.Type {
  71. case msgAppResp:
  72. n.elapsed = 0
  73. case msgVoteResp:
  74. if m.Index >= 0 {
  75. n.elapsed = 0
  76. }
  77. }
  78. }
  79. }
  80. // Next applies all available committed commands.
  81. func (n *Node) Next() {
  82. ents := n.sm.nextEnts()
  83. for i := range ents {
  84. switch ents[i].Type {
  85. case normal:
  86. // dispatch to the application state machine
  87. case config:
  88. c := new(ConfigCmd)
  89. err := json.Unmarshal(ents[i].Data, c)
  90. if err != nil {
  91. // warning
  92. continue
  93. }
  94. n.updateConf(c)
  95. default:
  96. panic("unexpected entry type")
  97. }
  98. }
  99. }
  100. // Tick triggers the node to do a tick.
  101. // If the current elapsed is greater or equal than the timeout,
  102. // node will send corresponding message to the statemachine.
  103. func (n *Node) Tick() {
  104. timeout, msgType := n.election, msgHup
  105. if n.sm.state == stateLeader {
  106. timeout, msgType = n.heartbeat, msgBeat
  107. }
  108. if n.elapsed >= timeout {
  109. n.Step(Message{Type: msgType})
  110. n.elapsed = 0
  111. } else {
  112. n.elapsed++
  113. }
  114. }
  115. func (n *Node) confMessage(c *ConfigCmd) Message {
  116. data, err := json.Marshal(c)
  117. if err != nil {
  118. panic(err)
  119. }
  120. return Message{Type: msgProp, Entries: []Entry{Entry{Type: config, Data: data}}}
  121. }
  122. func (n *Node) updateConf(c *ConfigCmd) {
  123. switch c.Type {
  124. case "add":
  125. n.sm.Add(c.Addr)
  126. case "remove":
  127. n.sm.Remove(c.Addr)
  128. default:
  129. // warn
  130. }
  131. }