Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceLiZhixin committed Aug 13, 2021
1 parent b670a11 commit d6ef9c0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
23 changes: 11 additions & 12 deletions cluster/loadbalance/consistent_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
)

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

Expand Down Expand Up @@ -89,8 +89,8 @@ func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocat
return selector.Select(invocation)
}

// ConsistentHashSelector implementation of Selector:get invoker based on load balancing strategy
type ConsistentHashSelector struct {
// consistentHashSelector implementation of Selector:get invoker based on load balancing strategy
type consistentHashSelector struct {
hashCode uint32
replicaNum int
virtualInvokers map[uint32]protocol.Invoker
Expand All @@ -99,9 +99,9 @@ type ConsistentHashSelector struct {
}

func newConsistentHashSelector(invokers []protocol.Invoker, methodName string,
hashCode uint32) *ConsistentHashSelector {
hashCode uint32) *consistentHashSelector {

selector := &ConsistentHashSelector{}
selector := &consistentHashSelector{}
selector.virtualInvokers = make(map[uint32]protocol.Invoker)
selector.hashCode = hashCode
url := invokers[0].GetURL()
Expand Down Expand Up @@ -131,13 +131,13 @@ func newConsistentHashSelector(invokers []protocol.Invoker, methodName string,
}

// Select gets invoker based on load balancing strategy
func (c *ConsistentHashSelector) Select(invocation protocol.Invocation) protocol.Invoker {
func (c *consistentHashSelector) Select(invocation protocol.Invocation) protocol.Invoker {
key := c.toKey(invocation.Arguments())
digest := md5.Sum([]byte(key))
return c.selectForKey(c.hash(digest, 0))
}

func (c *ConsistentHashSelector) toKey(args []interface{}) string {
func (c *consistentHashSelector) toKey(args []interface{}) string {
var sb strings.Builder
for i := range c.argumentIndex {
if i >= 0 && i < len(args) {
Expand All @@ -147,7 +147,7 @@ func (c *ConsistentHashSelector) toKey(args []interface{}) string {
return sb.String()
}

func (c *ConsistentHashSelector) selectForKey(hash uint32) protocol.Invoker {
func (c *consistentHashSelector) selectForKey(hash uint32) protocol.Invoker {
idx := sort.Search(len(c.keys), func(i int) bool {
return c.keys[i] >= hash
})
Expand All @@ -157,8 +157,7 @@ func (c *ConsistentHashSelector) selectForKey(hash uint32) protocol.Invoker {
return c.virtualInvokers[c.keys[idx]]
}

// nolint
func (c *ConsistentHashSelector) hash(digest [16]byte, i int) uint32 {
return uint32((digest[3+i*4]&0xFF)<<24) | uint32((digest[2+i*4]&0xFF)<<16) |
uint32((digest[1+i*4]&0xFF)<<8) | uint32(digest[i*4]&0xFF)&0xFFFFFFF
func (c *consistentHashSelector) hash(digest [16]byte, i int) uint32 {
return (uint32(digest[3+i*4]&0xFF) << 24) | (uint32(digest[2+i*4]&0xFF) << 16) |
(uint32(digest[1+i*4]&0xFF) << 8) | uint32(digest[i*4]&0xFF)&0xFFFFFFF
}
8 changes: 4 additions & 4 deletions cluster/loadbalance/consistent_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
const (
ip = "192.168.1.0"
port8080 = 8080
port8082 = 8082
port8081 = 8081

url8080Short = "dubbo://192.168.1.0:8080"
url8081Short = "dubbo://192.168.1.0:8081"
Expand All @@ -52,7 +52,7 @@ func TestConsistentHashSelectorSuite(t *testing.T) {

type consistentHashSelectorSuite struct {
suite.Suite
selector *ConsistentHashSelector
selector *consistentHashSelector
}

func (s *consistentHashSelectorSuite) SetupTest() {
Expand Down Expand Up @@ -114,9 +114,9 @@ func (s *consistentHashLoadBalanceSuite) SetupTest() {
func (s *consistentHashLoadBalanceSuite) TestSelect() {
args := []interface{}{"name", "password", "age"}
invoker := s.lb.Select(s.invokers, invocation.NewRPCInvocation("echo", args, nil))
s.Equal(invoker.GetURL().Location, fmt.Sprintf("%s:%d", ip, port8080))
s.Equal(fmt.Sprintf("%s:%d", ip, port8081), invoker.GetURL().Location)

args = []interface{}{"ok", "abc"}
invoker = s.lb.Select(s.invokers, invocation.NewRPCInvocation("echo", args, nil))
s.Equal(invoker.GetURL().Location, fmt.Sprintf("%s:%d", ip, port8082))
s.Equal(fmt.Sprintf("%s:%d", ip, port8080), invoker.GetURL().Location)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ require (
replace (
github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.4
github.com/envoyproxy/go-control-plane => github.com/envoyproxy/go-control-plane v0.8.0
github.com/shirou/gopsutil => github.com/shirou/gopsutil v0.0.0-20181107111621-48177ef5f880
google.golang.org/grpc => google.golang.org/grpc v1.26.0
)
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,10 @@ github.com/sean-/conswriter v0.0.0-20180208195008-f5ae3917a627/go.mod h1:7zjs06q
github.com/sean-/pager v0.0.0-20180208200047-666be9bf53b5/go.mod h1:BeybITEsBEg6qbIiqJ6/Bqeq25bCLbL7YFmpaFfJDuM=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shirou/gopsutil v0.0.0-20181107111621-48177ef5f880 h1:1Ge4j/3uB2rxzPWD3TC+daeCw+w91z8UCUL/7WH5gn8=
github.com/shirou/gopsutil v0.0.0-20181107111621-48177ef5f880/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+DfgoHVedieIzIXE8uylPue0U=
github.com/shirou/gopsutil v3.20.11-0.20201116082039-2fb5da2f2449+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto=
github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
4 changes: 2 additions & 2 deletions registry/event/protocol_ports_metadata_customizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ func endpointsStr(protocolMap map[string]int) string {

// nolint
type endpoint struct {
Port int `json:"port, omitempty"`
Protocol string `json:"protocol, omitempty"`
Port int `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
}
1 change: 1 addition & 0 deletions remoting/etcdv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
DialOptions: []grpc.DialOption{grpc.WithBlock()},
})
if err != nil {
cancel()
return nil, perrors.WithMessage(err, "new raw client block connect to server")
}

Expand Down

0 comments on commit d6ef9c0

Please sign in to comment.