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

Develop #2

Merged
merged 9 commits into from
Jul 4, 2019
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
3 changes: 1 addition & 2 deletions cluster/cluster_impl/failover_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ func Test_FailoverInvoke2(t *testing.T) {
urlParams.Set(constant.RETRIES_KEY, "2")
urlParams.Set("methods.test."+constant.RETRIES_KEY, "3")

ivc := &invocation.RPCInvocation{}
ivc.SetMethod("test")
ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
result := normalInvoke(t, 3, urlParams, ivc)
assert.NoError(t, result.Error())
count = 0
Expand Down
3 changes: 1 addition & 2 deletions cluster/loadbalance/least_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func TestLeastActiveByWeight(t *testing.T) {
invokers = append(invokers, protocol.NewBaseInvoker(url))
}

inv := new(invocation.RPCInvocation)
inv.SetMethod("test")
inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
protocol.BeginCount(invokers[2].GetUrl(), inv.MethodName())

loop = 10000
Expand Down
6 changes: 2 additions & 4 deletions cluster/loadbalance/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func Test_RandomlbSelectWeight(t *testing.T) {
urlParams.Set("methods.test."+constant.WEIGHT_KEY, "10000000000000")
urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams))
invokers = append(invokers, protocol.NewBaseInvoker(urll))
ivc := &invocation.RPCInvocation{}
ivc.SetMethod("test")
ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))

var selectedInvoker []protocol.Invoker
var selected float64
Expand Down Expand Up @@ -99,8 +98,7 @@ func Test_RandomlbSelectWarmup(t *testing.T) {
urlParams.Set(constant.REMOTE_TIMESTAMP_KEY, strconv.FormatInt(time.Now().Add(time.Minute*(-9)).Unix(), 10))
urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams))
invokers = append(invokers, protocol.NewBaseInvoker(urll))
ivc := &invocation.RPCInvocation{}
ivc.SetMethod("test")
ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))

var selectedInvoker []protocol.Invoker
var selected float64
Expand Down
2 changes: 1 addition & 1 deletion common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
SERVICE_KEY = "service"
METHODS_KEY = "methods"
TIMEOUT_KEY = "timeout"
BEAN_NAME_KEY = "bean.name"
)

const (
Expand All @@ -44,7 +45,6 @@ const (
WEIGHT_KEY = "weight"
WARMUP_KEY = "warmup"
RETRIES_KEY = "retries"
BEAN_NAME = "bean.name"
)

const (
Expand Down
4 changes: 3 additions & 1 deletion common/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func (p *Proxy) Implement(v common.RPCService) {
}
}

inv = invocation_impl.NewRPCInvocationForConsumer(methodName, nil, inArr, reply.Interface(), p.callBack, common.URL{}, nil)
inv = invocation_impl.NewRPCInvocationWithOptions(invocation_impl.WithMethodName(methodName),
invocation_impl.WithArguments(inArr), invocation_impl.WithReply(reply.Interface()),
invocation_impl.WithCallBack(p.callBack))

for k, value := range p.attachments {
inv.SetAttachments(k, value)
Expand Down
10 changes: 2 additions & 8 deletions common/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,15 @@ type TestService struct {
Echo func(interface{}, *interface{}) error
}

func (s *TestService) Service() string {
func (s *TestService) Reference() string {
return "com.test.Path"
}
func (s *TestService) Version() string {
return ""
}

type TestServiceInt int

func (s *TestServiceInt) Service() string {
func (s *TestServiceInt) Reference() string {
return "com.test.TestServiceInt"
}
func (s *TestServiceInt) Version() string {
return ""
}

func TestProxy_Implement(t *testing.T) {

Expand Down
15 changes: 7 additions & 8 deletions common/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import (

// rpc service interface
type RPCService interface {
Service() string // Path InterfaceName
Version() string
Reference() string // rpc service id or reference id
}

// for lowercase func
Expand Down Expand Up @@ -149,7 +148,7 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error)
return "", perrors.New(s)
}

sname = rcvr.Service()
sname = rcvr.Reference()
if server := sm.GetService(protocol, sname); server != nil {
return "", perrors.New("service already defined: " + sname)
}
Expand All @@ -172,8 +171,8 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error)
return strings.TrimSuffix(methods, ","), nil
}

func (sm *serviceMap) UnRegister(protocol, serviceName string) error {
if protocol == "" || serviceName == "" {
func (sm *serviceMap) UnRegister(protocol, serviceId string) error {
if protocol == "" || serviceId == "" {
return perrors.New("protocol or serviceName is nil")
}
sm.mutex.RLock()
Expand All @@ -182,16 +181,16 @@ func (sm *serviceMap) UnRegister(protocol, serviceName string) error {
sm.mutex.RUnlock()
return perrors.New("no services for " + protocol)
}
_, ok = svcs[serviceName]
_, ok = svcs[serviceId]
if !ok {
sm.mutex.RUnlock()
return perrors.New("no service for " + serviceName)
return perrors.New("no service for " + serviceId)
}
sm.mutex.RUnlock()

sm.mutex.Lock()
defer sm.mutex.Unlock()
delete(svcs, serviceName)
delete(svcs, serviceId)
delete(sm.serviceMap, protocol)

return nil
Expand Down
17 changes: 4 additions & 13 deletions common/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ func (s *TestService) MethodTwo(arg1, arg2, arg3 interface{}) (interface{}, erro
func (s *TestService) MethodThree() error {
return nil
}
func (s *TestService) Service() string {
func (s *TestService) Reference() string {
return "com.test.Path"
}
func (s *TestService) Version() string {
return ""
}
func (s *TestService) MethodMapper() map[string]string {
return map[string]string{
"MethodTwo": "methodTwo",
Expand All @@ -65,22 +62,16 @@ func (s *testService) Method3(ctx context.Context, args []interface{}, rsp *stru
func (s *testService) Method4(ctx context.Context, args []interface{}, rsp *struct{}) *testService {
return nil
}
func (s *testService) Service() string {
func (s *testService) Reference() string {
return "com.test.Path"
}
func (s *testService) Version() string {
return ""
}

type TestService1 struct {
}

func (s *TestService1) Service() string {
func (s *TestService1) Reference() string {
return "com.test.Path1"
}
func (s *TestService1) Version() string {
return ""
}

func TestServiceMap_Register(t *testing.T) {
// lowercase
Expand Down Expand Up @@ -180,7 +171,7 @@ func TestSuiteMethod(t *testing.T) {

// wrong number of in return
s1 := &testService{}
method, ok = reflect.TypeOf(s1).MethodByName("Version")
method, ok = reflect.TypeOf(s1).MethodByName("Reference")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
Expand Down
40 changes: 23 additions & 17 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC
setBaseValue := func(f reflect.Value) {
ok, value := config.GetProperty(getKeyPrefix(val, id) + key)
if ok {
if f.Kind() == reflect.Int64 {
switch f.Kind() {
case reflect.Int64:
x, err := strconv.Atoi(value)
if err != nil {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
Expand All @@ -120,21 +121,16 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC
val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the int64 value {%v} from config center is overflow", int64(x)))
}
}

}

if f.Kind() == reflect.String {
case reflect.String:
f.SetString(value)
}
if f.Kind() == reflect.Bool {
case reflect.Bool:
x, err := strconv.ParseBool(value)
if err != nil {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
val.Type().Name(), val.Type().Field(i).Name, err)
}
f.SetBool(x)
}
if f.Kind() == reflect.Float64 {
case reflect.Float64:
x, err := strconv.ParseFloat(value, 64)
if err != nil {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
Expand All @@ -147,7 +143,10 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC
val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the float64 value {%v} from config center is overflow", x))
}
}
default:
logger.Warnf("The kind of field {%v} is not supported ", f.Kind().String())
}

}

}
Expand Down Expand Up @@ -180,25 +179,32 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC
}
if f.Kind() == reflect.Map {

//initiate config
s := reflect.New(f.Type().Elem().Elem())
prefix := s.MethodByName("Prefix").Call(nil)[0].String()
m := config.GetSubProperty(prefix)
for k := range m {
f.SetMapIndex(reflect.ValueOf(k), reflect.New(f.Type().Elem().Elem()))
if f.Type().Elem().Kind() == reflect.Ptr {
//initiate config
s := reflect.New(f.Type().Elem().Elem())
prefix := s.MethodByName("Prefix").Call(nil)[0].String()
m := config.GetSubProperty(prefix)
for k := range m {
f.SetMapIndex(reflect.ValueOf(k), reflect.New(f.Type().Elem().Elem()))
}
}

//iter := f.MapRange()

for _, k := range f.MapKeys() {
v := f.MapIndex(k)
if v.Kind() == reflect.Ptr {
switch v.Kind() {
case reflect.Ptr:
if v.Elem().Kind() == reflect.Struct {
setFieldValue(v.Elem(), k, config)
} else {
setBaseValue(v.Elem())
}
case reflect.Int64, reflect.String, reflect.Bool, reflect.Float64:
setBaseValue(v)
default:
logger.Warnf("The kind of field {%v} is not supported ", v.Kind().String())
}

}
}

Expand Down
23 changes: 12 additions & 11 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func Test_refresh(t *testing.T) {
c := &BaseConfig{}
mockMap := map[string]string{}
mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100"
mockMap["dubbo.reference.MockService.MockService.retries"] = "10"
mockMap["dubbo.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.reference.com.MockService.MockService.retries"] = "10"
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"

Expand Down Expand Up @@ -88,7 +88,7 @@ func Test_refresh(t *testing.T) {
},
References: map[string]*ReferenceConfig{
"MockService": {
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Expand All @@ -98,13 +98,14 @@ func Test_refresh(t *testing.T) {
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser",
Retries: 2,
Loadbalance: "random",
},
{InterfaceId: "MockService",
InterfaceName: "MockService",
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser1",
Retries: 2,
Loadbalance: "random",
Expand All @@ -128,8 +129,8 @@ func Test_refreshProvider(t *testing.T) {
c := &BaseConfig{}
mockMap := map[string]string{}
mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100"
mockMap["dubbo.service.MockService.MockService.retries"] = "10"
mockMap["dubbo.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.service.com.MockService.MockService.retries"] = "10"
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"
mockMap["dubbo.protocols.jsonrpc1.name"] = "jsonrpc"
Expand Down Expand Up @@ -183,7 +184,7 @@ func Test_refreshProvider(t *testing.T) {
},
Services: map[string]*ServiceConfig{
"MockService": {
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Expand All @@ -193,13 +194,13 @@ func Test_refreshProvider(t *testing.T) {
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser",
Retries: 2,
Loadbalance: "random",
},
{InterfaceId: "MockService",
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser1",
Retries: 2,
Loadbalance: "random",
Expand Down
Loading