Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
10 changes: 9 additions & 1 deletion metadata/info/metadata_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package info

import (
"net/url"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -113,7 +114,7 @@ func addUrl(m map[string][]*common.URL, url *common.URL) {
func removeUrl(m map[string][]*common.URL, url *common.URL) {
if urls, ok := m[url.ServiceKey()]; ok {
for i, u := range urls {
if u == url {
if urlEqual(u, url) {
m[url.ServiceKey()] = deleteItem(urls, i)
break
}
Expand All @@ -124,6 +125,13 @@ func removeUrl(m map[string][]*common.URL, url *common.URL) {
}
}

func urlEqual(left, right *common.URL) bool {
if left == nil || right == nil {
return left == right
}
return left.String() == right.String() && slices.Equal(left.Methods, right.Methods)
}
Comment thread
Alanxtl marked this conversation as resolved.
Resolved

func deleteItem(slice []*common.URL, index int) []*common.URL {
copy(slice[index:], slice[index+1:])
slice = slice[:len(slice)-1]
Expand Down
19 changes: 19 additions & 0 deletions metadata/info/metadata_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ func TestMetadataInfoAddService(t *testing.T) {
assert.Empty(t, metadataInfo.GetExportedServiceURLs())
}

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

metadataInfo.AddService(url)
metadataInfo.RemoveService(url.Clone())

assert.Empty(t, metadataInfo.Services)
assert.Empty(t, metadataInfo.GetExportedServiceURLs())
}

func TestHessian(t *testing.T) {
metadataInfo := &MetadataInfo{
App: "test",
Expand Down Expand Up @@ -93,6 +105,13 @@ func TestMetadataInfoAddSubscribeURL(t *testing.T) {
assert.Empty(t, info.GetSubscribedURLs())
}

func TestMetadataInfoRemoveSubscribeURLWithClonedURL(t *testing.T) {
info := NewMetadataInfo("dubbo", "tag")
info.AddSubscribeURL(serviceUrl)
info.RemoveSubscribeURL(serviceUrl.Clone())
assert.Empty(t, info.GetSubscribedURLs())
}

func TestNewMetadataInfo(t *testing.T) {
info := NewMetadataInfo("dubbo", "tag")
assert.Equal(t, "dubbo", info.App)
Expand Down
16 changes: 16 additions & 0 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ func AddSubscribeURL(registryId string, url *common.URL) {
}
registryMetadataInfo[registryId].AddSubscribeURL(url)
}

func RemoveService(registryId string, url *common.URL) {
metadataInfo, exist := registryMetadataInfo[registryId]
if !exist {
return
}
metadataInfo.RemoveService(url)
}

func RemoveSubscribeURL(registryId string, url *common.URL) {
metadataInfo, exist := registryMetadataInfo[registryId]
if !exist {
return
}
metadataInfo.RemoveSubscribeURL(url)
}
41 changes: 41 additions & 0 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,47 @@ func TestGetMetadataInfo(t *testing.T) {
}
}

func TestRemoveService(t *testing.T) {
registryID := "reg-remove-service"
url := common.NewURLWithOptions(
common.WithProtocol("dubbo"),
common.WithInterface("org.apache.dubbo.metadata.TestService"),
common.WithPath("org.apache.dubbo.metadata.TestService"),
common.WithMethods([]string{"Test"}),
common.WithPort("20880"),
common.WithParamsValue(constant.ApplicationKey, "dubbo"),
common.WithParamsValue(constant.ApplicationTagKey, "v1"),
)
defer delete(registryMetadataInfo, registryID)

AddService(registryID, url)
assert.Len(t, registryMetadataInfo[registryID].GetExportedServiceURLs(), 1)

RemoveService(registryID, url)
assert.Empty(t, registryMetadataInfo[registryID].GetExportedServiceURLs())
assert.Empty(t, registryMetadataInfo[registryID].Services)
}

func TestRemoveSubscribeURL(t *testing.T) {
registryID := "reg-remove-subscribe"
url := common.NewURLWithOptions(
common.WithProtocol("tri"),
common.WithInterface("org.apache.dubbo.metadata.TestService"),
common.WithPath("org.apache.dubbo.metadata.TestService"),
common.WithMethods([]string{"Test"}),
common.WithPort("20880"),
common.WithParamsValue(constant.ApplicationKey, "dubbo"),
common.WithParamsValue(constant.ApplicationTagKey, "v1"),
)
defer delete(registryMetadataInfo, registryID)

AddSubscribeURL(registryID, url)
assert.Len(t, registryMetadataInfo[registryID].GetSubscribedURLs(), 1)

RemoveSubscribeURL(registryID, url)
assert.Empty(t, registryMetadataInfo[registryID].GetSubscribedURLs())
}

func TestGetMetadataService(t *testing.T) {
tests := []struct {
name string
Expand Down
62 changes: 62 additions & 0 deletions registry/servicediscovery/service_discovery_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type serviceDiscoveryRegistry struct {
url *common.URL
serviceDiscovery registry.ServiceDiscovery
instances []registry.ServiceInstance
instanceURLs map[registry.ServiceInstance]*common.URL
serviceNameMapping mapping.ServiceNameMapping
metadataReport report.MetadataReport
serviceListeners map[string]registry.ServiceInstancesChangedListener
Expand All @@ -75,6 +76,7 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) {
return &serviceDiscoveryRegistry{
url: url,
serviceDiscovery: serviceDiscovery,
instanceURLs: make(map[registry.ServiceInstance]*common.URL),
serviceNameMapping: extension.GetGlobalServiceNameMapping(),
metadataReport: metadata.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")),
serviceListeners: make(map[string]registry.ServiceInstancesChangedListener),
Expand Down Expand Up @@ -107,6 +109,7 @@ func (s *serviceDiscoveryRegistry) RegisterService() error {
}
s.lock.Lock()
s.instances = append(s.instances, instance)
s.instanceURLs[instance] = url
s.lock.Unlock()
}
return nil
Expand Down Expand Up @@ -147,18 +150,33 @@ func (s *serviceDiscoveryRegistry) UnRegisterService() error {
s.lock.Unlock()

var errs []error
var removed []registry.ServiceInstance
var removedURLs []*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.RUnlock()
}

s.lock.Lock()
s.instances = keep
for _, instance := range removed {
delete(s.instanceURLs, instance)
}
s.lock.Unlock()
if err := s.syncExportedMetadataAfterUnregister(removedURLs, keep); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}

Expand Down Expand Up @@ -188,6 +206,50 @@ func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr
if err != nil {
return err
}
if id, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey); exist {
metadata.RemoveSubscribeURL(id, url)
}
return nil
}

func (s *serviceDiscoveryRegistry) syncExportedMetadataAfterUnregister(removedURLs []*common.URL, keep []registry.ServiceInstance) error {
if len(removedURLs) == 0 {
return nil
}
registryID, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey)
if !exist {
return nil
}
metadataInfo := metadata.GetMetadataInfo(registryID)
if metadataInfo == nil {
return nil
}
for _, removedURL := range removedURLs {
metadata.RemoveService(registryID, removedURL)
}
remainingURLs := metadataInfo.GetExportedServiceURLs()
if len(remainingURLs) == 0 {
metadataInfo.Revision = "0"
return nil
}
instance := createInstance(metadataInfo, remainingURLs[0])
revision := instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName]
metadataInfo.Revision = revision
if metadata.GetMetadataType() == constant.RemoteMetadataStorageType {
if s.metadataReport == nil {
return perrors.New("can not publish app metadata cause report instance not found")
}
if err := s.metadataReport.PublishAppMetadata(metadataInfo.App, revision, metadataInfo); err != nil {
return err
}
}
for _, keepInstance := range keep {
keepInstance.SetServiceMetadata(metadataInfo)
keepInstance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] = revision
if err := s.serviceDiscovery.Update(keepInstance); err != nil {
return perrors.WithMessage(err, "Update service failed")
}
}
return nil
}

Expand Down
Loading
Loading