node.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "math/rand"
  19. "sync"
  20. "time"
  21. "go.etcd.io/etcd/raft"
  22. "go.etcd.io/etcd/raft/raftpb"
  23. )
  24. type node struct {
  25. raft.Node
  26. id uint64
  27. iface iface
  28. stopc chan struct{}
  29. pausec chan bool
  30. // stable
  31. storage *raft.MemoryStorage
  32. mu sync.Mutex // guards state
  33. state raftpb.HardState
  34. }
  35. func startNode(id uint64, peers []raft.Peer, iface iface) *node {
  36. st := raft.NewMemoryStorage()
  37. c := &raft.Config{
  38. ID: id,
  39. ElectionTick: 10,
  40. HeartbeatTick: 1,
  41. Storage: st,
  42. MaxSizePerMsg: 1024 * 1024,
  43. MaxInflightMsgs: 256,
  44. MaxUncommittedEntriesSize: 1 << 30,
  45. }
  46. rn := raft.StartNode(c, peers)
  47. n := &node{
  48. Node: rn,
  49. id: id,
  50. storage: st,
  51. iface: iface,
  52. pausec: make(chan bool),
  53. }
  54. n.start()
  55. return n
  56. }
  57. func (n *node) start() {
  58. n.stopc = make(chan struct{})
  59. ticker := time.Tick(5 * time.Millisecond)
  60. go func() {
  61. for {
  62. select {
  63. case <-ticker:
  64. n.Tick()
  65. case rd := <-n.Ready():
  66. if !raft.IsEmptyHardState(rd.HardState) {
  67. n.mu.Lock()
  68. n.state = rd.HardState
  69. n.mu.Unlock()
  70. n.storage.SetHardState(n.state)
  71. }
  72. n.storage.Append(rd.Entries)
  73. time.Sleep(time.Millisecond)
  74. // simulate async send, more like real world...
  75. for _, m := range rd.Messages {
  76. mlocal := m
  77. go func() {
  78. time.Sleep(time.Duration(rand.Int63n(10)) * time.Millisecond)
  79. n.iface.send(mlocal)
  80. }()
  81. }
  82. n.Advance()
  83. case m := <-n.iface.recv():
  84. go n.Step(context.TODO(), m)
  85. case <-n.stopc:
  86. n.Stop()
  87. log.Printf("raft.%d: stop", n.id)
  88. n.Node = nil
  89. close(n.stopc)
  90. return
  91. case p := <-n.pausec:
  92. recvms := make([]raftpb.Message, 0)
  93. for p {
  94. select {
  95. case m := <-n.iface.recv():
  96. recvms = append(recvms, m)
  97. case p = <-n.pausec:
  98. }
  99. }
  100. // step all pending messages
  101. for _, m := range recvms {
  102. n.Step(context.TODO(), m)
  103. }
  104. }
  105. }
  106. }()
  107. }
  108. // stop stops the node. stop a stopped node might panic.
  109. // All in memory state of node is discarded.
  110. // All stable MUST be unchanged.
  111. func (n *node) stop() {
  112. n.iface.disconnect()
  113. n.stopc <- struct{}{}
  114. // wait for the shutdown
  115. <-n.stopc
  116. }
  117. // restart restarts the node. restart a started node
  118. // blocks and might affect the future stop operation.
  119. func (n *node) restart() {
  120. // wait for the shutdown
  121. <-n.stopc
  122. c := &raft.Config{
  123. ID: n.id,
  124. ElectionTick: 10,
  125. HeartbeatTick: 1,
  126. Storage: n.storage,
  127. MaxSizePerMsg: 1024 * 1024,
  128. MaxInflightMsgs: 256,
  129. MaxUncommittedEntriesSize: 1 << 30,
  130. }
  131. n.Node = raft.RestartNode(c)
  132. n.start()
  133. n.iface.connect()
  134. }
  135. // pause pauses the node.
  136. // The paused node buffers the received messages and replies
  137. // all of them when it resumes.
  138. func (n *node) pause() {
  139. n.pausec <- true
  140. }
  141. // resume resumes the paused node.
  142. func (n *node) resume() {
  143. n.pausec <- false
  144. }