node.go 3.1 KB

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