-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
171 lines (143 loc) · 3.27 KB
/
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright © 2021 Hedzr Yeh.
package hash
import (
"fmt"
"hash/crc32"
"sort"
"sync"
"github.com/hedzr/lb/lbapi"
)
// New make a new load-balancer instance with Ketama Hashing algorithm
func New(opts ...lbapi.Opt) lbapi.Balancer {
return (&hashS{
hasher: crc32.ChecksumIEEE,
replica: 32,
keys: make(map[uint32]lbapi.Peer),
peers: make(map[lbapi.Peer]bool),
}).init(opts...)
}
// WithHashFunc allows a custom hash function to be specified.
// The default Hasher hash func is crc32.ChecksumIEEE.
func WithHashFunc(hashFunc Hasher) lbapi.Opt {
return func(balancer lbapi.Balancer) {
if l, ok := balancer.(*hashS); ok {
l.hasher = hashFunc
}
}
}
// WithReplica allows a custom replica number to be specified.
// The default replica number is 32.
func WithReplica(replica int) lbapi.Opt {
return func(balancer lbapi.Balancer) {
if l, ok := balancer.(*hashS); ok {
l.replica = replica
}
}
}
// Hasher is a hash function
type Hasher func(data []byte) uint32
// hashS is a impl with ketama consist hash algor
type hashS struct {
hasher Hasher
replica int
hashRing []uint32
keys map[uint32]lbapi.Peer
peers map[lbapi.Peer]bool
rw sync.RWMutex
}
func (s *hashS) init(opts ...lbapi.Opt) *hashS {
for _, opt := range opts {
opt(s)
}
return s
}
func (s *hashS) Next(factor lbapi.Factor) (next lbapi.Peer, c lbapi.Constrainable) {
var hash uint32
if h, ok := factor.(lbapi.FactorHashable); ok {
hash = h.HashCode()
} else {
hash = s.hasher([]byte(factor.Factor()))
}
next = s.miniNext(hash)
if next != nil {
if fc, ok := factor.(lbapi.FactorComparable); ok {
next, c, _ = fc.ConstrainedBy(next)
} else if nested, ok := next.(lbapi.BalancerLite); ok {
next, c = nested.Next(factor)
}
}
return
}
func (s *hashS) miniNext(hash uint32) (next lbapi.Peer) {
s.rw.RLock()
defer s.rw.RUnlock()
ix := sort.Search(len(s.hashRing), func(i int) bool {
return s.hashRing[i] >= hash
})
if ix == len(s.hashRing) {
ix = 0
}
hashValue := s.hashRing[ix]
if p, ok := s.keys[hashValue]; ok {
if _, ok = s.peers[p]; ok {
next = p
}
}
return
}
func (s *hashS) Count() int {
s.rw.RLock()
defer s.rw.RUnlock()
return len(s.peers)
}
func (s *hashS) Add(peers ...lbapi.Peer) {
s.rw.Lock()
defer s.rw.Unlock()
for _, p := range peers {
s.peers[p] = true
for i := 0; i < s.replica; i++ {
hash := s.hasher(s.peerToBinaryID(p, i))
s.hashRing = append(s.hashRing, hash)
s.keys[hash] = p
}
}
sort.Slice(s.hashRing, func(i, j int) bool {
return s.hashRing[i] < s.hashRing[j]
})
}
func (s *hashS) peerToBinaryID(p lbapi.Peer, replica int) []byte {
str := fmt.Sprintf("%v-%05d", p, replica)
return []byte(str)
}
func (s *hashS) Remove(peer lbapi.Peer) {
s.rw.Lock()
defer s.rw.Unlock()
if _, ok := s.peers[peer]; ok {
delete(s.peers, peer)
}
var keys []uint32
var km = make(map[uint32]bool)
for i, p := range s.keys {
if p == peer {
keys = append(keys, i)
km[i] = true
}
}
for _, key := range keys {
delete(s.keys, key)
}
var vn []uint32
for _, x := range s.hashRing {
if _, ok := km[x]; !ok {
vn = append(vn, x)
}
}
s.hashRing = vn
}
func (s *hashS) Clear() {
s.rw.Lock()
defer s.rw.Unlock()
s.hashRing = nil
s.keys = make(map[uint32]lbapi.Peer)
s.peers = make(map[lbapi.Peer]bool)
}