-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathchord_test.go
275 lines (239 loc) · 6.02 KB
/
chord_test.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package chord
import (
"runtime"
"testing"
"time"
)
type MultiLocalTrans struct {
remote Transport
hosts map[string]*LocalTransport
}
func InitMLTransport() *MultiLocalTrans {
hosts := make(map[string]*LocalTransport)
remote := &BlackholeTransport{}
ml := &MultiLocalTrans{hosts: hosts}
ml.remote = remote
return ml
}
func (ml *MultiLocalTrans) ListVnodes(host string) ([]*Vnode, error) {
if local, ok := ml.hosts[host]; ok {
return local.ListVnodes(host)
}
return ml.remote.ListVnodes(host)
}
// Ping a Vnode, check for liveness
func (ml *MultiLocalTrans) Ping(v *Vnode) (bool, error) {
if local, ok := ml.hosts[v.Host]; ok {
return local.Ping(v)
}
return ml.remote.Ping(v)
}
// Request a nodes predecessor
func (ml *MultiLocalTrans) GetPredecessor(v *Vnode) (*Vnode, error) {
if local, ok := ml.hosts[v.Host]; ok {
return local.GetPredecessor(v)
}
return ml.remote.GetPredecessor(v)
}
// Notify our successor of ourselves
func (ml *MultiLocalTrans) Notify(target, self *Vnode) ([]*Vnode, error) {
if local, ok := ml.hosts[target.Host]; ok {
return local.Notify(target, self)
}
return ml.remote.Notify(target, self)
}
// Find a successor
func (ml *MultiLocalTrans) FindSuccessors(v *Vnode, n int, k []byte) ([]*Vnode, error) {
if local, ok := ml.hosts[v.Host]; ok {
return local.FindSuccessors(v, n, k)
}
return ml.remote.FindSuccessors(v, n, k)
}
// Clears a predecessor if it matches a given vnode. Used to leave.
func (ml *MultiLocalTrans) ClearPredecessor(target, self *Vnode) error {
if local, ok := ml.hosts[target.Host]; ok {
return local.ClearPredecessor(target, self)
}
return ml.remote.ClearPredecessor(target, self)
}
// Instructs a node to skip a given successor. Used to leave.
func (ml *MultiLocalTrans) SkipSuccessor(target, self *Vnode) error {
if local, ok := ml.hosts[target.Host]; ok {
return local.SkipSuccessor(target, self)
}
return ml.remote.SkipSuccessor(target, self)
}
func (ml *MultiLocalTrans) Register(v *Vnode, o VnodeRPC) {
local, ok := ml.hosts[v.Host]
if !ok {
local = InitLocalTransport(nil).(*LocalTransport)
ml.hosts[v.Host] = local
}
local.Register(v, o)
}
func (ml *MultiLocalTrans) Deregister(host string) {
delete(ml.hosts, host)
}
func TestDefaultConfig(t *testing.T) {
conf := DefaultConfig("test")
if conf.Hostname != "test" {
t.Fatalf("bad hostname")
}
if conf.NumVnodes != 8 {
t.Fatalf("bad num vnodes")
}
if conf.NumSuccessors != 8 {
t.Fatalf("bad num succ")
}
if conf.HashFunc == nil {
t.Fatalf("bad hash")
}
if conf.hashBits != 160 {
t.Fatalf("bad hash bits")
}
if conf.StabilizeMin != time.Duration(15*time.Second) {
t.Fatalf("bad min stable")
}
if conf.StabilizeMax != time.Duration(45*time.Second) {
t.Fatalf("bad max stable")
}
if conf.Delegate != nil {
t.Fatalf("bad delegate")
}
}
func fastConf() *Config {
conf := DefaultConfig("test")
conf.StabilizeMin = time.Duration(15 * time.Millisecond)
conf.StabilizeMax = time.Duration(45 * time.Millisecond)
return conf
}
func TestCreateShutdown(t *testing.T) {
// Start the timer thread
time.After(15)
conf := fastConf()
numGo := runtime.NumGoroutine()
r, err := Create(conf, nil)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
r.Shutdown()
after := runtime.NumGoroutine()
if after != numGo {
t.Fatalf("unexpected routines! A:%d B:%d", after, numGo)
}
}
func TestJoin(t *testing.T) {
// Create a multi transport
ml := InitMLTransport()
// Create the initial ring
conf := fastConf()
r, err := Create(conf, ml)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Create a second ring
conf2 := fastConf()
conf2.Hostname = "test2"
r2, err := Join(conf2, ml, "test")
if err != nil {
t.Fatalf("failed to join local node! Got %s", err)
}
// Shutdown
r.Shutdown()
r2.Shutdown()
}
func TestJoinDeadHost(t *testing.T) {
// Create a multi transport
ml := InitMLTransport()
// Create the initial ring
conf := fastConf()
_, err := Join(conf, ml, "noop")
if err == nil {
t.Fatalf("expected err!")
}
}
func TestLeave(t *testing.T) {
// Create a multi transport
ml := InitMLTransport()
// Create the initial ring
conf := fastConf()
r, err := Create(conf, ml)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Create a second ring
conf2 := fastConf()
conf2.Hostname = "test2"
r2, err := Join(conf2, ml, "test")
if err != nil {
t.Fatalf("failed to join local node! Got %s", err)
}
// Wait for some stabilization
<-time.After(100 * time.Millisecond)
// Node 1 should leave
r.Leave()
ml.Deregister("test")
// Wait for stabilization
<-time.After(100 * time.Millisecond)
// Verify r2 ring is still in tact
num := len(r2.vnodes)
for idx, vn := range r2.vnodes {
if vn.successors[0] != &r2.vnodes[(idx+1)%num].Vnode {
t.Fatalf("bad successor! Got:%s:%s", vn.successors[0].Host,
vn.successors[0])
}
}
}
func TestLookupBadN(t *testing.T) {
// Create a multi transport
ml := InitMLTransport()
// Create the initial ring
conf := fastConf()
r, err := Create(conf, ml)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
_, err = r.Lookup(10, []byte("test"))
if err == nil {
t.Fatalf("expected err!")
}
}
func TestLookup(t *testing.T) {
// Create a multi transport
ml := InitMLTransport()
// Create the initial ring
conf := fastConf()
r, err := Create(conf, ml)
if err != nil {
t.Fatalf("unexpected err. %s", err)
}
// Create a second ring
conf2 := fastConf()
conf2.Hostname = "test2"
r2, err := Join(conf2, ml, "test")
if err != nil {
t.Fatalf("failed to join local node! Got %s", err)
}
// Wait for some stabilization
<-time.After(100 * time.Millisecond)
// Try key lookup
keys := [][]byte{[]byte("test"), []byte("foo"), []byte("bar")}
for _, k := range keys {
vn1, err := r.Lookup(3, k)
if err != nil {
t.Fatalf("unexpected err %s", err)
}
vn2, err := r2.Lookup(3, k)
if err != nil {
t.Fatalf("unexpected err %s", err)
}
if len(vn1) != len(vn2) {
t.Fatalf("result len differs!")
}
for idx := range vn1 {
if vn1[idx].String() != vn2[idx].String() {
t.Fatalf("results differ!")
}
}
}
}