-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathxds_dependency_manager.go
More file actions
1008 lines (891 loc) · 36.1 KB
/
xds_dependency_manager.go
File metadata and controls
1008 lines (891 loc) · 36.1 KB
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2025 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 xdsdepmgr provides the implementation of the xDS dependency manager
// that manages all the xDS watches and resources as described in gRFC A74.
package xdsdepmgr
import (
"context"
"fmt"
"maps"
"net/url"
"sync"
"google.golang.org/grpc/grpclog"
internalgrpclog "google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/xds/xdsclient"
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
)
const prefix = "[xdsdepmgr %p] "
var logger = grpclog.Component("xds")
func prefixLogger(p *DependencyManager) *internalgrpclog.PrefixLogger {
return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p))
}
// ConfigWatcher is the interface for consumers of aggregated xDS configuration
// from the DependencyManager. The only consumer of this configuration is
// currently the xDS resolver.
type ConfigWatcher interface {
// Update is invoked when a new, validated xDS configuration is available.
//
// Implementations must treat the received config as read-only and should
// not modify it.
Update(*xdsresource.XDSConfig)
// Error is invoked when an error is received from the listener or route
// resource watcher. This includes cases where:
// - The listener or route resource watcher reports a resource error.
// - The received listener resource is a socket listener, not an API
// listener. TODO(i/8114): Implement this check.
// - The received route configuration does not contain a virtual host
// matching the channel's default authority.
Error(error)
}
// xdsResourceState is a generic struct to hold the state of a watched xDS
// resource.
type xdsResourceState[T any, U any] struct {
lastUpdate *T
lastErr error
updateReceived bool
stop func()
extras U // to store any additional state specific to the watcher
}
func (x *xdsResourceState[T, U]) setLastUpdate(update *T) {
x.lastUpdate = update
x.updateReceived = true
x.lastErr = nil
}
func (x *xdsResourceState[T, U]) setLastError(err error) {
x.lastErr = err
x.updateReceived = true
x.lastUpdate = nil
}
type dnsExtras struct {
dnsR resolver.Resolver
}
type routeExtras struct {
virtualHost *xdsresource.VirtualHost
}
// DependencyManager registers watches on the xDS client for all required xDS
// resources, resolves dependencies between them, and returns a complete
// configuration to the xDS resolver.
type DependencyManager struct {
// The following fields are initialized at creation time and are read-only
// after that.
logger *internalgrpclog.PrefixLogger
watcher ConfigWatcher
xdsClient xdsclient.XDSClient
ldsResourceName string
dataplaneAuthority string
nodeID string
// Used to serialize callbacks from DNS resolvers to avoid deadlocks. Since
// the resolver's Build() is called with the dependency manager lock held,
// direct callbacks to ClientConn (which also require that lock) would
// deadlock.
dnsSerializer *grpcsync.CallbackSerializer
dnsSerializerCancel func()
// All the fields below are protected by mu.
mu sync.Mutex
stopped bool
listenerWatcher *xdsResourceState[xdsresource.ListenerUpdate, struct{}]
rdsResourceName string
routeConfigWatcher *xdsResourceState[xdsresource.RouteConfigUpdate, routeExtras]
clustersFromLastRouteConfig map[string]bool
clusterWatchers map[string]*xdsResourceState[xdsresource.ClusterUpdate, struct{}]
endpointWatchers map[string]*xdsResourceState[xdsresource.EndpointsUpdate, struct{}]
dnsResolvers map[string]*xdsResourceState[xdsresource.DNSUpdate, dnsExtras]
clusterSubscriptions map[string]*clusterRef
}
// New creates a new DependencyManager.
//
// - listenerName is the name of the Listener resource to request from the
// management server.
// - dataplaneAuthority is used to select the best matching virtual host from
// the route configuration received from the management server.
// - xdsClient is the xDS client to use to register resource watches.
// - watcher is the ConfigWatcher interface that will receive the aggregated
// XDSConfig updates and errors.
func New(listenerName, dataplaneAuthority string, xdsClient xdsclient.XDSClient, watcher ConfigWatcher) *DependencyManager {
ctx, cancel := context.WithCancel(context.Background())
dm := &DependencyManager{
ldsResourceName: listenerName,
dataplaneAuthority: dataplaneAuthority,
xdsClient: xdsClient,
watcher: watcher,
nodeID: xdsClient.BootstrapConfig().Node().GetId(),
dnsSerializer: grpcsync.NewCallbackSerializer(ctx),
dnsSerializerCancel: cancel,
clustersFromLastRouteConfig: make(map[string]bool),
endpointWatchers: make(map[string]*xdsResourceState[xdsresource.EndpointsUpdate, struct{}]),
dnsResolvers: make(map[string]*xdsResourceState[xdsresource.DNSUpdate, dnsExtras]),
clusterWatchers: make(map[string]*xdsResourceState[xdsresource.ClusterUpdate, struct{}]),
clusterSubscriptions: make(map[string]*clusterRef),
}
dm.logger = prefixLogger(dm)
// The dependency manager starts by watching the listener resource and
// discovers other resources as required. For example, the listener resource
// will contain the name of the route configuration resource, which will be
// subsequently watched.œ
dm.listenerWatcher = &xdsResourceState[xdsresource.ListenerUpdate, struct{}]{}
lw := &xdsResourceWatcher[xdsresource.ListenerUpdate]{
onUpdate: func(update *xdsresource.ListenerUpdate, onDone func()) {
dm.onListenerResourceUpdate(update, onDone)
},
onError: func(err error, onDone func()) {
dm.onListenerResourceError(err, onDone)
},
onAmbientError: func(err error, onDone func()) {
dm.onListenerResourceAmbientError(err, onDone)
},
}
dm.listenerWatcher.stop = xdsresource.WatchListener(dm.xdsClient, listenerName, lw)
return dm
}
// Close cancels all registered resource watches.
func (m *DependencyManager) Close() {
m.mu.Lock()
defer m.mu.Unlock()
if m.stopped {
return
}
m.stopped = true
m.listenerWatcher.stop()
if m.routeConfigWatcher != nil {
m.routeConfigWatcher.stop()
}
for name, cluster := range m.clusterWatchers {
cluster.stop()
delete(m.clusterWatchers, name)
}
for name, endpoint := range m.endpointWatchers {
endpoint.stop()
delete(m.endpointWatchers, name)
}
// We cannot wait for the dns serializer to finish here, as the callbacks
// try to grab the dependency manager lock, which is already held here.
m.dnsSerializerCancel()
for name, dnsResolver := range m.dnsResolvers {
dnsResolver.stop()
delete(m.dnsResolvers, name)
}
}
// annotateErrorWithNodeID annotates the given error with the provided xDS node
// ID.
func (m *DependencyManager) annotateErrorWithNodeID(err error) error {
return fmt.Errorf("[xDS node id: %v]: %v", m.nodeID, err)
}
// maybeSendUpdateLocked verifies that all expected resources have been
// received, and if so, delivers the complete xDS configuration to the watcher.
func (m *DependencyManager) maybeSendUpdateLocked() {
if m.listenerWatcher.lastUpdate == nil || m.routeConfigWatcher == nil || m.routeConfigWatcher.lastUpdate == nil {
return
}
config := &xdsresource.XDSConfig{
Listener: m.listenerWatcher.lastUpdate,
RouteConfig: m.routeConfigWatcher.lastUpdate,
VirtualHost: m.routeConfigWatcher.extras.virtualHost,
Clusters: make(map[string]*xdsresource.ClusterResult),
}
edsResourcesSeen := make(map[string]bool)
dnsResourcesSeen := make(map[string]bool)
clusterResourcesSeen := make(map[string]bool)
haveAllResources := true
// Start watches for all clusters. Wait for all the clusters with static
// reference(from route config) to be resolved before sending the update.
for cluster := range m.clusterSubscriptions {
clusterConfig := make(map[string]*xdsresource.ClusterResult)
ok, leafClusters, err := m.populateClusterConfigLocked(cluster, 0, clusterConfig, edsResourcesSeen, dnsResourcesSeen, clusterResourcesSeen)
if !ok && m.clusterSubscriptions[cluster].staticRefCount > 0 {
haveAllResources = false
}
// If there are no leaf clusters, add that as error.
if ok && len(leafClusters) == 0 {
config.Clusters[cluster] = &xdsresource.ClusterResult{Err: m.annotateErrorWithNodeID(fmt.Errorf("aggregate cluster graph has no leaf clusters"))}
}
if err != nil {
config.Clusters[cluster] = &xdsresource.ClusterResult{Err: err}
}
// Only if all the dependencies for the cluster is resolved, add the
// clusters to the config. This is to ensure we do not send partial
// updates for dynamic clusters.
if ok {
maps.Copy(config.Clusters, clusterConfig)
}
}
// Cancel resources not seen in the tree.
for name, ep := range m.endpointWatchers {
if _, ok := edsResourcesSeen[name]; !ok {
ep.stop()
delete(m.endpointWatchers, name)
}
}
for name, dr := range m.dnsResolvers {
if _, ok := dnsResourcesSeen[name]; !ok {
dr.stop()
delete(m.dnsResolvers, name)
}
}
for name, cluster := range m.clusterWatchers {
if _, ok := clusterResourcesSeen[name]; !ok {
cluster.stop()
delete(m.clusterWatchers, name)
}
}
if haveAllResources {
m.watcher.Update(config)
}
}
// populateClusterConfigLocked resolves and populates the
// configuration for the given cluster and its children, including its
// associated endpoint or aggregate children. For aggregate clusters, it
// recursively resolves the configuration for its child clusters.
//
// This function traverses the cluster dependency graph (e.g., from an Aggregate
// cluster down to its leaf clusters and their endpoints/DNS resources) to
// ensure all necessary xDS resources are watched and fully resolved before
// configuration is considered ready.
//
// Parameters:
//
// clusterName: The name of the cluster resource to resolve.
// depth: The current recursion depth.
// clusterConfigs: Map to store the resolved cluster configuration.
// endpointResourcesSeen: Stores which EDS resource names have been encountered.
// dnsResourcesSeen: Stores which DNS resource names have been encountered.
// clustersSeen: Stores which cluster resource names have been encountered.
//
// Returns:
//
// bool: Returns true if the cluster configuration (and all its
// dependencies) is fully resolved (i.e either update or
// error has been received).
// []string: A slice of all "leaf" cluster names discovered in the
// traversal starting from `clusterName`. For
// non-aggregate clusters, this will contain only `clusterName`.
// error: Error that needs to be propagated up the tree (like
// max depth exceeded or an error propagated from a
// child cluster).
func (m *DependencyManager) populateClusterConfigLocked(clusterName string, depth int, clusterConfigs map[string]*xdsresource.ClusterResult, endpointResourcesSeen, dnsResourcesSeen, clustersSeen map[string]bool) (bool, []string, error) {
const aggregateClusterMaxDepth = 16
clustersSeen[clusterName] = true
if depth >= aggregateClusterMaxDepth {
err := m.annotateErrorWithNodeID(fmt.Errorf("aggregate cluster graph exceeds max depth (%d)", aggregateClusterMaxDepth))
clusterConfigs[clusterName] = &xdsresource.ClusterResult{Err: err}
return true, nil, err
}
// If cluster is already seen in the tree, return.
if _, ok := clusterConfigs[clusterName]; ok {
return true, nil, nil
}
// If cluster watcher does not exist, create one.
state, ok := m.clusterWatchers[clusterName]
if !ok {
m.clusterWatchers[clusterName] = newClusterWatcher(clusterName, m)
return false, nil, nil
}
// If a watch exists but no update received yet, return.
if !state.updateReceived {
return false, nil, nil
}
// If there was a resource error, propagate it up.
if state.lastErr != nil {
return true, nil, state.lastErr
}
clusterConfigs[clusterName] = &xdsresource.ClusterResult{
Config: xdsresource.ClusterConfig{
Cluster: state.lastUpdate,
},
}
update := state.lastUpdate
switch update.ClusterType {
case xdsresource.ClusterTypeEDS:
return m.populateEDSClusterLocked(clusterName, update, clusterConfigs, endpointResourcesSeen)
case xdsresource.ClusterTypeLogicalDNS:
return m.populateLogicalDNSClusterLocked(clusterName, update, clusterConfigs, dnsResourcesSeen)
case xdsresource.ClusterTypeAggregate:
return m.populateAggregateClusterLocked(clusterName, update, depth, clusterConfigs, endpointResourcesSeen, dnsResourcesSeen, clustersSeen)
default:
clusterConfigs[clusterName] = &xdsresource.ClusterResult{Err: m.annotateErrorWithNodeID(fmt.Errorf("cluster type %v of cluster %s not supported", update.ClusterType, clusterName))}
return true, nil, nil
}
}
func (m *DependencyManager) populateEDSClusterLocked(clusterName string, update *xdsresource.ClusterUpdate, clusterConfigs map[string]*xdsresource.ClusterResult, endpointResourcesSeen map[string]bool) (bool, []string, error) {
edsName := clusterName
if update.EDSServiceName != "" {
edsName = update.EDSServiceName
}
endpointResourcesSeen[edsName] = true
// If endpoint watcher does not exist, create one.
if _, ok := m.endpointWatchers[edsName]; !ok {
m.endpointWatchers[edsName] = newEndpointWatcher(edsName, m)
return false, nil, nil
}
endpointState := m.endpointWatchers[edsName]
// If the resource does not have any update yet, return.
if !endpointState.updateReceived {
return false, nil, nil
}
// Store the update and error.
clusterConfigs[clusterName].Config.EndpointConfig = &xdsresource.EndpointConfig{
EDSUpdate: endpointState.lastUpdate,
ResolutionNote: endpointState.lastErr,
}
return true, []string{clusterName}, nil
}
func (m *DependencyManager) populateLogicalDNSClusterLocked(clusterName string, update *xdsresource.ClusterUpdate, clusterConfigs map[string]*xdsresource.ClusterResult, dnsResourcesSeen map[string]bool) (bool, []string, error) {
target := update.DNSHostName
dnsResourcesSeen[target] = true
// If dns resolver does not exist, create one.
if _, ok := m.dnsResolvers[target]; !ok {
state := m.newDNSResolver(target)
if state == nil {
return false, nil, nil
}
m.dnsResolvers[target] = state
return false, nil, nil
}
dnsState := m.dnsResolvers[target]
// If no update received, return false.
if !dnsState.updateReceived {
return false, nil, nil
}
clusterConfigs[clusterName].Config.EndpointConfig = &xdsresource.EndpointConfig{
DNSEndpoints: dnsState.lastUpdate,
ResolutionNote: dnsState.lastErr,
}
return true, []string{clusterName}, nil
}
func (m *DependencyManager) populateAggregateClusterLocked(clusterName string, update *xdsresource.ClusterUpdate, depth int, clusterConfigs map[string]*xdsresource.ClusterResult, endpointResourcesSeen, dnsResourcesSeen, clustersSeen map[string]bool) (bool, []string, error) {
var leafClusters []string
haveAllResources := true
for _, child := range update.PrioritizedClusterNames {
ok, childLeafClusters, err := m.populateClusterConfigLocked(child, depth+1, clusterConfigs, endpointResourcesSeen, dnsResourcesSeen, clustersSeen)
if !ok {
haveAllResources = false
}
if err != nil {
clusterConfigs[clusterName] = &xdsresource.ClusterResult{Err: err}
return true, leafClusters, err
}
leafClusters = append(leafClusters, childLeafClusters...)
}
if !haveAllResources {
return false, leafClusters, nil
}
if haveAllResources && len(leafClusters) == 0 {
clusterConfigs[clusterName] = &xdsresource.ClusterResult{Err: m.annotateErrorWithNodeID(fmt.Errorf("aggregate cluster graph has no leaf clusters"))}
return true, leafClusters, nil
}
clusterConfigs[clusterName].Config.AggregateConfig = &xdsresource.AggregateConfig{
LeafClusters: leafClusters,
}
return true, leafClusters, nil
}
func (m *DependencyManager) applyRouteConfigUpdateLocked(update *xdsresource.RouteConfigUpdate) {
matchVH := xdsresource.FindBestMatchingVirtualHost(m.dataplaneAuthority, update.VirtualHosts)
if matchVH == nil {
err := m.annotateErrorWithNodeID(fmt.Errorf("could not find VirtualHost for %q", m.dataplaneAuthority))
m.routeConfigWatcher.setLastError(err)
m.watcher.Error(err)
return
}
m.routeConfigWatcher.setLastUpdate(update)
m.routeConfigWatcher.extras.virtualHost = matchVH
// Get the clusters to be watched from the routes in the virtual host.
// If the ClusterSpecifierPlugin field is set, we ignore it for now as the
// clusters will be determined dynamically for it.
newClusters := make(map[string]bool)
for _, rt := range matchVH.Routes {
for _, cluster := range rt.WeightedClusters {
newClusters[cluster.Name] = true
}
}
// Add subscriptions for all new clusters.
for cluster := range newClusters {
// If the cluster already has a reference, increase its static
// reference.
if sub, ok := m.clusterSubscriptions[cluster]; ok {
sub.staticRefCount++
continue
}
// If cluster is not present in subscriptions, add it with static
// ref count as 1.
m.clusterSubscriptions[cluster] = &clusterRef{
staticRefCount: 1,
}
}
// Unsubscribe to clusters from last route config.
for cluster := range m.clustersFromLastRouteConfig {
clusterRef, ok := m.clusterSubscriptions[cluster]
if !ok {
// Should not reach here as the cluster was present in last
// route config so should be present in current cluster
// subscriptions.
continue
}
clusterRef.staticRefCount--
if clusterRef.staticRefCount == 0 && clusterRef.dynamicRefCount == 0 {
delete(m.clusterSubscriptions, cluster)
}
}
m.clustersFromLastRouteConfig = newClusters
// maybeSendUpdate is called to update the configuration with the new route,
// start watching the newly added clusters and stop watching clusters that
// are not needed anymore.
m.maybeSendUpdateLocked()
}
func (m *DependencyManager) onListenerResourceUpdate(update *xdsresource.ListenerUpdate, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped {
return
}
if m.logger.V(2) {
m.logger.Infof("Received update for Listener resource %q: %+v", m.ldsResourceName, update)
}
m.listenerWatcher.setLastUpdate(update)
if update.APIListener != nil && update.APIListener.InlineRouteConfig != nil {
// If there was a previous route config watcher because of a non-inline
// route configuration, cancel it.
m.rdsResourceName = ""
if m.routeConfigWatcher != nil {
m.routeConfigWatcher.stop()
}
m.routeConfigWatcher = &xdsResourceState[xdsresource.RouteConfigUpdate, routeExtras]{stop: func() {}}
m.applyRouteConfigUpdateLocked(update.APIListener.InlineRouteConfig)
return
}
// We get here only if there was no inline route configuration. If the route
// config name has not changed, send an update with existing route
// configuration and the newly received listener configuration.
if update.APIListener == nil {
m.logger.Errorf("Received a listener resource with no api_listener configuration")
return
}
if m.rdsResourceName == update.APIListener.RouteConfigName {
m.maybeSendUpdateLocked()
return
}
// If the route config name has changed, cancel the old watcher and start a
// new one. At this point, since the new route config name has not yet been
// resolved, no update is sent to the channel, and therefore the old route
// configuration (if received) is used until the new one is received.
m.rdsResourceName = update.APIListener.RouteConfigName
if m.routeConfigWatcher != nil {
m.routeConfigWatcher.stop()
}
rw := &xdsResourceWatcher[xdsresource.RouteConfigUpdate]{
onUpdate: func(update *xdsresource.RouteConfigUpdate, onDone func()) {
m.onRouteConfigResourceUpdate(m.rdsResourceName, update, onDone)
},
onError: func(err error, onDone func()) {
m.onRouteConfigResourceError(m.rdsResourceName, err, onDone)
},
onAmbientError: func(err error, onDone func()) {
m.onRouteConfigResourceAmbientError(m.rdsResourceName, err, onDone)
},
}
if m.routeConfigWatcher != nil {
m.routeConfigWatcher.stop = xdsresource.WatchRouteConfig(m.xdsClient, m.rdsResourceName, rw)
} else {
m.routeConfigWatcher = &xdsResourceState[xdsresource.RouteConfigUpdate, routeExtras]{
stop: xdsresource.WatchRouteConfig(m.xdsClient, m.rdsResourceName, rw),
}
}
}
func (m *DependencyManager) onListenerResourceError(err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped {
return
}
m.logger.Warningf("Received resource error for Listener resource %q: %v", m.ldsResourceName, m.annotateErrorWithNodeID(err))
if m.routeConfigWatcher != nil {
m.routeConfigWatcher.stop()
}
m.listenerWatcher.setLastError(err)
m.rdsResourceName = ""
m.routeConfigWatcher = nil
m.watcher.Error(fmt.Errorf("listener resource error: %v", m.annotateErrorWithNodeID(err)))
}
// onListenerResourceAmbientError handles ambient errors received from the
// listener resource watcher. Since ambient errors do not impact the current
// state of the resource, no change is made to the current configuration and the
// errors are only logged for visibility.
func (m *DependencyManager) onListenerResourceAmbientError(err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped {
return
}
m.logger.Warningf("Listener resource ambient error: %v", m.annotateErrorWithNodeID(err))
}
func (m *DependencyManager) onRouteConfigResourceUpdate(resourceName string, update *xdsresource.RouteConfigUpdate, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.rdsResourceName != resourceName {
return
}
if m.logger.V(2) {
m.logger.Infof("Received update for RouteConfiguration resource %q: %+v", resourceName, update)
}
m.applyRouteConfigUpdateLocked(update)
}
func (m *DependencyManager) onRouteConfigResourceError(resourceName string, err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.rdsResourceName != resourceName {
return
}
m.routeConfigWatcher.setLastError(err)
m.logger.Warningf("Received resource error for RouteConfiguration resource %q: %v", resourceName, m.annotateErrorWithNodeID(err))
m.watcher.Error(fmt.Errorf("route resource error: %v", m.annotateErrorWithNodeID(err)))
}
// onRouteResourceAmbientError handles ambient errors received from the route
// resource watcher. Since ambient errors do not impact the current state of the
// resource, no change is made to the current configuration and the errors are
// only logged for visibility.
func (m *DependencyManager) onRouteConfigResourceAmbientError(resourceName string, err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.rdsResourceName != resourceName {
return
}
m.logger.Warningf("Route resource ambient error %q: %v", resourceName, m.annotateErrorWithNodeID(err))
}
func newClusterWatcher(resourceName string, depMgr *DependencyManager) *xdsResourceState[xdsresource.ClusterUpdate, struct{}] {
w := &xdsResourceWatcher[xdsresource.ClusterUpdate]{
onUpdate: func(u *xdsresource.ClusterUpdate, onDone func()) {
depMgr.onClusterResourceUpdate(resourceName, u, onDone)
},
onError: func(err error, onDone func()) {
depMgr.onClusterResourceError(resourceName, err, onDone)
},
onAmbientError: func(err error, onDone func()) {
depMgr.onClusterAmbientError(resourceName, err, onDone)
},
}
return &xdsResourceState[xdsresource.ClusterUpdate, struct{}]{
stop: xdsresource.WatchCluster(depMgr.xdsClient, resourceName, w),
}
}
// Records a successful Cluster resource update, clears any previous error.
func (m *DependencyManager) onClusterResourceUpdate(resourceName string, update *xdsresource.ClusterUpdate, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.clusterWatchers[resourceName] == nil {
return
}
if m.logger.V(2) {
m.logger.Infof("Received update for Cluster resource %q: %+v", resourceName, update)
}
m.clusterWatchers[resourceName].setLastUpdate(update)
m.maybeSendUpdateLocked()
}
// Records a resource error for a Cluster resource, clears the last successful
// update since we want to stop using the resource if we get a resource error.
func (m *DependencyManager) onClusterResourceError(resourceName string, err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.clusterWatchers[resourceName] == nil {
return
}
m.logger.Warningf("Received resource error for Cluster resource %q: %v", resourceName, m.annotateErrorWithNodeID(err))
m.clusterWatchers[resourceName].setLastError(err)
m.maybeSendUpdateLocked()
}
// Ambient errors from cluster resource are logged and the last successful
// update is retained because it should continue to be used.
func (m *DependencyManager) onClusterAmbientError(resourceName string, err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.clusterWatchers[resourceName] == nil {
return
}
m.logger.Warningf("Cluster resource ambient error %q: %v", resourceName, m.annotateErrorWithNodeID(err))
}
func newEndpointWatcher(resourceName string, depMgr *DependencyManager) *xdsResourceState[xdsresource.EndpointsUpdate, struct{}] {
w := &xdsResourceWatcher[xdsresource.EndpointsUpdate]{
onUpdate: func(u *xdsresource.EndpointsUpdate, onDone func()) {
depMgr.onEndpointUpdate(resourceName, u, onDone)
},
onError: func(err error, onDone func()) {
depMgr.onEndpointResourceError(resourceName, err, onDone)
},
onAmbientError: func(err error, onDone func()) {
depMgr.onEndpointAmbientError(resourceName, err, onDone)
},
}
return &xdsResourceState[xdsresource.EndpointsUpdate, struct{}]{
stop: xdsresource.WatchEndpoints(depMgr.xdsClient, resourceName, w),
}
}
// Records a successful Endpoint resource update, clears any previous error from
// the state.
func (m *DependencyManager) onEndpointUpdate(resourceName string, update *xdsresource.EndpointsUpdate, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.endpointWatchers[resourceName] == nil {
return
}
if m.logger.V(2) {
m.logger.Infof("Received update for Endpoint resource %q: %+v", resourceName, update)
}
m.endpointWatchers[resourceName].setLastUpdate(update)
m.maybeSendUpdateLocked()
}
// Records a resource error and clears the last successful update since the
// endpoints should not be used after getting a resource error.
func (m *DependencyManager) onEndpointResourceError(resourceName string, err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.endpointWatchers[resourceName] == nil {
return
}
m.logger.Warningf("Received resource error for Endpoint resource %q: %v", resourceName, m.annotateErrorWithNodeID(err))
// Send an empty EndpointsUpdate instead of nil to avoid nil-check handling
// in the CDS balancer. The priority balancer will handle the case of having
// no endpoints and transition the channel to Transient Failure if needed.
m.endpointWatchers[resourceName].lastUpdate = &xdsresource.EndpointsUpdate{}
m.endpointWatchers[resourceName].lastErr = err
m.endpointWatchers[resourceName].updateReceived = true
m.maybeSendUpdateLocked()
}
// Logs the ambient error and does not update the state, as the last successful
// update for endpoints should continue to be used.
func (m *DependencyManager) onEndpointAmbientError(resourceName string, err error, onDone func()) {
m.mu.Lock()
defer m.mu.Unlock()
defer onDone()
if m.stopped || m.endpointWatchers[resourceName] == nil {
return
}
m.logger.Warningf("Endpoint resource ambient error %q: %v", resourceName, m.annotateErrorWithNodeID(err))
}
// Converts the DNS resolver state to an internal update, handling address-only
// updates by wrapping them into endpoints. It records the update and clears any
// previous error.
func (m *DependencyManager) onDNSUpdate(resourceName string, update *resolver.State) {
m.mu.Lock()
defer m.mu.Unlock()
if m.stopped || m.dnsResolvers[resourceName] == nil {
return
}
if m.logger.V(2) {
m.logger.Infof("Received update from DNS resolver for resource %q: %+v", resourceName, update)
}
m.dnsResolvers[resourceName].setLastUpdate(&xdsresource.DNSUpdate{Endpoints: update.Endpoints})
m.maybeSendUpdateLocked()
}
// Records a DNS resolver error. It clears the last update only if no successful
// update has been received yet, then triggers a dependency update.
//
// If a previous good update was received, the error is logged and the previous
// update is retained for continued use. Errors are suppressed if a resource
// error was already received, as further propagation would have no downstream
// effect.
func (m *DependencyManager) onDNSError(resourceName string, err error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.stopped || m.dnsResolvers[resourceName] == nil {
return
}
err = fmt.Errorf("dns resolver error for target %q: %v", resourceName, m.annotateErrorWithNodeID(err))
m.logger.Warningf("%v", err)
state := m.dnsResolvers[resourceName]
if state.updateReceived {
return
}
// Send an empty DNSUpdate instead of nil to avoid nil-check handling in the
// CDS balancer. The priority balancer will handle the case of having no
// endpoints and transition the channel to Transient Failure if needed.
state.lastUpdate = &xdsresource.DNSUpdate{}
state.lastErr = err
state.updateReceived = true
m.maybeSendUpdateLocked()
}
// RequestDNSReresolution calls all the DNS resolver's ResolveNow.
func (m *DependencyManager) RequestDNSReresolution(opt resolver.ResolveNowOptions) {
m.mu.Lock()
defer m.mu.Unlock()
for _, res := range m.dnsResolvers {
if res.extras.dnsR != nil {
res.extras.dnsR.ResolveNow(opt)
}
}
}
type resolverClientConn struct {
target string
depMgr *DependencyManager
}
func (rcc *resolverClientConn) UpdateState(state resolver.State) error {
rcc.depMgr.dnsSerializer.TrySchedule(func(context.Context) {
rcc.depMgr.onDNSUpdate(rcc.target, &state)
})
return nil
}
func (rcc *resolverClientConn) ReportError(err error) {
rcc.depMgr.dnsSerializer.TrySchedule(func(context.Context) {
rcc.depMgr.onDNSError(rcc.target, err)
})
}
func (rcc *resolverClientConn) NewAddress(addresses []resolver.Address) {
rcc.UpdateState(resolver.State{Addresses: addresses})
}
func (rcc *resolverClientConn) ParseServiceConfig(string) *serviceconfig.ParseResult {
return &serviceconfig.ParseResult{Err: fmt.Errorf("service config not supported")}
}
func (m *DependencyManager) newDNSResolver(target string) *xdsResourceState[xdsresource.DNSUpdate, dnsExtras] {
rcc := &resolverClientConn{
target: target,
depMgr: m,
}
u, err := url.Parse("dns:///" + target)
if err != nil {
err := fmt.Errorf("failed to parse DNS target %q: %v", target, m.annotateErrorWithNodeID(err))
m.logger.Warningf("%v", err)
rcc.ReportError(err)
return &xdsResourceState[xdsresource.DNSUpdate, dnsExtras]{
stop: func() {},
}
}
r, err := resolver.Get("dns").Build(resolver.Target{URL: *u}, rcc, resolver.BuildOptions{})
if err != nil {
rcc.ReportError(err)
err := fmt.Errorf("failed to build DNS resolver for target %q: %v", target, m.annotateErrorWithNodeID(err))
m.logger.Warningf("%v", err)
return &xdsResourceState[xdsresource.DNSUpdate, dnsExtras]{
stop: func() {},
}
}
return &xdsResourceState[xdsresource.DNSUpdate, dnsExtras]{
extras: dnsExtras{dnsR: r},
stop: r.Close,
}
}
// xdsResourceWatcher is a generic implementation of the xdsresource.Watcher
// interface.
type xdsResourceWatcher[T any] struct {
onUpdate func(*T, func())
onError func(error, func())
onAmbientError func(error, func())
}
func (x *xdsResourceWatcher[T]) ResourceChanged(update *T, onDone func()) {
x.onUpdate(update, onDone)
}
func (x *xdsResourceWatcher[T]) ResourceError(err error, onDone func()) {
x.onError(err, onDone)
}
func (x *xdsResourceWatcher[T]) AmbientError(err error, onDone func()) {
x.onAmbientError(err, onDone)
}
// xdsClusterSubscriberKey is the type used as the key to store the
// ClusterSubscriber interface in the Attributes field of resolver.states.
type xdsClusterSubscriberKey struct{}
// SetXDSClusterSubscriber returns a copy of state in which the Attributes field
// is updated with the ClusterSubscriber interface.
func SetXDSClusterSubscriber(state resolver.State, subs ClusterSubscriber) resolver.State {
state.Attributes = state.Attributes.WithValue(xdsClusterSubscriberKey{}, subs)
return state
}
// XDSClusterSubscriberFromResolverState returns ClusterSubscriber interface
// stored as an attribute in the resolver state.
func XDSClusterSubscriberFromResolverState(state resolver.State) ClusterSubscriber {
if v := state.Attributes.Value(xdsClusterSubscriberKey{}); v != nil {
return v.(ClusterSubscriber)
}
return nil
}
// ClusterSubscriber allows dynamic subscription to clusters. This is useful for
// scenarios where the cluster name was not present in the RouteConfiguration,
// e.g. when the route uses a ClusterSpecifierPlugin.
//
// The xDS resolver will pass this interface to the LB policies as an attribute
// in the resolver update.
type ClusterSubscriber interface {
// SubscribeToCluster creates a dynamic subscription for the named cluster.
//
// The returned cancel function must be called when the subscription is no
// longer needed. It is safe to call cancel multiple times.
SubscribeToCluster(clusterName string) (cancel func())
}
// clusterRef represents a reference to a cluster and maintains reference count
// of the number of users of the cluster.
type clusterRef struct {
// Access to these field is protected by DependencyManager's mutex and so
// they don't need to be atomic.
// staticRefCount comes from cluster being specified in the route
// configuration.
staticRefCount int32
// dynamicRefCount comes from cluster being referenced by RPCs or being
// dynamically subscribed by the balancers in case of cluster specifier
// plugin being used.
dynamicRefCount int32
}
// SubscribeToCluster increments the reference count for the cluster. If the
// cluster is not already being tracked, it is added to the clusterSubscriptions
// map. It returns a function to unsubscribe from the cluster i.e. decrease its
// refcount. This returned function is idempotent, meaning it can be called
// multiple times without any additional effect. Calling Subscribe in a blocking
// manner while handling an update will lead to a deadlock.
func (m *DependencyManager) SubscribeToCluster(name string) func() {
m.mu.Lock()
defer m.mu.Unlock()
// If the cluster is already being tracked, increment its dynamic refcount
// and return.
subs, ok := m.clusterSubscriptions[name]
if ok {
subs.dynamicRefCount++
return sync.OnceFunc(func() { m.unsubscribeFromCluster(name) })
}
// If the cluster is not being tracked, add it with dynamic refcount as 1.
m.clusterSubscriptions[name] = &clusterRef{
dynamicRefCount: 1,
}
m.maybeSendUpdateLocked()
return sync.OnceFunc(func() { m.unsubscribeFromCluster(name) })
}
// unsubscribeFromCluster decrements the reference count for the cluster. If
// both the reference counts reaches zero, it removes the cluster from the
// clusterSubscriptions map in the DependencyManager. Calling
// unsubscribeFromCluster in a blocking manner while handling an update will
// lead to a deadlock.
func (m *DependencyManager) unsubscribeFromCluster(name string) {
m.mu.Lock()
defer m.mu.Unlock()
c := m.clusterSubscriptions[name]
c.dynamicRefCount--
// This should not happen as unsubscribe returned from the
// ClusterSubscription is wrapped in sync.OnceFunc()
if c.dynamicRefCount < 0 {