huynhhongan2005 commented on code in PR #3827:
URL: https://github.com/apache/dubbo-go/pull/3827#discussion_r4582991713


##########
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:
   Good catch! Instead of filtering `keep` with `slices.Contains`, I introduced 
`serviceLifecycleMu` to serialize `Register`, `RegisterService`, and 
`unregisterService` at the lifecycle level. Now new registrations cannot 
interleave mid-unregistration, and `keep` accurately retains only instances 
that failed to unregister, keeping `MetadataInfo` and `Revision` consistent.



##########
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:
   Thanks for pointing this out. By serializing `RegisterService` and 
`unregisterService` with `serviceLifecycleMu`, an in-flight unregistration 
completes both locally and on the backend before a new instance at the same 
address can be registered, preventing the old unregister from deleting the 
newly registered backend entry.



-- 
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]

Reply via email to