network.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package rafttest
  2. import (
  3. "math/rand"
  4. "sync"
  5. "time"
  6. "github.com/coreos/etcd/raft/raftpb"
  7. )
  8. // a network interface
  9. type iface interface {
  10. send(m raftpb.Message)
  11. recv() chan raftpb.Message
  12. disconnect()
  13. connect()
  14. }
  15. // a network
  16. type network interface {
  17. // drop message at given rate (1.0 drops all messages)
  18. drop(from, to uint64, rate float64)
  19. // delay message for (0, d] randomly at given rate (1.0 delay all messages)
  20. // do we need rate here?
  21. delay(from, to uint64, d time.Duration, rate float64)
  22. disconnect(id uint64)
  23. connect(id uint64)
  24. // heal heals the network
  25. heal()
  26. }
  27. type raftNetwork struct {
  28. mu sync.Mutex
  29. disconnected map[uint64]bool
  30. dropmap map[conn]float64
  31. delaymap map[conn]delay
  32. recvQueues map[uint64]chan raftpb.Message
  33. }
  34. type conn struct {
  35. from, to uint64
  36. }
  37. type delay struct {
  38. d time.Duration
  39. rate float64
  40. }
  41. func newRaftNetwork(nodes ...uint64) *raftNetwork {
  42. pn := &raftNetwork{
  43. recvQueues: make(map[uint64]chan raftpb.Message),
  44. dropmap: make(map[conn]float64),
  45. delaymap: make(map[conn]delay),
  46. disconnected: make(map[uint64]bool),
  47. }
  48. for _, n := range nodes {
  49. pn.recvQueues[n] = make(chan raftpb.Message, 1024)
  50. }
  51. return pn
  52. }
  53. func (rn *raftNetwork) nodeNetwork(id uint64) iface {
  54. return &nodeNetwork{id: id, raftNetwork: rn}
  55. }
  56. func (rn *raftNetwork) send(m raftpb.Message) {
  57. rn.mu.Lock()
  58. to := rn.recvQueues[m.To]
  59. if rn.disconnected[m.To] {
  60. to = nil
  61. }
  62. drop := rn.dropmap[conn{m.From, m.To}]
  63. delay := rn.delaymap[conn{m.From, m.To}]
  64. rn.mu.Unlock()
  65. if to == nil {
  66. return
  67. }
  68. if drop != 0 && rand.Float64() < drop {
  69. return
  70. }
  71. // TODO: shall we delay without blocking the send call?
  72. if delay.d != 0 && rand.Float64() < delay.rate {
  73. rd := rand.Int63n(int64(delay.d))
  74. time.Sleep(time.Duration(rd))
  75. }
  76. select {
  77. case to <- m:
  78. default:
  79. // drop messages when the receiver queue is full.
  80. }
  81. }
  82. func (rn *raftNetwork) recvFrom(from uint64) chan raftpb.Message {
  83. rn.mu.Lock()
  84. fromc := rn.recvQueues[from]
  85. if rn.disconnected[from] {
  86. fromc = nil
  87. }
  88. rn.mu.Unlock()
  89. return fromc
  90. }
  91. func (rn *raftNetwork) drop(from, to uint64, rate float64) {
  92. rn.mu.Lock()
  93. defer rn.mu.Unlock()
  94. rn.dropmap[conn{from, to}] = rate
  95. }
  96. func (rn *raftNetwork) delay(from, to uint64, d time.Duration, rate float64) {
  97. rn.mu.Lock()
  98. defer rn.mu.Unlock()
  99. rn.delaymap[conn{from, to}] = delay{d, rate}
  100. }
  101. func (rn *raftNetwork) heal() {
  102. rn.mu.Lock()
  103. defer rn.mu.Unlock()
  104. rn.dropmap = make(map[conn]float64)
  105. rn.delaymap = make(map[conn]delay)
  106. }
  107. func (rn *raftNetwork) disconnect(id uint64) {
  108. rn.mu.Lock()
  109. defer rn.mu.Unlock()
  110. rn.disconnected[id] = true
  111. }
  112. func (rn *raftNetwork) connect(id uint64) {
  113. rn.mu.Lock()
  114. defer rn.mu.Unlock()
  115. rn.disconnected[id] = false
  116. }
  117. type nodeNetwork struct {
  118. id uint64
  119. *raftNetwork
  120. }
  121. func (nt *nodeNetwork) connect() {
  122. nt.raftNetwork.connect(nt.id)
  123. }
  124. func (nt *nodeNetwork) disconnect() {
  125. nt.raftNetwork.disconnect(nt.id)
  126. }
  127. func (nt *nodeNetwork) send(m raftpb.Message) {
  128. nt.raftNetwork.send(m)
  129. }
  130. func (nt *nodeNetwork) recv() chan raftpb.Message {
  131. return nt.recvFrom(nt.id)
  132. }