node.go 2.8 KB

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