Alanxtl commented on code in PR #3827:
URL: https://github.com/apache/dubbo-go/pull/3827#discussion_r4582564962
##########
registry/servicediscovery/service_discovery_registry.go:
##########
@@ -247,30 +248,36 @@ func (s *serviceDiscoveryRegistry) UnRegisterService()
error {
func (s *serviceDiscoveryRegistry) unregisterService(targetURL *common.URL)
error {
s.lock.Lock()
- keep := s.instances[:0]
- origin := s.instances[:]
+ origin := append([]registry.ServiceInstance(nil), s.instances...)
s.lock.Unlock()
var errs []error
- keepInstanceURLs := make(map[registry.ServiceInstance]*common.URL)
+ unregistered := make(map[registry.ServiceInstance]struct{}, len(origin))
for _, v := range origin {
if err := s.serviceDiscovery.Unregister(v); err != nil {
- // fail to unregister
- keep = append(keep, v)
errs = append(errs, err)
- s.lock.RLock()
- if sourceURL, ok := s.instanceURLs[v]; ok {
- keepInstanceURLs[v] = sourceURL
- }
- s.lock.RUnlock()
+ } else {
+ unregistered[v] = struct{}{}
}
}
s.lock.Lock()
- s.instances = keep
- s.instanceURLs = keepInstanceURLs
+ newInstances := make([]registry.ServiceInstance, 0, len(s.instances))
+ var keep []registry.ServiceInstance
+ for _, inst := range s.instances {
+ if _, ok := unregistered[inst]; ok {
+ delete(s.instanceURLs, inst)
+ } else {
+ newInstances = append(newInstances, inst)
+ if slices.Contains(origin, inst) {
Review Comment:
Instances registered after the snapshot are retained in newInstances but
excluded from keep. syncExportedMetadataAfterUnregister then replaces all
exported metadata with the URLs in keep. If all instances in the snapshot are
unregistered successfully, the metadata is cleared and the revision becomes
"0", even though a newly registered instance remains in the backend.
##########
registry/servicediscovery/service_discovery_registry.go:
##########
@@ -247,30 +248,36 @@ func (s *serviceDiscoveryRegistry) UnRegisterService()
error {
func (s *serviceDiscoveryRegistry) unregisterService(targetURL *common.URL)
error {
s.lock.Lock()
- keep := s.instances[:0]
- origin := s.instances[:]
+ origin := append([]registry.ServiceInstance(nil), s.instances...)
s.lock.Unlock()
var errs []error
- keepInstanceURLs := make(map[registry.ServiceInstance]*common.URL)
+ unregistered := make(map[registry.ServiceInstance]struct{}, len(origin))
for _, v := range origin {
if err := s.serviceDiscovery.Unregister(v); err != nil {
- // fail to unregister
- keep = append(keep, v)
errs = append(errs, err)
- s.lock.RLock()
- if sourceURL, ok := s.instanceURLs[v]; ok {
- keepInstanceURLs[v] = sourceURL
- }
- s.lock.RUnlock()
+ } else {
+ unregistered[v] = struct{}{}
}
}
s.lock.Lock()
- s.instances = keep
- s.instanceURLs = keepInstanceURLs
+ newInstances := make([]registry.ServiceInstance, 0, len(s.instances))
+ var keep []registry.ServiceInstance
+ for _, inst := range s.instances {
+ if _, ok := unregistered[inst]; ok {
Review Comment:
unregistered identifies instances by object identity, while registry
backends typically identify them by service name and address. If an old
instance A is being unregistered while RegisterService registers a new object
A′ at the same address, the old unregister can delete A′’s backend entry. This
code then retains A′ in s.instances because it is a different object. The local
state says the instance is registered, but the backend no longer has it.
##########
registry/servicediscovery/service_discovery_registry.go:
##########
@@ -247,30 +248,36 @@ func (s *serviceDiscoveryRegistry) UnRegisterService()
error {
func (s *serviceDiscoveryRegistry) unregisterService(targetURL *common.URL)
error {
s.lock.Lock()
- keep := s.instances[:0]
- origin := s.instances[:]
+ origin := append([]registry.ServiceInstance(nil), s.instances...)
s.lock.Unlock()
var errs []error
- keepInstanceURLs := make(map[registry.ServiceInstance]*common.URL)
+ unregistered := make(map[registry.ServiceInstance]struct{}, len(origin))
for _, v := range origin {
if err := s.serviceDiscovery.Unregister(v); err != nil {
- // fail to unregister
- keep = append(keep, v)
errs = append(errs, err)
- s.lock.RLock()
- if sourceURL, ok := s.instanceURLs[v]; ok {
- keepInstanceURLs[v] = sourceURL
- }
- s.lock.RUnlock()
+ } else {
+ unregistered[v] = struct{}{}
}
}
s.lock.Lock()
- s.instances = keep
- s.instanceURLs = keepInstanceURLs
+ newInstances := make([]registry.ServiceInstance, 0, len(s.instances))
+ var keep []registry.ServiceInstance
+ for _, inst := range s.instances {
+ if _, ok := unregistered[inst]; ok {
+ delete(s.instanceURLs, inst)
+ } else {
+ newInstances = append(newInstances, inst)
+ if slices.Contains(origin, inst) {
+ keep = append(keep, inst)
+ }
+ }
+ }
+ s.instances = newInstances
s.lock.Unlock()
+
if err := s.syncExportedMetadataAfterUnregister(targetURL, origin,
keep); err != nil {
Review Comment:
RegisterService and syncExportedMetadataAfterUnregister both write to the
same MetadataInfo.Revision without common synchronization. A test running the
two actual paths concurrently reported a write/write race under go test -race.
These writes predate this PR, so this is not a newly introduced regression, but
it remains relevant to the production lifecycle being examined here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]