2016-08-15 22:52:09 +08:00
|
|
|
package hashring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2018-04-25 14:30:58 +08:00
|
|
|
"sync"
|
2016-08-15 22:52:09 +08:00
|
|
|
// "hash"
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-04-09 11:33:06 +08:00
|
|
|
//DefaultVirualSpots default virual spots
|
|
|
|
DefaultVirualSpots = 400
|
2016-08-15 22:52:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
nodeKey string
|
|
|
|
spotValue uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodesArray []node
|
|
|
|
|
|
|
|
func (p nodesArray) Len() int { return len(p) }
|
|
|
|
func (p nodesArray) Less(i, j int) bool { return p[i].spotValue < p[j].spotValue }
|
|
|
|
func (p nodesArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
func (p nodesArray) Sort() { sort.Sort(p) }
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//HashRing store nodes and weigths
|
2016-08-15 22:52:09 +08:00
|
|
|
type HashRing struct {
|
|
|
|
virualSpots int
|
|
|
|
nodes nodesArray
|
|
|
|
weights map[string]int
|
2018-04-25 14:30:58 +08:00
|
|
|
mu sync.RWMutex
|
2016-08-15 22:52:09 +08:00
|
|
|
}
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//NewHashRing create a hash ring with virual spots
|
2016-08-15 22:52:09 +08:00
|
|
|
func NewHashRing(spots int) *HashRing {
|
|
|
|
if spots == 0 {
|
|
|
|
spots = DefaultVirualSpots
|
|
|
|
}
|
|
|
|
|
|
|
|
h := &HashRing{
|
|
|
|
virualSpots: spots,
|
|
|
|
weights: make(map[string]int),
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//AddNodes add nodes to hash ring
|
2016-08-15 22:52:09 +08:00
|
|
|
func (h *HashRing) AddNodes(nodeWeight map[string]int) {
|
2018-04-25 14:30:58 +08:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2016-08-15 22:52:09 +08:00
|
|
|
for nodeKey, w := range nodeWeight {
|
|
|
|
h.weights[nodeKey] = w
|
|
|
|
}
|
|
|
|
h.generate()
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//AddNode add node to hash ring
|
2016-08-15 22:52:09 +08:00
|
|
|
func (h *HashRing) AddNode(nodeKey string, weight int) {
|
2018-04-25 14:30:58 +08:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2016-08-15 22:52:09 +08:00
|
|
|
h.weights[nodeKey] = weight
|
|
|
|
h.generate()
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//RemoveNode remove node
|
2016-08-15 22:52:09 +08:00
|
|
|
func (h *HashRing) RemoveNode(nodeKey string) {
|
2018-04-25 14:30:58 +08:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2016-08-15 22:52:09 +08:00
|
|
|
delete(h.weights, nodeKey)
|
|
|
|
h.generate()
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//UpdateNode update node with weight
|
2016-08-15 22:52:09 +08:00
|
|
|
func (h *HashRing) UpdateNode(nodeKey string, weight int) {
|
2018-04-25 14:30:58 +08:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2016-08-15 22:52:09 +08:00
|
|
|
h.weights[nodeKey] = weight
|
|
|
|
h.generate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HashRing) generate() {
|
|
|
|
var totalW int
|
|
|
|
for _, w := range h.weights {
|
|
|
|
totalW += w
|
|
|
|
}
|
|
|
|
|
|
|
|
totalVirtualSpots := h.virualSpots * len(h.weights)
|
|
|
|
|
2019-12-21 21:32:02 +08:00
|
|
|
var nodeLen, nodeIndex int64
|
|
|
|
for _, w := range h.weights {
|
|
|
|
nodeLen += int64(math.Floor(float64(w) / float64(totalW) * float64(totalVirtualSpots)))
|
|
|
|
}
|
|
|
|
h.nodes = make(nodesArray, nodeLen)
|
2016-08-15 22:52:09 +08:00
|
|
|
for nodeKey, w := range h.weights {
|
|
|
|
spots := int(math.Floor(float64(w) / float64(totalW) * float64(totalVirtualSpots)))
|
|
|
|
for i := 1; i <= spots; i++ {
|
|
|
|
hash := sha1.New()
|
|
|
|
hash.Write([]byte(nodeKey + ":" + strconv.Itoa(i)))
|
|
|
|
hashBytes := hash.Sum(nil)
|
|
|
|
n := node{
|
|
|
|
nodeKey: nodeKey,
|
|
|
|
spotValue: genValue(hashBytes[6:10]),
|
|
|
|
}
|
2019-12-21 21:32:02 +08:00
|
|
|
h.nodes[nodeIndex] = n
|
|
|
|
nodeIndex += 1
|
2016-08-15 22:52:09 +08:00
|
|
|
hash.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.nodes.Sort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func genValue(bs []byte) uint32 {
|
|
|
|
if len(bs) < 4 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
v := (uint32(bs[3]) << 24) | (uint32(bs[2]) << 16) | (uint32(bs[1]) << 8) | (uint32(bs[0]))
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:33:06 +08:00
|
|
|
//GetNode get node with key
|
2016-08-15 22:52:09 +08:00
|
|
|
func (h *HashRing) GetNode(s string) string {
|
2018-04-25 14:30:58 +08:00
|
|
|
h.mu.RLock()
|
|
|
|
defer h.mu.RUnlock()
|
2016-08-15 22:52:09 +08:00
|
|
|
if len(h.nodes) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
hash := sha1.New()
|
|
|
|
hash.Write([]byte(s))
|
|
|
|
hashBytes := hash.Sum(nil)
|
|
|
|
v := genValue(hashBytes[6:10])
|
|
|
|
i := sort.Search(len(h.nodes), func(i int) bool { return h.nodes[i].spotValue >= v })
|
|
|
|
|
|
|
|
if i == len(h.nodes) {
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
return h.nodes[i].nodeKey
|
|
|
|
}
|