node.go 2.6 KB

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