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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
/*
*
* 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 rls
import (
"context"
"fmt"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/stubserver"
rlstest "google.golang.org/grpc/internal/testutils/rls"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
rlspb "google.golang.org/grpc/internal/proto/grpc_lookup_v1"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
testpb "google.golang.org/grpc/interop/grpc_testing"
)
// Test verifies the scenario where there is no matching entry in the data cache
// and no pending request either, and the ensuing RLS request is throttled.
func (s) TestPick_DataCacheMiss_NoPendingEntry_ThrottledWithDefaultTarget(t *testing.T) {
// Start an RLS server and set the throttler to always throttle requests.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
overrideAdaptiveThrottler(t, alwaysThrottlingThrottler())
// Build RLS service config with a default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
defBackendCh, defBackendAddress := startBackend(t)
rlsConfig.RouteLookupConfig.DefaultTarget = defBackendAddress
// Register a manual resolver and push the RLS service config through it.
r := startManualResolverWithConfig(t, rlsConfig)
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the default target.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, defBackendCh)
// Make sure no RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, false)
}
// Test verifies the scenario where there is no matching entry in the data cache
// and no pending request either, and the ensuing RLS request is throttled.
// There is no default target configured in the service config, so the RPC is
// expected to fail with an RLS throttled error.
func (s) TestPick_DataCacheMiss_NoPendingEntry_ThrottledWithoutDefaultTarget(t *testing.T) {
// Start an RLS server and set the throttler to always throttle requests.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
overrideAdaptiveThrottler(t, alwaysThrottlingThrottler())
// Build an RLS config without a default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
// Register a manual resolver and push the RLS service config through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and expect it to fail with RLS throttled error.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndVerifyError(ctx, t, cc, codes.Unavailable, errRLSThrottled)
// Make sure no RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, false)
}
// Test verifies the scenario where there is no matching entry in the data cache
// and no pending request either, and the ensuing RLS request is not throttled.
// The RLS response does not contain any backends, so the RPC fails with a
// deadline exceeded error.
func (s) TestPick_DataCacheMiss_NoPendingEntry_NotThrottled(t *testing.T) {
// Start an RLS server and set the throttler to never throttle requests.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Build an RLS config without a default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
// Register a manual resolver and push the RLS service config through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and expect it to fail with deadline exceeded error. We use a
// smaller timeout to ensure that the test doesn't run very long.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer cancel()
makeTestRPCAndVerifyError(ctx, t, cc, codes.DeadlineExceeded, context.DeadlineExceeded)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
}
// Test verifies the scenario where there is no matching entry in the data
// cache, but there is a pending request. So, we expect no RLS request to be
// sent out. The pick should be queued and not delegated to the default target.
func (s) TestPick_DataCacheMiss_PendingEntryExists(t *testing.T) {
tests := []struct {
name string
withDefaultTarget bool
}{
{
name: "withDefaultTarget",
withDefaultTarget: true,
},
{
name: "withoutDefaultTarget",
withDefaultTarget: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// A unary interceptor which blocks the RouteLookup RPC on the fake
// RLS server until the test is done. The first RPC by the client
// will cause the LB policy to send out an RLS request. This will
// also lead to creation of a pending entry, and further RPCs by the
// client should not result in RLS requests being sent out.
rlsReqCh := make(chan struct{}, 1)
interceptor := func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
rlsReqCh <- struct{}{}
<-ctx.Done()
return nil, ctx.Err()
}
// Start an RLS server and set the throttler to never throttle.
rlsServer, _ := rlstest.SetupFakeRLSServer(t, nil, grpc.UnaryInterceptor(interceptor))
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Build RLS service config with an optional default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
if test.withDefaultTarget {
_, defBackendAddress := startBackend(t)
rlsConfig.RouteLookupConfig.DefaultTarget = defBackendAddress
}
// Register a manual resolver and push the RLS service config
// through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC that results in the RLS request being sent out. And
// since the RLS server is configured to block on the first request,
// this RPC will block until its context expires. This ensures that
// we have a pending cache entry for the duration of the test.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
go func() {
client := testgrpc.NewTestServiceClient(cc)
client.EmptyCall(ctx, &testpb.Empty{})
}()
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
// Make another RPC and expect it to fail the same way.
ctx, cancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer cancel()
makeTestRPCAndVerifyError(ctx, t, cc, codes.DeadlineExceeded, context.DeadlineExceeded)
// Make sure no RLS request is sent out this time around.
verifyRLSRequest(t, rlsReqCh, false)
})
}
}
// Test verifies the scenario where there is a matching entry in the data cache
// which is valid and there is no pending request. The pick is expected to be
// delegated to the child policy.
func (s) TestPick_DataCacheHit_NoPendingEntry_ValidEntry(t *testing.T) {
// Start an RLS server and set the throttler to never throttle requests.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Build the RLS config without a default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
// Start a test backend, and setup the fake RLS server to return this as a
// target in the RLS response.
testBackendCh, testBackendAddress := startBackend(t)
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{testBackendAddress}}}
})
// Register a manual resolver and push the RLS service config through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
// Make another RPC and expect it to find the target in the data cache.
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure no RLS request is sent out this time around.
verifyRLSRequest(t, rlsReqCh, false)
}
// Test verifies the scenario where there is a matching entry in the data cache
// which is valid and there is no pending request. The pick is expected to be
// delegated to the child policy.
func (s) TestPick_DataCacheHit_NoPendingEntry_ValidEntry_WithHeaderData(t *testing.T) {
// Start an RLS server and set the throttler to never throttle requests.
rlsServer, _ := rlstest.SetupFakeRLSServer(t, nil)
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Build the RLS config without a default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
// Start a test backend which expects the header data contents sent from the
// RLS server to be part of RPC metadata as X-Google-RLS-Data header.
const headerDataContents = "foo,bar,baz"
backend := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
gotHeaderData := metadata.ValueFromIncomingContext(ctx, "x-google-rls-data")
if len(gotHeaderData) != 1 || gotHeaderData[0] != headerDataContents {
return nil, fmt.Errorf("got metadata in `X-Google-RLS-Data` is %v, want %s", gotHeaderData, headerDataContents)
}
return &testpb.Empty{}, nil
},
}
if err := backend.StartServer(); err != nil {
t.Fatalf("Failed to start backend: %v", err)
}
t.Logf("Started TestService backend at: %q", backend.Address)
defer backend.Stop()
// Setup the fake RLS server to return the above backend as a target in the
// RLS response. Also, populate the header data field in the response.
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{
Targets: []string{backend.Address},
HeaderData: headerDataContents,
}}
})
// Register a manual resolver and push the RLS service config through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend with the header
// data sent by the RLS server.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("EmptyCall() RPC: %v", err)
}
}
// Test verifies the scenario where there is a matching entry in the data cache
// which is stale and there is no pending request. The pick is expected to be
// delegated to the child policy with a proactive cache refresh.
func (s) TestPick_DataCacheHit_NoPendingEntry_StaleEntry(t *testing.T) {
// We expect the same pick behavior (i.e delegated to the child policy) for
// a proactive refresh whether the control channel is throttled or not.
tests := []struct {
name string
throttled bool
}{
{
name: "throttled",
throttled: true,
},
{
name: "notThrottled",
throttled: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Start an RLS server and setup the throttler appropriately.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
var throttler *fakeThrottler
firstRPCDone := grpcsync.NewEvent()
if test.throttled {
throttler = oneTimeAllowingThrottler(firstRPCDone)
overrideAdaptiveThrottler(t, throttler)
} else {
throttler = neverThrottlingThrottler()
overrideAdaptiveThrottler(t, throttler)
}
// Build the RLS config without a default target. Set the stale age
// to a very low value to force entries to become stale quickly.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
rlsConfig.RouteLookupConfig.MaxAge = durationpb.New(time.Minute)
rlsConfig.RouteLookupConfig.StaleAge = durationpb.New(defaultTestShortTimeout)
// Start a test backend, and setup the fake RLS server to return
// this as a target in the RLS response.
testBackendCh, testBackendAddress := startBackend(t)
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{testBackendAddress}}}
})
// Register a manual resolver and push the RLS service config
// through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
firstRPCDone.Fire()
// The cache entry has a large maxAge, but a small stateAge. We keep
// retrying until the cache entry becomes stale, in which case we expect a
// proactive cache refresh.
//
// If the control channel is not throttled, then we expect an RLS request
// to be sent out. If the control channel is throttled, we expect the fake
// throttler's channel to be signalled.
for {
// Make another RPC and expect it to find the target in the data cache.
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
if !test.throttled {
select {
case <-time.After(defaultTestShortTimeout):
// Go back and retry the RPC.
case <-rlsReqCh:
return
}
} else {
select {
case <-time.After(defaultTestShortTimeout):
// Go back and retry the RPC.
case <-throttler.throttleCh:
return
}
}
}
})
}
}
// Test verifies scenarios where there is a matching entry in the data cache
// which has expired and there is no pending request.
func (s) TestPick_DataCacheHit_NoPendingEntry_ExpiredEntry(t *testing.T) {
tests := []struct {
name string
throttled bool
withDefaultTarget bool
}{
{
name: "throttledWithDefaultTarget",
throttled: true,
withDefaultTarget: true,
},
{
name: "throttledWithoutDefaultTarget",
throttled: true,
withDefaultTarget: false,
},
{
name: "notThrottled",
throttled: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Start an RLS server and setup the throttler appropriately.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
var throttler *fakeThrottler
firstRPCDone := grpcsync.NewEvent()
if test.throttled {
throttler = oneTimeAllowingThrottler(firstRPCDone)
overrideAdaptiveThrottler(t, throttler)
} else {
throttler = neverThrottlingThrottler()
overrideAdaptiveThrottler(t, throttler)
}
// Build the RLS config with a very low value for maxAge. This will
// ensure that cache entries become invalid very soon.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
rlsConfig.RouteLookupConfig.MaxAge = durationpb.New(defaultTestShortTimeout)
// Start a default backend if needed.
var defBackendCh chan struct{}
if test.withDefaultTarget {
var defBackendAddress string
defBackendCh, defBackendAddress = startBackend(t)
rlsConfig.RouteLookupConfig.DefaultTarget = defBackendAddress
}
// Start a test backend, and setup the fake RLS server to return
// this as a target in the RLS response.
testBackendCh, testBackendAddress := startBackend(t)
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{testBackendAddress}}}
})
// Register a manual resolver and push the RLS service config
// through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
firstRPCDone.Fire()
// Keep retrying the RPC until the cache entry expires. Expected behavior
// is dependent on the scenario being tested.
switch {
case test.throttled && test.withDefaultTarget:
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, defBackendCh)
<-throttler.throttleCh
case test.throttled && !test.withDefaultTarget:
makeTestRPCAndVerifyError(ctx, t, cc, codes.Unavailable, errRLSThrottled)
<-throttler.throttleCh
case !test.throttled:
for {
// The backend to which the RPC is routed does not change after the
// cache entry expires because the control channel is not throttled.
// So, we need to keep retrying until the cache entry expires, at
// which point we expect an RLS request to be sent out and the RPC to
// get routed to the same testBackend.
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
select {
case <-time.After(defaultTestShortTimeout):
// Go back and retry the RPC.
case <-rlsReqCh:
return
}
}
}
})
}
}
// Test verifies scenarios where there is a matching entry in the data cache
// which has expired and is in backoff and there is no pending request.
func (s) TestPick_DataCacheHit_NoPendingEntry_ExpiredEntryInBackoff(t *testing.T) {
tests := []struct {
name string
withDefaultTarget bool
}{
{
name: "withDefaultTarget",
withDefaultTarget: true,
},
{
name: "withoutDefaultTarget",
withDefaultTarget: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Start an RLS server and set the throttler to never throttle requests.
rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil)
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Override the backoff strategy to return a large backoff which
// will make sure the date cache entry remains in backoff for the
// duration of the test.
origBackoffStrategy := defaultBackoffStrategy
defaultBackoffStrategy = &fakeBackoffStrategy{backoff: defaultTestTimeout}
defer func() { defaultBackoffStrategy = origBackoffStrategy }()
// Build the RLS config with a very low value for maxAge. This will
// ensure that cache entries become invalid very soon.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
rlsConfig.RouteLookupConfig.MaxAge = durationpb.New(defaultTestShortTimeout)
// Start a default backend if needed.
var defBackendCh chan struct{}
if test.withDefaultTarget {
var defBackendAddress string
defBackendCh, defBackendAddress = startBackend(t)
rlsConfig.RouteLookupConfig.DefaultTarget = defBackendAddress
}
// Start a test backend, and set up the fake RLS server to return this as
// a target in the RLS response.
testBackendCh, testBackendAddress := startBackend(t)
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{testBackendAddress}}}
})
// Register a manual resolver and push the RLS service config through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
// Set up the fake RLS server to return errors. This will push the cache
// entry into backoff.
var rlsLastErr = status.Error(codes.DeadlineExceeded, "last RLS request failed")
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Err: rlsLastErr}
})
// Since the RLS server is now configured to return errors, this will push
// the cache entry into backoff. The pick will be delegated to the default
// backend if one exits, and will fail with the error returned by the RLS
// server otherwise.
if test.withDefaultTarget {
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, defBackendCh)
} else {
makeTestRPCAndVerifyError(ctx, t, cc, codes.Unavailable, rlsLastErr)
}
})
}
}
// Test verifies scenarios where there is a matching entry in the data cache
// which is stale and there is a pending request.
func (s) TestPick_DataCacheHit_PendingEntryExists_StaleEntry(t *testing.T) {
tests := []struct {
name string
withDefaultTarget bool
}{
{
name: "withDefaultTarget",
withDefaultTarget: true,
},
{
name: "withoutDefaultTarget",
withDefaultTarget: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// A unary interceptor which simply calls the underlying handler
// until the first client RPC is done. We want one client RPC to
// succeed to ensure that a data cache entry is created. For
// subsequent client RPCs which result in RLS requests, this
// interceptor blocks until the test's context expires. And since we
// configure the RLS LB policy with a really low value for max age,
// this allows us to simulate the condition where the it has an
// expired entry and a pending entry in the cache.
rlsReqCh := make(chan struct{}, 1)
firstRPCDone := grpcsync.NewEvent()
interceptor := func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
select {
case rlsReqCh <- struct{}{}:
default:
}
if firstRPCDone.HasFired() {
<-ctx.Done()
return nil, ctx.Err()
}
return handler(ctx, req)
}
// Start an RLS server and set the throttler to never throttle.
rlsServer, _ := rlstest.SetupFakeRLSServer(t, nil, grpc.UnaryInterceptor(interceptor))
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Build RLS service config with an optional default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
if test.withDefaultTarget {
_, defBackendAddress := startBackend(t)
rlsConfig.RouteLookupConfig.DefaultTarget = defBackendAddress
}
// Low value for stale age to force entries to become stale quickly.
rlsConfig.RouteLookupConfig.MaxAge = durationpb.New(time.Minute)
rlsConfig.RouteLookupConfig.StaleAge = durationpb.New(defaultTestShortTimeout)
// Start a test backend, and setup the fake RLS server to return
// this as a target in the RLS response.
testBackendCh, testBackendAddress := startBackend(t)
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{testBackendAddress}}}
})
// Register a manual resolver and push the RLS service config
// through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
firstRPCDone.Fire()
// The cache entry has a large maxAge, but a small stateAge. We keep
// retrying until the cache entry becomes stale, in which case we expect a
// proactive cache refresh.
for {
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
select {
case <-time.After(defaultTestShortTimeout):
// Go back and retry the RPC.
case <-rlsReqCh:
return
}
}
})
}
}
// Test verifies scenarios where there is a matching entry in the data cache
// which is expired and there is a pending request.
func (s) TestPick_DataCacheHit_PendingEntryExists_ExpiredEntry(t *testing.T) {
tests := []struct {
name string
withDefaultTarget bool
}{
{
name: "withDefaultTarget",
withDefaultTarget: true,
},
{
name: "withoutDefaultTarget",
withDefaultTarget: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// A unary interceptor which simply calls the underlying handler
// until the first client RPC is done. We want one client RPC to
// succeed to ensure that a data cache entry is created. For
// subsequent client RPCs which result in RLS requests, this
// interceptor blocks until the test's context expires. And since we
// configure the RLS LB policy with a really low value for max age,
// this allows us to simulate the condition where the it has an
// expired entry and a pending entry in the cache.
rlsReqCh := make(chan struct{}, 1)
firstRPCDone := grpcsync.NewEvent()
interceptor := func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
select {
case rlsReqCh <- struct{}{}:
default:
}
if firstRPCDone.HasFired() {
<-ctx.Done()
return nil, ctx.Err()
}
return handler(ctx, req)
}
// Start an RLS server and set the throttler to never throttle.
rlsServer, _ := rlstest.SetupFakeRLSServer(t, nil, grpc.UnaryInterceptor(interceptor))
overrideAdaptiveThrottler(t, neverThrottlingThrottler())
// Build RLS service config with an optional default target.
rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
if test.withDefaultTarget {
_, defBackendAddress := startBackend(t)
rlsConfig.RouteLookupConfig.DefaultTarget = defBackendAddress
}
// Set a low value for maxAge to ensure cache entries expire soon.
rlsConfig.RouteLookupConfig.MaxAge = durationpb.New(defaultTestShortTimeout)
// Start a test backend, and setup the fake RLS server to return
// this as a target in the RLS response.
testBackendCh, testBackendAddress := startBackend(t)
rlsServer.SetResponseCallback(func(_ context.Context, req *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{testBackendAddress}}}
})
// Register a manual resolver and push the RLS service config
// through it.
r := startManualResolverWithConfig(t, rlsConfig)
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
}
defer cc.Close()
// Make an RPC and ensure it gets routed to the test backend.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
makeTestRPCAndExpectItToReachBackend(ctx, t, cc, testBackendCh)
// Make sure an RLS request is sent out.
verifyRLSRequest(t, rlsReqCh, true)
firstRPCDone.Fire()
// At this point, we have a cache entry with a small maxAge, and the
// RLS server is configured to block on further RLS requests. As we
// retry the RPC, at some point the cache entry would expire and
// force us to send an RLS request which would block on the server,
// giving us a pending cache entry for the duration of the test.
go func() {
for client := testgrpc.NewTestServiceClient(cc); ctx.Err() == nil; <-time.After(defaultTestShortTimeout) {
client.EmptyCall(ctx, &testpb.Empty{})
}
}()
verifyRLSRequest(t, rlsReqCh, true)
// Another RPC at this point should find the pending entry and be queued.
// But since we pass a small deadline, this RPC should fail with a
// deadline exceeded error since the pending request does not return until
// the test is done. And since we have a pending entry, we expect no RLS
// request to be sent out.
sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
defer sCancel()
makeTestRPCAndVerifyError(sCtx, t, cc, codes.DeadlineExceeded, context.DeadlineExceeded)
verifyRLSRequest(t, rlsReqCh, false)
})
}
}
func TestIsFullMethodNameValid(t *testing.T) {
tests := []struct {
desc string
methodName string
want bool
}{
{
desc: "does not start with a slash",
methodName: "service/method",
want: false,
},
{
desc: "does not contain a method",
methodName: "/service",
want: false,
},
{
desc: "path has more elements",
methodName: "/service/path/to/method",
want: false,
},
{
desc: "valid",
methodName: "/service/method",
want: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
if got := isFullMethodNameValid(test.methodName); got != test.want {
t.Fatalf("isFullMethodNameValid(%q) = %v, want %v", test.methodName, got, test.want)
}
})
}
}
|