Skip to content
Snippets Groups Projects
Commit 1953c130 authored by Xin.Zh's avatar Xin.Zh Committed by GitHub
Browse files

Merge pull request #506 from aliiohs/featue/addCommentForConsistentHash

add the comment of ConsistentHash
parents 5f91c177 ab7b642d
No related branches found
No related tags found
No related merge requests found
notifications:
commits: commits@dubbo.apache.org
issues: notifications@dubbo.apache.org
pullrequests: notifications@dubbo.apache.org
jira_options: link label link label
......@@ -27,7 +27,9 @@ import (
"strconv"
"strings"
)
import (
gxsort "github.com/dubbogo/gost/sort"
)
import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common/constant"
......@@ -40,7 +42,7 @@ const (
ConsistentHash = "consistenthash"
// HashNodes ...
HashNodes = "hash.nodes"
// HashArguments ...
// HashArguments key of hash arguments in url
HashArguments = "hash.arguments"
)
......@@ -53,16 +55,16 @@ func init() {
extension.SetLoadbalance(ConsistentHash, NewConsistentHashLoadBalance)
}
// ConsistentHashLoadBalance ...
// ConsistentHashLoadBalance implementation of load balancing: using consistent hashing
type ConsistentHashLoadBalance struct {
}
// NewConsistentHashLoadBalance ...
// NewConsistentHashLoadBalance creates NewConsistentHashLoadBalance
func NewConsistentHashLoadBalance() cluster.LoadBalance {
return &ConsistentHashLoadBalance{}
}
// Select ...
// Select gets invoker based on load balancing strategy
func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
methodName := invocation.MethodName()
key := invokers[0].GetUrl().ServiceKey() + "." + methodName
......@@ -85,27 +87,12 @@ func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocat
return selector.Select(invocation)
}
// Uint32Slice ...
type Uint32Slice []uint32
func (s Uint32Slice) Len() int {
return len(s)
}
func (s Uint32Slice) Less(i, j int) bool {
return s[i] < s[j]
}
func (s Uint32Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// ConsistentHashSelector ...
// ConsistentHashSelector implementation of Selector:get invoker based on load balancing strategy
type ConsistentHashSelector struct {
hashCode uint32
replicaNum int
virtualInvokers map[uint32]protocol.Invoker
keys Uint32Slice
keys gxsort.Uint32Slice
argumentIndex []int
}
......@@ -141,7 +128,7 @@ func newConsistentHashSelector(invokers []protocol.Invoker, methodName string,
return selector
}
// Select ...
// Select gets invoker based on load balancing strategy
func (c *ConsistentHashSelector) Select(invocation protocol.Invocation) protocol.Invoker {
key := c.toKey(invocation.Arguments())
digest := md5.Sum([]byte(key))
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment