-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathketama.go
233 lines (187 loc) · 4.78 KB
/
ketama.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Package ketama implements consistent hashing compatible with Algorithm::ConsistentHash::Ketama
/*
This implementation draws from the Daisuke Maki's Perl module, which itself is
based on the original libketama code. That code was licensed under the GPLv2,
and thus so is this.
The major API change from libketama is that Algorithm::ConsistentHash::Ketama allows hashing
arbitrary strings, instead of just memcached server IP addresses.
*/
package ketama
import (
"crypto/md5"
"errors"
"fmt"
"sort"
)
type Bucket struct {
Label string
Weight int
}
type continuumPoint struct {
bucket Bucket
point uint
}
type HashFunc int
const (
// Old broken hashing function
HashFunc1 HashFunc = iota
// `Correct` binary search
HashFunc2
)
var (
ErrNegativeWeight = errors.New("ketama: negative weight is not allowed")
)
type Continuum struct {
ring points
hash HashFunc
}
type points []continuumPoint
func (c points) Less(i, j int) bool { return c[i].point < c[j].point }
func (c points) Len() int { return len(c) }
func (c points) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func md5Digest(in string) []byte {
h := md5.New()
h.Write([]byte(in))
return h.Sum(nil)
}
func hashString(in string) uint {
digest := md5Digest(in)
return uint(digest[3])<<24 | uint(digest[2])<<16 | uint(digest[1])<<8 | uint(digest[0])
}
func fixWeight(weight int) int {
// libmemcached treats 0 weight to be the same as 1 so we should behave
// the same way
if weight == 0 {
return 1
}
return weight
}
func NewWithHash(buckets []Bucket, hash HashFunc) (*Continuum, error) {
numbuckets := len(buckets)
if numbuckets == 0 {
// let them error when they try to use it
return nil, nil
}
ring := make(points, 0, numbuckets*160)
totalweight := 0
for _, b := range buckets {
if b.Weight < 0 {
return nil, ErrNegativeWeight
}
totalweight += fixWeight(b.Weight)
}
for i, b := range buckets {
pct := float32(fixWeight(b.Weight)) / float32(totalweight)
// this is the equivalent of C's promotion rules, but in Go, to maintain exact compatibility with the C library
limit := int(float32(float64(pct) * 40.0 * float64(numbuckets)))
for k := 0; k < limit; k++ {
/* 40 hashes, 4 numbers per hash = 160 points per bucket */
ss := fmt.Sprintf("%s-%d", b.Label, k)
digest := md5Digest(ss)
for h := 0; h < 4; h++ {
point := continuumPoint{
point: uint(digest[3+h*4])<<24 | uint(digest[2+h*4])<<16 | uint(digest[1+h*4])<<8 | uint(digest[h*4]),
bucket: buckets[i],
}
ring = append(ring, point)
}
}
}
sort.Sort(ring)
return &Continuum{
ring: ring,
hash: hash,
}, nil
}
func New(buckets []Bucket) (*Continuum, error) {
return NewWithHash(buckets, HashFunc1)
}
func (c Continuum) Hash(thing string) string {
if len(c.ring) == 0 {
return ""
}
h := hashString(thing)
// the above md5 is way more expensive than this branch
var i uint
switch c.hash {
case HashFunc1:
i = search(c.ring, h)
case HashFunc2:
i = uint(sort.Search(len(c.ring), func(i int) bool { return c.ring[i].point >= h }))
if i >= uint(len(c.ring)) {
i = 0
}
}
return c.ring[i].bucket.Label
}
func (c Continuum) HashMultiple(thing string, count int) []string {
if len(c.ring) == 0 {
return nil
}
h := hashString(thing)
// the above md5 is way more expensive than this branch
var i uint
switch c.hash {
case HashFunc1:
i = search(c.ring, h)
case HashFunc2:
i = uint(sort.Search(len(c.ring), func(i int) bool { return c.ring[i].point >= h }))
if i >= uint(len(c.ring)) {
i = 0
}
}
seen := make(map[string]struct{})
result := make([]string, 0, count)
for len(result) < count {
label := c.ring[i].bucket.Label
if _, ok := seen[label]; !ok {
result = append(result, label)
seen[label] = struct{}{}
}
i++
if i >= uint(len(c.ring)) {
i = 0
}
}
return result
}
// This function taken from
// https://github.com/lestrrat/Algorithm-ConsistentHash-Ketama/blob/master/xs/Ketama.xs
// In order to maintain compatibility, we must reproduce the same integer
// underflow bug introduced in
// https://github.com/lestrrat/Algorithm-ConsistentHash-Ketama/commit/1efbcc0ead13114f8e4e454a8064b842b14da6f3
func search(ring points, h uint) uint {
var maxp = uint(len(ring))
var lowp = uint(0)
var highp = maxp
for {
midp := (lowp + highp) / 2
if midp >= maxp {
if midp == maxp {
midp = 1
} else {
midp = maxp
}
return midp - 1
}
midval := ring[midp].point
var midval1 uint
if midp == 0 {
midval1 = 0
} else {
midval1 = ring[midp-1].point
}
if h <= midval && h > midval1 {
return midp
}
if midval < h {
lowp = midp + 1
} else {
// NOTE(dgryski): Maintaining compatibility with Algorithm::ConsistentHash::Ketama depends on integer underflow here
highp = midp - 1
}
if lowp > highp {
return 0
}
}
}