From 6e574cc9c1770a3862c289d4ea4da7d295cbae18 Mon Sep 17 00:00:00 2001 From: "vito.he" <hxmhlt@163.com> Date: Sun, 22 Mar 2020 15:44:37 +0800 Subject: [PATCH] Add:format dir --- common/extension/metadata_report_factory.go | 8 +- config/instance/metedata_report.go | 6 +- go.mod | 1 + go.sum | 2 + metadata/{ => exporter}/exporter.go | 2 +- metadata/{ => report}/report.go | 2 +- .../{ => report_factory}/report_factory.go | 7 +- metadata/service/inmemory/in_memory.go | 116 ++++++++++++++++++ metadata/{ => service}/service.go | 35 ++++-- 9 files changed, 159 insertions(+), 20 deletions(-) rename metadata/{ => exporter}/exporter.go (98%) rename metadata/{ => report}/report.go (98%) rename metadata/{ => report_factory}/report_factory.go (84%) create mode 100644 metadata/service/inmemory/in_memory.go rename metadata/{ => service}/service.go (56%) diff --git a/common/extension/metadata_report_factory.go b/common/extension/metadata_report_factory.go index 0ae0793bb..7a113c460 100644 --- a/common/extension/metadata_report_factory.go +++ b/common/extension/metadata_report_factory.go @@ -18,20 +18,20 @@ package extension import ( - "github.com/apache/dubbo-go/metadata" + "github.com/apache/dubbo-go/metadata/report_factory" ) var ( - metaDataReportFactories = make(map[string]func() metadata.MetadataReportFactory, 8) + metaDataReportFactories = make(map[string]func() report_factory.MetadataReportFactory, 8) ) // SetMetadataReportFactory ... -func SetMetadataReportFactory(name string, v func() metadata.MetadataReportFactory) { +func SetMetadataReportFactory(name string, v func() report_factory.MetadataReportFactory) { metaDataReportFactories[name] = v } // GetMetadataReportFactory ... -func GetMetadataReportFactory(name string) metadata.MetadataReportFactory { +func GetMetadataReportFactory(name string) report_factory.MetadataReportFactory { if metaDataReportFactories[name] == nil { panic("metadata report for " + name + " is not existing, make sure you have import the package.") } diff --git a/config/instance/metedata_report.go b/config/instance/metedata_report.go index cd54b0a79..6adc7e8a6 100644 --- a/config/instance/metedata_report.go +++ b/config/instance/metedata_report.go @@ -18,22 +18,22 @@ package instance import ( + "github.com/apache/dubbo-go/metadata/report" "sync" ) import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/metadata" ) var ( - instance metadata.MetadataReport + instance report.MetadataReport once sync.Once ) // GetMetadataReportInstance ... -func GetMetadataReportInstance(url *common.URL) metadata.MetadataReport { +func GetMetadataReportInstance(url *common.URL) report.MetadataReport { once.Do(func() { instance = extension.GetMetadataReportFactory(url.Protocol).CreateMetadataReport(url) }) diff --git a/go.mod b/go.mod index a4294a62d..5aaa1152b 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/dubbogo/getty v1.3.3 github.com/dubbogo/go-zookeeper v1.0.0 github.com/dubbogo/gost v1.7.0 + github.com/emirpasic/gods v1.12.0 github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/go-errors/errors v1.0.1 // indirect github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect diff --git a/go.sum b/go.sum index 6dc55c634..65aba5e0e 100644 --- a/go.sum +++ b/go.sum @@ -116,6 +116,8 @@ github.com/duosecurity/duo_api_golang v0.0.0-20190308151101-6c680f768e74 h1:2MIh github.com/duosecurity/duo_api_golang v0.0.0-20190308151101-6c680f768e74/go.mod h1:UqXY1lYT/ERa4OEAywUqdok1T4RCRdArkhic1Opuavo= github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0 h1:ZoRgc53qJCfSLimXqJDrmBhnt5GChDsExMCK7t48o0Y= github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= github.com/envoyproxy/go-control-plane v0.8.0 h1:uE6Fp4fOcAJdc1wTQXLJ+SYistkbG1dNoi6Zs1+Ybvk= github.com/envoyproxy/go-control-plane v0.8.0/go.mod h1:GSSbY9P1neVhdY7G4wu+IK1rk/dqhiCC/4ExuWJZVuk= github.com/envoyproxy/protoc-gen-validate v0.0.14 h1:YBW6/cKy9prEGRYLnaGa4IDhzxZhRCtKsax8srGKDnM= diff --git a/metadata/exporter.go b/metadata/exporter/exporter.go similarity index 98% rename from metadata/exporter.go rename to metadata/exporter/exporter.go index 5d47f8bd8..a6290c1ea 100644 --- a/metadata/exporter.go +++ b/metadata/exporter/exporter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package metadata +package exporter import ( "github.com/apache/dubbo-go/common" diff --git a/metadata/report.go b/metadata/report/report.go similarity index 98% rename from metadata/report.go rename to metadata/report/report.go index 3fcc71241..d3436101a 100644 --- a/metadata/report.go +++ b/metadata/report/report.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package metadata +package report import ( "github.com/apache/dubbo-go/common" diff --git a/metadata/report_factory.go b/metadata/report_factory/report_factory.go similarity index 84% rename from metadata/report_factory.go rename to metadata/report_factory/report_factory.go index 19b1004ee..f4497f415 100644 --- a/metadata/report_factory.go +++ b/metadata/report_factory/report_factory.go @@ -15,16 +15,17 @@ * limitations under the License. */ -package metadata +package report_factory import ( "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/metadata/report" ) var ( - MetadataReportInstance MetadataReport + MetadataReportInstance report.MetadataReport ) type MetadataReportFactory interface { - CreateMetadataReport(*common.URL) MetadataReport + CreateMetadataReport(*common.URL) report.MetadataReport } diff --git a/metadata/service/inmemory/in_memory.go b/metadata/service/inmemory/in_memory.go new file mode 100644 index 000000000..981af666e --- /dev/null +++ b/metadata/service/inmemory/in_memory.go @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package inmemory + +import ( + "sync" +) + +import ( + "github.com/emirpasic/gods/sets" + "github.com/emirpasic/gods/sets/treeset" +) + +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/metadata/service" +) + +// InMemoryMetadataService is store and query the metadata info in memory when each service registry +type MetadataService struct { + service.BaseMetadataService + exportedServiceURLs sync.Map + subscribedServiceURLs sync.Map +} + +// urlComparator: defined as utils.Comparator for treeset to compare the URL +func urlComparator(a, b interface{}) int { + url1 := a.(common.URL) + url2 := b.(common.URL) + switch { + case url1.String() > url2.String(): + return 1 + case url1.String() < url2.String(): + return -1 + default: + return 0 + } +} + +// addURL: add URL in memory +func addURL(targetMap sync.Map, url common.URL) bool { + var ( + urlSet interface{} + loaded bool + lock sync.RWMutex + ) + if urlSet, loaded = targetMap.LoadOrStore(url.ServiceKey(), treeset.NewWith(urlComparator)); loaded { + lock.RLock() + if urlSet.(treeset.Set).Contains(url) { + lock.RUnlock() + return false + } + lock.RUnlock() + } + lock.Lock() + urlSet.(treeset.Set).Add(url) + lock.Unlock() + return true +} + +// name... +func removeURL(targetMap sync.Map, url common.URL) string { + if value, loaded := targetMap.Load(url.ServiceKey()); loaded { + value.(treeset.Set).Remove(url) + if value.(treeset.Set).Empty() { + targetMap.Delete(url.ServiceKey()) + } + } +} + +// ExportURL: store the in memory treeset +func (mts *MetadataService) ExportURL(url common.URL) bool { + return addURL(mts.exportedServiceURLs, url) +} + +func (MetadataService) UnexportURL(url common.URL) bool { + panic("implement me") +} + +func (mts *MetadataService) SubscribeURL(url common.URL) bool { + return addURL(mts.subscribedServiceURLs, url) +} + +func (MetadataService) UnsubscribeURL(url common.URL) bool { + panic("implement me") +} + +func (MetadataService) PublishServiceDefinition(url common.URL) { + panic("implement me") +} + +func (MetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) sets.Set { + panic("implement me") +} + +func (MetadataService) GetServiceDefinition(interfaceName string, version string, group string) string { + panic("implement me") +} + +func (MetadataService) GetServiceDefinitionByServiceKey(serviceKey string) string { + panic("implement me") +} diff --git a/metadata/service.go b/metadata/service/service.go similarity index 56% rename from metadata/service.go rename to metadata/service/service.go index d85703c95..af819cba8 100644 --- a/metadata/service.go +++ b/metadata/service/service.go @@ -15,23 +15,42 @@ * limitations under the License. */ -package metadata +package service + +import ( + "github.com/emirpasic/gods/sets" +) import ( "github.com/apache/dubbo-go/common" - gxset "github.com/dubbogo/gost/container/set" + "github.com/apache/dubbo-go/config" ) +// Metadataservice is used to define meta data related behaviors type MetadataService interface { ServiceName() string - ExportURL(url *common.URL) bool - UnexportURL(url *common.URL) bool + ExportURL(url common.URL) bool + UnexportURL(url common.URL) bool RefreshMetadata(exportedRevision string, subscribedRevision string) bool - SubscribeURL(url *common.URL) bool - UnsubscribeURL(url *common.URL) bool - PublishServiceDefinition(url *common.URL) + SubscribeURL(url common.URL) bool + UnsubscribeURL(url common.URL) bool + PublishServiceDefinition(url common.URL) - GetExportedURLs(serviceInterface string, group string, version string, protocol string) gxset.HashSet + GetExportedURLs(serviceInterface string, group string, version string, protocol string) sets.Set GetServiceDefinition(interfaceName string, version string, group string) string GetServiceDefinitionByServiceKey(serviceKey string) string } + +// BaseMetadataService: is used for the common logic for struct who will implement interface MetadataService +type BaseMetadataService struct { +} + +// ServiceName: get the service's name in meta service , which is application name +func (mts *BaseMetadataService) ServiceName() string { + return config.GetApplicationConfig().Name +} + +// RefreshMetadata: used for event listener's calling, to refresh metadata +func (mts *BaseMetadataService) RefreshMetadata(exportedRevision string, subscribedRevision string) bool { + return true +} -- GitLab