node.go 3.2 KB

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