-
Notifications
You must be signed in to change notification settings - Fork 4
/
balancer_test.go
424 lines (390 loc) · 10.3 KB
/
balancer_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
package consistent
import (
"context"
"errors"
"fmt"
"hash/maphash"
"reflect"
"sync"
"testing"
"unsafe"
"github.com/cespare/xxhash/v2"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/resolver"
"github.com/authzed/consistent/hashring"
)
type fakeSubConn struct {
balancer.SubConn
id string
}
func (fakeSubConn) Connect() {}
func keys(members []hashring.Member) []string {
keys := make([]string, 0, len(members))
for _, member := range members {
keys = append(keys, member.Key())
}
return keys
}
// Note: this is testing picker behavior and not the hashring
// behavior itself, see `pkg/consistent` for tests of the hashring.
func TestConsistentHashringPickerPick(t *testing.T) {
// Override the intn function with one that uses a stable seed.
intn = func(n uint8) int {
h := new(maphash.Hash)
// This hack sets an unexported field using reflection.
var seed maphash.Seed
field := reflect.ValueOf(&seed).Elem().Field(0)
unsafeField := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
unsafeField.SetUint(uint64(1))
h.SetSeed(seed)
out := int(h.Sum64())
if out < 0 {
out = -out
}
return out % int(n)
}
tests := []struct {
name string
spread uint8
rf uint16
info balancer.PickInfo
want balancer.PickResult
}{
{
name: "pick one",
spread: 1,
rf: 100,
info: balancer.PickInfo{
Ctx: context.WithValue(context.Background(), CtxKey, []byte("test")),
},
want: balancer.PickResult{
SubConn: &fakeSubConn{id: "1"},
},
},
{
name: "pick another",
spread: 1,
rf: 100,
info: balancer.PickInfo{
Ctx: context.WithValue(context.Background(), CtxKey, []byte("test2")),
},
want: balancer.PickResult{
SubConn: &fakeSubConn{id: "3"},
},
},
{
name: "pick with spread",
spread: 2,
rf: 100,
info: balancer.PickInfo{
Ctx: context.WithValue(context.Background(), CtxKey, []byte("test")),
},
want: balancer.PickResult{
// without spread, this would always be 1.
// it can be 1 or 3 with spread 2, but pinning the seed makes it always 3 in the test
SubConn: &fakeSubConn{id: "3"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &picker{
hashring: hashring.MustNew(xxhash.Sum64, tt.rf),
spread: tt.spread,
}
require.NoError(t, p.hashring.Add(subConnMember{key: "1", SubConn: &fakeSubConn{id: "1"}}))
require.NoError(t, p.hashring.Add(subConnMember{key: "2", SubConn: &fakeSubConn{id: "2"}}))
require.NoError(t, p.hashring.Add(subConnMember{key: "3", SubConn: &fakeSubConn{id: "3"}}))
got, err := p.Pick(tt.info)
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
func TestConsistentHashringBalancerConfigServiceConfigJSON(t *testing.T) {
tests := []struct {
name string
replicationFactor uint16
spread uint8
want string
}{
{
name: "sets rf and spread",
replicationFactor: 300,
spread: 2,
want: `{"loadBalancingConfig":[{"consistent-hashring":{"replicationFactor":300,"spread":2}}]}`,
},
{
name: "sets rf",
replicationFactor: 300,
want: `{"loadBalancingConfig":[{"consistent-hashring":{"replicationFactor":300}}]}`,
},
{
name: "sets spread",
spread: 1,
want: `{"loadBalancingConfig":[{"consistent-hashring":{"spread":1}}]}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &BalancerConfig{
ReplicationFactor: tt.replicationFactor,
Spread: tt.spread,
}
got, err := c.ServiceConfigJSON()
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
func TestConsistentHashringBalancerUpdateClientConnState(t *testing.T) {
type balancerState struct {
ConnectivityState connectivity.State
err error
memberKeys []string
spread uint8
replicationFactor uint16
}
tests := []struct {
name string
s []balancer.ClientConnState
expectedStates []balancerState
expectedConnState connectivity.State
wantErr bool
}{
{
name: "no hashring",
expectedStates: []balancerState{},
expectedConnState: connectivity.TransientFailure,
wantErr: true,
},
{
name: "configures hashring, no addresses",
s: []balancer.ClientConnState{{
ResolverState: resolver.State{},
BalancerConfig: &BalancerConfig{
ReplicationFactor: 100,
Spread: 1,
},
}},
expectedStates: []balancerState{
{
ConnectivityState: connectivity.TransientFailure,
err: errors.Join(nil, fmt.Errorf("produced zero addresses")),
},
},
expectedConnState: connectivity.TransientFailure,
wantErr: true,
},
{
name: "configures hashring, 3 addresses",
s: []balancer.ClientConnState{{
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
{ServerName: "t", Addr: "3"},
},
},
BalancerConfig: &BalancerConfig{
ReplicationFactor: 100,
Spread: 1,
},
}},
expectedStates: []balancerState{
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2", "t3"},
replicationFactor: 100,
spread: 1,
},
},
expectedConnState: connectivity.Idle,
},
{
name: "existing hashring with 3 nodes, 1 removed",
s: []balancer.ClientConnState{{
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
{ServerName: "t", Addr: "3"},
},
},
BalancerConfig: &BalancerConfig{
ReplicationFactor: 100,
Spread: 1,
},
}, {
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
},
},
}},
expectedStates: []balancerState{
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2", "t3"},
replicationFactor: 100,
spread: 1,
},
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2"},
replicationFactor: 100,
spread: 1,
},
},
expectedConnState: connectivity.Idle,
},
{
name: "existing hashring with 3 nodes, 1 added",
s: []balancer.ClientConnState{{
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
{ServerName: "t", Addr: "3"},
},
},
BalancerConfig: &BalancerConfig{
ReplicationFactor: 100,
Spread: 1,
},
}, {
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
{ServerName: "t", Addr: "3"},
{ServerName: "t", Addr: "4"},
},
},
}},
expectedStates: []balancerState{
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2", "t3"},
replicationFactor: 100,
spread: 1,
},
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2", "t3", "t4"},
replicationFactor: 100,
spread: 1,
},
},
expectedConnState: connectivity.Idle,
},
{
name: "existing hashring with 3 nodes, 1 replaced",
s: []balancer.ClientConnState{{
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
{ServerName: "t", Addr: "3"},
},
},
BalancerConfig: &BalancerConfig{
ReplicationFactor: 100,
Spread: 1,
},
}, {
ResolverState: resolver.State{
Addresses: []resolver.Address{
{ServerName: "t", Addr: "1"},
{ServerName: "t", Addr: "2"},
{ServerName: "t", Addr: "4"},
},
},
}},
expectedStates: []balancerState{
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2", "t3"},
replicationFactor: 100,
spread: 1,
},
{
ConnectivityState: connectivity.Connecting,
memberKeys: []string{"t1", "t2", "t4"},
replicationFactor: 100,
spread: 1,
},
},
expectedConnState: connectivity.Idle,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := NewBuilder(xxhash.Sum64)
cc := newFakeClientConn()
bb := b.Build(cc, balancer.BuildOptions{})
cb := bb.(*ringBalancer)
tt := tt
done := make(chan struct{})
go func() {
i := 0
if len(tt.expectedStates) == 0 {
done <- struct{}{}
return
}
for {
s := <-cc.stateCh
expected := tt.expectedStates[i]
require.Equal(t, expected.ConnectivityState, s.ConnectivityState)
if expected.err != nil {
require.Equal(t, base.NewErrPicker(expected.err), s.Picker)
} else {
p := s.Picker.(*picker)
require.Equal(t, expected.spread, p.spread)
require.ElementsMatch(t, expected.memberKeys, keys(p.hashring.Members()))
}
i++
done <- struct{}{}
}
}()
for _, state := range tt.s {
if err := cb.UpdateClientConnState(state); (err != nil) != tt.wantErr {
t.Errorf("UpdateClientConnState() error = %v, wantErr %v", err, tt.wantErr)
}
<-done
}
require.Equal(t, tt.expectedConnState, cb.csEvltr.CurrentState())
})
}
}
type fakeClientConn struct {
balancer.ClientConn
stateCh chan balancer.State
mu sync.Mutex
subConns map[balancer.SubConn]resolver.Address
}
func newFakeClientConn() *fakeClientConn {
return &fakeClientConn{
subConns: make(map[balancer.SubConn]resolver.Address),
stateCh: make(chan balancer.State),
}
}
func (c *fakeClientConn) NewSubConn(addrs []resolver.Address, _ balancer.NewSubConnOptions) (balancer.SubConn, error) {
sc := &fakeSubConn{}
c.mu.Lock()
defer c.mu.Unlock()
c.subConns[sc] = addrs[0]
return sc, nil
}
func (c *fakeClientConn) RemoveSubConn(sc balancer.SubConn) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.subConns, sc)
}
func (c *fakeClientConn) UpdateState(s balancer.State) {
c.stateCh <- s
}