forked from apache/dubbo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory.go
More file actions
1040 lines (932 loc) · 34.8 KB
/
Copy pathdirectory.go
File metadata and controls
1040 lines (932 loc) · 34.8 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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 directory
import (
"fmt"
"net/url"
"os"
"reflect"
"sync"
"time"
)
import (
"github.com/dubbogo/gost/log/logger"
perrors "github.com/pkg/errors"
)
import (
"dubbo.apache.org/dubbo-go/v3/cluster/directory"
"dubbo.apache.org/dubbo-go/v3/cluster/directory/base"
"dubbo.apache.org/dubbo-go/v3/cluster/directory/static"
"dubbo.apache.org/dubbo-go/v3/cluster/router/chain"
"dubbo.apache.org/dubbo-go/v3/common"
commonConfig "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config_center"
_ "dubbo.apache.org/dubbo-go/v3/config_center/configurator"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/graceful_shutdown"
"dubbo.apache.org/dubbo-go/v3/metrics"
metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry"
protocolbase "dubbo.apache.org/dubbo-go/v3/protocol/base"
"dubbo.apache.org/dubbo-go/v3/protocol/protocolwrapper"
"dubbo.apache.org/dubbo-go/v3/registry"
"dubbo.apache.org/dubbo-go/v3/remoting"
)
func init() {
extension.SetDefaultRegistryDirectory(NewRegistryDirectory)
extension.SetDirectory(constant.RegistryProtocol, NewRegistryDirectory)
extension.SetDirectory(constant.ServiceRegistryProtocol, NewServiceDiscoveryRegistryDirectory)
}
// RegistryDirectory implementation of Directory:
// Invoker list returned from this Directory's list method have been filtered by Routers
type RegistryDirectory struct {
*base.Directory
cacheInvokers []protocolbase.Invoker
invokersLock sync.RWMutex
serviceType string
registry registry.Registry
cacheInvokersMap *sync.Map // use sync.map
consumerURL *common.URL
cacheOriginUrl *common.URL
cachedProviderEvents []*registry.ServiceEvent
configurators []config_center.Configurator
configuratorsLock sync.RWMutex
consumerConfigurationListener *consumerConfigurationListener
referenceConfigurationListener *referenceConfigurationListener
registerLock sync.Mutex // this lock if for register
subscribedUrlLock sync.RWMutex
SubscribedUrl *common.URL
RegisteredUrl *common.URL
closingTombstones *sync.Map // map[string]closingTombstone
closingTombstoneTTL time.Duration
}
type closingTombstone struct {
InstanceKey string
ServiceKey string
Address string
// Timestamp is the export timestamp of the closing instance's URL. A re-add
// carrying a different timestamp is a genuine restart, not a stale
// pre-shutdown registry snapshot.
Timestamp string
Source string
ExpireAt time.Time
}
var defaultClosingTombstoneTTL = func() time.Duration {
if duration, err := time.ParseDuration(global.DefaultShutdownConfig().ClosingInvokerExpireTime); err == nil && duration > 0 {
return duration
}
return 30 * time.Second
}()
func ensureRegistriesAttribute(url *common.URL) {
if registries, ok := registriesFromAttribute(url); ok {
url.SetAttribute(constant.RegistriesConfigKey, registries)
return
}
if registries, ok := registriesFromAttribute(url.SubURL); ok {
url.SetAttribute(constant.RegistriesConfigKey, registries)
return
}
url.SetAttribute(constant.RegistriesConfigKey, map[string]*global.RegistryConfig{
constant.DefaultKey: global.DefaultRegistryConfig(),
})
}
func registriesFromAttribute(url *common.URL) (map[string]*global.RegistryConfig, bool) {
if url == nil {
return nil, false
}
registriesRaw, ok := url.GetAttribute(constant.RegistriesConfigKey)
if !ok {
return nil, false
}
switch registries := registriesRaw.(type) {
case map[string]*global.RegistryConfig:
if registries != nil {
return registries, true
}
case map[string]global.RegistryConfig:
if registries != nil {
converted := make(map[string]*global.RegistryConfig, len(registries))
for key, registryConfig := range registries {
registryConfigCopy := registryConfig
converted[key] = ®istryConfigCopy
}
return converted, true
}
}
return nil, false
}
// NewRegistryDirectory will create a new RegistryDirectory
func NewRegistryDirectory(url *common.URL, registry registry.Registry) (directory.Directory, error) {
if url.SubURL == nil {
return nil, perrors.Errorf("url is invalid, suburl can not be nil")
}
logger.Debugf("[Registry][Directory] new RegistryDirectory for service=%s", url.Key())
commonConfig.EnsureApplicationAttribute(url, url.SubURL)
ensureRegistriesAttribute(url)
dir := &RegistryDirectory{
Directory: base.NewDirectory(url),
cacheInvokers: []protocolbase.Invoker{},
cacheInvokersMap: &sync.Map{},
serviceType: url.SubURL.Service(),
registry: registry,
closingTombstones: &sync.Map{},
closingTombstoneTTL: defaultClosingTombstoneTTL,
}
dir.consumerURL = dir.getConsumerUrl(url.SubURL)
if routerChain, err := chain.NewRouterChain(url); err == nil {
dir.SetRouterChain(routerChain)
} else {
logger.Warnf("[Registry][Directory] fail to create router chain, url=%s err=%v", url.SubURL, err)
}
dir.consumerConfigurationListener = newConsumerConfigurationListener(dir, url)
dir.consumerConfigurationListener.addNotifyListener(dir)
dir.referenceConfigurationListener = newReferenceConfigurationListener(dir, url)
graceful_shutdown.DefaultClosingDirectoryRegistry().Register(dir.closingServiceKey(), dir)
if err := dir.registry.LoadSubscribeInstances(url.SubURL, dir); err != nil {
return nil, err
}
metrics.Publish(metricsRegistry.NewDirectoryEvent(metricsRegistry.NumAllInc))
return dir, nil
}
// subscribe from registry
func (dir *RegistryDirectory) Subscribe(url *common.URL) error {
logger.Infof("[Registry][Directory] start subscribing for service=%s with a new go routine", url.Key())
dir.setSubscribedURL(url)
// Get the timeout time from the registration center configuration (default time 5s)
registerUrl := dir.registry.GetURL()
var timeoutStr string
if registerUrl != nil {
if val := registerUrl.GetParam(constant.RegistryTimeoutKey, ""); val != "" {
timeoutStr = val
}
}
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
logger.Warnf("[Registry][Directory] invalid timeout value=%s, using default=%s", timeoutStr, constant.DefaultRegTimeout)
timeout, _ = time.ParseDuration(constant.DefaultRegTimeout)
}
serviceKey := url.Key()
go func() {
if err := dir.registry.Subscribe(url, dir); err != nil {
logger.Errorf("[Registry][Directory] registry.Subscribe(url=%v dir=%v) = err=%v", url, dir, err)
}
}()
// Registration is bounded by registry timeout (default 5s), but subscription
// stays decoupled from registration so discovery can continue even on register error.
if err := dir.registerConsumerWithTimeout(url, timeout, serviceKey); err != nil {
return err
}
logger.Infof("[Registry][Directory] register completed successfully for service=%s", serviceKey)
return nil
}
func (dir *RegistryDirectory) registerConsumerWithTimeout(url *common.URL, timeout time.Duration, serviceKey string) error {
registerErrCh := make(chan error, 1)
urlToReg := getConsumerUrlToRegistry(url.Clone())
go func() {
registerErrCh <- dir.registry.Register(urlToReg)
}()
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case err := <-registerErrCh:
if err != nil {
registryURL := dir.registry.GetURL()
registryURLString := ""
if registryURL != nil {
registryURLString = registryURL.String()
}
logger.Errorf("[Registry][Directory] consumer service %v register registry %v error, err=%v",
url.String(), registryURLString, err)
return err
}
return nil
case <-timer.C:
logger.Errorf("[Registry][Directory] register timed out for service=%s", serviceKey)
go func() {
err := <-registerErrCh
if err != nil {
return
}
if unRegErr := dir.registry.UnRegister(urlToReg.Clone()); unRegErr != nil {
logger.Warnf("[Registry][Directory] register timed out for service=%s, but late unregister failed, err=%v", serviceKey, unRegErr)
}
}()
return fmt.Errorf("register timed out for service: %s", serviceKey)
}
}
// Notify monitor changes from registry,and update the cacheServices
func (dir *RegistryDirectory) Notify(event *registry.ServiceEvent) {
if event == nil {
return
}
start := time.Now()
dir.refreshInvokers(event)
metrics.Publish(metricsRegistry.NewNotifyEvent(start))
}
// NotifyAll notify the events that are complete Service Event List.
// After notify the address, the callback func will be invoked.
func (dir *RegistryDirectory) NotifyAll(events []*registry.ServiceEvent, callback func()) {
dir.refreshAllInvokers(events, callback)
}
// refreshInvokers refreshes service's events.
func (dir *RegistryDirectory) refreshInvokers(event *registry.ServiceEvent) {
if event != nil {
logger.Debugf("[Registry][Directory] refresh invokers with %+v", event)
} else {
logger.Debug("[Registry][Directory] refresh invokers with nil")
}
var oldInvoker []protocolbase.Invoker
if event != nil {
oldInvoker, _ = dir.cacheInvokerByEvent(event)
}
dir.setNewInvokers()
for _, v := range oldInvoker {
if v != nil {
v.Destroy()
}
}
}
// refreshAllInvokers the argument is the complete list of the service events, we can safely assume any cached invoker
// not in the incoming list can be removed. The Action of serviceEvent should be EventTypeUpdate or EventTypeAdd.
func (dir *RegistryDirectory) refreshAllInvokers(events []*registry.ServiceEvent, callback func()) {
var (
oldInvokers []protocolbase.Invoker
incomingProviders []*registry.ServiceEvent
providerEvents []*registry.ServiceEvent
configuratorURLs []*common.URL
)
for _, event := range events {
if event.Action != remoting.EventTypeUpdate && event.Action != remoting.EventTypeAdd {
panic("Your implements of register center is wrong, " +
"please check the Action of ServiceEvent should be EventTypeUpdate")
}
if isConfiguratorURL(event.Service) {
configuratorURLs = append(configuratorURLs, event.Service)
continue
}
incomingProviders = append(incomingProviders, event)
}
if configuratorURLs != nil {
dir.replaceConfigurators(configuratorURLs)
}
defer callback()
if len(incomingProviders) > 0 {
providerEvents = incomingProviders
dir.cacheProviderEvents(providerEvents)
} else if len(events) > 0 {
providerEvents = dir.cachedProviderEventsSnapshot()
} else {
dir.cacheProviderEvents(nil)
}
if len(providerEvents) == 0 && len(events) > 0 {
return
}
dir.overrideUrl(dir.GetDirectoryUrl())
referenceUrl := dir.GetDirectoryUrl().SubURL
// loop the events to check the Action should be EventTypeUpdate.
for _, event := range providerEvents {
// Originally it will Merge URL many times, now we just execute once.
// MergeURL is executed once and put the result into Event. After this, the key will get from Event.Key().
newUrl := dir.convertUrl(event)
newUrl = newUrl.MergeURL(referenceUrl)
dir.overrideUrl(newUrl)
event.Update(newUrl)
}
func() {
// this lock is work at batch update of InvokeCache
dir.registerLock.Lock()
defer dir.registerLock.Unlock()
// get need clear invokers from original invoker list
dir.cacheInvokersMap.Range(func(k, v any) bool {
key, ok := k.(string)
if !ok {
logger.Errorf("[Registry][Directory] cached invoker has unexpected key type %T", k)
dir.cacheInvokersMap.Delete(k)
return true
}
if !dir.eventMatched(key, providerEvents) {
// delete unused invoker from cache
if invoker := dir.uncacheInvokerWithKey(key); invoker != nil {
oldInvokers = append(oldInvokers, invoker)
}
}
return true
})
// loop the serviceEvents
for _, event := range providerEvents {
logger.Debugf("[Registry][Directory] registry changed, result{%s}", event)
if event != nil && event.Service != nil {
logger.Infof("[Registry][Directory] selector add service url{%s}", event.Service.String())
}
if event != nil && event.Service != nil && constant.RouterProtocol == event.Service.Protocol {
dir.configRouters()
}
if oldInvoker, _ := dir.doCacheInvoker(event.Service, event); oldInvoker != nil {
oldInvokers = append(oldInvokers, oldInvoker)
}
}
}()
dir.setNewInvokers()
// destroy unused invokers
for _, invoker := range oldInvokers {
go invoker.Destroy()
}
}
// eventMatched checks if a cached invoker appears in the incoming invoker list, if no, then it is safe to remove.
func (dir *RegistryDirectory) eventMatched(key string, events []*registry.ServiceEvent) bool {
for _, event := range events {
if dir.invokerCacheKey(event) == key {
return true
}
}
return false
}
// invokerCacheKey generates the key in the cache for a given ServiceEvent.
func (dir *RegistryDirectory) invokerCacheKey(event *registry.ServiceEvent) string {
// If the url is merged, then return Event.Key() directly.
if event.Updated() {
return event.Key()
}
referenceUrl := dir.GetDirectoryUrl().SubURL
newUrl := event.Service.MergeURL(referenceUrl)
event.Update(newUrl)
return event.Key()
}
// setNewInvokers groups the invokers from the cache first, then set the result to both directory and router chain.
func (dir *RegistryDirectory) setNewInvokers() {
newInvokers := dir.toGroupInvokers()
dir.invokersLock.Lock()
defer dir.invokersLock.Unlock()
dir.cacheInvokers = newInvokers
dir.RouterChain().SetInvokers(newInvokers)
}
// cacheInvokerByEvent caches invokers from the service event
func (dir *RegistryDirectory) cacheInvokerByEvent(event *registry.ServiceEvent) ([]protocolbase.Invoker, error) {
// judge is override or others
if event != nil {
switch event.Action {
case remoting.EventTypeAdd, remoting.EventTypeUpdate:
u := dir.convertUrl(event)
if u == nil && dir.cacheOriginUrl == nil {
return nil, nil
}
logger.Infof("[Registry][Directory] selector add service url{%s}", event.Service)
if u != nil && constant.RouterProtocol == u.Protocol {
dir.configRouters()
}
return []protocolbase.Invoker{dir.cacheInvoker(u, event)}, nil
case remoting.EventTypeDel:
logger.Infof("[Registry][Directory] selector delete service url{%s}", event.Service)
return dir.uncacheInvoker(event), nil
default:
return nil, fmt.Errorf("illegal event type: %v", event.Action)
}
}
return nil, nil
}
// configRouters configures dynamic routers into the router chain, but, the current impl is incorrect, see FIXME above.
func (dir *RegistryDirectory) configRouters() {
}
// convertUrl processes override:// and router://
func (dir *RegistryDirectory) convertUrl(res *registry.ServiceEvent) *common.URL {
ret := res.Service
if ret.Protocol == constant.OverrideProtocol || // 1.for override url in 2.6.x
ret.GetParam(constant.CategoryKey, constant.DefaultCategory) == constant.ConfiguratorsCategory {
dir.appendConfigurator(extension.GetDefaultConfigurator(ret))
ret = nil
} else if ret.Protocol == constant.RouterProtocol || // 2.for router
ret.GetParam(constant.CategoryKey, constant.DefaultCategory) == constant.RouterCategory {
ret = nil
}
return ret
}
func isConfiguratorURL(url *common.URL) bool {
return url != nil && (url.Protocol == constant.OverrideProtocol ||
url.GetParam(constant.CategoryKey, constant.DefaultCategory) == constant.ConfiguratorsCategory)
}
func (dir *RegistryDirectory) replaceConfigurators(urls []*common.URL) {
dir.configuratorsLock.Lock()
defer dir.configuratorsLock.Unlock()
dir.configurators = registry.ToConfigurators(urls, extension.GetDefaultConfiguratorFunc())
}
func (dir *RegistryDirectory) cacheProviderEvents(events []*registry.ServiceEvent) {
dir.registerLock.Lock()
defer dir.registerLock.Unlock()
dir.cachedProviderEvents = cloneServiceEvents(events)
}
func (dir *RegistryDirectory) cachedProviderEventsSnapshot() []*registry.ServiceEvent {
dir.registerLock.Lock()
defer dir.registerLock.Unlock()
return cloneServiceEvents(dir.cachedProviderEvents)
}
func cloneServiceEvents(events []*registry.ServiceEvent) []*registry.ServiceEvent {
if len(events) == 0 {
return nil
}
cloned := make([]*registry.ServiceEvent, 0, len(events))
for _, event := range events {
if event == nil {
continue
}
clonedEvent := ®istry.ServiceEvent{
Action: event.Action,
KeyFunc: event.KeyFunc,
}
if event.Service != nil {
clonedEvent.Service = event.Service.Clone()
}
cloned = append(cloned, clonedEvent)
}
return cloned
}
func (dir *RegistryDirectory) toGroupInvokers() []protocolbase.Invoker {
groupInvokersMap := dir.groupInvokersFromCache()
if len(groupInvokersMap) == 1 {
for _, invokers := range groupInvokersMap {
return invokers
}
}
return dir.joinGroupInvokers(groupInvokersMap)
}
func (dir *RegistryDirectory) groupInvokersFromCache() map[string][]protocolbase.Invoker {
groupInvokersMap := make(map[string][]protocolbase.Invoker)
dir.cacheInvokersMap.Range(func(key, value any) bool {
invoker, ok := cachedInvoker(key, value)
if !ok {
dir.cacheInvokersMap.Delete(key)
return true
}
group := invoker.GetURL().GetParam(constant.GroupKey, "")
groupInvokersMap[group] = append(groupInvokersMap[group], invoker)
return true
})
return groupInvokersMap
}
func (dir *RegistryDirectory) joinGroupInvokers(groupInvokersMap map[string][]protocolbase.Invoker) []protocolbase.Invoker {
groupInvokersList := make([]protocolbase.Invoker, 0, len(groupInvokersMap))
for _, invokers := range groupInvokersMap {
staticDir := static.NewDirectory(invokers)
clusterKey := dir.GetURL().SubURL.GetParam(constant.ClusterKey, constant.DefaultCluster)
cluster, err := extension.GetCluster(clusterKey)
if err != nil {
logger.Errorf("[Registry][Directory] directory get cluster %s error, err=%w, will skip this group",
clusterKey, err)
continue
}
if cluster == nil {
logger.Errorf("[Registry][Directory] directory cluster is nil for key %s, will skip this group", clusterKey)
continue
}
if err = staticDir.BuildRouterChain(invokers, dir.GetURL()); err != nil {
logger.Errorf("[Registry][Directory] buildRouterChain error, err=%v", err)
continue
}
groupInvokersList = append(groupInvokersList, cluster.Join(staticDir))
}
return groupInvokersList
}
func (dir *RegistryDirectory) uncacheInvokerWithClusterID(clusterID string) []protocolbase.Invoker {
logger.Debugf("[Registry][Directory] all service will be deleted in cache invokers with clusterID=%s", clusterID)
invokerKeys := make([]string, 0)
dir.cacheInvokersMap.Range(func(key, cacheInvoker any) bool {
invoker, ok := cachedInvoker(key, cacheInvoker)
if !ok {
dir.cacheInvokersMap.Delete(key)
return true
}
keyString, ok := key.(string)
if !ok {
logger.Errorf("[Registry][Directory] cached invoker has unexpected key type %T", key)
dir.cacheInvokersMap.Delete(key)
return true
}
if invoker.GetURL().GetParam(constant.MeshClusterIDKey, "") == clusterID {
invokerKeys = append(invokerKeys, keyString)
}
return true
})
uncachedInvokers := make([]protocolbase.Invoker, 0)
for _, v := range invokerKeys {
uncachedInvokers = append(uncachedInvokers, dir.uncacheInvokerWithKey(v))
}
return uncachedInvokers
}
// uncacheInvoker will return abandoned Invoker, if no Invoker to be abandoned, return nil
func (dir *RegistryDirectory) uncacheInvoker(event *registry.ServiceEvent) []protocolbase.Invoker {
defer metrics.Publish(metricsRegistry.NewDirectoryEvent(metricsRegistry.NumDisableTotal))
dir.clearClosingTombstone(event.Key())
if clusterID := event.Service.GetParam(constant.MeshClusterIDKey, ""); event.Service.Location == constant.MeshAnyAddrMatcher && clusterID != "" {
dir.uncacheInvokerWithClusterID(clusterID)
}
return []protocolbase.Invoker{dir.uncacheInvokerWithKey(event.Key())}
}
func (dir *RegistryDirectory) uncacheInvokerWithKey(key string) protocolbase.Invoker {
logger.Debugf("[Registry][Directory] service will be deleted in cache invokers, key=%s", key)
protocolbase.RemoveUrlKeyUnhealthyStatus(key)
if cacheInvoker, ok := dir.cacheInvokersMap.Load(key); ok {
dir.cacheInvokersMap.Delete(key)
invoker, valid := cachedInvoker(key, cacheInvoker)
if !valid {
return nil
}
return invoker
}
return nil
}
// RemoveClosingInstance removes a single service instance from the directory by instanceKey.
// It is intended to be called by graceful shutdown logic before registry updates converge.
func (dir *RegistryDirectory) RemoveClosingInstance(instanceKey string) bool {
if instanceKey == "" {
return false
}
var removed protocolbase.Invoker
func() {
dir.registerLock.Lock()
defer dir.registerLock.Unlock()
if cacheInvoker, ok := dir.cacheInvokersMap.Load(instanceKey); ok {
removed, _ = cachedInvoker(instanceKey, cacheInvoker)
}
dir.markClosingTombstone(instanceKey, removed, "closing-event")
removed = dir.uncacheInvokerWithKey(instanceKey)
if removed != nil {
dir.setNewInvokers()
}
}()
if removed != nil {
removed.Destroy()
return true
}
return false
}
func (dir *RegistryDirectory) markClosingTombstone(instanceKey string, invoker protocolbase.Invoker, source string) {
if instanceKey == "" {
return
}
tombstone := closingTombstone{
InstanceKey: instanceKey,
Source: source,
ExpireAt: time.Now().Add(dir.closingTombstoneTTL),
}
if invoker != nil && invoker.GetURL() != nil {
tombstone.ServiceKey = invoker.GetURL().ServiceKey()
tombstone.Address = invoker.GetURL().Location
tombstone.Timestamp = invoker.GetURL().GetParam(constant.TimestampKey, "")
}
dir.closingTombstones.Store(instanceKey, tombstone)
}
func (dir *RegistryDirectory) activeClosingTombstone(instanceKey string) (closingTombstone, bool) {
if instanceKey == "" {
return closingTombstone{}, false
}
tombstoneValue, ok := dir.closingTombstones.Load(instanceKey)
if !ok {
return closingTombstone{}, false
}
tombstone := tombstoneValue.(closingTombstone)
if time.Now().After(tombstone.ExpireAt) {
dir.closingTombstones.Delete(instanceKey)
return closingTombstone{}, false
}
return tombstone, true
}
func (dir *RegistryDirectory) clearClosingTombstone(instanceKey string) {
if instanceKey == "" {
return
}
dir.closingTombstones.Delete(instanceKey)
}
func (dir *RegistryDirectory) cleanupExpiredClosingTombstones() {
now := time.Now()
dir.closingTombstones.Range(func(key, value any) bool {
tombstone := value.(closingTombstone)
if now.After(tombstone.ExpireAt) {
dir.closingTombstones.Delete(key)
}
return true
})
}
// cacheInvoker will return abandoned Invoker,if no Invoker to be abandoned,return nil
func (dir *RegistryDirectory) cacheInvoker(url *common.URL, event *registry.ServiceEvent) protocolbase.Invoker {
dir.overrideUrl(dir.GetDirectoryUrl())
referenceUrl := dir.GetDirectoryUrl().SubURL
if url == nil && dir.cacheOriginUrl != nil {
url = dir.cacheOriginUrl
} else {
dir.cacheOriginUrl = url
}
if url == nil {
logger.Error("[Registry][Directory] url is nil, pls check if service url is subscribe successfully")
return nil
}
// check the url's protocol is equal to the protocol which is configured in reference config or referenceUrl is not care about protocol
if url.Protocol == referenceUrl.Protocol || referenceUrl.Protocol == "" {
newUrl := url.MergeURL(referenceUrl)
dir.overrideUrl(newUrl)
event.Update(newUrl)
if v, ok := dir.doCacheInvoker(newUrl, event); ok {
return v
}
}
return nil
}
func (dir *RegistryDirectory) doCacheInvoker(newUrl *common.URL, event *registry.ServiceEvent) (protocolbase.Invoker, bool) {
key := event.Key()
dir.cleanupExpiredClosingTombstones()
if tombstone, ok := dir.activeClosingTombstone(key); ok {
// A tombstone guards against re-adding an instance from a stale
// pre-shutdown registry snapshot. If the re-add carries a different
// export timestamp, the instance has genuinely restarted with the same
// address: vetoing it would keep the directory empty until the next
// registry event, which may never come.
newTimestamp := newUrl.GetParam(constant.TimestampKey, "")
if tombstone.Timestamp == "" || newTimestamp == "" || newTimestamp == tombstone.Timestamp {
logger.Infof("[Registry][Directory] skip rebuilding closing instance due to tombstone, instance key: %s", key)
return nil, true
}
logger.Infof("[Registry][Directory] instance %s restarted with a new export timestamp, clearing closing tombstone", key)
dir.clearClosingTombstone(key)
}
cacheInvoker, ok := dir.cacheInvokersMap.Load(key)
var existingInvoker protocolbase.Invoker
if ok {
existingInvoker, ok = cachedInvoker(key, cacheInvoker)
if !ok {
dir.cacheInvokersMap.Delete(key)
}
}
if !ok {
logger.Debugf("[Registry][Directory] service will be added in cache invokers, url=%s", newUrl)
newInvoker := extension.GetProtocol(protocolwrapper.FILTER).Refer(newUrl)
if newInvoker != nil {
dir.cacheInvokersMap.Store(key, newInvoker)
} else {
logger.Warnf("[Registry][Directory] service will be added in cache invokers fail, result is null, url=%s", newUrl.String())
}
} else {
metrics.Publish(metricsRegistry.NewDirectoryEvent(metricsRegistry.NumValidTotal))
// if cached invoker has the same URL with the new URL, then no need to re-refer, and no need to destroy
// the old invoker.
if common.GetCompareURLEqualFunc()(newUrl, existingInvoker.GetURL()) {
return nil, true
}
logger.Debugf("[Registry][Directory] service will be updated in cache invokers, newUrl=%s oldUrl=%s", newUrl, existingInvoker.GetURL())
newInvoker := extension.GetProtocol(protocolwrapper.FILTER).Refer(newUrl)
if newInvoker != nil {
dir.cacheInvokersMap.Store(key, newInvoker)
return existingInvoker, true
} else {
logger.Warnf("[Registry][Directory] service will be updated in cache invokers fail, result is null, url=%s", newUrl.String())
}
}
return nil, false
}
func cachedInvoker(key, value any) (protocolbase.Invoker, bool) {
invoker, ok := value.(protocolbase.Invoker)
if !ok || isNilInvoker(invoker) {
logger.Errorf("[Registry][Directory] cached invoker has unexpected type %T for key %v", value, key)
return nil, false
}
return invoker, true
}
func isNilInvoker(invoker protocolbase.Invoker) bool {
if invoker == nil {
return true
}
value := reflect.ValueOf(invoker)
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
return value.IsNil()
default:
return false
}
}
// List selected protocol invokers from the directory
func (dir *RegistryDirectory) List(invocation protocolbase.Invocation) []protocolbase.Invoker {
routerChain := dir.RouterChain()
if routerChain == nil {
return dir.snapshotCacheInvokers()
}
return routerChain.Route(dir.consumerURL, invocation)
}
// IsAvailable whether the directory is available
func (dir *RegistryDirectory) IsAvailable() bool {
if dir.IsDestroyed() {
return false
}
for _, ivk := range dir.snapshotCacheInvokers() {
if ivk.IsAvailable() {
return true
}
}
metrics.Publish(metricsRegistry.NewDirectoryEvent(metricsRegistry.NumToReconnectTotal))
return false
}
// Destroy method
func (dir *RegistryDirectory) Destroy() {
// TODO:unregister & unsubscribe
dir.DoDestroy(func() {
graceful_shutdown.DefaultClosingDirectoryRegistry().Unregister(dir.closingServiceKey(), dir)
registeredURL, subscribedURL := dir.snapshotRegistryURLs()
if registeredURL != nil {
err := dir.registry.UnRegister(registeredURL)
if err != nil {
logger.Warnf("[Registry][Directory] unregister consumer url failed, url=%s err=%v", registeredURL.String(), err)
}
}
if subscribedURL != nil {
err := dir.registry.UnSubscribe(subscribedURL, dir)
if err != nil {
logger.Warnf("[Registry][Directory] unsubscribe consumer url failed, url=%s err=%v", subscribedURL.String(), err)
}
}
invokers := dir.swapCacheInvokers()
for _, ivk := range invokers {
ivk.Destroy()
}
dir.cacheInvokersMap.Range(func(key, value any) bool {
dir.cacheInvokersMap.Delete(key)
return true
})
dir.registerLock.Lock()
dir.cachedProviderEvents = nil
dir.registerLock.Unlock()
})
metrics.Publish(metricsRegistry.NewDirectoryEvent(metricsRegistry.NumAllDec))
}
func (dir *RegistryDirectory) closingServiceKey() string {
if dir.GetURL() == nil {
return ""
}
serviceKey := dir.GetURL().ServiceKey()
if serviceKey == "" && dir.GetURL().SubURL != nil {
serviceKey = dir.GetURL().SubURL.ServiceKey()
}
return serviceKey
}
func (dir *RegistryDirectory) overrideUrl(targetUrl *common.URL) {
// Use a read-only snapshot to avoid sharing mutable configurator slice during overrides.
doOverrideUrl(dir.snapshotConfigurators(), targetUrl)
doOverrideUrl(dir.consumerConfigurationListener.Configurators(), targetUrl)
doOverrideUrl(dir.referenceConfigurationListener.Configurators(), targetUrl)
}
func (dir *RegistryDirectory) snapshotCacheInvokers() []protocolbase.Invoker {
dir.invokersLock.RLock()
defer dir.invokersLock.RUnlock()
invokers := make([]protocolbase.Invoker, len(dir.cacheInvokers))
copy(invokers, dir.cacheInvokers)
return invokers
}
func (dir *RegistryDirectory) swapCacheInvokers() []protocolbase.Invoker {
dir.invokersLock.Lock()
defer dir.invokersLock.Unlock()
invokers := make([]protocolbase.Invoker, len(dir.cacheInvokers))
copy(invokers, dir.cacheInvokers)
dir.cacheInvokers = []protocolbase.Invoker{}
return invokers
}
func (dir *RegistryDirectory) appendConfigurator(configurator config_center.Configurator) {
dir.configuratorsLock.Lock()
defer dir.configuratorsLock.Unlock()
dir.configurators = append(dir.configurators, configurator)
}
func (dir *RegistryDirectory) snapshotConfigurators() []config_center.Configurator {
dir.configuratorsLock.RLock()
defer dir.configuratorsLock.RUnlock()
configurators := make([]config_center.Configurator, len(dir.configurators))
copy(configurators, dir.configurators)
return configurators
}
func (dir *RegistryDirectory) setSubscribedURL(url *common.URL) {
dir.subscribedUrlLock.Lock()
defer dir.subscribedUrlLock.Unlock()
dir.SubscribedUrl = url
}
func (dir *RegistryDirectory) snapshotRegistryURLs() (registeredURL *common.URL, subscribedURL *common.URL) {
dir.subscribedUrlLock.RLock()
defer dir.subscribedUrlLock.RUnlock()
return dir.RegisteredUrl, dir.SubscribedUrl
}
func (dir *RegistryDirectory) getConsumerUrl(c *common.URL) *common.URL {
processID := fmt.Sprintf("%d", os.Getpid())
localIP := common.GetLocalIp()
params := url.Values{}
c.RangeParams(func(key, value string) bool {
params.Add(key, value)
return true
})
params.Add("pid", processID)
params.Add("ip", localIP)
params.Add("protocol", c.Protocol)
return common.NewURLWithOptions(common.WithProtocol("consumer"), common.WithIp(localIP), common.WithPath(c.Path),
common.WithParams(params))
}
func doOverrideUrl(configurators []config_center.Configurator, targetUrl *common.URL) {
for _, v := range configurators {
v.Configure(targetUrl)
}
}
type referenceConfigurationListener struct {
registry.BaseConfigurationListener
directory *RegistryDirectory
url *common.URL
}
func newReferenceConfigurationListener(dir *RegistryDirectory, url *common.URL) *referenceConfigurationListener {
listener := &referenceConfigurationListener{directory: dir, url: url}
listener.InitWith(
url.ColonSeparatedKey()+constant.ConfiguratorSuffix,
listener,
extension.GetDefaultConfiguratorFunc(),
)
return listener
}
// Process handle events and update Invokers
func (l *referenceConfigurationListener) Process(event *config_center.ConfigChangeEvent) {
l.BaseConfigurationListener.Process(event)
// FIXME: this doesn't trigger dir.overrideUrl()
l.directory.refreshInvokers(nil)
}
type consumerConfigurationListener struct {
registry.BaseConfigurationListener
listeners []registry.NotifyListener
directory *RegistryDirectory
}
func newConsumerConfigurationListener(dir *RegistryDirectory, url *common.URL) *consumerConfigurationListener {
listener := &consumerConfigurationListener{directory: dir}
application := commonConfig.EnsureApplicationAttribute(url, url.SubURL)
listener.InitWith(
application.Name+constant.ConfiguratorSuffix,
listener,
extension.GetDefaultConfiguratorFunc(),
)
return listener
}
func (l *consumerConfigurationListener) addNotifyListener(listener registry.NotifyListener) {
l.listeners = append(l.listeners, listener)
}
// Process handles events from Configuration Center and update Invokers
func (l *consumerConfigurationListener) Process(event *config_center.ConfigChangeEvent) {
l.BaseConfigurationListener.Process(event)
// FIXME: this doesn't trigger dir.overrideUrl()
l.directory.refreshInvokers(nil)
}
// ServiceDiscoveryRegistryDirectory implementation of Directory:
// Invoker list returned from this Directory's list method have been filtered by Routers
type ServiceDiscoveryRegistryDirectory struct {
*base.Directory
*RegistryDirectory
}
// NewServiceDiscoveryRegistryDirectory will create a new ServiceDiscoveryRegistryDirectory
func NewServiceDiscoveryRegistryDirectory(url *common.URL, registry registry.Registry) (directory.Directory, error) {
dic, err := NewRegistryDirectory(url, registry)
registryDirectory, _ := dic.(*RegistryDirectory)