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

consistent hash load balance #261

Merged
merged 2 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix comment.
Signed-off-by: imxyb <xyb4638@gmail.com>
  • Loading branch information
imxyb committed Dec 13, 2019
commit 80e5bf59810c3b0872fa3fee787bc2eee0b44821
23 changes: 12 additions & 11 deletions cluster/loadbalance/consistent_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"sort"
"strconv"
"strings"
)

import (
"github.com/apache/dubbo-go/cluster"
imxyb marked this conversation as resolved.
Show resolved Hide resolved
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
Expand All @@ -39,17 +41,19 @@ const (
HashArguments = "hash.arguments"
)

var selectors = make(map[string]*ConsistentHashSelector)
var re = regexp.MustCompile(constant.COMMA_SPLIT_PATTERN)
var (
selectors = make(map[string]*ConsistentHashSelector)
re = regexp.MustCompile(constant.COMMA_SPLIT_PATTERN)
)

func init() {
extension.SetLoadbalance(ConsistentHash, NewConsistentHash)
extension.SetLoadbalance(ConsistentHash, NewConsistentHashLoadBalance)
}

type ConsistentHashLoadBalance struct {
}

func NewConsistentHash() cluster.LoadBalance {
func NewConsistentHashLoadBalance() cluster.LoadBalance {
return &ConsistentHashLoadBalance{}
}

Expand All @@ -62,14 +66,14 @@ func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocat
for _, invoker := range invokers {
b, err := json.Marshal(invoker)
if err != nil {
panic(fmt.Sprintf("parse json failed:%s", err.Error()))
return nil
}
imxyb marked this conversation as resolved.
Show resolved Hide resolved
bs = append(bs, b...)
}
hashCode := crc32.ChecksumIEEE(bs)
selector, ok := selectors[key]
if !ok || selector.hashCode != hashCode {
selectors[key] = NewConsistentHashSelector(invokers, methodName, hashCode)
selectors[key] = newConsistentHashSelector(invokers, methodName, hashCode)
selector = selectors[key]
}
return selector.Select(invocation)
Expand Down Expand Up @@ -97,11 +101,8 @@ type ConsistentHashSelector struct {
argumentIndex []int
}

func NewConsistentHashSelector(invokers []protocol.Invoker, methodName string,
func newConsistentHashSelector(invokers []protocol.Invoker, methodName string,
hashCode uint32) *ConsistentHashSelector {
imxyb marked this conversation as resolved.
Show resolved Hide resolved
if len(invokers) == 0 {
panic("none of invokers")
}

selector := &ConsistentHashSelector{}
selector.virtualInvokers = make(map[uint32]protocol.Invoker)
Expand All @@ -112,7 +113,7 @@ func NewConsistentHashSelector(invokers []protocol.Invoker, methodName string,
for _, index := range indices {
i, err := strconv.Atoi(index)
if err != nil {
panic(err)
return nil
}
imxyb marked this conversation as resolved.
Show resolved Hide resolved
selector.argumentIndex = append(selector.argumentIndex, i)
}
Expand Down
8 changes: 6 additions & 2 deletions cluster/loadbalance/consistent_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package loadbalance
import (
"context"
"testing"
)

import (
"github.com/stretchr/testify/suite"
imxyb marked this conversation as resolved.
Show resolved Hide resolved
)

import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
Expand All @@ -43,7 +47,7 @@ func (s *consistentHashSelectorSuite) SetupTest() {
url, _ := common.NewURL(context.TODO(),
"dubbo://192.168.1.0:20000/org.apache.demo.HelloService?methods.echo.hash.arguments=0,1")
invokers = append(invokers, protocol.NewBaseInvoker(url))
s.selector = NewConsistentHashSelector(invokers, "echo", 999944)
s.selector = newConsistentHashSelector(invokers, "echo", 999944)
}

func (s *consistentHashSelectorSuite) TestToKey() {
Expand Down Expand Up @@ -92,7 +96,7 @@ func (s *consistentHashLoadBalanceSuite) SetupTest() {
s.invoker3 = protocol.NewBaseInvoker(s.url3)

s.invokers = append(s.invokers, s.invoker1, s.invoker2, s.invoker3)
s.lb = NewConsistentHash()
s.lb = NewConsistentHashLoadBalance()
}

func (s *consistentHashLoadBalanceSuite) TestSelect() {
Expand Down