@@ -22,9 +22,7 @@ import (
2222 "context"
2323 "encoding/json"
2424 "errors"
25- "fmt"
2625 "strings"
27- "sync"
2826 "testing"
2927 "time"
3028
@@ -37,15 +35,10 @@ import (
3735 internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
3836 "google.golang.org/grpc/internal/testutils"
3937 xdsinternal "google.golang.org/grpc/internal/xds"
40- "google.golang.org/grpc/internal/xds/bootstrap"
41- "google.golang.org/grpc/internal/xds/clients"
4238 "google.golang.org/grpc/internal/xds/testutils/fakeclient"
4339 "google.golang.org/grpc/internal/xds/xdsclient"
4440 "google.golang.org/grpc/resolver"
4541 "google.golang.org/grpc/serviceconfig"
46-
47- "github.com/google/go-cmp/cmp"
48- "github.com/google/go-cmp/cmp/cmpopts"
4942)
5043
5144const (
@@ -58,7 +51,6 @@ const (
5851
5952var (
6053 testBackendEndpoints = []resolver.Endpoint {{Addresses : []resolver.Address {{Addr : "1.1.1.1:1" }}}}
61- toleranceCmpOpt = cmp.Options {cmpopts .EquateApprox (0 , 1e-5 ), cmp .AllowUnexported (loadData {}, localityData {}, requestData {}, serverLoadData {})}
6254)
6355
6456type s struct {
@@ -69,205 +61,6 @@ func Test(t *testing.T) {
6961 grpctest .RunSubTests (t , s {})
7062}
7163
72- // testLoadReporter records load data pertaining to a single cluster.
73- //
74- // It implements loadReporter interface for the picker. Tests can use it to
75- // override the loadStore in the picker to verify load reporting.
76- type testLoadReporter struct {
77- cluster , service string
78-
79- mu sync.Mutex
80- drops map [string ]uint64
81- localityRPCCount map [clients.Locality ]* rpcCountData
82- }
83-
84- // CallStarted records a call started for the clients.Locality.
85- func (lr * testLoadReporter ) CallStarted (locality clients.Locality ) {
86- lr .mu .Lock ()
87- defer lr .mu .Unlock ()
88- if _ , ok := lr .localityRPCCount [locality ]; ! ok {
89- lr .localityRPCCount [locality ] = & rpcCountData {}
90- }
91- lr .localityRPCCount [locality ].inProgress ++
92- lr .localityRPCCount [locality ].issued ++
93- }
94-
95- // CallFinished records a call finished for the clients.Locality.
96- func (lr * testLoadReporter ) CallFinished (locality clients.Locality , err error ) {
97- lr .mu .Lock ()
98- defer lr .mu .Unlock ()
99- if lr .localityRPCCount == nil {
100- return
101- }
102- lrc := lr .localityRPCCount [locality ]
103- lrc .inProgress --
104- if err == nil {
105- lrc .succeeded ++
106- } else {
107- lrc .errored ++
108- }
109- }
110-
111- // CallServerLoad records a server load for the clients.Locality.
112- func (lr * testLoadReporter ) CallServerLoad (locality clients.Locality , name string , val float64 ) {
113- lr .mu .Lock ()
114- defer lr .mu .Unlock ()
115- if lr .localityRPCCount == nil {
116- return
117- }
118- lrc , ok := lr .localityRPCCount [locality ]
119- if ! ok {
120- return
121- }
122- if lrc .serverLoads == nil {
123- lrc .serverLoads = make (map [string ]* rpcLoadData )
124- }
125- if _ , ok := lrc .serverLoads [name ]; ! ok {
126- lrc .serverLoads [name ] = & rpcLoadData {}
127- }
128- rld := lrc .serverLoads [name ]
129- rld .add (val )
130- }
131-
132- // CallDropped records a call dropped for the category.
133- func (lr * testLoadReporter ) CallDropped (category string ) {
134- lr .mu .Lock ()
135- defer lr .mu .Unlock ()
136- lr .drops [category ]++
137- }
138-
139- // stats returns and resets all loads reported for a cluster and service,
140- // except inProgress rpc counts.
141- //
142- // It returns nil if the store doesn't contain any (new) data.
143- func (lr * testLoadReporter ) stats () * loadData {
144- lr .mu .Lock ()
145- defer lr .mu .Unlock ()
146-
147- sd := newLoadData (lr .cluster , lr .service )
148- for category , val := range lr .drops {
149- if val == 0 {
150- continue
151- }
152- if category != "" {
153- // Skip drops without category. They are counted in total_drops, but
154- // not in per category. One example is drops by circuit breaking.
155- sd .drops [category ] = val
156- }
157- sd .totalDrops += val
158- lr .drops [category ] = 0 // clear drops for next report
159- }
160- for locality , countData := range lr .localityRPCCount {
161- if countData .succeeded == 0 && countData .errored == 0 && countData .inProgress == 0 && countData .issued == 0 {
162- continue
163- }
164-
165- ld := localityData {
166- requestStats : requestData {
167- succeeded : countData .succeeded ,
168- errored : countData .errored ,
169- inProgress : countData .inProgress ,
170- issued : countData .issued ,
171- },
172- loadStats : make (map [string ]serverLoadData ),
173- }
174- // clear localityRPCCount for next report
175- countData .succeeded = 0
176- countData .errored = 0
177- countData .inProgress = 0
178- countData .issued = 0
179- for key , rld := range countData .serverLoads {
180- s , c := rld .loadAndClear () // get and clear serverLoads for next report
181- if c == 0 {
182- continue
183- }
184- ld .loadStats [key ] = serverLoadData {sum : s , count : c }
185- }
186- sd .localityStats [locality ] = ld
187- }
188- if sd .totalDrops == 0 && len (sd .drops ) == 0 && len (sd .localityStats ) == 0 {
189- return nil
190- }
191- return sd
192- }
193-
194- // loadData contains all load data reported to the LoadStore since the most recent
195- // call to stats().
196- type loadData struct {
197- // cluster is the name of the cluster this data is for.
198- cluster string
199- // service is the name of the EDS service this data is for.
200- service string
201- // totalDrops is the total number of dropped requests.
202- totalDrops uint64
203- // drops is the number of dropped requests per category.
204- drops map [string ]uint64
205- // localityStats contains load reports per locality.
206- localityStats map [clients.Locality ]localityData
207- }
208-
209- // localityData contains load data for a single locality.
210- type localityData struct {
211- // requestStats contains counts of requests made to the locality.
212- requestStats requestData
213- // loadStats contains server load data for requests made to the locality,
214- // indexed by the load type.
215- loadStats map [string ]serverLoadData
216- }
217-
218- // requestData contains request counts.
219- type requestData struct {
220- // succeeded is the number of succeeded requests.
221- succeeded uint64
222- // errored is the number of requests which ran into errors.
223- errored uint64
224- // inProgress is the number of requests in flight.
225- inProgress uint64
226- // issued is the total number requests that were sent.
227- issued uint64
228- }
229-
230- // serverLoadData contains server load data.
231- type serverLoadData struct {
232- // count is the number of load reports.
233- count uint64
234- // sum is the total value of all load reports.
235- sum float64
236- }
237-
238- func newLoadData (cluster , service string ) * loadData {
239- return & loadData {
240- cluster : cluster ,
241- service : service ,
242- drops : make (map [string ]uint64 ),
243- localityStats : make (map [clients.Locality ]localityData ),
244- }
245- }
246-
247- type rpcCountData struct {
248- succeeded uint64
249- errored uint64
250- inProgress uint64
251- issued uint64
252- serverLoads map [string ]* rpcLoadData
253- }
254-
255- type rpcLoadData struct {
256- sum float64
257- count uint64
258- }
259-
260- func (rld * rpcLoadData ) add (v float64 ) {
261- rld .sum += v
262- rld .count ++
263- }
264-
265- func (rld * rpcLoadData ) loadAndClear () (s float64 , c uint64 ) {
266- s , rld .sum = rld .sum , 0
267- c , rld .count = rld .count , 0
268- return s , c
269- }
270-
27164func init () {
27265 NewRandomWRR = testutils .NewTestWRR
27366}
@@ -419,117 +212,6 @@ func (s) TestClusterNameInAddressAttributes(t *testing.T) {
419212 }
420213}
421214
422- // TestUpdateLRSServer covers the cases
423- // - the init config specifies "" as the LRS server
424- // - config modifies LRS server to a different string
425- // - config sets LRS server to nil to stop load reporting
426- func (s ) TestUpdateLRSServer (t * testing.T ) {
427- var testLocality = clients.Locality {
428- Region : "test-region" ,
429- Zone : "test-zone" ,
430- SubZone : "test-sub-zone" ,
431- }
432-
433- xdsC := fakeclient .NewClient ()
434-
435- builder := balancer .Get (Name )
436- cc := testutils .NewBalancerClientConn (t )
437- b := builder .Build (cc , balancer.BuildOptions {})
438- defer b .Close ()
439-
440- endpoints := make ([]resolver.Endpoint , len (testBackendEndpoints ))
441- for i , e := range testBackendEndpoints {
442- endpoints [i ] = xdsinternal .SetLocalityIDInEndpoint (e , testLocality )
443- }
444- testLRSServerConfig , err := bootstrap .ServerConfigForTesting (bootstrap.ServerConfigTestingOptions {
445- URI : "trafficdirector.googleapis.com:443" ,
446- ChannelCreds : []bootstrap.ChannelCreds {{Type : "google_default" }},
447- })
448- if err != nil {
449- t .Fatalf ("Failed to create LRS server config for testing: %v" , err )
450- }
451- if err := b .UpdateClientConnState (balancer.ClientConnState {
452- ResolverState : xdsclient .SetClient (resolver.State {Endpoints : endpoints }, xdsC ),
453- BalancerConfig : & LBConfig {
454- Cluster : testClusterName ,
455- EDSServiceName : testServiceName ,
456- LoadReportingServer : testLRSServerConfig ,
457- ChildPolicy : & internalserviceconfig.BalancerConfig {
458- Name : roundrobin .Name ,
459- },
460- },
461- }); err != nil {
462- t .Fatalf ("unexpected error from UpdateClientConnState: %v" , err )
463- }
464-
465- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
466- defer cancel ()
467-
468- got , err := xdsC .WaitForReportLoad (ctx )
469- if err != nil {
470- t .Fatalf ("xdsClient.ReportLoad failed with error: %v" , err )
471- }
472- if got .Server != testLRSServerConfig {
473- t .Fatalf ("xdsClient.ReportLoad called with {%q}: want {%q}" , got .Server , testLRSServerConfig )
474- }
475-
476- testLRSServerConfig2 , err := bootstrap .ServerConfigForTesting (bootstrap.ServerConfigTestingOptions {
477- URI : "trafficdirector-another.googleapis.com:443" ,
478- ChannelCreds : []bootstrap.ChannelCreds {{Type : "google_default" }},
479- })
480- if err != nil {
481- t .Fatalf ("Failed to create LRS server config for testing: %v" , err )
482- }
483-
484- // Update LRS server to a different name.
485- if err := b .UpdateClientConnState (balancer.ClientConnState {
486- ResolverState : xdsclient .SetClient (resolver.State {Endpoints : endpoints }, xdsC ),
487- BalancerConfig : & LBConfig {
488- Cluster : testClusterName ,
489- EDSServiceName : testServiceName ,
490- LoadReportingServer : testLRSServerConfig2 ,
491- ChildPolicy : & internalserviceconfig.BalancerConfig {
492- Name : roundrobin .Name ,
493- },
494- },
495- }); err != nil {
496- t .Fatalf ("unexpected error from UpdateClientConnState: %v" , err )
497- }
498- if err := xdsC .WaitForCancelReportLoad (ctx ); err != nil {
499- t .Fatalf ("unexpected error waiting form load report to be canceled: %v" , err )
500- }
501- got2 , err2 := xdsC .WaitForReportLoad (ctx )
502- if err2 != nil {
503- t .Fatalf ("xdsClient.ReportLoad failed with error: %v" , err2 )
504- }
505- if got2 .Server != testLRSServerConfig2 {
506- t .Fatalf ("xdsClient.ReportLoad called with {%q}: want {%q}" , got2 .Server , testLRSServerConfig2 )
507- }
508-
509- // Update LRS server to nil, to disable LRS.
510- if err := b .UpdateClientConnState (balancer.ClientConnState {
511- ResolverState : xdsclient .SetClient (resolver.State {Endpoints : endpoints }, xdsC ),
512- BalancerConfig : & LBConfig {
513- Cluster : testClusterName ,
514- EDSServiceName : testServiceName ,
515- ChildPolicy : & internalserviceconfig.BalancerConfig {
516- Name : roundrobin .Name ,
517- },
518- },
519- }); err != nil {
520- t .Fatalf ("unexpected error from UpdateClientConnState: %v" , err )
521- }
522- if err := xdsC .WaitForCancelReportLoad (ctx ); err != nil {
523- t .Fatalf ("unexpected error waiting form load report to be canceled: %v" , err )
524- }
525-
526- shortCtx , shortCancel := context .WithTimeout (context .Background (), defaultShortTestTimeout )
527- defer shortCancel ()
528- if s , err := xdsC .WaitForReportLoad (shortCtx ); err != context .DeadlineExceeded {
529- t .Fatalf ("unexpected load report to server: %q" , s )
530- }
531- }
532-
533215// Test verifies that child policies was updated on receipt of
534216// configuration update.
535217func (s ) TestChildPolicyUpdatedOnConfigUpdate (t * testing.T ) {
0 commit comments