node.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package raft
  2. import (
  3. "encoding/json"
  4. golog "log"
  5. )
  6. type Interface interface {
  7. Step(m Message) bool
  8. Msgs() []Message
  9. }
  10. type tick int
  11. type Config struct {
  12. NodeId int
  13. Addr 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 (n *Node) Id() int { return n.sm.id }
  33. func (n *Node) HasLeader() bool { return n.sm.lead != none }
  34. // Propose asynchronously proposes data be applied to the underlying state machine.
  35. func (n *Node) Propose(data []byte) { n.propose(Normal, data) }
  36. func (n *Node) propose(t int, data []byte) {
  37. n.Step(Message{Type: msgProp, Entries: []Entry{{Type: t, Data: data}}})
  38. }
  39. func (n *Node) Campaign() { n.Step(Message{Type: msgHup}) }
  40. func (n *Node) Add(id int, addr string) { n.updateConf(AddNode, &Config{NodeId: id, Addr: addr}) }
  41. func (n *Node) Remove(id int) { n.updateConf(RemoveNode, &Config{NodeId: id}) }
  42. func (n *Node) Msgs() []Message { return n.sm.Msgs() }
  43. func (n *Node) Step(m Message) bool {
  44. l := len(n.sm.msgs)
  45. if !n.sm.Step(m) {
  46. return false
  47. }
  48. for _, m := range n.sm.msgs[l:] {
  49. switch m.Type {
  50. case msgAppResp:
  51. // We just heard from the leader of the same term.
  52. n.elapsed = 0
  53. case msgVoteResp:
  54. // We just heard from the candidate the node voted for.
  55. if m.Index >= 0 {
  56. n.elapsed = 0
  57. }
  58. }
  59. }
  60. return true
  61. }
  62. // Next returns all the appliable entries
  63. func (n *Node) Next() []Entry {
  64. ents := n.sm.nextEnts()
  65. for i := range ents {
  66. switch ents[i].Type {
  67. case Normal:
  68. case AddNode:
  69. c := new(Config)
  70. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  71. golog.Println(err)
  72. continue
  73. }
  74. n.sm.addNode(c.NodeId)
  75. case RemoveNode:
  76. c := new(Config)
  77. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  78. golog.Println(err)
  79. continue
  80. }
  81. n.sm.removeNode(c.NodeId)
  82. default:
  83. panic("unexpected entry type")
  84. }
  85. }
  86. return ents
  87. }
  88. // Tick triggers the node to do a tick.
  89. // If the current elapsed is greater or equal than the timeout,
  90. // node will send corresponding message to the statemachine.
  91. func (n *Node) Tick() {
  92. if !n.sm.promotable() {
  93. return
  94. }
  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. }