Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: reconcile metadata after unregister and unsubscribe
  • Loading branch information
AsperforMias committed May 31, 2026
commit 891fbd4ca3c611507b7010af5ed54c893e664eb5
25 changes: 24 additions & 1 deletion metadata/info/metadata_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ func deleteItem(slice []*common.URL, index int) []*common.URL {

func (info *MetadataInfo) RemoveService(url *common.URL) {
service := NewServiceInfoWithURL(url)
delete(info.Services, service.GetMatchKey())
removeUrl(info.exportedServiceURLs, url)
if replacement := info.findExportedServiceURL(service.GetMatchKey()); replacement != nil {
info.Services[service.GetMatchKey()] = NewServiceInfoWithURL(replacement)
return
}
delete(info.Services, service.GetMatchKey())
}

// AddSubscribeURL client subscribe a service url
Expand Down Expand Up @@ -170,6 +174,25 @@ func (info *MetadataInfo) GetSubscribedURLs() []*common.URL {
return res
}

func (info *MetadataInfo) ReplaceExportedServices(urls []*common.URL) {
info.Services = make(map[string]*ServiceInfo)
info.exportedServiceURLs = make(map[string][]*common.URL)
for _, serviceURL := range urls {
info.AddService(serviceURL)
}
}

func (info *MetadataInfo) findExportedServiceURL(matchKey string) *common.URL {
for _, urls := range info.exportedServiceURLs {
for _, serviceURL := range urls {
if NewServiceInfoWithURL(serviceURL).GetMatchKey() == matchKey {
return serviceURL
}
}
}
return nil
}

// ServiceInfo the information of service
type ServiceInfo struct {
Name string `json:"name,omitempty" hessian:"name"`
Expand Down
16 changes: 16 additions & 0 deletions metadata/info/metadata_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func TestMetadataInfoRemoveServiceWithClonedURL(t *testing.T) {
assert.Empty(t, metadataInfo.GetExportedServiceURLs())
}

func TestMetadataInfoRemoveServiceKeepsRemainingMatchKeyService(t *testing.T) {
metadataInfo := NewMetadataInfo("foo", "")
url1, err := common.NewURL("dubbo://127.0.0.1:20000?application=foo&interface=com.foo.Bar&methods=GetPetByID&side=provider&version=1.0.0")
require.NoError(t, err)
url2, err := common.NewURL("dubbo://127.0.0.1:20001?application=foo&interface=com.foo.Bar&methods=GetPetByID&side=provider&version=1.0.0")
require.NoError(t, err)

metadataInfo.AddService(url1)
metadataInfo.AddService(url2)
metadataInfo.RemoveService(url1.Clone())

require.Len(t, metadataInfo.Services, 1)
assert.Len(t, metadataInfo.GetExportedServiceURLs(), 1)
assert.Equal(t, url2, metadataInfo.GetExportedServiceURLs()[0])
}

func TestHessian(t *testing.T) {
metadataInfo := &MetadataInfo{
App: "test",
Expand Down
55 changes: 35 additions & 20 deletions registry/servicediscovery/service_discovery_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,37 +144,36 @@ func createInstance(meta *info.MetadataInfo, url *common.URL) registry.ServiceIn
}

func (s *serviceDiscoveryRegistry) UnRegisterService() error {
return s.unregisterService(nil)
}

func (s *serviceDiscoveryRegistry) unregisterService(targetURL *common.URL) error {
s.lock.Lock()
keep := s.instances[:0]
origin := s.instances[:]
s.lock.Unlock()

var errs []error
var removed []registry.ServiceInstance
var removedURLs []*common.URL
keepInstanceURLs := make(map[registry.ServiceInstance]*common.URL)

for _, v := range origin {
if err := s.serviceDiscovery.Unregister(v); err != nil {
// fail to unregister
keep = append(keep, v)
errs = append(errs, err)
continue
}
removed = append(removed, v)
s.lock.RLock()
if sourceURL, ok := s.instanceURLs[v]; ok {
removedURLs = append(removedURLs, sourceURL)
s.lock.RLock()
if sourceURL, ok := s.instanceURLs[v]; ok {
keepInstanceURLs[v] = sourceURL
}
s.lock.RUnlock()
}
s.lock.RUnlock()
}

s.lock.Lock()
s.instances = keep
for _, instance := range removed {
delete(s.instanceURLs, instance)
}
s.instanceURLs = keepInstanceURLs
s.lock.Unlock()
if err := s.syncExportedMetadataAfterUnregister(removedURLs, keep); err != nil {
if err := s.syncExportedMetadataAfterUnregister(targetURL, origin, keep); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
Expand All @@ -184,7 +183,7 @@ func (s *serviceDiscoveryRegistry) UnRegister(url *common.URL) error {
if !shouldRegister(url) {
return nil
}
return s.UnRegisterService()
return s.unregisterService(url)
}

func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registry.NotifyListener) error {
Expand Down Expand Up @@ -212,10 +211,7 @@ func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr
return nil
}

func (s *serviceDiscoveryRegistry) syncExportedMetadataAfterUnregister(removedURLs []*common.URL, keep []registry.ServiceInstance) error {
if len(removedURLs) == 0 {
return nil
}
func (s *serviceDiscoveryRegistry) syncExportedMetadataAfterUnregister(targetURL *common.URL, origin []registry.ServiceInstance, keep []registry.ServiceInstance) error {
registryID, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey)
if !exist {
return nil
Expand All @@ -224,8 +220,12 @@ func (s *serviceDiscoveryRegistry) syncExportedMetadataAfterUnregister(removedUR
if metadataInfo == nil {
return nil
}
for _, removedURL := range removedURLs {
metadata.RemoveService(registryID, removedURL)

keepURLs := s.getInstanceURLs(keep)
if len(origin) > 0 {
metadataInfo.ReplaceExportedServices(keepURLs)
} else if targetURL != nil {
metadata.RemoveService(registryID, targetURL)
}
remainingURLs := metadataInfo.GetExportedServiceURLs()
if len(remainingURLs) == 0 {
Expand All @@ -235,6 +235,9 @@ func (s *serviceDiscoveryRegistry) syncExportedMetadataAfterUnregister(removedUR
instance := createInstance(metadataInfo, remainingURLs[0])
revision := instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName]
metadataInfo.Revision = revision
if len(keepURLs) == 0 {
return nil
}
if metadata.GetMetadataType() == constant.RemoteMetadataStorageType {
if s.metadataReport == nil {
return perrors.New("can not publish app metadata cause report instance not found")
Expand All @@ -253,6 +256,18 @@ func (s *serviceDiscoveryRegistry) syncExportedMetadataAfterUnregister(removedUR
return nil
}

func (s *serviceDiscoveryRegistry) getInstanceURLs(instances []registry.ServiceInstance) []*common.URL {
urls := make([]*common.URL, 0, len(instances))
s.lock.RLock()
defer s.lock.RUnlock()
for _, instance := range instances {
if sourceURL, ok := s.instanceURLs[instance]; ok {
urls = append(urls, sourceURL)
}
}
return urls
}

func parseServices(literalServices string) *gxset.HashSet {
set := gxset.NewSet()
if len(literalServices) == 0 {
Expand Down
48 changes: 48 additions & 0 deletions registry/servicediscovery/service_discovery_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,54 @@ func TestServiceDiscoveryRegistryUnRegisterServicePartialFailSyncsMetadata(t *te
assert.Contains(t, mockSD.updatedIDs, providerURL1.Address())
}

func TestServiceDiscoveryRegistryUnRegisterWithoutTrackedInstancesReconcilesMetadata(t *testing.T) {
_, mockMapping := setupEnvironment(t)
regID := fmt.Sprintf("mock-reg-%d", time.Now().UnixNano())

registryURL, err := common.NewURL(testRegistryURL,
common.WithParamsValue(constant.RegistryKey, "mock"),
common.WithParamsValue(constant.RegistryIdKey, regID))
require.NoError(t, err)

reg, err := newServiceDiscoveryRegistry(registryURL)
require.NoError(t, err)

providerURL1, err := common.NewURL("dubbo://127.0.0.1:20880/org.apache.dubbo.test.TestService",
common.WithParamsValue(constant.ApplicationKey, testApp),
common.WithInterface(testInterface),
common.WithMethods([]string{"MethodA"}),
common.WithParamsValue(constant.SideKey, constant.SideProvider),
)
require.NoError(t, err)
providerURL2, err := common.NewURL("tri://127.0.0.1:20881/org.apache.dubbo.test.TestService",
common.WithParamsValue(constant.ApplicationKey, testApp),
common.WithInterface(testInterface),
common.WithMethods([]string{"MethodB"}),
common.WithParamsValue(constant.SideKey, constant.SideProvider),
)
require.NoError(t, err)

err = reg.Register(providerURL1)
require.NoError(t, err)
err = reg.Register(providerURL2)
require.NoError(t, err)
assert.True(t, mockMapping.mapCalled)

metaInfo := metadata.GetMetadataInfo(regID)
require.NotNil(t, metaInfo)
require.Len(t, metaInfo.GetExportedServiceURLs(), 2)

err = reg.UnRegister(providerURL1.Clone())
require.NoError(t, err)

require.Len(t, metaInfo.GetExportedServiceURLs(), 1)
assert.Equal(t, providerURL2, metaInfo.GetExportedServiceURLs()[0])
assert.Len(t, metaInfo.Services, 1)

expectedRevision := createInstance(metaInfo, providerURL2).GetMetadata()[constant.ExportedServicesRevisionPropertyName]
assert.Equal(t, expectedRevision, metaInfo.Revision)
}

// setupEnvironment initializes the test environment.
func setupEnvironment(_ *testing.T) (*mockServiceDiscovery, *mockServiceNameMapping) {
appConfig := global.DefaultApplicationConfig()
Expand Down
Loading