diff --git a/common/constant/key.go b/common/constant/key.go index 07057fa17c6d3c17b00dc8a4520da87e17e94ae0..9c59575eb83e6d7742c9783b22fec14dd52ede73 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -278,6 +278,4 @@ const ( // SERVICE_DISCOVERY_KEY indicate which service discovery instance will be used SERVICE_DISCOVERY_KEY = "service_discovery" - LANGUAGE_KEY = "language" - GO_LANG = "golang" ) diff --git a/config/remote_config.go b/config/remote_config.go index 4d17cc1c41c8033d550be7b2f0159485eabcec62..5e0330c571715d99e63688ee944c61f8e48117bb 100644 --- a/config/remote_config.go +++ b/config/remote_config.go @@ -19,10 +19,16 @@ package config import ( "time" +) +import ( "github.com/apache/dubbo-go/common/logger" ) +// RemoteConfig: usually we need some middleware, including nacos, zookeeper +// this represents an instance of this middleware +// so that other module, like config center, registry could reuse the config +// but now, only metadata report, metadata service, service discovery use this structure type RemoteConfig struct { Address string `yaml:"address" json:"address,omitempty"` TimeoutStr string `default:"5s" yaml:"timeout" json:"timeout,omitempty"` @@ -31,6 +37,8 @@ type RemoteConfig struct { Params map[string]string `yaml:"params" json:"address,omitempty"` } +// Timeout return timeout duration. +// if the configure is invalid, or missing, the default value 5s will be returned func (rc *RemoteConfig) Timeout() time.Duration { if res, err := time.ParseDuration(rc.TimeoutStr); err == nil { return res diff --git a/config/remote_config_test.go b/config/remote_config_test.go index 99facb7dda98ba46ac7b5a6f86de070de8ca3d78..82535fd60b932aecb7c6c3ee8206130fad9e7161 100644 --- a/config/remote_config_test.go +++ b/config/remote_config_test.go @@ -19,7 +19,8 @@ package config import ( "testing" - +) +import ( "github.com/stretchr/testify/assert" ) diff --git a/config/service_config.go b/config/service_config.go index 43c53bc9c2b596a9270c2e1e0c0ccfa71bb15ee4..a500a44419f99fccc6f0dc18814e38ed005cb737 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -44,9 +44,7 @@ import ( "github.com/apache/dubbo-go/protocol/protocolwrapper" ) -// ServiceConfig is a newest structure to support Dubbo 2.7.5 -// But I think it's not very necessary, -// we should think about how to reuse current ProviderConfig rather than use this +// ServiceConfig is the configuration of the service provider type ServiceConfig struct { context context.Context id string @@ -86,12 +84,12 @@ type ServiceConfig struct { exporters []protocol.Exporter } -// Prefix ... +// Prefix return dubbo.service.${interface}. func (c *ServiceConfig) Prefix() string { return constant.ServiceConfigPrefix + c.InterfaceName + "." } -// UnmarshalYAML ... +// UnmarshalYAML unmarshals the ServiceConfig by @unmarshal function func (c *ServiceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := defaults.Set(c); err != nil { return err @@ -143,7 +141,7 @@ func getRandomPort(protocolConfigs []*ProtocolConfig) *list.List { return ports } -// Export ... +// Export export the service func (c *ServiceConfig) Export() error { // TODO: config center start here @@ -251,7 +249,7 @@ func (c *ServiceConfig) Unexport() { c.unexported.Store(true) } -// Implement ... +// Implement only store the @s and return func (c *ServiceConfig) Implement(s common.RPCService) { c.rpcService = s } diff --git a/config_center/nacos/impl.go b/config_center/nacos/impl.go index 18873f7c4ac2766d53001718521a8fd11cc3bff8..bbf707b93811663d0a259c6704e1008bfa91c5c1 100644 --- a/config_center/nacos/impl.go +++ b/config_center/nacos/impl.go @@ -38,9 +38,14 @@ import ( const ( nacosClientName = "nacos config_center" - maxKeysNum = 9999 + // the number is a little big tricky + // it will be used in query which looks up all keys with the target group + // now, one key represents one application + // so only a group has more than 9999 applications will failed + maxKeysNum = 9999 ) +// nacosDynamicConfiguration is the implementation of DynamicConfiguration based on nacos type nacosDynamicConfiguration struct { url *common.URL rootPath string diff --git a/filter/rejected_execution_handler.go b/filter/rejected_execution_handler.go index fc971010953c7802c4db384f910b47c07b4b5bdb..3d1e1c1e641a836411ce0f71f97acf5d5a55f6d1 100644 --- a/filter/rejected_execution_handler.go +++ b/filter/rejected_execution_handler.go @@ -26,7 +26,7 @@ import ( /** * If the invocation cannot pass any validation in filter, like ExecuteLimitFilter and TpsLimitFilter, * the implementation will be used. - * The event case is that sometimes you want to return the default value when the request was rejected. + * The common case is that sometimes you want to return the default value when the request was rejected. * Or you want to be warned if any request was rejected. * In such situation, implement this interface and register it by invoking extension.SetRejectedExecutionHandler. */ diff --git a/metadata/definition/definition.go b/metadata/definition/definition.go index c3b3bd2769ae984112250d3103aebc490e739507..dbbc0c8f1685edf1a26ab1fe4ad091c501e76f5f 100644 --- a/metadata/definition/definition.go +++ b/metadata/definition/definition.go @@ -42,10 +42,12 @@ type ServiceDefinition struct { Types []TypeDefinition } +// ToBytes convert ServiceDefinition to json string func (def *ServiceDefinition) ToBytes() ([]byte, error) { return json.Marshal(def) } +// String will iterate all methods and parameters and convert them to json string func (def *ServiceDefinition) String() string { var methodStr strings.Builder for _, m := range def.Methods { diff --git a/metadata/identifier/base_metadata_identifier.go b/metadata/identifier/base_metadata_identifier.go index 64290c668f14277a5f2c8b9e7603ca50e7713fd6..2371f7ca02f403a11251b9b0cbb23369b27683e2 100644 --- a/metadata/identifier/base_metadata_identifier.go +++ b/metadata/identifier/base_metadata_identifier.go @@ -49,7 +49,7 @@ func joinParams(joinChar string, params []string) string { return joinedStr } -// getIdentifierKey will return string format as service:Version:Group:Side:param1:param2... +// getIdentifierKey returns string that format is service:Version:Group:Side:param1:param2... func (mdi *BaseMetadataIdentifier) getIdentifierKey(params ...string) string { return mdi.ServiceInterface + constant.KEY_SEPARATOR + mdi.Version + @@ -58,7 +58,7 @@ func (mdi *BaseMetadataIdentifier) getIdentifierKey(params ...string) string { joinParams(constant.KEY_SEPARATOR, params) } -// getFilePathKey will return string format as metadata/path/Version/Group/Side/param1/param2... +// getFilePathKey returns string that format is metadata/path/Version/Group/Side/param1/param2... func (mdi *BaseMetadataIdentifier) getFilePathKey(params ...string) string { path := serviceToPath(mdi.ServiceInterface) @@ -71,7 +71,7 @@ func (mdi *BaseMetadataIdentifier) getFilePathKey(params ...string) string { } -// serviceToPath... +// serviceToPath uss URL encode to decode the @serviceInterface func serviceToPath(serviceInterface string) string { if serviceInterface == constant.ANY_VALUE { return "" @@ -85,7 +85,7 @@ func serviceToPath(serviceInterface string) string { } -//withPathSeparator... +// withPathSeparator return "/" + @path func withPathSeparator(path string) string { if len(path) != 0 { path = constant.PATH_SEPARATOR + path diff --git a/metadata/identifier/metadata_identifier.go b/metadata/identifier/metadata_identifier.go index 18b330ae083d55cf77330d19c144b2d4a6bde862..7e50c4c6b9427bd9d439daa7464d96a2ea94fd39 100644 --- a/metadata/identifier/metadata_identifier.go +++ b/metadata/identifier/metadata_identifier.go @@ -23,12 +23,12 @@ type MetadataIdentifier struct { BaseMetadataIdentifier } -// GetIdentifierKey will return string format as service:Version:Group:Side:Application +// GetIdentifierKey returns string that format is service:Version:Group:Side:Application func (mdi *MetadataIdentifier) GetIdentifierKey() string { return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.Application) } -// GetFilePathKey will return string format as metadata/path/Version/Group/Side/Application +// GetFilePathKey returns string that format is metadata/path/Version/Group/Side/Application func (mdi *MetadataIdentifier) GetFilePathKey() string { return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.Application) } diff --git a/metadata/identifier/service_metadata_identifier.go b/metadata/identifier/service_metadata_identifier.go index 7a1bc283dcfbcdc978ea9144caf226511df28461..b9e65967e0f707a6efcc9f8ded2ce5dec4f058b8 100644 --- a/metadata/identifier/service_metadata_identifier.go +++ b/metadata/identifier/service_metadata_identifier.go @@ -29,6 +29,9 @@ type ServiceMetadataIdentifier struct { BaseMetadataIdentifier } +// NewServiceMetadataIdentifier create instance. +// The ServiceInterface is the @url.Service() +// other parameters are read from @url func NewServiceMetadataIdentifier(url common.URL) *ServiceMetadataIdentifier { return &ServiceMetadataIdentifier{ BaseMetadataIdentifier: BaseMetadataIdentifier{ @@ -41,12 +44,12 @@ func NewServiceMetadataIdentifier(url common.URL) *ServiceMetadataIdentifier { } } -// GetIdentifierKey will return string format as service:Version:Group:Side:Protocol:"revision-"+Revision +// GetIdentifierKey returns string that format is service:Version:Group:Side:Protocol:"revision"+Revision func (mdi *ServiceMetadataIdentifier) GetIdentifierKey() string { return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.Protocol, constant.KEY_REVISON_PREFIX+mdi.Revision) } -// GetFilePathKey will return string format as metadata/path/Version/Group/Side/Protocol/"revision"+Revision +// GetFilePathKey returns string that format is metadata/path/Version/Group/Side/Protocol/"revision"+Revision func (mdi *ServiceMetadataIdentifier) GetFilePathKey() string { return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.Protocol, constant.KEY_REVISON_PREFIX+mdi.Revision) } diff --git a/metadata/identifier/subscribe_metadata_identifier.go b/metadata/identifier/subscribe_metadata_identifier.go index fa35ab79d66fe39062462e6f5ae43b562a3c6a91..b1e37db971ada56a77bc3b716606b6fc8d137d34 100644 --- a/metadata/identifier/subscribe_metadata_identifier.go +++ b/metadata/identifier/subscribe_metadata_identifier.go @@ -23,12 +23,12 @@ type SubscriberMetadataIdentifier struct { MetadataIdentifier } -// GetIdentifierKey will return string format as service:Version:Group:Side:Revision +// GetIdentifierKey returns string that format is service:Version:Group:Side:Revision func (mdi *SubscriberMetadataIdentifier) GetIdentifierKey() string { return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.Revision) } -// GetFilePathKey will return string format as metadata/path/Version/Group/Side/Revision +// GetFilePathKey returns string that format is metadata/path/Version/Group/Side/Revision func (mdi *SubscriberMetadataIdentifier) GetFilePathKey() string { return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.Revision) } diff --git a/metadata/mapping/dynamic/service_name_mapping.go b/metadata/mapping/dynamic/service_name_mapping.go index 9a8d8299a979cf0a1a08a605c67b35112ca24171..84039ace9a2d56eca96bf36afc46d28e2a5ebe60 100644 --- a/metadata/mapping/dynamic/service_name_mapping.go +++ b/metadata/mapping/dynamic/service_name_mapping.go @@ -21,10 +21,6 @@ import ( "strconv" "sync" "time" - - "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/common/logger" - "github.com/apache/dubbo-go/metadata/mapping" ) import ( @@ -33,10 +29,13 @@ import ( ) import ( - common_cfg "github.com/apache/dubbo-go/common/config" + commonCfg "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/config" "github.com/apache/dubbo-go/config_center" + "github.com/apache/dubbo-go/metadata/mapping" ) const ( @@ -87,12 +86,15 @@ func (d *DynamicConfigurationServiceNameMapping) buildGroup(serviceInterface str return defaultGroup + slash + serviceInterface } -var serviceNameMappingInstance *DynamicConfigurationServiceNameMapping -var serviceNameMappingOnce sync.Once +var ( + serviceNameMappingInstance *DynamicConfigurationServiceNameMapping + serviceNameMappingOnce sync.Once +) +// GetNameMappingInstance return an instance, if not found, it creates one func GetNameMappingInstance() mapping.ServiceNameMapping { serviceNameMappingOnce.Do(func() { - dc := common_cfg.GetEnvInstance().GetDynamicConfiguration() + dc := commonCfg.GetEnvInstance().GetDynamicConfiguration() serviceNameMappingInstance = &DynamicConfigurationServiceNameMapping{dc: dc} }) return serviceNameMappingInstance diff --git a/metadata/mapping/memory/service_name_mapping.go b/metadata/mapping/memory/service_name_mapping.go deleted file mode 100644 index ef2e5fa06cd7a923016da6bf8e511600749dc37a..0000000000000000000000000000000000000000 --- a/metadata/mapping/memory/service_name_mapping.go +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 memory - -import ( - "sync" - - gxset "github.com/dubbogo/gost/container/set" - - "github.com/apache/dubbo-go/metadata/mapping" -) - -import ( - "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/config" -) - -func init() { - extension.SetGlobalServiceNameMapping(GetNameMappingInstance) -} - -type InMemoryServiceNameMapping struct{} - -func (i InMemoryServiceNameMapping) Map(serviceInterface string, group string, version string, protocol string) error { - return nil -} - -func (i InMemoryServiceNameMapping) Get(serviceInterface string, group string, version string, protocol string) (*gxset.HashSet, error) { - return gxset.NewSet(config.GetApplicationConfig().Name), nil -} - -var serviceNameMappingInstance *InMemoryServiceNameMapping -var serviceNameMappingOnce sync.Once - -func GetNameMappingInstance() mapping.ServiceNameMapping { - serviceNameMappingOnce.Do(func() { - serviceNameMappingInstance = &InMemoryServiceNameMapping{} - }) - return serviceNameMappingInstance -} diff --git a/metadata/report/nacos/report.go b/metadata/report/nacos/report.go index a119e0651fa1e0248b11b2252e36677f90cf1832..2bf40db519b2f131de586f5f506339286baf5c88 100644 --- a/metadata/report/nacos/report.go +++ b/metadata/report/nacos/report.go @@ -39,9 +39,9 @@ import ( ) func init() { - ftry := &nacosMetadataReportFactory{} + ins := &nacosMetadataReportFactory{} extension.SetMetadataReportFactory("nacos", func() factory.MetadataReportFactory { - return ftry + return ins }) } diff --git a/metadata/service/exporter/configurable/exporter.go b/metadata/service/exporter/configurable/exporter.go index e43fdda8861e1fe60010a19033eb4d7b03a38800..f8b4b0c0174cb0e5a8753b814f89ed4d332e2fbe 100644 --- a/metadata/service/exporter/configurable/exporter.go +++ b/metadata/service/exporter/configurable/exporter.go @@ -56,7 +56,7 @@ func (exporter *MetadataServiceExporter) Export() error { } serviceConfig.InterfaceName = constant.METADATA_SERVICE_NAME // identify this is a golang server - serviceConfig.Params = map[string]string{constant.LANGUAGE_KEY: constant.GO_LANG} + serviceConfig.Params = map[string]string{} serviceConfig.Group = config.GetApplicationConfig().Name // now the error will always be nil serviceConfig.Version, _ = exporter.metadataService.Version() diff --git a/metadata/service/inmemory/metadata_service_proxy_factory.go b/metadata/service/inmemory/metadata_service_proxy_factory.go index 0afadca8e87d7572452d534f688a66188996c878..1f8eeaa55f4a0240746508fee2ff088e3a653ca5 100644 --- a/metadata/service/inmemory/metadata_service_proxy_factory.go +++ b/metadata/service/inmemory/metadata_service_proxy_factory.go @@ -19,7 +19,9 @@ package inmemory import ( "encoding/json" +) +import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" @@ -35,6 +37,10 @@ func init() { }) } +// createProxy creates an instance of MetadataServiceProxy +// we read the metadata from ins.Metadata() +// and then create an Invoker instance +// also we will mark this proxy as golang's proxy func createProxy(ins registry.ServiceInstance) service.MetadataService { urls := buildStandardMetadataServiceURL(ins) if len(urls) == 0 { @@ -45,15 +51,12 @@ func createProxy(ins registry.ServiceInstance) service.MetadataService { u := urls[0] p := extension.GetProtocol(u.Protocol) invoker := p.Refer(*u) - golang := u.GetParam(constant.LANGUAGE_KEY, "") - return &MetadataServiceProxy{invkr: invoker, - golangServer: golang == constant.GO_LANG, + return &MetadataServiceProxy{ + invkr: invoker, } } // buildStandardMetadataServiceURL will use standard format to build the metadata service url. -// Now we don't need to support spring-cloud format metadata service url. -// func buildStandardMetadataServiceURL(ins registry.ServiceInstance) []*common.URL { ps := getMetadataServiceUrlParams(ins) res := make([]*common.URL, 0, len(ps)) diff --git a/metadata/service/inmemory/metadata_service_proxy_factory_test.go b/metadata/service/inmemory/metadata_service_proxy_factory_test.go index 652169ab78109fc30662896422110cf51fa73fc1..96020e1eb762442f946ccf8b368d6ebe9429d05e 100644 --- a/metadata/service/inmemory/metadata_service_proxy_factory_test.go +++ b/metadata/service/inmemory/metadata_service_proxy_factory_test.go @@ -18,15 +18,83 @@ package inmemory import ( + "context" "encoding/json" "testing" +) +import ( "github.com/stretchr/testify/assert" ) +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/protocol" + "github.com/apache/dubbo-go/registry" +) + func TestMetadataService_GetMetadataServiceUrlParams(t *testing.T) { str := `{"dubbo":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}}` tmp := make(map[string]map[string]string) err := json.Unmarshal([]byte(str), &tmp) assert.Nil(t, err) } + +func TestCreateProxy(t *testing.T) { + extension.SetProtocol("mock", func() protocol.Protocol { + return &mockProtocol{} + }) + ins := ®istry.DefaultServiceInstance{ + Id: "test-id", + ServiceName: "com.dubbo", + Host: "localhost", + Port: 8080, + Enable: true, + Healthy: true, + } + + pxy := createProxy(ins) + assert.Nil(t, pxy) + + ins.Metadata = map[string]string{constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"mock":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}}`} + pxy = createProxy(ins) + assert.NotNil(t, pxy) +} + +type mockProtocol struct { +} + +func (m mockProtocol) Export(invoker protocol.Invoker) protocol.Exporter { + panic("implement me") +} + +func (m mockProtocol) Refer(url common.URL) protocol.Invoker { + return &mockInvoker{} +} + +func (m mockProtocol) Destroy() { + panic("implement me") +} + +type mockInvoker struct { +} + +func (m *mockInvoker) GetUrl() common.URL { + panic("implement me") +} + +func (m *mockInvoker) IsAvailable() bool { + panic("implement me") +} + +func (m *mockInvoker) Destroy() { + panic("implement me") +} + +func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { + return &protocol.RPCResult{ + Rest: &[]interface{}{"dubbo://localhost"}, + } +} diff --git a/metadata/service/inmemory/service.go b/metadata/service/inmemory/service.go index ec626f7f43f6be71b6e5f8562628a48555a5c71d..6fe44cfc71c9f6532035a4df081d7ce4a653bf1f 100644 --- a/metadata/service/inmemory/service.go +++ b/metadata/service/inmemory/service.go @@ -19,9 +19,6 @@ package inmemory import ( "sort" "sync" - - "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/config" ) import ( @@ -32,7 +29,9 @@ import ( import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" + "github.com/apache/dubbo-go/config" "github.com/apache/dubbo-go/metadata/definition" "github.com/apache/dubbo-go/metadata/service" ) diff --git a/metadata/service/inmemory/service_proxy.go b/metadata/service/inmemory/service_proxy.go index 69e349a0f8cb6038f090b7007af1169d685db0bd..840bd9a9d5629921840039597cfb06ae4bef2c6b 100644 --- a/metadata/service/inmemory/service_proxy.go +++ b/metadata/service/inmemory/service_proxy.go @@ -21,7 +21,9 @@ import ( "context" "reflect" "time" +) +import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/logger" @@ -36,7 +38,7 @@ import ( // so in client-side, if we want to get the metadata information, // we must call metadata service // this is the stub, or proxy -// for now, only GetExportedURLs will be implemented +// for now, only GetExportedURLs need to be implemented type MetadataServiceProxy struct { invkr protocol.Invoker golangServer bool diff --git a/metadata/service/inmemory/service_proxy_test.go b/metadata/service/inmemory/service_proxy_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0d75517e418133ffbf3804ec96f061dda09b9e5e --- /dev/null +++ b/metadata/service/inmemory/service_proxy_test.go @@ -0,0 +1,82 @@ +/* + * 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 ( + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/metadata/service" + "github.com/apache/dubbo-go/protocol" + "github.com/apache/dubbo-go/registry" +) + +func TestMetadataServiceProxy_GetExportedURLs(t *testing.T) { + + pxy := createPxy() + assert.NotNil(t, pxy) + res, err := pxy.GetExportedURLs(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE) + assert.Nil(t, err) + assert.Len(t, res, 1) + +} + +// TestNewMetadataService: those methods are not implemented +// when we implement them, adding UT +func TestNewMetadataService(t *testing.T) { + pxy := createPxy() + pxy.ServiceName() + pxy.PublishServiceDefinition(common.URL{}) + pxy.GetServiceDefinition(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE) + pxy.Version() + pxy.GetSubscribedURLs() + pxy.UnsubscribeURL(common.URL{}) + pxy.GetServiceDefinitionByServiceKey("any") + pxy.ExportURL(common.URL{}) + pxy.SubscribeURL(common.URL{}) + pxy.MethodMapper() + pxy.UnexportURL(common.URL{}) + pxy.RefreshMetadata(constant.ANY_VALUE, constant.ANY_VALUE) + +} + +func createPxy() service.MetadataService { + extension.SetProtocol("mock", func() protocol.Protocol { + return &mockProtocol{} + }) + + ins := ®istry.DefaultServiceInstance{ + Id: "test-id", + ServiceName: "com.dubbo", + Host: "localhost", + Port: 8080, + Enable: true, + Healthy: true, + Metadata: map[string]string{constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"mock":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}}`}, + } + + return extension.GetMetadataServiceProxyFactory(local).GetProxy(ins) +} diff --git a/metadata/service/remote/service.go b/metadata/service/remote/service.go index fa00a8dc2a29303c97cb0c33ca885b66fd16c1ba..af9d1f0079a3367f1dbdd9f543865d80ceacf8fc 100644 --- a/metadata/service/remote/service.go +++ b/metadata/service/remote/service.go @@ -19,15 +19,16 @@ package remote import ( "sync" +) +import ( "go.uber.org/atomic" - - "github.com/apache/dubbo-go/common/extension" ) import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/config" "github.com/apache/dubbo-go/metadata/definition" diff --git a/metadata/service/remote/service_proxy.go b/metadata/service/remote/service_proxy.go index 5e37d39fd47d2da2b049f233860783015b414a8c..b7fe6f46854e649bae2df1b0a9916002af0ba8c9 100644 --- a/metadata/service/remote/service_proxy.go +++ b/metadata/service/remote/service_proxy.go @@ -19,7 +19,8 @@ package remote import ( "strings" - +) +import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/logger" @@ -141,7 +142,7 @@ func newMetadataServiceProxy(ins registry.ServiceInstance) service.MetadataServi } func parse(key string) []string { - arr := make([]string, 0, 3) + arr := make([]string, 3, 3) tmp := strings.SplitN(key, "/", 2) if len(tmp) > 1 { arr[0] = tmp[0] diff --git a/metadata/service/remote/service_proxy_test.go b/metadata/service/remote/service_proxy_test.go new file mode 100644 index 0000000000000000000000000000000000000000..31a9ba1fd128b6b4e3bd6d20702e279b0a067762 --- /dev/null +++ b/metadata/service/remote/service_proxy_test.go @@ -0,0 +1,135 @@ +/* + * 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 remote + +import ( + "testing" +) +import ( + "github.com/stretchr/testify/assert" +) + +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/config/instance" + "github.com/apache/dubbo-go/metadata/identifier" + "github.com/apache/dubbo-go/metadata/report" + "github.com/apache/dubbo-go/metadata/report/factory" + "github.com/apache/dubbo-go/metadata/service" + "github.com/apache/dubbo-go/registry" +) + +func TestMetadataServiceProxy_GetExportedURLs(t *testing.T) { + pxy := createProxy() + res, err := pxy.GetExportedURLs(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE) + assert.Nil(t, err) + assert.Len(t, res, 2) +} + +func TestMetadataServiceProxy_GetServiceDefinition(t *testing.T) { + pxy := createProxy() + res, err := pxy.GetServiceDefinition(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE) + assert.Nil(t, err) + assert.Equal(t, "definition", res) +} + +// TestMetadataServiceProxy test those unimportant method +// in fact, we don't use them +func TestMetadataServiceProxy(t *testing.T) { + pxy := createProxy() + pxy.ServiceName() + pxy.PublishServiceDefinition(common.URL{}) + pxy.Version() + pxy.GetSubscribedURLs() + pxy.UnsubscribeURL(common.URL{}) + pxy.GetServiceDefinitionByServiceKey("any") + pxy.ExportURL(common.URL{}) + pxy.SubscribeURL(common.URL{}) + pxy.MethodMapper() + pxy.UnexportURL(common.URL{}) + pxy.Reference() + pxy.RefreshMetadata(constant.ANY_VALUE, constant.ANY_VALUE) +} + +func createProxy() service.MetadataService { + + prepareTest() + + ins := ®istry.DefaultServiceInstance{ + Id: "test-id", + ServiceName: "com.dubbo", + Host: "localhost", + Port: 8080, + Enable: true, + Healthy: true, + Metadata: map[string]string{constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"mock":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}}`}, + } + return newMetadataServiceProxy(ins) +} + +func prepareTest() { + extension.SetMetadataReportFactory("mock", func() factory.MetadataReportFactory { + return &mockMetadataReportFactory{} + }) + u, _ := common.NewURL("mock://localhost") + instance.GetMetadataReportInstance(&u) +} + +type mockMetadataReportFactory struct { +} + +func (m *mockMetadataReportFactory) CreateMetadataReport(*common.URL) report.MetadataReport { + return &mockMetadataReport{} +} + +type mockMetadataReport struct { +} + +func (m mockMetadataReport) StoreProviderMetadata(*identifier.MetadataIdentifier, string) error { + panic("implement me") +} + +func (m mockMetadataReport) StoreConsumerMetadata(*identifier.MetadataIdentifier, string) error { + panic("implement me") +} + +func (m mockMetadataReport) SaveServiceMetadata(*identifier.ServiceMetadataIdentifier, common.URL) error { + panic("implement me") +} + +func (m mockMetadataReport) RemoveServiceMetadata(*identifier.ServiceMetadataIdentifier) error { + panic("implement me") +} + +func (m mockMetadataReport) GetExportedURLs(*identifier.ServiceMetadataIdentifier) []string { + return []string{"mock://localhost1", "mock://localhost2"} +} + +func (m mockMetadataReport) SaveSubscribedData(*identifier.SubscriberMetadataIdentifier, []common.URL) error { + panic("implement me") +} + +func (m mockMetadataReport) GetSubscribedURLs(*identifier.SubscriberMetadataIdentifier) []string { + panic("implement me") +} + +func (m mockMetadataReport) GetServiceDefinition(*identifier.MetadataIdentifier) string { + return "definition" +} diff --git a/metadata/service/service.go b/metadata/service/service.go index ae04858247b3999846a7fa827653c932b53ebdb9..f6509d0a72eb26e488dfb4fdeef5f4bbfd6b1bea 100644 --- a/metadata/service/service.go +++ b/metadata/service/service.go @@ -19,7 +19,9 @@ package service import ( "sync" +) +import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/registry" diff --git a/protocol/dubbo/client.go b/protocol/dubbo/client.go index 981f295b3975c1735eea251d3f568523aca56863..6d1b771bf4108d17372e0ceb5ca818323278afd2 100644 --- a/protocol/dubbo/client.go +++ b/protocol/dubbo/client.go @@ -280,7 +280,7 @@ func (c *Client) call(ct CallType, request *Request, response *Response, callbac } select { - case <-getty.GetTimeWheel().After(3 * time.Second): + case <-getty.GetTimeWheel().After(c.opts.RequestTimeout): c.removePendingResponse(SequenceType(rsp.seq)) return perrors.WithStack(errClientReadTimeout) case <-rsp.done: diff --git a/registry/base_registry.go b/registry/base_registry.go index 504e087091ae30264676c90feb49d5690b85c164..ad1a3b61741e003625612ad58409eb8615271a84 100644 --- a/registry/base_registry.go +++ b/registry/base_registry.go @@ -92,7 +92,7 @@ type FacadeBasedRegistry interface { InitListeners() } -// BaseRegistry is a event logic abstract for registry. It implement Registry interface. +// BaseRegistry is a common logic abstract for registry. It implement Registry interface. type BaseRegistry struct { context context.Context facadeBasedRegistry FacadeBasedRegistry diff --git a/registry/consul/registry.go b/registry/consul/registry.go index c9e0718346258b6b38f2a793dc215bcf8e65cdb7..bd394be44356c201b6bf7c88a61bfb2dafa0eb15 100644 --- a/registry/consul/registry.go +++ b/registry/consul/registry.go @@ -74,6 +74,8 @@ func newConsulRegistry(url *common.URL) (registry.Registry, error) { return r, nil } +// Register register @url +// it delegate the job to register() method func (r *consulRegistry) Register(url common.URL) error { var err error @@ -87,6 +89,7 @@ func (r *consulRegistry) Register(url common.URL) error { return nil } +// register actually register the @url func (r *consulRegistry) register(url common.URL) error { service, err := buildService(url) if err != nil { @@ -95,6 +98,8 @@ func (r *consulRegistry) register(url common.URL) error { return r.client.Agent().ServiceRegister(service) } +// UnRegister unregister the @url +// it delegate the job to unregister() method func (r *consulRegistry) UnRegister(url common.URL) error { var err error @@ -108,10 +113,12 @@ func (r *consulRegistry) UnRegister(url common.URL) error { return nil } +// unregister actually unregister the @url func (r *consulRegistry) unregister(url common.URL) error { return r.client.Agent().ServiceDeregister(buildId(url)) } +// Subscribe subscribe the @url with the @notifyListener func (r *consulRegistry) Subscribe(url *common.URL, notifyListener registry.NotifyListener) error { role, _ := strconv.Atoi(r.URL.GetParam(constant.ROLE_KEY, "")) if role == common.CONSUMER { @@ -120,11 +127,13 @@ func (r *consulRegistry) Subscribe(url *common.URL, notifyListener registry.Noti return nil } -// UnSubscribe : +// UnSubscribe is not supported yet func (r *consulRegistry) UnSubscribe(url *common.URL, notifyListener registry.NotifyListener) error { return perrors.New("UnSubscribe not support in consulRegistry") } +// subscribe actually subscribe the @url +// it loops forever until success func (r *consulRegistry) subscribe(url *common.URL, notifyListener registry.NotifyListener) { for { if !r.IsAvailable() {