node.go 2.9 KB

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