Skip to content

Commit

Permalink
Merge pull request #946 from apache/1.5.5
Browse files Browse the repository at this point in the history
Ready for release 1.5.5
  • Loading branch information
AlexStocks authored Dec 23, 2020
2 parents 2415f0f + 56bf5c8 commit 23578b3
Show file tree
Hide file tree
Showing 29 changed files with 80 additions and 75 deletions.
2 changes: 1 addition & 1 deletion common/proxy/proxy_factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (pi *ProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocati
args := invocation.Arguments()

// get service
svc := common.ServiceMap.GetService(proto, path)
svc := common.ServiceMap.GetServiceByServiceKey(proto, url.ServiceKey())
if svc == nil {
logger.Errorf("cannot find service [%s] in %s", path, proto)
result.SetError(perrors.Errorf("cannot find service [%s] in %s", path, proto))
Expand Down
28 changes: 17 additions & 11 deletions common/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,17 @@ type serviceMap struct {
}

// GetService gets a service defination by protocol and name
func (sm *serviceMap) GetService(protocol, name string) *Service {
func (sm *serviceMap) GetService(protocol, interfaceName, group, version string) *Service {
serviceKey := ServiceKey(interfaceName, group, version)
return sm.GetServiceByServiceKey(protocol, serviceKey)
}

// GetService gets a service defination by protocol and service key
func (sm *serviceMap) GetServiceByServiceKey(protocol, serviceKey string) *Service {
sm.mutex.RLock()
defer sm.mutex.RUnlock()
if s, ok := sm.serviceMap[protocol]; ok {
if srv, ok := s[name]; ok {
if srv, ok := s[serviceKey]; ok {
return srv
}
return nil
Expand All @@ -180,7 +186,7 @@ func (sm *serviceMap) GetInterface(interfaceName string) []*Service {
}

// Register registers a service by @interfaceName and @protocol
func (sm *serviceMap) Register(interfaceName, protocol string, rcvr RPCService) (string, error) {
func (sm *serviceMap) Register(interfaceName, protocol, group, version string, rcvr RPCService) (string, error) {
if sm.serviceMap[protocol] == nil {
sm.serviceMap[protocol] = make(map[string]*Service)
}
Expand All @@ -203,8 +209,8 @@ func (sm *serviceMap) Register(interfaceName, protocol string, rcvr RPCService)
return "", perrors.New(s)
}

sname = rcvr.Reference()
if server := sm.GetService(protocol, sname); server != nil {
sname = ServiceKey(interfaceName, group, version)
if server := sm.GetService(protocol, interfaceName, group, version); server != nil {
return "", perrors.New("service already defined: " + sname)
}
s.name = sname
Expand All @@ -228,9 +234,9 @@ func (sm *serviceMap) Register(interfaceName, protocol string, rcvr RPCService)
}

// UnRegister cancels a service by @interfaceName, @protocol and @serviceId
func (sm *serviceMap) UnRegister(interfaceName, protocol, serviceId string) error {
if protocol == "" || serviceId == "" {
return perrors.New("protocol or serviceName is nil")
func (sm *serviceMap) UnRegister(interfaceName, protocol, serviceKey string) error {
if protocol == "" || serviceKey == "" {
return perrors.New("protocol or serviceKey is nil")
}

var (
Expand All @@ -248,9 +254,9 @@ func (sm *serviceMap) UnRegister(interfaceName, protocol, serviceId string) erro
if !ok {
return perrors.New("no services for " + protocol)
}
s, ok := svcs[serviceId]
s, ok := svcs[serviceKey]
if !ok {
return perrors.New("no service for " + serviceId)
return perrors.New("no service for " + serviceKey)
}
svrs, ok = sm.interfaceMap[interfaceName]
if !ok {
Expand All @@ -276,7 +282,7 @@ func (sm *serviceMap) UnRegister(interfaceName, protocol, serviceId string) erro
sm.interfaceMap[interfaceName] = append(sm.interfaceMap[interfaceName], svrs[i])
}
}
delete(svcs, serviceId)
delete(svcs, serviceKey)
if len(sm.serviceMap[protocol]) == 0 {
delete(sm.serviceMap, protocol)
}
Expand Down
28 changes: 14 additions & 14 deletions common/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,23 @@ func TestServiceMapRegister(t *testing.T) {
// lowercase
s0 := &testService{}
// methods, err := ServiceMap.Register("testporotocol", s0)
_, err := ServiceMap.Register(testInterfaceName, "testporotocol", s0)
_, err := ServiceMap.Register(testInterfaceName, "testporotocol", "", "v0", s0)
assert.EqualError(t, err, "type testService is not exported")

// succ
s := &TestService{}
methods, err := ServiceMap.Register(testInterfaceName, "testporotocol", s)
methods, err := ServiceMap.Register(testInterfaceName, "testporotocol", "", "v1", s)
assert.NoError(t, err)
assert.Equal(t, "MethodOne,MethodThree,methodTwo", methods)

// repeat
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", s)
assert.EqualError(t, err, "service already defined: com.test.Path")
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", "", "v1", s)
assert.EqualError(t, err, "service already defined: testService:v1")

// no method
s1 := &TestService1{}
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", s1)
assert.EqualError(t, err, "type com.test.Path1 has no exported methods of suitable type")
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", "", "v2", s1)
assert.EqualError(t, err, "type testService:v2 has no exported methods of suitable type")

ServiceMap = &serviceMap{
serviceMap: make(map[string]map[string]*Service),
Expand All @@ -111,22 +111,22 @@ func TestServiceMapRegister(t *testing.T) {

func TestServiceMapUnRegister(t *testing.T) {
s := &TestService{}
_, err := ServiceMap.Register("TestService", testProtocol, s)
_, err := ServiceMap.Register("TestService", testProtocol, "", "v1", s)
assert.NoError(t, err)
assert.NotNil(t, ServiceMap.GetService(testProtocol, referenceTestPath))
assert.NotNil(t, ServiceMap.GetService(testProtocol, "TestService", "", "v1"))
assert.Equal(t, 1, len(ServiceMap.GetInterface("TestService")))

err = ServiceMap.UnRegister("", "", referenceTestPath)
assert.EqualError(t, err, "protocol or serviceName is nil")
err = ServiceMap.UnRegister("", "", ServiceKey("TestService", "", "v1"))
assert.EqualError(t, err, "protocol or serviceKey is nil")

err = ServiceMap.UnRegister("", "protocol", referenceTestPath)
err = ServiceMap.UnRegister("", "protocol", ServiceKey("TestService", "", "v1"))
assert.EqualError(t, err, "no services for protocol")

err = ServiceMap.UnRegister("", testProtocol, referenceTestPathDistinct)
assert.EqualError(t, err, "no service for com.test.Path1")
err = ServiceMap.UnRegister("", testProtocol, ServiceKey("TestService", "", "v0"))
assert.EqualError(t, err, "no service for TestService:v0")

// succ
err = ServiceMap.UnRegister("TestService", testProtocol, referenceTestPath)
err = ServiceMap.UnRegister("TestService", testProtocol, ServiceKey("TestService", "", "v1"))
assert.NoError(t, err)
}

Expand Down
4 changes: 3 additions & 1 deletion common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type URL struct {
noCopy noCopy

baseUrl
Path string // like /com.ikurento.dubbo.UserProvider3
Path string // like /com.ikurento.dubbo.UserProvider
Username string
Password string
Methods []string
Expand Down Expand Up @@ -317,6 +317,8 @@ func isMatchCategory(category1 string, category2 string) bool {
}

func (c *URL) String() string {
c.paramsLock.Lock()
defer c.paramsLock.Unlock()
var buf strings.Builder
if len(c.Username) == 0 && len(c.Password) == 0 {
buf.WriteString(fmt.Sprintf("%s://%s:%s%s?", c.Protocol, c.Ip, c.Port, c.Path))
Expand Down
6 changes: 3 additions & 3 deletions config/config_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestLoad(t *testing.T) {

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
err := common.ServiceMap.UnRegister("com.MockService", "mock", "MockService")
err := common.ServiceMap.UnRegister("com.MockService", "mock", common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
assert.Nil(t, err)
consumerConfig = nil
providerConfig = nil
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestLoadWithSingleReg(t *testing.T) {

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
common.ServiceMap.UnRegister("com.MockService", "mock", "MockService")
common.ServiceMap.UnRegister("com.MockService", "mock", common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
consumerConfig = nil
providerConfig = nil
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestWithNoRegLoad(t *testing.T) {

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
common.ServiceMap.UnRegister("com.MockService", "mock", "MockService")
common.ServiceMap.UnRegister("com.MockService", "mock", common.ServiceKey("com.MockService", "huadong_idc", "1.0.0"))
consumerConfig = nil
providerConfig = nil
}
Expand Down
4 changes: 2 additions & 2 deletions config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
// Refer ...
func (c *ReferenceConfig) Refer(_ interface{}) {
cfgURL := common.NewURLWithOptions(
common.WithPath(c.id),
common.WithPath(c.InterfaceName),
common.WithProtocol(c.Protocol),
common.WithParams(c.getUrlMap()),
common.WithParamsValue(constant.BEAN_NAME_KEY, c.id),
Expand All @@ -117,7 +117,7 @@ func (c *ReferenceConfig) Refer(_ interface{}) {
c.urls = append(c.urls, serviceUrl)
} else {
if serviceUrl.Path == "" {
serviceUrl.Path = "/" + c.id
serviceUrl.Path = "/" + c.InterfaceName
}
// merge url need to do
newUrl := common.MergeUrl(serviceUrl, cfgURL)
Expand Down
4 changes: 2 additions & 2 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *ServiceConfig) Export() error {
proxyFactory := extension.GetProxyFactory(providerConfig.ProxyFactory)
for _, proto := range protocolConfigs {
// registry the service reflect
methods, err := common.ServiceMap.Register(c.InterfaceName, proto.Name, c.rpcService)
methods, err := common.ServiceMap.Register(c.InterfaceName, proto.Name, c.Group, c.Version, c.rpcService)
if err != nil {
formatErr := perrors.Errorf("The service %v export the protocol %v error! Error message is %v.", c.InterfaceName, proto.Name, err.Error())
logger.Errorf(formatErr.Error())
Expand All @@ -184,7 +184,7 @@ func (c *ServiceConfig) Export() error {
nextPort = nextPort.Next()
}
ivkURL := common.NewURLWithOptions(
common.WithPath(c.id),
common.WithPath(c.InterfaceName),
common.WithProtocol(proto.Name),
common.WithIp(proto.Ip),
common.WithPort(port),
Expand Down
8 changes: 6 additions & 2 deletions filter/filter_impl/access_log_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ func (ef *AccessLogFilter) logIntoChannel(accessLogData AccessLogData) {
func (ef *AccessLogFilter) buildAccessLogData(_ protocol.Invoker, invocation protocol.Invocation) map[string]string {
dataMap := make(map[string]string, 16)
attachments := invocation.Attachments()
if v, ok := attachments[constant.INTERFACE_KEY]; ok && v != nil {
dataMap[constant.INTERFACE_KEY] = v.(string)
itf := attachments[constant.INTERFACE_KEY]
if itf == nil || len(itf.(string)) == 0 {
itf = attachments[constant.PATH_KEY]
}
if itf != nil {
dataMap[constant.INTERFACE_KEY] = itf.(string)
}
if v, ok := attachments[constant.METHOD_KEY]; ok && v != nil {
dataMap[constant.METHOD_KEY] = v.(string)
Expand Down
3 changes: 1 addition & 2 deletions filter/filter_impl/generic_service_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package filter_impl
import (
"context"
"reflect"
"strings"
)

import (
Expand Down Expand Up @@ -75,7 +74,7 @@ func (ef *GenericServiceFilter) Invoke(ctx context.Context, invoker protocol.Inv
url := invoker.GetUrl()
methodName = invocation.Arguments()[0].(string)
// get service
svc := common.ServiceMap.GetService(url.Protocol, strings.TrimPrefix(url.Path, "/"))
svc := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
// get method
method := svc.Method()[methodName]
if method == nil {
Expand Down
2 changes: 1 addition & 1 deletion filter/filter_impl/generic_service_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestGenericServiceFilterInvoke(t *testing.T) {
hessian.Object("222")},
}
s := &TestService{}
_, _ = common.ServiceMap.Register("TestService", "testprotocol", s)
_, _ = common.ServiceMap.Register("com.test.Path", "testprotocol", "", "", s)
rpcInvocation := invocation.NewRPCInvocation(methodName, aurguments, nil)
filter := GetGenericServiceFilter()
url, _ := common.NewURL("testprotocol://127.0.0.1:20000/com.test.Path")
Expand Down
5 changes: 2 additions & 3 deletions metadata/definition/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
)

func TestBuildServiceDefinition(t *testing.T) {
Expand All @@ -44,9 +43,9 @@ func TestBuildServiceDefinition(t *testing.T) {
"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
protocol, serviceName, group, version, beanName))
assert.NoError(t, err)
_, err = common.ServiceMap.Register(serviceName, protocol, &UserProvider{})
_, err = common.ServiceMap.Register(serviceName, protocol, group, version, &UserProvider{})
assert.NoError(t, err)
service := common.ServiceMap.GetService(url.Protocol, url.GetParam(constant.BEAN_NAME_KEY, url.Service()))
service := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
sd := BuildServiceDefinition(*service, url)
assert.Equal(t, "{canonicalName:com.ikurento.user.UserProvider, codeSource:, methods:[{name:GetUser,parameterTypes:[{type:slice}],returnType:ptr,params:[] }], types:[]}", sd.String())
}
4 changes: 2 additions & 2 deletions metadata/report/delegate/delegate_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func getMockDefinition(id *identifier.MetadataIdentifier, t *testing.T) *definit
"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
protocol, id.ServiceInterface, id.Group, id.Version, beanName))
assert.NoError(t, err)
_, err = common.ServiceMap.Register(id.ServiceInterface, protocol, &definition.UserProvider{})
_, err = common.ServiceMap.Register(id.ServiceInterface, protocol, id.Group, id.Version, &definition.UserProvider{})
assert.NoError(t, err)
service := common.ServiceMap.GetService(url.Protocol, url.GetParam(constant.BEAN_NAME_KEY, url.Service()))
service := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
return definition.BuildServiceDefinition(*service, url)
}
2 changes: 1 addition & 1 deletion metadata/service/exporter/configurable/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestConfigurableExporter(t *testing.T) {
assert.Equal(t, false, exported.IsExported())
assert.NoError(t, exported.Export(registryURL))
assert.Equal(t, true, exported.IsExported())
assert.Regexp(t, "dubbo://:20003/MetadataService*", exported.GetExportedURLs()[0].String())
assert.Regexp(t, "dubbo://:20003/org.apache.dubbo.metadata.MetadataService*", exported.GetExportedURLs()[0].String())
exported.Unexport()
assert.Equal(t, false, exported.IsExported())
})
Expand Down
4 changes: 2 additions & 2 deletions metadata/service/inmemory/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (mts *MetadataService) getAllService(services *sync.Map) []*common.URL {
urls := value.(*skip.SkipList)
for i := uint64(0); i < urls.Len(); i++ {
url := urls.ByPosition(i).(*common.URL)
if url.GetParam(constant.INTERFACE_KEY, url.Path) != constant.METADATA_SERVICE_NAME {
if url.Service() != constant.METADATA_SERVICE_NAME {
res = append(res, url)
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (mts *MetadataService) PublishServiceDefinition(url *common.URL) error {
interfaceName := url.GetParam(constant.INTERFACE_KEY, "")
isGeneric := url.GetParamBool(constant.GENERIC_KEY, false)
if len(interfaceName) > 0 && !isGeneric {
tmpService := common.ServiceMap.GetService(url.Protocol, url.GetParam(constant.BEAN_NAME_KEY, url.Service()))
tmpService := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
sd := definition.BuildServiceDefinition(*tmpService, url)
data, err := sd.ToBytes()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion metadata/service/inmemory/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestMetadataService(t *testing.T) {
assert.Equal(t, 0, len(list4))

userProvider := &definition.UserProvider{}
common.ServiceMap.Register(serviceName, protocol, userProvider)
common.ServiceMap.Register(serviceName, protocol, group, version, userProvider)
mts.PublishServiceDefinition(u)
expected := "{\"CanonicalName\":\"com.ikurento.user.UserProvider\",\"CodeSource\":\"\"," +
"\"Methods\":[{\"Name\":\"GetUser\",\"ParameterTypes\":[\"slice\"],\"ReturnType\":\"ptr\"," +
Expand Down
2 changes: 1 addition & 1 deletion metadata/service/remote/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (mts *MetadataService) PublishServiceDefinition(url *common.URL) error {
interfaceName := url.GetParam(constant.INTERFACE_KEY, "")
isGeneric := url.GetParamBool(constant.GENERIC_KEY, false)
if len(interfaceName) > 0 && !isGeneric {
sv := common.ServiceMap.GetService(url.Protocol, url.GetParam(constant.BEAN_NAME_KEY, url.Service()))
sv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
sd := definition.BuildServiceDefinition(*sv, url)
id := &identifier.MetadataIdentifier{
BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{
Expand Down
2 changes: 1 addition & 1 deletion metadata/service/remote/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func mockInmemoryProc(t *testing.T) *inmemory.MetadataService {
_, err = mts.SubscribeURL(u)
assert.NoError(t, err)

_, err = common.ServiceMap.Register(serviceName, protocol, userProvider)
_, err = common.ServiceMap.Register(serviceName, protocol, group, version, userProvider)
assert.NoError(t, err)
err = mts.PublishServiceDefinition(u)
assert.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions protocol/dubbo/dubbo_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func NewDubboExporter(key string, invoker protocol.Invoker, exporterMap *sync.Ma

// Unexport unexport dubbo service exporter.
func (de *DubboExporter) Unexport() {
serviceId := de.GetInvoker().GetUrl().GetParam(constant.BEAN_NAME_KEY, "")
interfaceName := de.GetInvoker().GetUrl().GetParam(constant.INTERFACE_KEY, "")
de.BaseExporter.Unexport()
err := common.ServiceMap.UnRegister(interfaceName, DUBBO, serviceId)
err := common.ServiceMap.UnRegister(interfaceName, DUBBO, de.GetInvoker().GetUrl().ServiceKey())
if err != nil {
logger.Errorf("[DubboExporter.Unexport] error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo/dubbo_invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func InitTest(t *testing.T) (protocol.Protocol, *common.URL) {

hessian.RegisterPOJO(&User{})

methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "dubbo", &UserProvider{})
methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "dubbo", "", "", &UserProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetBigPkg,GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4,GetUser5,GetUser6", methods)

Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo/impl/hessian.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func buildServerSidePackageBody(pkg *DubboPackage) {
"dubboVersion": dubboVersion,
"argsTypes": argsTypes,
"args": args,
"service": common.ServiceMap.GetService(DUBBO, svc.Path), // path as a key
"service": common.ServiceMap.GetService(DUBBO, svc.Interface, svc.Group, svc.Version), // path as a key
"attachments": attachments,
})
}
Expand Down
3 changes: 1 addition & 2 deletions protocol/grpc/grpc_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func NewGrpcExporter(key string, invoker protocol.Invoker, exporterMap *sync.Map

// Unexport and unregister gRPC service from registry and memory.
func (gg *GrpcExporter) Unexport() {
serviceId := gg.GetInvoker().GetUrl().GetParam(constant.BEAN_NAME_KEY, "")
interfaceName := gg.GetInvoker().GetUrl().GetParam(constant.INTERFACE_KEY, "")
gg.BaseExporter.Unexport()
err := common.ServiceMap.UnRegister(interfaceName, GRPC, serviceId)
err := common.ServiceMap.UnRegister(interfaceName, GRPC, gg.GetInvoker().GetUrl().ServiceKey())
if err != nil {
logger.Errorf("[GrpcExporter.Unexport] error: %v", err)
}
Expand Down
Loading

0 comments on commit 23578b3

Please sign in to comment.