network.go 3.9 KB

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