node.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafttest
  15. import (
  16. "context"
  17. "log"
  18. "sync"
  19. "time"
  20. "go.etcd.io/etcd/raft"
  21. "go.etcd.io/etcd/raft/raftpb"
  22. )
  23. type node struct {
  24. raft.Node
  25. id uint64
  26. iface iface
  27. stopc chan struct{}
  28. pausec chan bool
  29. // stable
  30. storage *raft.MemoryStorage
  31. mu sync.Mutex // guards state
  32. state raftpb.HardState
  33. }
  34. func startNode(id uint64, peers []raft.Peer, iface iface) *node {
  35. st := raft.NewMemoryStorage()
  36. c := &raft.Config{
  37. ID: id,
  38. ElectionTick: 10,
  39. HeartbeatTick: 1,
  40. Storage: st,
  41. MaxSizePerMsg: 1024 * 1024,
  42. MaxInflightMsgs: 256,
  43. MaxUncommittedEntriesSize: 1 << 30,
  44. }
  45. rn := raft.StartNode(c, peers)
  46. n := &node{
  47. Node: rn,
  48. id: id,
  49. storage: st,
  50. iface: iface,
  51. pausec: make(chan bool),
  52. }
  53. n.start()
  54. return n
  55. }
  56. func (n *node) start() {
  57. n.stopc = make(chan struct{})
  58. ticker := time.Tick(5 * time.Millisecond)
  59. go func() {
  60. for {
  61. select {
  62. case <-ticker:
  63. n.Tick()
  64. case rd := <-n.Ready():
  65. if !raft.IsEmptyHardState(rd.HardState) {
  66. n.mu.Lock()
  67. n.state = rd.HardState
  68. n.mu.Unlock()
  69. n.storage.SetHardState(n.state)
  70. }
  71. n.storage.Append(rd.Entries)
  72. time.Sleep(time.Millisecond)
  73. // TODO: make send async, more like real world...
  74. for _, m := range rd.Messages {
  75. n.iface.send(m)
  76. }
  77. n.Advance()
  78. case m := <-n.iface.recv():
  79. go n.Step(context.TODO(), m)
  80. case <-n.stopc:
  81. n.Stop()
  82. log.Printf("raft.%d: stop", n.id)
  83. n.Node = nil
  84. close(n.stopc)
  85. return
  86. case p := <-n.pausec:
  87. recvms := make([]raftpb.Message, 0)
  88. for p {
  89. select {
  90. case m := <-n.iface.recv():
  91. recvms = append(recvms, m)
  92. case p = <-n.pausec:
  93. }
  94. }
  95. // step all pending messages
  96. for _, m := range recvms {
  97. n.Step(context.TODO(), m)
  98. }
  99. }
  100. }
  101. }()
  102. }
  103. // stop stops the node. stop a stopped node might panic.
  104. // All in memory state of node is discarded.
  105. // All stable MUST be unchanged.
  106. func (n *node) stop() {
  107. n.iface.disconnect()
  108. n.stopc <- struct{}{}
  109. // wait for the shutdown
  110. <-n.stopc
  111. }
  112. // restart restarts the node. restart a started node
  113. // blocks and might affect the future stop operation.
  114. func (n *node) restart() {
  115. // wait for the shutdown
  116. <-n.stopc
  117. c := &raft.Config{
  118. ID: n.id,
  119. ElectionTick: 10,
  120. HeartbeatTick: 1,
  121. Storage: n.storage,
  122. MaxSizePerMsg: 1024 * 1024,
  123. MaxInflightMsgs: 256,
  124. MaxUncommittedEntriesSize: 1 << 30,
  125. }
  126. n.Node = raft.RestartNode(c)
  127. n.start()
  128. n.iface.connect()
  129. }
  130. // pause pauses the node.
  131. // The paused node buffers the received messages and replies
  132. // all of them when it resumes.
  133. func (n *node) pause() {
  134. n.pausec <- true
  135. }
  136. // resume resumes the paused node.
  137. func (n *node) resume() {
  138. n.pausec <- false
  139. }