diff --git a/go.mod b/go.mod index 337fa3996c35650952361a7c044d006025c9921c..fe1891ea6e70ee3f8c605243e4d464d4b22dc73f 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,10 @@ module github.com/apache/dubbo-go require ( - github.com/Workiva/go-datastructures v1.0.50 + github.com/Workiva/go-datastructures v1.0.52 github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190802083043-4cd0c391755e // indirect github.com/apache/dubbo-go-hessian2 v1.4.0 - github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 // indirect github.com/coreos/bbolt v1.3.3 // indirect github.com/coreos/etcd v3.3.13+incompatible @@ -17,7 +16,6 @@ require ( github.com/dubbogo/go-zookeeper v1.0.0 github.com/dubbogo/gost v1.8.0 github.com/emicklei/go-restful/v3 v3.0.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/go-resty/resty/v2 v2.1.0 diff --git a/go.sum b/go.sum index 2e628c263df67d333c4d7b2289f855385daf0e8f..73c3da87a0bb784ef17fed34687a024cb18b7f22 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,8 @@ github.com/SermoDigital/jose v0.0.0-20180104203859-803625baeddc h1:LkkwnbY+S8Wmw github.com/SermoDigital/jose v0.0.0-20180104203859-803625baeddc/go.mod h1:ARgCUhI1MHQH+ONky/PAtmVHQrP5JlGY0F3poXOp/fA= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= -github.com/Workiva/go-datastructures v1.0.50 h1:slDmfW6KCHcC7U+LP3DDBbm4fqTwZGn1beOFPfGaLvo= -github.com/Workiva/go-datastructures v1.0.50/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA= +github.com/Workiva/go-datastructures v1.0.52 h1:PLSK6pwn8mYdaoaCZEMsXBpBotr4HHn9abU0yMQt0NI= +github.com/Workiva/go-datastructures v1.0.52/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA= github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af h1:DBNMBMuMiWYu0b+8KMJuWmfCkcxl09JwdlqwDZZ6U14= github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af/go.mod h1:5Jv4cbFiHJMsVxt52+i0Ha45fjshj6wxYr1r19tB9bw= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 h1:rFw4nCn9iMW+Vajsk51NtYIcwSTkXr+JGrMd36kTDJw= @@ -122,8 +122,6 @@ github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633 h1:H2pdYOb3KQ1 github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/emicklei/go-restful/v3 v3.0.0 h1:Duxxa4x0WIHW3bYEDmoAPNjmy8Rbqn+utcF74dlF/G8= github.com/emicklei/go-restful/v3 v3.0.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= -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= @@ -384,8 +382,6 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/nacos-group/nacos-sdk-go v0.0.0-20190723125407-0242d42e3dbb h1:lbmvw8r9W55w+aQgWn35W1nuleRIECMoqUrmwAOAvoI= -github.com/nacos-group/nacos-sdk-go v0.0.0-20190723125407-0242d42e3dbb/go.mod h1:CEkSvEpoveoYjA81m4HNeYQ0sge0LFGKSEqO3JKHllo= github.com/nacos-group/nacos-sdk-go v0.0.0-20191128082542-fe1b325b125c h1:WoCa3AvgQMVKNs+RIFlWPRgY9QVJwUxJDrGxHs0fcRo= github.com/nacos-group/nacos-sdk-go v0.0.0-20191128082542-fe1b325b125c/go.mod h1:CEkSvEpoveoYjA81m4HNeYQ0sge0LFGKSEqO3JKHllo= github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2 h1:BQ1HW7hr4IVovMwWg0E0PYcyW8CzqDcVmaew9cujU4s= diff --git a/metadata/service/inmemory/service.go b/metadata/service/inmemory/service.go index 5c2ff2dc7390ce18ad6c9a58005552e9c870c3c5..c59949401f419b44ce155a914a7afff7c327a8fe 100644 --- a/metadata/service/inmemory/service.go +++ b/metadata/service/inmemory/service.go @@ -22,9 +22,8 @@ import ( ) import ( - "github.com/emirpasic/gods/sets" - "github.com/emirpasic/gods/sets/treeset" - "github.com/emirpasic/gods/utils" + cm "github.com/Workiva/go-datastructures/common" + "github.com/Workiva/go-datastructures/slice/skip" ) import ( @@ -54,14 +53,17 @@ func NewMetadataService() *MetadataService { } } -// urlComparator is defined as utils.Comparator for treeset to compare the URL -func urlComparator(a, b interface{}) int { - url1 := a.(*common.URL) - url2 := b.(*common.URL) +// comparator is defined as Comparator for skip list to compare the URL +type comparator common.URL + +// Compare is defined as Comparator for skip list to compare the URL +func (c comparator) Compare(comp cm.Comparator) int { + a := common.URL(c).String() + b := common.URL(comp.(comparator)).String() switch { - case url1.String() > url2.String(): + case a > b: return 1 - case url1.String() < url2.String(): + case a < b: return -1 default: return 0 @@ -75,9 +77,10 @@ func (mts *MetadataService) addURL(targetMap *sync.Map, url *common.URL) bool { loaded bool ) logger.Debug(url.ServiceKey()) - if urlSet, loaded = targetMap.LoadOrStore(url.ServiceKey(), treeset.NewWith(urlComparator)); loaded { + if urlSet, loaded = targetMap.LoadOrStore(url.ServiceKey(), skip.New(uint64(0))); loaded { mts.lock.RLock() - if urlSet.(*treeset.Set).Contains(url) { + wantedUrl := urlSet.(*skip.SkipList).Get(comparator(*url)) + if len(wantedUrl) > 0 && wantedUrl[0] != nil { mts.lock.RUnlock() return false } @@ -85,11 +88,12 @@ func (mts *MetadataService) addURL(targetMap *sync.Map, url *common.URL) bool { } mts.lock.Lock() //double chk - if urlSet.(*treeset.Set).Contains(url) { + wantedUrl := urlSet.(*skip.SkipList).Get(comparator(*url)) + if len(wantedUrl) > 0 && wantedUrl[0] != nil { mts.lock.Unlock() return false } - urlSet.(*treeset.Set).Add(url) + urlSet.(*skip.SkipList).Insert(comparator(*url)) mts.lock.Unlock() return true } @@ -98,64 +102,65 @@ func (mts *MetadataService) addURL(targetMap *sync.Map, url *common.URL) bool { func (mts *MetadataService) removeURL(targetMap *sync.Map, url *common.URL) { if value, loaded := targetMap.Load(url.ServiceKey()); loaded { mts.lock.Lock() - value.(*treeset.Set).Remove(url) + value.(*skip.SkipList).Delete(comparator(*url)) mts.lock.Unlock() mts.lock.RLock() defer mts.lock.RUnlock() - if value.(*treeset.Set).Empty() { + if value.(*skip.SkipList).Len() == 0 { targetMap.Delete(url.ServiceKey()) } } } // getAllService can return all the exportedUrlString except for metadataService -func (mts *MetadataService) getAllService(services *sync.Map) sets.Set { - sets := treeset.NewWith(utils.StringComparator) +func (mts *MetadataService) getAllService(services *sync.Map) *skip.SkipList { + skipList := skip.New(uint64(0)) services.Range(func(key, value interface{}) bool { - urls := value.(*treeset.Set) - urls.Each(func(index int, value interface{}) { - url := value.(*common.URL) + urls := value.(*skip.SkipList) + for i := uint64(0); i < urls.Len(); i++ { + url := common.URL(urls.ByPosition(i).(comparator)) if url.GetParam(constant.INTERFACE_KEY, url.Path) != "MetadataService" { - sets.Add(url.String()) + skipList.Insert(comparator(url)) } - }) + } return true }) - return sets + return skipList } // getSpecifiedService can return specified service url by serviceKey -func (mts *MetadataService) getSpecifiedService(services *sync.Map, serviceKey string, protocol string) sets.Set { - targetSets := treeset.NewWith(utils.StringComparator) - serviceSet, loaded := services.Load(serviceKey) +func (mts *MetadataService) getSpecifiedService(services *sync.Map, serviceKey string, protocol string) *skip.SkipList { + skipList := skip.New(uint64(0)) + serviceList, loaded := services.Load(serviceKey) if loaded { - serviceSet.(*treeset.Set).Each(func(index int, value interface{}) { - url := value.(*common.URL) + urls := serviceList.(*skip.SkipList) + for i := uint64(0); i < urls.Len(); i++ { + url := common.URL(urls.ByPosition(i).(comparator)) if len(protocol) == 0 || url.Protocol == protocol || url.GetParam(constant.PROTOCOL_KEY, "") == protocol { - targetSets.Add(value.(*common.URL).String()) + skipList.Insert(comparator(url)) } - }) + } } - return targetSets + return skipList } -// ExportURL can store the in memory treeset +// ExportURL can store the in memory func (mts *MetadataService) ExportURL(url common.URL) (bool, error) { return mts.addURL(mts.exportedServiceURLs, &url), nil } -// UnexportURL can remove the url store in memory treeset +// UnexportURL can remove the url store in memory func (mts *MetadataService) UnexportURL(url common.URL) error { mts.removeURL(mts.exportedServiceURLs, &url) return nil } -// SubscribeURL can store the in memory treeset +// SubscribeURL can store the in memory func (mts *MetadataService) SubscribeURL(url common.URL) (bool, error) { return mts.addURL(mts.subscribedServiceURLs, &url), nil } -// UnsubscribeURL can remove the url store in memory treeset +// UnsubscribeURL can remove the url store in memory func (mts *MetadataService) UnsubscribeURL(url common.URL) error { mts.removeURL(mts.subscribedServiceURLs, &url) return nil @@ -189,7 +194,7 @@ func (mts *MetadataService) PublishServiceDefinition(url common.URL) error { } // GetExportedURLs get all exported urls -func (mts *MetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) (sets.Set, error) { +func (mts *MetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) (*skip.SkipList, error) { if serviceInterface == constant.ANY_VALUE { return mts.getAllService(mts.exportedServiceURLs), nil } else { @@ -199,7 +204,7 @@ func (mts *MetadataService) GetExportedURLs(serviceInterface string, group strin } // GetSubscribedURLs get all subscribedUrl -func (mts *MetadataService) GetSubscribedURLs() (sets.Set, error) { +func (mts *MetadataService) GetSubscribedURLs() (*skip.SkipList, error) { return mts.getAllService(mts.subscribedServiceURLs), nil } diff --git a/metadata/service/inmemory/service_test.go b/metadata/service/inmemory/service_test.go index 7d701cd6b9ce3b3f9264a9ab79322d810e680c64..9e593db282e7f4fa55d52c49129a15a9b389c67f 100644 --- a/metadata/service/inmemory/service_test.go +++ b/metadata/service/inmemory/service_test.go @@ -63,38 +63,63 @@ func TestMetadataService(t *testing.T) { version := "0.0.1" protocol := "dubbo" beanName := "UserProvider" - u, _ := common.NewURL(fmt.Sprintf(`%v://127.0.0.1:20000/com.ikurento.user.UserProvider? - anyhost=true&application=BDTService&category=providers&default.timeout=10000& - dubbo=dubbo-provider-golang-1.0.0&environment=dev&interface=%v&ip=192.168.56.1& - methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447& - revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v& - version=%v&bean.name=%v`, protocol, serviceName, group, version, beanName)) + + u2, err := common.NewURL(fmt.Sprintf( + "%v://127.0.0.1:20000/com.ikurento.user.UserProvider2?anyhost=true&"+ + "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ + "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ + "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", + protocol, serviceName, group, version, beanName)) + assert.NoError(t, err) + mts.ExportURL(u2) + + u3, err := common.NewURL(fmt.Sprintf( + "%v://127.0.0.1:20000/com.ikurento.user.UserProvider3?anyhost=true&"+ + "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ + "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ + "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", + protocol, serviceName, group, version, beanName)) + assert.NoError(t, err) + mts.ExportURL(u3) + + u, err := common.NewURL(fmt.Sprintf( + "%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+ + "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ + "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ + "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", + protocol, serviceName, group, version, beanName)) + assert.NoError(t, err) mts.ExportURL(u) - sets, _ := mts.GetExportedURLs(serviceName, group, version, protocol) - assert.Equal(t, 1, sets.Size()) + list, _ := mts.GetExportedURLs(serviceName, group, version, protocol) + assert.Equal(t, uint64(3), list.Len()) + iter := list.IterAtPosition(0) + for iter.Next() { + comparator := iter.Value() + fmt.Println(comparator) + } mts.SubscribeURL(u) mts.SubscribeURL(u) - sets2, _ := mts.GetSubscribedURLs() - assert.Equal(t, 1, sets2.Size()) + list2, _ := mts.GetSubscribedURLs() + assert.Equal(t, uint64(1), list2.Len()) mts.UnexportURL(u) - sets11, _ := mts.GetExportedURLs(serviceName, group, version, protocol) - assert.Equal(t, 0, sets11.Size()) + list3, _ := mts.GetExportedURLs(serviceName, group, version, protocol) + assert.Equal(t, uint64(2), list3.Len()) mts.UnsubscribeURL(u) - sets22, _ := mts.GetSubscribedURLs() - assert.Equal(t, 0, sets22.Size()) + list4, _ := mts.GetSubscribedURLs() + assert.Equal(t, uint64(0), list4.Len()) userProvider := &UserProvider{} common.ServiceMap.Register(serviceName, protocol, userProvider) mts.PublishServiceDefinition(u) - expected := `{"CanonicalName":"com.ikurento.user.UserProvider","CodeSource":"", - "Methods":[{"Name":"GetUser","ParameterTypes":["slice"], - "ReturnType":"ptr","Parameters":null}],"Types":null}` + expected := "{\"CanonicalName\":\"com.ikurento.user.UserProvider\",\"CodeSource\":\"\"," + + "\"Methods\":[{\"Name\":\"GetUser\",\"ParameterTypes\":[\"slice\"],\"ReturnType\":\"ptr\"," + + "\"Parameters\":null}],\"Types\":null}" def1, _ := mts.GetServiceDefinition(serviceName, group, version) - assert.Equal(t, def1, expected) + assert.Equal(t, expected, def1) serviceKey := definition.ServiceDescriperBuild(serviceName, group, version) def2, _ := mts.GetServiceDefinitionByServiceKey(serviceKey) - assert.Equal(t, def2, expected) + assert.Equal(t, expected, def2) } diff --git a/metadata/service/service.go b/metadata/service/service.go index 21e50335e23e588c0fe2b219a464bd98dfe1f8d9..8ff63b50598f5bad16e3d1cc76fe6dca16e2fc04 100644 --- a/metadata/service/service.go +++ b/metadata/service/service.go @@ -18,7 +18,7 @@ package service import ( - "github.com/emirpasic/gods/sets" + "github.com/Workiva/go-datastructures/slice/skip" ) import ( @@ -41,9 +41,9 @@ type MetadataService interface { // PublishServiceDefinition will generate the target url's code info PublishServiceDefinition(url common.URL) error // GetExportedURLs will get the target exported url in metadata - GetExportedURLs(serviceInterface string, group string, version string, protocol string) (sets.Set, error) + GetExportedURLs(serviceInterface string, group string, version string, protocol string) (*skip.SkipList, error) // GetExportedURLs will get the target subscribed url in metadata - GetSubscribedURLs() (sets.Set, error) + GetSubscribedURLs() (*skip.SkipList, error) // GetServiceDefinition will get the target service info store in metadata GetServiceDefinition(interfaceName string, group string, version string) (string, error) // GetServiceDefinition will get the target service info store in metadata by service key