network.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "math/rand"
  17. "sync"
  18. "time"
  19. "github.com/coreos/etcd/raft/raftpb"
  20. )
  21. // a network interface
  22. type iface interface {
  23. send(m raftpb.Message)
  24. recv() chan raftpb.Message
  25. disconnect()
  26. connect()
  27. }
  28. // a network
  29. type network interface {
  30. // drop message at given rate (1.0 drops all messages)
  31. drop(from, to uint64, rate float64)
  32. // delay message for (0, d] randomly at given rate (1.0 delay all messages)
  33. // do we need rate here?
  34. delay(from, to uint64, d time.Duration, rate float64)
  35. disconnect(id uint64)
  36. connect(id uint64)
  37. // heal heals the network
  38. heal()
  39. }
  40. type raftNetwork struct {
  41. mu sync.Mutex
  42. disconnected map[uint64]bool
  43. dropmap map[conn]float64
  44. delaymap map[conn]delay
  45. recvQueues map[uint64]chan raftpb.Message
  46. }
  47. type conn struct {
  48. from, to uint64
  49. }
  50. type delay struct {
  51. d time.Duration
  52. rate float64
  53. }
  54. func newRaftNetwork(nodes ...uint64) *raftNetwork {
  55. pn := &raftNetwork{
  56. recvQueues: make(map[uint64]chan raftpb.Message),
  57. dropmap: make(map[conn]float64),
  58. delaymap: make(map[conn]delay),
  59. disconnected: make(map[uint64]bool),
  60. }
  61. for _, n := range nodes {
  62. pn.recvQueues[n] = make(chan raftpb.Message, 1024)
  63. }
  64. return pn
  65. }
  66. func (rn *raftNetwork) nodeNetwork(id uint64) iface {
  67. return &nodeNetwork{id: id, raftNetwork: rn}
  68. }
  69. func (rn *raftNetwork) send(m raftpb.Message) {
  70. rn.mu.Lock()
  71. to := rn.recvQueues[m.To]
  72. if rn.disconnected[m.To] {
  73. to = nil
  74. }
  75. drop := rn.dropmap[conn{m.From, m.To}]
  76. dl := rn.delaymap[conn{m.From, m.To}]
  77. rn.mu.Unlock()
  78. if to == nil {
  79. return
  80. }
  81. if drop != 0 && rand.Float64() < drop {
  82. return
  83. }
  84. // TODO: shall we dl without blocking the send call?
  85. if dl.d != 0 && rand.Float64() < dl.rate {
  86. rd := rand.Int63n(int64(dl.d))
  87. time.Sleep(time.Duration(rd))
  88. }
  89. // use marshal/unmarshal to copy message to avoid data race.
  90. b, err := m.Marshal()
  91. if err != nil {
  92. panic(err)
  93. }
  94. var cm raftpb.Message
  95. err = cm.Unmarshal(b)
  96. if err != nil {
  97. panic(err)
  98. }
  99. select {
  100. case to <- cm:
  101. default:
  102. // drop messages when the receiver queue is full.
  103. }
  104. }
  105. func (rn *raftNetwork) recvFrom(from uint64) chan raftpb.Message {
  106. rn.mu.Lock()
  107. fromc := rn.recvQueues[from]
  108. if rn.disconnected[from] {
  109. fromc = nil
  110. }
  111. rn.mu.Unlock()
  112. return fromc
  113. }
  114. func (rn *raftNetwork) drop(from, to uint64, rate float64) {
  115. rn.mu.Lock()
  116. defer rn.mu.Unlock()
  117. rn.dropmap[conn{from, to}] = rate
  118. }
  119. func (rn *raftNetwork) delay(from, to uint64, d time.Duration, rate float64) {
  120. rn.mu.Lock()
  121. defer rn.mu.Unlock()
  122. rn.delaymap[conn{from, to}] = delay{d, rate}
  123. }
  124. func (rn *raftNetwork) heal() {
  125. rn.mu.Lock()
  126. defer rn.mu.Unlock()
  127. rn.dropmap = make(map[conn]float64)
  128. rn.delaymap = make(map[conn]delay)
  129. }
  130. func (rn *raftNetwork) disconnect(id uint64) {
  131. rn.mu.Lock()
  132. defer rn.mu.Unlock()
  133. rn.disconnected[id] = true
  134. }
  135. func (rn *raftNetwork) connect(id uint64) {
  136. rn.mu.Lock()
  137. defer rn.mu.Unlock()
  138. rn.disconnected[id] = false
  139. }
  140. type nodeNetwork struct {
  141. id uint64
  142. *raftNetwork
  143. }
  144. func (nt *nodeNetwork) connect() {
  145. nt.raftNetwork.connect(nt.id)
  146. }
  147. func (nt *nodeNetwork) disconnect() {
  148. nt.raftNetwork.disconnect(nt.id)
  149. }
  150. func (nt *nodeNetwork) send(m raftpb.Message) {
  151. nt.raftNetwork.send(m)
  152. }
  153. func (nt *nodeNetwork) recv() chan raftpb.Message {
  154. return nt.recvFrom(nt.id)
  155. }