node.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 int64
  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 int64, 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, []int64{id}),
  29. }
  30. return n
  31. }
  32. func (n *Node) Id() int64 { return n.sm.id }
  33. func (n *Node) Index() int { return n.sm.log.lastIndex() }
  34. func (n *Node) Term() int { return n.sm.term }
  35. func (n *Node) Applied() int { return n.sm.log.applied }
  36. func (n *Node) HasLeader() bool { return n.sm.lead != none }
  37. func (n *Node) IsLeader() bool { return n.sm.lead == n.Id() }
  38. func (n *Node) Leader() int { return n.sm.lead }
  39. // Propose asynchronously proposes data be applied to the underlying state machine.
  40. func (n *Node) Propose(data []byte) { n.propose(Normal, data) }
  41. func (n *Node) propose(t int, data []byte) {
  42. n.Step(Message{Type: msgProp, Entries: []Entry{{Type: t, Data: data}}})
  43. }
  44. func (n *Node) Campaign() { n.Step(Message{Type: msgHup}) }
  45. func (n *Node) Add(id int64, addr string) { n.updateConf(AddNode, &Config{NodeId: id, Addr: addr}) }
  46. func (n *Node) Remove(id int64) { n.updateConf(RemoveNode, &Config{NodeId: id}) }
  47. func (n *Node) Msgs() []Message { return n.sm.Msgs() }
  48. func (n *Node) Step(m Message) bool {
  49. l := len(n.sm.msgs)
  50. if !n.sm.Step(m) {
  51. return false
  52. }
  53. for _, m := range n.sm.msgs[l:] {
  54. switch m.Type {
  55. case msgAppResp:
  56. // We just heard from the leader of the same term.
  57. n.elapsed = 0
  58. case msgVoteResp:
  59. // We just heard from the candidate the node voted for.
  60. if m.Index >= 0 {
  61. n.elapsed = 0
  62. }
  63. }
  64. }
  65. return true
  66. }
  67. // Next returns all the appliable entries
  68. func (n *Node) Next() []Entry {
  69. ents := n.sm.nextEnts()
  70. for i := range ents {
  71. switch ents[i].Type {
  72. case Normal:
  73. case AddNode:
  74. c := new(Config)
  75. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  76. golog.Println(err)
  77. continue
  78. }
  79. n.sm.addNode(c.NodeId)
  80. case RemoveNode:
  81. c := new(Config)
  82. if err := json.Unmarshal(ents[i].Data, c); err != nil {
  83. golog.Println(err)
  84. continue
  85. }
  86. n.sm.removeNode(c.NodeId)
  87. default:
  88. panic("unexpected entry type")
  89. }
  90. }
  91. return ents
  92. }
  93. // Tick triggers the node to do a tick.
  94. // If the current elapsed is greater or equal than the timeout,
  95. // node will send corresponding message to the statemachine.
  96. func (n *Node) Tick() {
  97. if !n.sm.promotable() {
  98. return
  99. }
  100. timeout, msgType := n.election, msgHup
  101. if n.sm.state == stateLeader {
  102. timeout, msgType = n.heartbeat, msgBeat
  103. }
  104. if n.elapsed >= timeout {
  105. n.Step(Message{Type: msgType})
  106. n.elapsed = 0
  107. } else {
  108. n.elapsed++
  109. }
  110. }
  111. func (n *Node) updateConf(t int, c *Config) {
  112. data, err := json.Marshal(c)
  113. if err != nil {
  114. panic(err)
  115. }
  116. n.propose(t, data)
  117. }