-
Notifications
You must be signed in to change notification settings - Fork 47
/
consistent_hash.go
317 lines (275 loc) · 8.63 KB
/
consistent_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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package consistenthash
import (
"encoding/json"
"fmt"
"sort"
"strings"
"sync"
)
const (
// DefaultReplicas is the default number of bucket virtual replicas.
DefaultReplicas = 16
)
var (
_ json.Marshaler = (*ConsistentHash)(nil)
_ fmt.Stringer = (*ConsistentHash)(nil)
)
// New returns a new consistent hash.
func New(opts ...Option) *ConsistentHash {
var ch ConsistentHash
for _, opt := range opts {
opt(&ch)
}
return &ch
}
// Option mutates a consistent hash.
type Option func(*ConsistentHash)
// OptBuckets adds buckets to the consistent hash.
//
// It is functionally equiavalent to looping over the buckets
// and calling `AddBuckets(bucketsj...)` for it.
func OptBuckets(buckets ...string) Option {
return func(ch *ConsistentHash) {
ch.AddBuckets(buckets...)
}
}
// OptReplicas sets the bucket virtual replica count.
//
// More virtual replicas can help with making item assignments
// more uniform, but the tradeoff is every operation takes a little
// longer as log2 of the number of buckets times the number of virtual replicas.
//
// If not provided, the default (16) is used.
func OptReplicas(replicas int) Option {
return func(ch *ConsistentHash) { ch.replicas = replicas }
}
// OptHashFunction sets the hash function.
//
// The default hash function is `consistenthash.StableHash` which uses
// a stable crc64 hash function to preserve ordering between process restarts.
func OptHashFunction(hashFunction HashFunction) Option {
return func(ch *ConsistentHash) { ch.hashFunction = hashFunction }
}
// ConsistentHash creates hashed assignments for each bucket.
type ConsistentHash struct {
mu sync.RWMutex
replicas int
buckets map[string]struct{}
hashFunction HashFunction
hashring []HashedBucket
}
//
// properties with defaults
//
// ReplicasOrDefault is the default number of bucket virtual replicas.
func (ch *ConsistentHash) ReplicasOrDefault() int {
if ch.replicas > 0 {
return ch.replicas
}
return DefaultReplicas
}
// HashFunctionOrDefault returns the provided hash function or a default.
func (ch *ConsistentHash) HashFunctionOrDefault() HashFunction {
if ch.hashFunction != nil {
return ch.hashFunction
}
return StableHash
}
//
// Write methods
//
// AddBuckets adds a list of buckets to the consistent hash, and returns
// a boolean indiciating if _any_ buckets were added.
//
// If any of the new buckets do not exist on the hash ring the
// new bucket will be inserted `ReplicasOrDefault` number
// of times into the internal hashring.
//
// If any of the new buckets already exist on the hash ring
// no action is taken for that bucket.
//
// Calling `AddBuckets` is safe to do concurrently
// and acquires a write lock on the consistent hash reference.
func (ch *ConsistentHash) AddBuckets(newBuckets ...string) (ok bool) {
ch.mu.Lock()
defer ch.mu.Unlock()
if ch.buckets == nil {
ch.buckets = make(map[string]struct{})
}
for _, newBucket := range newBuckets {
if _, ok := ch.buckets[newBucket]; ok {
continue
}
ok = true
ch.buckets[newBucket] = struct{}{}
ch.insertUnsafe(newBucket)
}
return
}
// RemoveBucket removes a bucket from the consistent hash, and returns
// a boolean indicating if the provided bucket was found.
//
// If the bucket exists on the hash ring, the bucket and its replicas are removed.
//
// If the bucket does not exist on the ring, no action is taken.
//
// Calling `RemoveBucket` is safe to do concurrently
// and acquires a write lock on the consistent hash reference.
func (ch *ConsistentHash) RemoveBucket(toRemove string) (ok bool) {
ch.mu.Lock()
defer ch.mu.Unlock()
if ch.buckets == nil {
return
}
if _, ok = ch.buckets[toRemove]; !ok {
return
}
delete(ch.buckets, toRemove)
for x := 0; x < ch.ReplicasOrDefault(); x++ {
index := ch.search(ch.bucketHashKey(toRemove, x))
ch.hashring = append(ch.hashring[:index], ch.hashring[index+1:]...)
}
return
}
//
// Read methods
//
// Buckets returns the buckets.
//
// Calling `Buckets` is safe to do concurrently and acquires
// a read lock on the consistent hash reference.
func (ch *ConsistentHash) Buckets() (buckets []string) {
ch.mu.RLock()
defer ch.mu.RUnlock()
for bucket := range ch.buckets {
buckets = append(buckets, bucket)
}
sort.Strings(buckets)
return
}
// Assignment returns the bucket assignment for a given item.
//
// Calling `Assignment` is safe to do concurrently and acquires
// a read lock on the consistent hash reference.
func (ch *ConsistentHash) Assignment(item string) (bucket string) {
ch.mu.RLock()
defer ch.mu.RUnlock()
bucket = ch.assignmentUnsafe(item)
return
}
// IsAssigned returns if a given bucket is assigned a given item.
//
// Calling `IsAssigned` is safe to do concurrently and acquires
// a read lock on the consistent hash reference.
func (ch *ConsistentHash) IsAssigned(bucket, item string) (ok bool) {
ch.mu.RLock()
defer ch.mu.RUnlock()
ok = bucket == ch.assignmentUnsafe(item)
return
}
// Assignments returns the assignments for a given list of items organized
// by the name of the bucket, and an array of the assigned items.
//
// Calling `Assignments` is safe to do concurrently and acquires
// a read lock on the consistent hash reference.
func (ch *ConsistentHash) Assignments(items ...string) map[string][]string {
ch.mu.RLock()
defer ch.mu.RUnlock()
output := make(map[string][]string)
for _, item := range items {
bucket := ch.assignmentUnsafe(item)
output[bucket] = append(output[bucket], item)
}
return output
}
// String returns a string form of the hash for debugging purposes.
//
// Calling `String` is safe to do concurrently and acquires
// a read lock on the consistent hash reference.
func (ch *ConsistentHash) String() string {
ch.mu.RLock()
defer ch.mu.RUnlock()
var output []string
for _, bucket := range ch.hashring {
output = append(output, fmt.Sprintf("%d:%s-%02d", bucket.Hashcode, bucket.Bucket, bucket.Replica))
}
return strings.Join(output, ", ")
}
// MarshalJSON marshals the consistent hash as json.
//
// The form of the returned json is the underlying []HashedBucket
// and there is no corresponding `UnmarshalJSON` because
// it is uncertain on the other end what the hashfunction is
// because functions can't be json serialized.
//
// You should use MarshalJSON for communicating information
// for debugging purposes only.
//
// Calling `MarshalJSON` is safe to do concurrently and acquires
// a read lock on the consistent hash reference.
func (ch *ConsistentHash) MarshalJSON() ([]byte, error) {
ch.mu.RLock()
defer ch.mu.RUnlock()
return json.Marshal(ch.hashring)
}
//
// internal / unexported helpers
//
// assignmentUnsafe searches for the item's matching bucket based
// on a binary search, and if the index returned is outside the
// ring length, the first index (0) is returned to simulate wrapping around.
func (ch *ConsistentHash) assignmentUnsafe(item string) (bucket string) {
index := ch.search(item)
if index >= len(ch.hashring) {
index = 0
}
bucket = ch.hashring[index].Bucket
return
}
// insert inserts a hashring bucket.
//
// insert uses an insertion sort such that the
// resulting ring will remain sorted after insert.
//
// it will also insert `ReplicasOrDefault` copies of the bucket
// to help distribute items across buckets more evenly.
func (ch *ConsistentHash) insertUnsafe(bucket string) {
for x := 0; x < ch.ReplicasOrDefault(); x++ {
ch.hashring = InsertionSort(ch.hashring, HashedBucket{
Bucket: bucket,
Replica: x,
Hashcode: ch.hashcode(ch.bucketHashKey(bucket, x)),
})
}
}
// search does a binary search for the first hashring index whose
// node hashcode is >= the hashcode of a given item.
func (ch *ConsistentHash) search(item string) (index int) {
return sort.Search(len(ch.hashring), ch.searchFn(ch.hashcode(item)))
}
// searchFn returns a closure searching for a given hashcode.
func (ch *ConsistentHash) searchFn(hashcode uint64) func(index int) bool {
return func(index int) bool {
return ch.hashring[index].Hashcode >= hashcode
}
}
// bucketHashKey formats a hash key for a given bucket virtual replica.
func (ch *ConsistentHash) bucketHashKey(bucket string, index int) string {
return bucket + "|" + fmt.Sprintf("%02d", index)
}
// hashcode creates a hashcode for a given string
func (ch *ConsistentHash) hashcode(item string) uint64 {
return ch.HashFunctionOrDefault()([]byte(item))
}
// HashedBucket is a bucket in the hashring
// that holds the hashcode, the bucket name (as Bucket)
// and the virtual replica index.
type HashedBucket struct {
Hashcode uint64 `json:"hashcode"`
Bucket string `json:"bucket"`
Replica int `json:"replica"`
}