-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
picker_test.go
383 lines (367 loc) · 12.9 KB
/
picker_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
/*
*
* Copyright 2021 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package ringhash
import (
"context"
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/grpclog"
igrpclog "google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/metadata"
)
var testSubConns []*testutils.TestSubConn
func init() {
for i := 0; i < 8; i++ {
testSubConns = append(testSubConns, testutils.NewTestSubConn(fmt.Sprint(i)))
}
}
func newTestRing(cStats []connectivity.State) *ring {
var items []*ringEntry
for i, st := range cStats {
testSC := testSubConns[i]
items = append(items, &ringEntry{
idx: i,
hash: uint64((i + 1) * 10),
sc: &subConn{
addr: testSC.String(),
sc: testSC,
state: st,
},
})
}
return &ring{items: items}
}
func (s) TestXdsPickerPickFirstTwo(t *testing.T) {
tests := []struct {
name string
ring *ring
hash uint64
wantSC balancer.SubConn
wantErr error
wantSCToConnect balancer.SubConn
}{
{
name: "picked is Ready",
ring: newTestRing([]connectivity.State{connectivity.Ready, connectivity.Idle}),
hash: 5,
wantSC: testSubConns[0],
},
{
name: "picked is connecting, queue",
ring: newTestRing([]connectivity.State{connectivity.Connecting, connectivity.Idle}),
hash: 5,
wantErr: balancer.ErrNoSubConnAvailable,
},
{
name: "picked is Idle, connect and queue",
ring: newTestRing([]connectivity.State{connectivity.Idle, connectivity.Idle}),
hash: 5,
wantErr: balancer.ErrNoSubConnAvailable,
wantSCToConnect: testSubConns[0],
},
{
name: "picked is TransientFailure, next is ready, return",
ring: newTestRing([]connectivity.State{connectivity.TransientFailure, connectivity.Ready}),
hash: 5,
wantSC: testSubConns[1],
},
{
name: "picked is TransientFailure, next is connecting, queue",
ring: newTestRing([]connectivity.State{connectivity.TransientFailure, connectivity.Connecting}),
hash: 5,
wantErr: balancer.ErrNoSubConnAvailable,
},
{
name: "picked is TransientFailure, next is Idle, connect and queue",
ring: newTestRing([]connectivity.State{connectivity.TransientFailure, connectivity.Idle}),
hash: 5,
wantErr: balancer.ErrNoSubConnAvailable,
wantSCToConnect: testSubConns[1],
},
{
name: "single channel in TransientFailure",
ring: newTestRing([]connectivity.State{connectivity.TransientFailure}),
hash: 5,
wantErr: errSingleSubConnInTransientFailure,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := newPicker(tt.ring, "", igrpclog.NewPrefixLogger(grpclog.Component("xds"), "rh_test"))
got, err := p.Pick(balancer.PickInfo{
Ctx: SetXDSRequestHash(context.Background(), tt.hash),
})
if err != tt.wantErr {
t.Errorf("Pick() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.SubConn != tt.wantSC {
t.Errorf("Pick() got = %v, want picked SubConn: %v", got, tt.wantSC)
}
if sc := tt.wantSCToConnect; sc != nil {
select {
case <-sc.(*testutils.TestSubConn).ConnectCh:
case <-time.After(defaultTestShortTimeout):
t.Errorf("timeout waiting for Connect() from SubConn %v", sc)
}
}
})
}
}
// TestPickerWithRequestHashKey tests that if an explicit request hash key is
// set, it will be used to pick a SubConn.
func (s) TestPickerWithRequestHashKey(t *testing.T) {
tests := []struct {
name string
values []string
ring *ring
wantSC balancer.SubConn
wantSCToConnect balancer.SubConn
wantErr error
}{
{
name: "hash key is not set, random picked an idle subconn, pick first ready SubConn",
ring: newTestRing([]connectivity.State{connectivity.Idle, connectivity.TransientFailure, connectivity.Connecting, connectivity.Ready, connectivity.Ready}),
wantSC: testSubConns[3],
wantSCToConnect: testSubConns[0],
},
{
name: "hash key is not set, random picks an idle subconn, no subconn ready",
ring: newTestRing([]connectivity.State{connectivity.Idle, connectivity.TransientFailure, connectivity.Connecting, connectivity.Shutdown}),
wantErr: balancer.ErrNoSubConnAvailable,
wantSCToConnect: testSubConns[0],
},
{
name: "hash key is not set, random picks a subconn in transient failure, pick first subchannel ready",
ring: newTestRing([]connectivity.State{connectivity.TransientFailure, connectivity.TransientFailure, connectivity.Connecting, connectivity.Ready, connectivity.Ready}),
wantSC: testSubConns[3],
},
{
name: "hash key is not set, random picked a subconn in transient failure, no subchannel ready",
ring: newTestRing([]connectivity.State{connectivity.TransientFailure, connectivity.TransientFailure, connectivity.Connecting}),
wantErr: errNoSubConnReady,
},
{
name: "hash key is set, return ready subconn",
values: []string{"test-value"}, // this hashes to the end of the test ring => ring entry 0 expected.
ring: newTestRing([]connectivity.State{connectivity.Ready, connectivity.TransientFailure, connectivity.Connecting, connectivity.Ready}),
wantSC: testSubConns[0],
},
{
name: "hash key is set, connect and queue the pick",
values: []string{"test-value"}, // this hashes to the end of the test ring => ring entry 0 expected.
ring: newTestRing([]connectivity.State{connectivity.Idle, connectivity.TransientFailure, connectivity.Connecting, connectivity.Ready}),
wantErr: balancer.ErrNoSubConnAvailable,
wantSCToConnect: testSubConns[0],
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
requestHashKey := "test-key"
ring := tt.ring
p := newPicker(ring, requestHashKey, igrpclog.NewPrefixLogger(grpclog.Component("xds"), "rh_test"))
p.randuint64 = func() uint64 {
// always return the first entry on the ring.
return 5
}
md := metadata.New(nil)
md.Set("test-key", tt.values...)
got, err := p.Pick(balancer.PickInfo{
Ctx: metadata.NewOutgoingContext(context.Background(), md),
})
if err != tt.wantErr {
t.Errorf("Pick() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.SubConn != tt.wantSC {
t.Errorf("Pick() got = %v, want picked SubConn: %v", got, tt.wantSC)
}
if sc := tt.wantSCToConnect; sc != nil {
select {
case <-sc.(*testutils.TestSubConn).ConnectCh:
case <-time.After(defaultTestShortTimeout):
t.Errorf("timeout waiting for Connect() from SubConn %v", sc)
}
}
})
}
}
// TestPickerPickTriggerTFConnect covers that if the picked SubConn is
// TransientFailures, all SubConns until a non-TransientFailure are queued for
// Connect().
func (s) TestPickerPickTriggerTFConnect(t *testing.T) {
ring := newTestRing([]connectivity.State{
connectivity.TransientFailure, connectivity.TransientFailure, connectivity.TransientFailure, connectivity.TransientFailure,
connectivity.Idle, connectivity.TransientFailure, connectivity.TransientFailure, connectivity.TransientFailure,
})
p := newPicker(ring, "", igrpclog.NewPrefixLogger(grpclog.Component("xds"), "rh_test"))
_, err := p.Pick(balancer.PickInfo{Ctx: SetXDSRequestHash(context.Background(), 5)})
if err == nil {
t.Fatalf("Pick() error = %v, want non-nil", err)
}
// The first 4 SubConns, all in TransientFailure, should be queued to
// connect.
for i := 0; i < 4; i++ {
it := ring.items[i]
if !it.sc.connectQueued {
t.Errorf("the %d-th SubConn is not queued for connect", i)
}
}
// The other SubConns, after the first Idle, should not be queued to
// connect.
for i := 5; i < len(ring.items); i++ {
it := ring.items[i]
if it.sc.connectQueued {
t.Errorf("the %d-th SubConn is unexpected queued for connect", i)
}
}
}
// TestPickerPickTriggerTFReturnReady covers that if the picked SubConn is
// TransientFailure, SubConn 2 and 3 are TransientFailure, 4 is Ready. SubConn 2
// and 3 will Connect(), and 4 will be returned.
func (s) TestPickerPickTriggerTFReturnReady(t *testing.T) {
ring := newTestRing([]connectivity.State{
connectivity.TransientFailure, connectivity.TransientFailure, connectivity.TransientFailure, connectivity.Ready,
})
p := newPicker(ring, "", igrpclog.NewPrefixLogger(grpclog.Component("xds"), "rh_test"))
pr, err := p.Pick(balancer.PickInfo{Ctx: SetXDSRequestHash(context.Background(), 5)})
if err != nil {
t.Fatalf("Pick() error = %v, want nil", err)
}
if wantSC := testSubConns[3]; pr.SubConn != wantSC {
t.Fatalf("Pick() = %v, want %v", pr.SubConn, wantSC)
}
// The first 3 SubConns, all in TransientFailure, should be queued to
// connect.
for i := 0; i < 3; i++ {
it := ring.items[i]
if !it.sc.connectQueued {
t.Errorf("the %d-th SubConn is not queued for connect", i)
}
}
}
// TestPickerPickTriggerTFWithIdle covers that if the picked SubConn is
// TransientFailure, SubConn 2 is TransientFailure, 3 is Idle (init Idle). Pick
// will be queue, SubConn 3 will Connect(), SubConn 4 and 5 (in TransientFailre)
// will not queue a Connect.
func (s) TestPickerPickTriggerTFWithIdle(t *testing.T) {
ring := newTestRing([]connectivity.State{
connectivity.TransientFailure, connectivity.TransientFailure, connectivity.Idle, connectivity.TransientFailure, connectivity.TransientFailure,
})
p := newPicker(ring, "", igrpclog.NewPrefixLogger(grpclog.Component("xds"), "rh_test"))
_, err := p.Pick(balancer.PickInfo{Ctx: SetXDSRequestHash(context.Background(), 5)})
if err == balancer.ErrNoSubConnAvailable {
t.Fatalf("Pick() error = %v, want %v", err, balancer.ErrNoSubConnAvailable)
}
// The first 2 SubConns, all in TransientFailure, should be queued to
// connect.
for i := 0; i < 2; i++ {
it := ring.items[i]
if !it.sc.connectQueued {
t.Errorf("the %d-th SubConn is not queued for connect", i)
}
}
// SubConn 3 was in Idle, so should Connect()
select {
case <-testSubConns[2].ConnectCh:
case <-time.After(defaultTestShortTimeout):
t.Errorf("timeout waiting for Connect() from SubConn %v", testSubConns[2])
}
// The other SubConns, after the first Idle, should not be queued to
// connect.
for i := 3; i < len(ring.items); i++ {
it := ring.items[i]
if it.sc.connectQueued {
t.Errorf("the %d-th SubConn is unexpected queued for connect", i)
}
}
}
func (s) TestNextSkippingDuplicatesNoDup(t *testing.T) {
testRing := newTestRing([]connectivity.State{connectivity.Idle, connectivity.Idle})
tests := []struct {
name string
ring *ring
cur *ringEntry
want *ringEntry
}{
{
name: "no dup",
ring: testRing,
cur: testRing.items[0],
want: testRing.items[1],
},
{
name: "only one entry",
ring: &ring{items: []*ringEntry{testRing.items[0]}},
cur: testRing.items[0],
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := nextSkippingDuplicates(tt.ring, tt.cur); !cmp.Equal(got, tt.want, cmpOpts) {
t.Errorf("nextSkippingDuplicates() = %v, want %v", got, tt.want)
}
})
}
}
// addDups adds duplicates of items[0] to the ring.
func addDups(r *ring, count int) *ring {
var (
items []*ringEntry
idx int
)
for i, it := range r.items {
itt := *it
itt.idx = idx
items = append(items, &itt)
idx++
if i == 0 {
// Add duplicate of items[0] to the ring
for j := 0; j < count; j++ {
itt2 := *it
itt2.idx = idx
items = append(items, &itt2)
idx++
}
}
}
return &ring{items: items}
}
func (s) TestNextSkippingDuplicatesMoreDup(t *testing.T) {
testRing := newTestRing([]connectivity.State{connectivity.Idle, connectivity.Idle})
// Make a new ring with duplicate SubConns.
dupTestRing := addDups(testRing, 3)
if got := nextSkippingDuplicates(dupTestRing, dupTestRing.items[0]); !cmp.Equal(got, dupTestRing.items[len(dupTestRing.items)-1], cmpOpts) {
t.Errorf("nextSkippingDuplicates() = %v, want %v", got, dupTestRing.items[len(dupTestRing.items)-1])
}
}
func (s) TestNextSkippingDuplicatesOnlyDup(t *testing.T) {
testRing := newTestRing([]connectivity.State{connectivity.Idle})
// Make a new ring with only duplicate SubConns.
dupTestRing := addDups(testRing, 3)
// This ring only has duplicates of items[0], should return nil.
if got := nextSkippingDuplicates(dupTestRing, dupTestRing.items[0]); got != nil {
t.Errorf("nextSkippingDuplicates() = %v, want nil", got)
}
}