-
Notifications
You must be signed in to change notification settings - Fork 76
/
socket.go
210 lines (178 loc) · 3.95 KB
/
socket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package paxi
import (
"math/rand"
"sync"
"time"
"github.com/ailidani/paxi/log"
)
// Socket integrates all networking interface and fault injections
type Socket interface {
// Send put message to outbound queue
Send(to ID, m interface{})
// MulticastZone send msg to all nodes in the same site
MulticastZone(zone int, m interface{})
// MulticastQuorum sends msg to random number of nodes
MulticastQuorum(quorum int, m interface{})
// Broadcast send to all peers
Broadcast(m interface{})
// Recv receives a message
Recv() interface{}
Close()
// Fault injection
Drop(id ID, t int) // drops every message send to ID last for t seconds
Slow(id ID, d int, t int) // delays every message send to ID for d ms and last for t seconds
Flaky(id ID, p float64, t int) // drop message by chance p for t seconds
Crash(t int) // node crash for t seconds
}
type socket struct {
id ID
addresses map[ID]string
nodes map[ID]Transport
crash bool
drop map[ID]bool
slow map[ID]int
flaky map[ID]float64
lock sync.RWMutex // locking map nodes
}
// NewSocket return Socket interface instance given self ID, node list, transport and codec name
func NewSocket(id ID, addrs map[ID]string) Socket {
socket := &socket{
id: id,
addresses: addrs,
nodes: make(map[ID]Transport),
crash: false,
drop: make(map[ID]bool),
slow: make(map[ID]int),
flaky: make(map[ID]float64),
}
socket.nodes[id] = NewTransport(addrs[id])
socket.nodes[id].Listen()
return socket
}
func (s *socket) Send(to ID, m interface{}) {
log.Debugf("node %s send message %+v to %v", s.id, m, to)
if s.crash {
return
}
if s.drop[to] {
return
}
if p, ok := s.flaky[to]; ok && p > 0 {
if rand.Float64() < p {
return
}
}
s.lock.RLock()
t, exists := s.nodes[to]
s.lock.RUnlock()
if !exists {
s.lock.RLock()
address, ok := s.addresses[to]
s.lock.RUnlock()
if !ok {
log.Errorf("socket does not have address of node %s", to)
return
}
t = NewTransport(address)
err := Retry(t.Dial, 100, time.Duration(50)*time.Millisecond)
if err != nil {
panic(err)
}
s.lock.Lock()
s.nodes[to] = t
s.lock.Unlock()
}
if delay, ok := s.slow[to]; ok && delay > 0 {
timer := time.NewTimer(time.Duration(delay) * time.Millisecond)
go func() {
<-timer.C
t.Send(m)
}()
return
}
t.Send(m)
}
func (s *socket) Recv() interface{} {
s.lock.RLock()
t := s.nodes[s.id]
s.lock.RUnlock()
for {
m := t.Recv()
if !s.crash {
return m
}
}
}
func (s *socket) MulticastZone(zone int, m interface{}) {
//log.Debugf("node %s broadcasting message %+v in zone %d", s.id, m, zone)
for id := range s.addresses {
if id == s.id {
continue
}
if id.Zone() == zone {
s.Send(id, m)
}
}
}
func (s *socket) MulticastQuorum(quorum int, m interface{}) {
//log.Debugf("node %s multicasting message %+v for %d nodes", s.id, m, quorum)
i := 0
for id := range s.addresses {
if id == s.id {
continue
}
s.Send(id, m)
i++
if i == quorum {
break
}
}
}
func (s *socket) Broadcast(m interface{}) {
//log.Debugf("node %s broadcasting message %+v", s.id, m)
for id := range s.addresses {
if id == s.id {
continue
}
s.Send(id, m)
}
}
func (s *socket) Close() {
for _, t := range s.nodes {
t.Close()
}
}
func (s *socket) Drop(id ID, t int) {
s.drop[id] = true
timer := time.NewTimer(time.Duration(t) * time.Second)
go func() {
<-timer.C
s.drop[id] = false
}()
}
func (s *socket) Slow(id ID, delay int, t int) {
s.slow[id] = delay
timer := time.NewTimer(time.Duration(t) * time.Second)
go func() {
<-timer.C
s.slow[id] = 0
}()
}
func (s *socket) Flaky(id ID, p float64, t int) {
s.flaky[id] = p
timer := time.NewTimer(time.Duration(t) * time.Second)
go func() {
<-timer.C
s.flaky[id] = 0
}()
}
func (s *socket) Crash(t int) {
s.crash = true
if t > 0 {
timer := time.NewTimer(time.Duration(t) * time.Second)
go func() {
<-timer.C
s.crash = false
}()
}
}