node.go 3.0 KB

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