Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ftr: triple multi params support #1344

Merged
merged 6 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI

on:
push:
branches: [master, develop]
branches: [master, develop, "1.5", "3.0"]
pull_request:
branches: "*"

Expand Down
7 changes: 1 addition & 6 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/logger"
)

// dubbo role type constant
Expand Down Expand Up @@ -206,11 +205,7 @@ func WithToken(token string) Option {
if len(token) > 0 {
value := token
if strings.ToLower(token) == "true" || strings.ToLower(token) == "default" {
u, err := uuid.NewV4()
if err != nil {
logger.Errorf("could not generator UUID: %v", err)
return
}
u := uuid.NewV4()
value = u.String()
}
url.SetParam(constant.TOKEN_KEY, value)
Expand Down
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ require (
github.com/creasty/defaults v1.5.1
github.com/dubbogo/go-zookeeper v1.0.3
github.com/dubbogo/gost v1.11.14
github.com/dubbogo/triple v1.0.1
github.com/dubbogo/triple v1.0.2
github.com/emicklei/go-restful/v3 v3.4.0
github.com/frankban/quicktest v1.4.1 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/ghodss/yaml v1.0.0
github.com/go-co-op/gocron v0.1.1
github.com/go-resty/resty/v2 v2.3.0
github.com/golang/mock v1.4.4
github.com/golang/protobuf v1.5.2
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267
github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8
github.com/hashicorp/vault/sdk v0.2.1
github.com/jinzhu/copier v0.3.2
github.com/magiconair/properties v1.8.5
github.com/mitchellh/mapstructure v1.4.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
github.com/nacos-group/nacos-sdk-go v1.0.8
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/opentracing/opentracing-go v1.2.0
github.com/pierrec/lz4 v2.2.6+incompatible // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.9.0
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/stretchr/objx v0.2.0 // indirect
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.7.0
github.com/zouyx/agollo/v3 v3.4.5
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0
Expand All @@ -44,10 +41,9 @@ require (
go.uber.org/zap v1.16.0
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.16.9
k8s.io/apimachinery v0.16.9
k8s.io/client-go v0.16.9
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
104 changes: 86 additions & 18 deletions go.sum

Large diffs are not rendered by default.

28 changes: 16 additions & 12 deletions protocol/dubbo3/dubbo3_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ func (dp *DubboProtocol) Export(invoker protocol.Invoker) protocol.Exporter {
valueOf := reflect.ValueOf(service)
typeOf := valueOf.Type()
numField := valueOf.NumMethod()
tripleService := &Dubbo3HessianService{proxyImpl: invoker}
tripleService := &UnaryService{proxyImpl: invoker}
for i := 0; i < numField; i++ {
ft := typeOf.Method(i)
if ft.Name == "Reference" {
continue
}
// num out is checked in common/rpc_service.go
if ft.Type.NumIn() != 3 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete the check cond?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thisk Block limit the number of method input params, if not 1 param, it would panic.
Now, we support any number of param.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome.

panic(fmt.Sprintf("function %s input params num = %d not supported, which should be 2", ft.Name, ft.Type.NumIn()-1))
// get all method params type
typs := make([]reflect.Type, 0)
for j := 2; j < ft.Type.NumIn(); j++ {
typs = append(typs, ft.Type.In(j))
}
typ := ft.Type.In(2)
tripleService.setReqParamsInterface(ft.Name, typ)
tripleService.setReqParamsTypes(ft.Name, typs)
}
service = tripleService
triSerializationType = tripleConstant.CodecType(serializationType)
Expand Down Expand Up @@ -164,25 +164,29 @@ type Dubbo3GrpcService interface {
ServiceDesc() *grpc.ServiceDesc
}

type Dubbo3HessianService struct {
type UnaryService struct {
proxyImpl protocol.Invoker
reqTypeMap sync.Map
}

func (d *Dubbo3HessianService) setReqParamsInterface(methodName string, typ reflect.Type) {
func (d *UnaryService) setReqParamsTypes(methodName string, typ []reflect.Type) {
d.reqTypeMap.Store(methodName, typ)
}

func (d *Dubbo3HessianService) GetReqParamsInteface(methodName string) (interface{}, bool) {
func (d *UnaryService) GetReqParamsInterfaces(methodName string) ([]interface{}, bool) {
val, ok := d.reqTypeMap.Load(methodName)
if !ok {
return nil, false
}
typ := val.(reflect.Type)
return reflect.New(typ).Interface(), true
typs := val.([]reflect.Type)
reqParamsInterfaces := make([]interface{}, 0, len(typs))
for _, typ := range typs {
reqParamsInterfaces = append(reqParamsInterfaces, reflect.New(typ).Interface())
}
return reqParamsInterfaces, true
}

func (d *Dubbo3HessianService) InvokeWithArgs(ctx context.Context, methodName string, arguments []interface{}) (interface{}, error) {
func (d *UnaryService) InvokeWithArgs(ctx context.Context, methodName string, arguments []interface{}) (interface{}, error) {
res := d.proxyImpl.Invoke(ctx, invocation.NewRPCInvocation(methodName, arguments, nil))
return res.Result(), res.Error()
}
Expand Down
63 changes: 63 additions & 0 deletions protocol/dubbo3/dubbo3_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package dubbo3

import (
"context"
"reflect"
"testing"
"time"
)

import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -85,3 +88,63 @@ func TestDubboProtocolRefer(t *testing.T) {
invokersLen = len(proto.(*DubboProtocol).Invokers())
assert.Equal(t, 0, invokersLen)
}

type MockUser struct {
Name string
}

func (m *MockUser) JavaClassName() string {
return "mockuser"
}

type MockService struct {
}

func (m *MockService) GetUser(ctx context.Context, user, user2 *MockUser) (*MockUser, error) {
return user, nil
}

func TestDubbo3UnaryService_GetReqParamsInterfaces(t *testing.T) {
hessian.RegisterPOJO(&MockUser{})
srv := UnaryService{}
valueOf := reflect.ValueOf(&MockService{})
typeOf := valueOf.Type()
numField := valueOf.NumMethod()
for i := 0; i < numField; i++ {
ft := typeOf.Method(i)
// num in/out is checked in common/rpc_service.go
typs := make([]reflect.Type, 0)
for j := 2; j < ft.Type.NumIn(); j++ {
typs = append(typs, ft.Type.In(j))
}
srv.setReqParamsTypes("GetUser", typs)
}
paramsInterfaces, ok := srv.GetReqParamsInterfaces("GetUser")
assert.True(t, ok)
enc := hessian.NewEncoder()
err := enc.Encode(&MockUser{
Name: "laurence",
})
assert.Nil(t, err)
data := enc.Buffer()
decoder := hessian.NewDecoder(data)
val, err := decoder.Decode()
assert.Nil(t, err)
assert.Equal(t, 2, len(paramsInterfaces))
subTest(t, val, paramsInterfaces)
args := make([]interface{}, 0, 1)
for _, v := range paramsInterfaces {
tempParamObj := reflect.ValueOf(v).Elem().Interface()
args = append(args, tempParamObj)
}
assert.Equal(t, "laurence", args[0].(*MockUser).Name)
assert.Equal(t, "laurence", args[1].(*MockUser).Name)
}

func subTest(t *testing.T, val, paramsInterfaces interface{}) {
list := paramsInterfaces.([]interface{})
for k, _ := range list {
err := hessian.ReflectResponse(val, list[k])
assert.Nil(t, err)
}
}
4 changes: 2 additions & 2 deletions registry/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type ServiceInstance interface {

// nolint
type Endpoint struct {
Port int `json:"port, omitempty"`
Protocol string `json:"protocol, omitempty"`
Port int `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
}

// DefaultServiceInstance the default implementation of ServiceInstance
Expand Down