From dcd25568679593d92c6ceccdcecb34f997b7087a Mon Sep 17 00:00:00 2001 From: zonghaishang <yiji@apache.org> Date: Thu, 13 Jun 2019 11:05:31 +0800 Subject: [PATCH] refactor & unit test --- cluster/loadbalance/least_active_test.go | 70 ++++++++++++++++++++++++ filter/impl/active_filter.go | 8 +-- go.mod | 1 + go.sum | 4 ++ protocol/RpcStatus.go | 14 ++--- 5 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 cluster/loadbalance/least_active_test.go diff --git a/cluster/loadbalance/least_active_test.go b/cluster/loadbalance/least_active_test.go new file mode 100644 index 000000000..8f2d5402f --- /dev/null +++ b/cluster/loadbalance/least_active_test.go @@ -0,0 +1,70 @@ +package loadbalance + +import ( + "context" + "fmt" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/protocol" + "github.com/apache/dubbo-go/protocol/invocation" +) + +func TestLeastActiveSelect(t *testing.T) { + loadBalance := NewLeastActiveLoadBalance() + + var invokers []protocol.Invoker + + url, _ := common.NewURL(context.TODO(), "dubbo://192.168.1.0:20000/org.apache.demo.HelloService") + invokers = append(invokers, protocol.NewBaseInvoker(url)) + i := loadBalance.Select(invokers, &invocation.RPCInvocation{}) + assert.True(t, i.GetUrl().URLEqual(url)) + + for i := 1; i < 10; i++ { + url, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService", i)) + invokers = append(invokers, protocol.NewBaseInvoker(url)) + } + loadBalance.Select(invokers, &invocation.RPCInvocation{}) +} + +func TestLeastActiveByWeight(t *testing.T) { + loadBalance := NewLeastActiveLoadBalance() + + var invokers []protocol.Invoker + loop := 3 + for i := 1; i <= loop; i++ { + url, _ := common.NewURL(context.TODO(), fmt.Sprintf("test%v://192.168.1.%v:20000/org.apache.demo.HelloService?weight=%v", i, i, i)) + invokers = append(invokers, protocol.NewBaseInvoker(url)) + } + + inv := new(invocation.RPCInvocation) + inv.SetMethod("test") + + // test3 active count equals 1 + protocol.BeginCount(invokers[2].GetUrl(), inv.MethodName()) + + loop = 10000 + + var ( + firstCount int + secondCount int + ) + + for i := 1; i <= loop; i++ { + invoker := loadBalance.Select(invokers, inv) + if invoker.GetUrl().Protocol == "test1" { + firstCount++ + } else if invoker.GetUrl().Protocol == "test2" { + secondCount++ + } + } + + assert.Equal(t, firstCount+secondCount, loop) + +} diff --git a/filter/impl/active_filter.go b/filter/impl/active_filter.go index 7ef1b2f70..65abaa505 100644 --- a/filter/impl/active_filter.go +++ b/filter/impl/active_filter.go @@ -14,13 +14,13 @@ package impl import ( - "github.com/dubbo/go-for-apache-dubbo/common/logger" + "github.com/apache/dubbo-go/common/logger" ) import ( - "github.com/dubbo/go-for-apache-dubbo/common/extension" - "github.com/dubbo/go-for-apache-dubbo/filter" - "github.com/dubbo/go-for-apache-dubbo/protocol" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/filter" + "github.com/apache/dubbo-go/protocol" ) const active = "active" diff --git a/go.mod b/go.mod index 4c2276429..c001cd60d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module github.com/apache/dubbo-go require ( + github.com/dubbo/go-for-apache-dubbo v1.0.0 github.com/dubbogo/getty v1.0.7 github.com/dubbogo/hessian2 v1.0.1 github.com/pkg/errors v0.8.1 diff --git a/go.sum b/go.sum index a4fd7f509..e554da49f 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dubbo/go-for-apache-dubbo v1.0.0 h1:d1+EmiQNGjOZuEprh7ru/aa/5/pu3lvl8vBkdFEvLuw= +github.com/dubbo/go-for-apache-dubbo v1.0.0/go.mod h1:seatOl29ahGPWuMz4+VqqB6BSNYdHLXtnpPJ54U988Q= +github.com/dubbogo/getty v0.0.0-20190523180329-bdf5e640ea53/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo= github.com/dubbogo/getty v1.0.7 h1:5Hg+JwXyCKm9Yr4yJkm98ahhnoa8c2h6br5QJxwQ+YU= github.com/dubbogo/getty v1.0.7/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo= +github.com/dubbogo/hessian2 v0.0.0-20190526221400-d5610bbd0a41/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM= github.com/dubbogo/hessian2 v1.0.1 h1:ztI7gJxR3Isxrrl2jE1IZKX61eNR93eRKGhn49vPEX8= github.com/dubbogo/hessian2 v1.0.1/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= diff --git a/protocol/RpcStatus.go b/protocol/RpcStatus.go index 89180b2dd..fb0f78999 100644 --- a/protocol/RpcStatus.go +++ b/protocol/RpcStatus.go @@ -19,7 +19,7 @@ import ( ) import ( - "github.com/dubbo/go-for-apache-dubbo/common" + "github.com/apache/dubbo-go/common" ) var ( @@ -35,22 +35,22 @@ func (rpc *RpcStatus) GetActive() int32 { } func GetStatus(url common.URL, methodName string) *RpcStatus { - identity := url.Key() - methodMap, found := methodStatistics.Load(identity) + identifier := url.Key() + methodMap, found := methodStatistics.Load(identifier) if !found { methodMap = sync.Map{} - methodStatistics.Store(identity, methodMap) + methodStatistics.Store(identifier, methodMap) } methodActive := methodMap.(sync.Map) rpcStatus, found := methodActive.Load(methodName) if !found { - rpcStatus = RpcStatus{} + rpcStatus = &RpcStatus{} methodActive.Store(methodName, rpcStatus) } - status := rpcStatus.(RpcStatus) - return &status + status := rpcStatus.(*RpcStatus) + return status } func BeginCount(url common.URL, methodName string) { -- GitLab