node.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. for i := range ents {
  69. switch ents[i].Type {
  70. case normal:
  71. case configAdd:
  72. c := new(config)
  73. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  74. golog.Println(err)
  75. continue
  76. }
  77. n.sm.Add(c.NodeId)
  78. case configRemove:
  79. c := new(config)
  80. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  81. golog.Println(err)
  82. continue
  83. }
  84. n.sm.Remove(c.NodeId)
  85. default:
  86. panic("unexpected entry type")
  87. }
  88. }
  89. return ents
  90. }
  91. // Tick triggers the node to do a tick.
  92. // If the current elapsed is greater or equal than the timeout,
  93. // node will send corresponding message to the statemachine.
  94. func (n *Node) Tick() {
  95. timeout, msgType := n.election, msgHup
  96. if n.sm.state == stateLeader {
  97. timeout, msgType = n.heartbeat, msgBeat
  98. }
  99. if n.elapsed >= timeout {
  100. n.Step(Message{Type: msgType})
  101. n.elapsed = 0
  102. } else {
  103. n.elapsed++
  104. }
  105. }
  106. func (n *Node) updateConf(t int, c *config) {
  107. data, err := json.Marshal(c)
  108. if err != nil {
  109. panic(err)
  110. }
  111. n.propose(t, data)
  112. }