node.go 3.2 KB

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