Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 33 additions & 2 deletions 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 All @@ -132,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 @@ -162,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
35 changes: 35 additions & 0 deletions metadata/info/metadata_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ 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 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 Expand Up @@ -93,6 +121,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
79 changes: 78 additions & 1 deletion 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 @@ -141,32 +144,46 @@ 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
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)
s.lock.RLock()
if sourceURL, ok := s.instanceURLs[v]; ok {
keepInstanceURLs[v] = sourceURL
}
s.lock.RUnlock()
}
}

s.lock.Lock()
s.instances = keep
s.instanceURLs = keepInstanceURLs
s.lock.Unlock()
if err := s.syncExportedMetadataAfterUnregister(targetURL, origin, keep); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}

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 All @@ -188,9 +205,69 @@ 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(targetURL *common.URL, origin []registry.ServiceInstance, keep []registry.ServiceInstance) error {
registryID, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey)
if !exist {
return nil
}
metadataInfo := metadata.GetMetadataInfo(registryID)
if metadataInfo == nil {
return nil
}

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 {
metadataInfo.Revision = "0"
return nil
}
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")
}
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
}

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