-
Notifications
You must be signed in to change notification settings - Fork 4
/
const_hash.go
131 lines (105 loc) · 2.2 KB
/
const_hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package container
import (
"bytes"
"container/ring"
"fmt"
"hash/crc32"
"math"
"sync"
)
type CHashNode interface {
Id() string
Health() bool
}
type CHash interface {
Adds([]CHashNode)
Del(id string)
Get(key string) CHashNode
}
type cHashSlot struct {
node CHashNode
slot uint32
}
type CHashRing struct {
ring *ring.Ring
lock sync.RWMutex
}
func (c *CHashRing) Adds(nodes []CHashNode) {
c.lock.Lock()
defer c.lock.Unlock()
if c.ring == nil {
c.ring = ring.New(1)
c.ring.Value = &cHashSlot{node: nodes[0], slot: 0}
nodes = nodes[1:]
}
curr := c.ring
next := c.ring.Next()
for i := 0; i < len(nodes); i++ {
cslot := curr.Value.(*cHashSlot).slot
nslot := next.Value.(*cHashSlot).slot
if nslot == 0 {
nslot = ^uint32(0)
}
slot := (nslot-cslot)/2 + cslot
r := ring.New(1)
r.Value = &cHashSlot{node: nodes[i], slot: slot}
curr.Link(r)
curr = next
next = next.Next()
}
}
func (c *CHashRing) Del(nodeId string) {
if c.ring == nil {
return
}
if c.ring == c.ring.Prev() {
return //one node cannot delete
}
c.lock.Lock()
defer c.lock.Unlock()
slot := c.ring.Value.(*cHashSlot)
if slot.node.Id() == nodeId {
prev := c.ring.Prev()
prev.Unlink(1)
c.ring = prev
return
}
for p := c.ring.Next(); p != c.ring; p = p.Next() {
slot := p.Value.(*cHashSlot)
if slot.node.Id() == nodeId {
prev := p.Prev()
prev.Unlink(1)
return
}
}
}
func (c *CHashRing) GetByKey(key string, mustHealth bool) CHashNode {
keyC32 := crc32.ChecksumIEEE([]byte(key))
return c.GetByC32(keyC32, mustHealth)
}
func (c *CHashRing) GetByC32(keyC32 uint32, mustHealth bool) CHashNode {
c.lock.RLock()
defer c.lock.RUnlock()
var nearDistance = float64(^uint32(0))
var nearNode CHashNode
c.ring.Do(func(r interface{}) {
hslot := r.(*cHashSlot)
if mustHealth && !hslot.node.Health() {
return
}
distance := math.Abs(float64(keyC32) - float64(hslot.slot))
if distance < nearDistance {
nearDistance = distance
nearNode = hslot.node
}
})
return nearNode
}
func (c *CHashRing) Debug() string {
var sbuf bytes.Buffer
c.ring.Do(func(r interface{}) {
hslot := r.(*cHashSlot)
sbuf.WriteString(fmt.Sprint(hslot.node, "=", hslot.slot, ","))
})
return sbuf.String()
}