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

Loadbalance round robin #66

Merged
merged 9 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
149 changes: 149 additions & 0 deletions cluster/loadbalance/round_robin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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/protocol"
"math"
"sync"
"sync/atomic"
"time"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里手工format一下代码,看下其他的包的import规范


const (
roundRobin = "roundrobin"

complete = 0
updating = 1
)
Copy link
Contributor

Choose a reason for hiding this comment

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

dubbogo const 的规范为全部大写.


var (
methodWeightMap sync.Map // [string]invokers
state int32 = complete // update lock acquired ?
recyclePeriod int64 = 60 * time.Second.Nanoseconds()
)

func init() {
extension.SetLoadbalance(roundRobin, NewRoundRobinLoadBalance)
}

type roundRobinLoadBalance struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

空 struct,一行内写完代码,不要搞成两行

}

func NewRoundRobinLoadBalance() cluster.LoadBalance {
return &roundRobinLoadBalance{}
}

func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {

Copy link
Contributor

Choose a reason for hiding this comment

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

go里面没有第一行空行的

Choose a reason for hiding this comment

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

go里面没有第一行空行的, 这句话什么意思? @AlexStocks

Copy link
Contributor

Choose a reason for hiding this comment

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

go里面没有第一行空行的, 这句话什么意思? @AlexStocks

我的意思是,一般的go项目里,没见过函数有第一行是空行的规矩。这个应该是java 的吧?

count := len(invokers)
if invokers == nil || count == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

if count == 0 判断即可,就算是 nil,其长度也为 0

return nil
}
if count == 1 {
return invokers[0]
}

key := invokers[0].GetUrl().Path + "." + invocation.MethodName()
Copy link
Contributor

Choose a reason for hiding this comment

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

用invokers[0].GetUrl().Key() + "." + invocation.MethodName() 是否可以,version/group 字段也要作为key字段考虑进来

Copy link
Member Author

Choose a reason for hiding this comment

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

不需要的

cache, _ := methodWeightMap.LoadOrStore(key, cachedInvokers{})
cachedInvokers := cache.(cachedInvokers)

clean := false
totalWeight := int64(0)
maxCurrentWeight := int64(math.MinInt64)
var selectedInvoker protocol.Invoker
var selectedWeightRobin weightedRoundRobin
now := time.Now()
Copy link
Contributor

Choose a reason for hiding this comment

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

上面这块代码,可以统一放进一个 var {
} 里面


for _, invoker := range invokers {

Copy link
Contributor

Choose a reason for hiding this comment

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

这里不要有空行

var weight = GetWeight(invoker, invocation)
if weight < 0 {
weight = 0
}

identify := invoker.GetUrl().Key()
loaded, found := cachedInvokers.LoadOrStore(identify, weightedRoundRobin{weight: weight})
weightRobin := loaded.(weightedRoundRobin)
if !found {
clean = true
}

if weightRobin.Weight() != weight {
weightRobin.setWeight(weight)
}

currentWeight := weightRobin.increaseCurrent()
weightRobin.lastUpdate = &now

if currentWeight > maxCurrentWeight {
maxCurrentWeight = currentWeight
selectedInvoker = invoker
selectedWeightRobin = weightRobin
}
totalWeight += weight
}

cleanIfRequired(clean, cachedInvokers, &now)

if selectedInvoker != nil {
selectedWeightRobin.Current(totalWeight)
return selectedInvoker
}

return invokers[0]
}

func cleanIfRequired(clean bool, invokers cachedInvokers, now *time.Time) {
if atomic.LoadInt32(&state) < updating && 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 {
invokers.Delete(identify)
}
return true
})
}
}

// Record the weight of the invoker
type weightedRoundRobin struct {
weight int64
current int64
lastUpdate *time.Time
}

func (robin *weightedRoundRobin) Weight() int64 {
return atomic.LoadInt64(&robin.weight)
}

func (robin *weightedRoundRobin) setWeight(weight int64) {
robin.weight = weight
robin.current = 0
}

func (robin *weightedRoundRobin) increaseCurrent() int64 {
return atomic.AddInt64(&robin.current, robin.weight)
}

func (robin *weightedRoundRobin) Current(delta int64) {
atomic.AddInt64(&robin.current, -1*delta)
}

type cachedInvokers struct {
sync.Map /*[string]weightedRoundRobin*/
}
1 change: 1 addition & 0 deletions cluster/loadbalance/round_robin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package loadbalance