-
Notifications
You must be signed in to change notification settings - Fork 933
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
NewLeastActiveLoadBalance #65
Changes from 6 commits
83409ca
a97fa47
bb3d59c
51651d6
dcd2556
87a797b
966ae48
417117e
b2585fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,21 @@ | |
package loadbalance | ||
|
||
import ( | ||
"github.com/dubbo/go-for-apache-dubbo/cluster" | ||
"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" | ||
"math/rand" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/cluster" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/protocol" | ||
) | ||
|
||
const ( | ||
leastActive = "leastactive" | ||
LeastActive = "leastactive" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const format |
||
|
||
func init() { | ||
extension.SetLoadbalance(leastActive, NewLeastActiveLoadBalance) | ||
extension.SetLoadbalance(LeastActive, NewLeastActiveLoadBalance) | ||
} | ||
|
||
type leastActiveLoadBalance struct { | ||
|
@@ -37,9 +39,8 @@ func NewLeastActiveLoadBalance() cluster.LoadBalance { | |
} | ||
|
||
func (lb *leastActiveLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker { | ||
|
||
count := len(invokers) | ||
if invokers == nil || count == 0 { | ||
if count == 0 { | ||
return nil | ||
} | ||
if count == 1 { | ||
|
@@ -48,17 +49,17 @@ func (lb *leastActiveLoadBalance) Select(invokers []protocol.Invoker, invocation | |
|
||
var ( | ||
leastActive int32 = -1 // The least active value of all invokers | ||
totalWeight int64 = 0 // The number of invokers having the same least active value (leastActive) | ||
totalWeight int64 = 0 // The number of invokers having the same least active value (LEAST_ACTIVE) | ||
firstWeight int64 = 0 // Initial value, used for comparision | ||
leastIndexes = make([]int, count) // The index of invokers having the same least active value (leastActive) | ||
leastCount = 0 // The number of invokers having the same least active value (leastActive) | ||
leastIndexes = make([]int, count) // The index of invokers having the same least active value (LEAST_ACTIVE) | ||
leastCount = 0 // The number of invokers having the same least active value (LEAST_ACTIVE) | ||
sameWeight = true // Every invoker has the same weight value? | ||
) | ||
|
||
for i := 0; i < count; i++ { | ||
invoker := invokers[i] | ||
// Active number | ||
active := filter.GetStatus(invoker.GetUrl(), invocation.MethodName()).GetActive() | ||
active := protocol.GetStatus(invoker.GetUrl(), invocation.MethodName()).GetActive() | ||
// current weight (maybe in warmUp) | ||
weight := GetWeight(invoker, invocation) | ||
// There are smaller active services | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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") | ||
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import format |
||
|
||
const active = "active" | ||
|
@@ -35,13 +35,13 @@ type ActiveFilter struct { | |
func (ef *ActiveFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
logger.Infof("invoking active filter. %v,%v", invocation.MethodName(), len(invocation.Arguments())) | ||
|
||
filter.BeginCount(invoker.GetUrl(), invocation.MethodName()) | ||
protocol.BeginCount(invoker.GetUrl(), invocation.MethodName()) | ||
return invoker.Invoke(invocation) | ||
} | ||
|
||
func (ef *ActiveFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
|
||
filter.EndCount(invoker.GetUrl(), invocation.MethodName()) | ||
protocol.EndCount(invoker.GetUrl(), invocation.MethodName()) | ||
return result | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module github.com/apache/dubbo-go | ||
|
||
require ( | ||
github.com/dubbo/go-for-apache-dubbo v1.0.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why require github.com/dubbo/go-for-apache-dubbo? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be left over from history. Trying to delete it will cause ci to fail There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed it. |
||
github.com/dubbogo/getty v1.0.7 | ||
github.com/dubbogo/hessian2 v1.0.1 | ||
github.com/pkg/errors v0.8.1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import format