Skip to content

Commit

Permalink
fix hash function bug (#1267)
Browse files Browse the repository at this point in the history
* build(deps): bump actions/cache from v2.1.4 to v2.1.5

Bumps [actions/cache](https://github.com/actions/cache) from v2.1.4 to v2.1.5.
- [Release notes](https://github.com/actions/cache/releases)
- [Commits](actions/cache@v2.1.4...1a9e213)

Signed-off-by: dependabot[bot] <support@github.com>

* improve etcd version and change create to put (#1203)

* rename

* Fix: left shift byte 24/16/8 bits, always get 0

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Xin.Zh <dragoncharlie@foxmail.com>
Co-authored-by: AlexStocks <alexstocks@foxmail.com>
Co-authored-by: randy <ztelur@gmail.com>
  • Loading branch information
5 people committed Jun 20, 2021
1 parent ffb278c commit dbc6132
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions cluster/loadbalance/consistent_hash.go
Expand Up @@ -49,26 +49,26 @@ const (
)

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

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

// ConsistentHashLoadBalance implementation of load balancing: using consistent hashing
type ConsistentHashLoadBalance struct{}
// consistentHashLoadBalance implementation of load balancing: using consistent hashing
type consistentHashLoadBalance struct{}

// NewConsistentHashLoadBalance creates NewConsistentHashLoadBalance
//
// The same parameters of the request is always sent to the same provider.
func NewConsistentHashLoadBalance() cluster.LoadBalance {
return &ConsistentHashLoadBalance{}
return &consistentHashLoadBalance{}
}

// Select gets invoker based on load balancing strategy
func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
func (lb *consistentHashLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
methodName := invocation.MethodName()
key := invokers[0].GetURL().ServiceKey() + "." + methodName

Expand All @@ -90,8 +90,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 @@ -100,9 +100,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 @@ -132,13 +132,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 @@ -148,7 +148,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 @@ -159,7 +159,7 @@ func (c *ConsistentHashSelector) selectForKey(hash uint32) protocol.Invoker {
}

// 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
}
2 changes: 1 addition & 1 deletion cluster/loadbalance/consistent_hash_test.go
Expand Up @@ -52,7 +52,7 @@ func TestConsistentHashSelectorSuite(t *testing.T) {

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

func (s *consistentHashSelectorSuite) SetupTest() {
Expand Down

0 comments on commit dbc6132

Please sign in to comment.