Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
8fb1ce9e
Commit
8fb1ce9e
authored
6 years ago
by
zonghaishang
Browse files
Options
Downloads
Patches
Plain Diff
fix bug & unit test.
parent
329c297d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cluster/loadbalance/round_robin.go
+11
-10
11 additions, 10 deletions
cluster/loadbalance/round_robin.go
cluster/loadbalance/round_robin_test.go
+34
-0
34 additions, 0 deletions
cluster/loadbalance/round_robin_test.go
with
45 additions
and
10 deletions
cluster/loadbalance/round_robin.go
+
11
−
10
View file @
8fb1ce9e
...
...
@@ -59,8 +59,8 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation
}
key
:=
invokers
[
0
]
.
GetUrl
()
.
Path
+
"."
+
invocation
.
MethodName
()
cache
,
_
:=
methodWeightMap
.
LoadOrStore
(
key
,
cachedInvokers
{})
cachedInvokers
:=
cache
.
(
cachedInvokers
)
cache
,
_
:=
methodWeightMap
.
LoadOrStore
(
key
,
&
cachedInvokers
{})
cachedInvokers
:=
cache
.
(
*
cachedInvokers
)
clean
:=
false
totalWeight
:=
int64
(
0
)
...
...
@@ -75,9 +75,9 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation
weight
=
0
}
identif
y
:=
invoker
.
GetUrl
()
.
Key
()
loaded
,
found
:=
cachedInvokers
.
LoadOrStore
(
identif
y
,
weightedRoundRobin
{
weight
:
weight
})
weightRobin
:=
loaded
.
(
weightedRoundRobin
)
identif
ier
:=
invoker
.
GetUrl
()
.
Key
()
loaded
,
found
:=
cachedInvokers
.
LoadOrStore
(
identif
ier
,
&
weightedRoundRobin
{
weight
:
weight
})
weightRobin
:=
loaded
.
(
*
weightedRoundRobin
)
if
!
found
{
clean
=
true
}
...
...
@@ -92,7 +92,7 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation
if
currentWeight
>
maxCurrentWeight
{
maxCurrentWeight
=
currentWeight
selectedInvoker
=
invoker
selectedWeightRobin
=
weightRobin
selectedWeightRobin
=
*
weightRobin
}
totalWeight
+=
weight
}
...
...
@@ -107,12 +107,13 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation
return
invokers
[
0
]
}
func
cleanIfRequired
(
clean
bool
,
invokers
cachedInvokers
,
now
*
time
.
Time
)
{
if
atomic
.
LoadInt32
(
&
state
)
<
UPDATING
&&
clean
&&
atomic
.
CompareAndSwapInt32
(
&
state
,
COMPLETE
,
UPDATING
)
{
func
cleanIfRequired
(
clean
bool
,
invokers
*
cachedInvokers
,
now
*
time
.
Time
)
{
if
clean
&&
atomic
.
CompareAndSwapInt32
(
&
state
,
COMPLETE
,
UPDATING
)
{
defer
atomic
.
CompareAndSwapInt32
(
&
state
,
UPDATING
,
COMPLETE
)
invokers
.
Range
(
func
(
identify
,
robin
interface
{})
bool
{
weightedRoundRobin
:=
robin
.
(
weightedRoundRobin
)
if
now
.
Sub
(
*
weightedRoundRobin
.
lastUpdate
)
.
Nanoseconds
()
>
recyclePeriod
{
weightedRoundRobin
:=
robin
.
(
*
weightedRoundRobin
)
elapsed
:=
now
.
Sub
(
*
weightedRoundRobin
.
lastUpdate
)
.
Nanoseconds
()
if
elapsed
>
recyclePeriod
{
invokers
.
Delete
(
identify
)
}
return
true
...
...
This diff is collapsed.
Click to expand it.
cluster/loadbalance/round_robin_test.go
+
34
−
0
View file @
8fb1ce9e
package
loadbalance
import
(
"context"
"fmt"
"testing"
)
import
(
"github.com/stretchr/testify/assert"
)
import
(
"github.com/dubbo/go-for-apache-dubbo/common"
"github.com/dubbo/go-for-apache-dubbo/protocol"
"github.com/dubbo/go-for-apache-dubbo/protocol/invocation"
)
func
TestRoundRobinSelect
(
t
*
testing
.
T
)
{
loadBalance
:=
NewRoundRobinLoadBalance
()
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
{})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment