forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsistenthash_test.go
446 lines (385 loc) · 12.1 KB
/
consistenthash_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package consistenthash
import (
"fmt"
"strings"
"sync"
"testing"
"github.com/spaolacci/murmur3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"trpc.group/trpc-go/trpc-go/log"
"trpc.group/trpc-go/trpc-go/naming/loadbalance"
"trpc.group/trpc-go/trpc-go/naming/registry"
)
// Test whether key takes effect.
// The returned node should not change for the same key in the same node list.
func TestConsistentHashGetOne(t *testing.T) {
ch := NewConsistentHash()
// test list 1
n, err := ch.Select("test", list1, loadbalance.WithKey("123"))
assert.Nil(t, err)
expectAddr := n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("123"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list1, loadbalance.WithKey("123456"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("123456"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list1, loadbalance.WithKey("12315"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("12315"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
// test list 4
n, err = ch.Select("test", list4, loadbalance.WithKey("Pony"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list4, loadbalance.WithKey("Pony"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list4, loadbalance.WithKey("John"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list4, loadbalance.WithKey("John"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list4, loadbalance.WithKey("Jack"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list4, loadbalance.WithKey("Jack"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
}
// Test whether key takes effect using custom.
// The returned node should not change for the same key in the same node list.
func TestCustomConsistentHashGetOne(t *testing.T) {
ch := NewCustomConsistentHash(murmur3.Sum64)
// test list 1
n, err := ch.Select("test", list1, loadbalance.WithKey("123"))
assert.Nil(t, err)
expectAddr := n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("123"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list1, loadbalance.WithKey("123456"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("123456"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list1, loadbalance.WithKey("12315"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("12315"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
// test list 4
n, err = ch.Select("test", list4, loadbalance.WithKey("Pony"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list4, loadbalance.WithKey("Pony"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list4, loadbalance.WithKey("John"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list4, loadbalance.WithKey("John"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
n, err = ch.Select("test", list4, loadbalance.WithKey("Jack"))
assert.Nil(t, err)
expectAddr = n.Address
n, err = ch.Select("test", list4, loadbalance.WithKey("Jack"))
assert.Nil(t, err)
assert.Equal(t, expectAddr, n.Address)
}
// Test hash-collision.
// The returned node should not equal for the different key.
func TestHashCollision(t *testing.T) {
const magicKey = "magic_key"
ch := NewCustomConsistentHash(func(data []byte) uint64 {
if id := strings.Index(string(data), magicKey); id != -1 {
// the hash value is determined by the byte after magic key
return uint64(data[id+len(magicKey)])
}
// hash must collide if missing magic key.
return 0
})
nodes := []*registry.Node{
{Address: "a"},
{Address: "b"},
{Address: "c"},
}
// consistent hash has two replicas, select three different keys(three different hashes), there's no possible that
// all of them shares the same address.
addresses := make(map[string]struct{})
n, err := ch.Select("test", nodes, loadbalance.WithKey(magicKey+"a"), loadbalance.WithReplicas(2))
require.Nil(t, err)
log.Debug(n.Address)
addresses[n.Address] = struct{}{}
n, err = ch.Select("test", nodes, loadbalance.WithKey(magicKey+"b"), loadbalance.WithReplicas(2))
require.Nil(t, err)
log.Debug(n.Address)
addresses[n.Address] = struct{}{}
n, err = ch.Select("test", nodes, loadbalance.WithKey(magicKey+"c"), loadbalance.WithReplicas(2))
require.Nil(t, err)
log.Debug(n.Address)
addresses[n.Address] = struct{}{}
require.Less(t, 1, len(addresses))
}
// Test empty node list.
// Should return an expected error.
func TestNilList(t *testing.T) {
ch := NewConsistentHash()
n, err := ch.Select("test", nil, loadbalance.WithKey("123"))
assert.Nil(t, n)
assert.Equal(t, loadbalance.ErrNoServerAvailable, err)
}
// Test empty opt.
// WithKey of opt must be provided.
// Should return an expected error.
func TestNilOpts(t *testing.T) {
ch := NewConsistentHash()
n, err := ch.Select("test", list1)
assert.Nil(t, n)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "missing key")
n, err = ch.Select("test", list1, loadbalance.WithKey("whatever"))
assert.Nil(t, err)
assert.NotNil(t, n)
}
// Test node list with only one node.
// Should return the same result each time.
func TestSingleNode(t *testing.T) {
ch := NewConsistentHash()
n, err := ch.Select("test", list2, loadbalance.WithKey("123"))
assert.Nil(t, err)
assert.Equal(t, list2[0].Address, n.Address)
n, err = ch.Select("test", list2, loadbalance.WithKey("456"))
assert.Nil(t, err)
assert.Equal(t, list2[0].Address, n.Address)
n, err = ch.Select("test", list2, loadbalance.WithKey("12306"))
assert.Nil(t, err)
assert.Equal(t, list2[0].Address, n.Address)
n, err = ch.Select("test", list2, loadbalance.WithKey("JackChen"))
assert.Nil(t, err)
assert.Equal(t, list2[0].Address, n.Address)
}
// Hash ring should be updated once length of node list changed.
func TestInterval(t *testing.T) {
ch := NewConsistentHash()
// On list length changed, recalculate hash ring immediately.
n, err := ch.Select("test", list2, loadbalance.WithKey("123"))
assert.Nil(t, err)
assert.Equal(t, list2[0].Address, n.Address)
n, err = ch.Select("test", list4, loadbalance.WithKey("123"))
assert.Nil(t, err)
assert.Equal(t, false, isInList(n.Address, list2))
assert.Equal(t, true, isInList(n.Address, list4))
}
// Test the influence to object mapping position if node is deleted.
func TestSubNode(t *testing.T) {
ch := NewConsistentHash()
var address1, address2, address3 string
n, err := ch.Select("test", list1, loadbalance.WithKey("123"))
assert.Nil(t, err)
address1 = n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("123456"))
assert.Nil(t, err)
address2 = n.Address
n, err = ch.Select("test", list1, loadbalance.WithKey("12315"))
assert.Nil(t, err)
address3 = n.Address
deletedAddress := address1
// Delete deletedAddress of list1.
// No key is effected except the key influenced by deletedAddress.
listTmp := deleteNode(deletedAddress, list1)
n, err = ch.Select("test", listTmp, loadbalance.WithKey("123"))
assert.Nil(t, err)
if address1 != deletedAddress {
assert.Equal(t, address1, n.Address)
} else {
assert.NotEqual(t, address1, n.Address)
}
n, err = ch.Select("test", listTmp, loadbalance.WithKey("123456"))
assert.Nil(t, err)
if address2 != deletedAddress {
assert.Equal(t, address2, n.Address)
} else {
assert.NotEqual(t, address2, n.Address)
}
n, err = ch.Select("test", listTmp, loadbalance.WithKey("12315"))
assert.Nil(t, err)
if address3 != deletedAddress {
assert.Equal(t, address3, n.Address)
} else {
assert.NotEqual(t, address3, n.Address)
}
}
// Test balance.
func TestBalance(t *testing.T) {
ch := NewConsistentHash()
counter := make(map[string]int)
for i := 0; i < 200; i++ {
n, err := ch.Select("test", list1, loadbalance.WithKey(fmt.Sprintf("%d", i)), loadbalance.WithReplicas(100))
assert.Nil(t, err)
if _, ok := counter[n.Address]; !ok {
counter[n.Address] = 0
} else {
counter[n.Address]++
}
}
for _, v := range counter {
assert.NotEqual(t, 0, v)
fmt.Println(v)
}
}
func TestIsNodeSliceEqualBCE(t *testing.T) {
isEqual := isNodeSliceEqualBCE(list1, list2)
assert.Equal(t, false, isEqual)
isEqual = isNodeSliceEqualBCE(list1, list1)
assert.Equal(t, true, isEqual)
isEqual = isNodeSliceEqualBCE(list1, nil)
assert.Equal(t, false, isEqual)
}
// Test concurrency safety. List changes every visit.
// This is an extreme situation. In fact, in most cases, node list of a service does not change
// frequently, but will only change on service scaling.
func TestParallel(t *testing.T) {
var wg sync.WaitGroup
ch := NewConsistentHash()
var lists [][]*registry.Node
var keys []string
var results []string
n, err := ch.Select("test", list1, loadbalance.WithKey("1"))
assert.Nil(t, err)
results = append(results, n.Address)
lists = append(lists, list1)
keys = append(keys, "1")
n, err = ch.Select("test", list2, loadbalance.WithKey("2"))
assert.Nil(t, err)
results = append(results, n.Address)
lists = append(lists, list2)
keys = append(keys, "2")
n, err = ch.Select("test", list3, loadbalance.WithKey("3"))
assert.Nil(t, err)
results = append(results, n.Address)
lists = append(lists, list3)
keys = append(keys, "3")
n, err = ch.Select("test", list4, loadbalance.WithKey("4"))
assert.Nil(t, err)
results = append(results, n.Address)
lists = append(lists, list4)
keys = append(keys, "4")
n, err = ch.Select("test", list5, loadbalance.WithKey("5"))
assert.Nil(t, err)
results = append(results, n.Address)
lists = append(lists, list5)
keys = append(keys, "5")
// To simulate a large concurrent goroutine.
// This is an extreme situation. In fact, in most cases, node list of a service does not change
// frequently, but will only change on service scaling.
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
n, err := ch.Select("test0", lists[i%5], loadbalance.WithKey(keys[i%5]))
assert.Nil(t, err)
assert.Equal(t, results[i%5], n.Address)
}(i)
}
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
n, err := ch.Select("test1", lists[0], loadbalance.WithKey(keys[0]))
assert.Nil(t, err)
assert.Equal(t, results[0], n.Address)
}(i)
}
wg.Wait()
}
// Test performance on current visit.
func BenchmarkParallel(b *testing.B) {
ch := NewConsistentHash()
b.SetParallelism(10) // 10 concurrency
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = ch.Select("test", list1, loadbalance.WithKey("HelloWorld"))
}
})
}
var list1 = []*registry.Node{
{
Address: "list1.ip.1:8080",
},
{
Address: "list1.ip.2:8080",
},
{
Address: "list1.ip.3:8080",
},
{
Address: "list1.ip.4:8080",
},
}
var list2 = []*registry.Node{
{
Address: "list2.ip.1:8080",
},
}
var list3 = []*registry.Node{
{
Address: "list3.ip.2:8080",
},
{
Address: "list3.ip.4:8080",
},
{
Address: "list3.ip.1:8080",
},
}
var list4 = []*registry.Node{
{
Address: "list4.ip.168:8080",
},
{
Address: "list4.ip.167:8080",
},
{
Address: "list4.ip.15:8080",
},
{
Address: "list4.ip.15:8081",
},
}
var list5 = []*registry.Node{
{
Address: "list5.ip.2:8080",
},
}
func deleteNode(address string, list []*registry.Node) []*registry.Node {
ret := make([]*registry.Node, 0, len(list))
for _, n := range list {
if n.Address != address {
ret = append(ret, n)
}
}
return ret
}
func isInList(address string, list []*registry.Node) bool {
for _, n := range list {
if n.Address == address {
return true
}
}
return false
}