-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.go
709 lines (594 loc) · 17.8 KB
/
ring.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// Package ring provides all functionality related to ring hashing and key
// distribution across multiple nodes. It contains the definition for Ring which is a full
// implementation of the ring hash data structure with an additional notification mechanism that
// leverages channels. Additionally, it provides a definition for a broadcasting ring, which is a
// data structure implementing the same KeyNodeWatcher interface used by the Ring,
// with the primary difference that every node always receives every hash/key in the broadcasting ring,
// whereas only a single node receives a hash/key at a time with the ordinary Ring.
// Lastly, it provides hashing algorithms which can be used for hashing keys onto the ring, although custom
// algorithms can also be used.
package ring
import (
"fmt"
"sort"
"sync"
)
type State struct {
NodesBySlice map[uint64]string `json:"nodesBySlice"`
SlicesByHash map[uint64]uint64 `json:"slicesByHash"`
HashesByKey map[string]uint64 `json:"hashesByKey"`
}
// Op is a struct describing the movement of a key-value pair of the ring changing --
// either moving from one slice of the ring to another, being added to the ring, or being removed.
type Op[T any] struct {
Key string
Node string
Payload T
Removed bool
Updated bool
RingChange bool
}
// Node is the struct describing a single node of the hash ring, with its corresponding
// identifier used for hashing and VFactor for creating virtual slices of the node.
type Node struct {
Identifier string
VFactor int
}
// InnerKey is a struct describing a single, unique key in the system.
// The key has an associated order property which specifies the order in which notifications
// occur with respect to the other keys that have hashed to the same position in the ring.
// A lower order implies that this key will appear in a change notification before the
// other keys which hash to the same position on the ring.
type InnerKey struct {
Key string
Order int
}
type Key[T any] struct {
*InnerKey
Value T
}
// Watcher is an interface whose implementation should register and deregister channels which can watch
// for changes in a hash ring depending on the filter condition of the hash ring.
type Watcher[T any] interface {
RegisterWatcher(filter Op[T]) chan Op[T]
DeregisterWatcher(op Op[T])
}
// Keys is an interface whose implementation should add and remove keys from a collection,
// using an optional hash key to perform the hashing operation involved in emplacing the key in the collection.
type Keys[T any] interface {
Emplace(key *Key[T], hk ...string) error
Update(key *Key[T]) error
Remove(string)
}
// Nodes is an interface whose implementation should create, update, delete, get, and list
// a collection of nodes, throwing errors if necessary.
type Nodes interface {
CreateNode(node Node) error
DeleteNode(identifier string)
UpdateNode(node Node) error
GetNode(identifier string) (Node, error)
ListNodes() []string
}
// KeyNodeWatcher is a composite of the three core interfaces of the hash ring.
// It encompasses all essential functionality of the hash ring.
type KeyNodeWatcher[T any] interface {
Keys[T]
Nodes
Watcher[T]
State() *State
}
type opChans[T any] struct {
msg chan Op[T]
done chan struct{}
wg *sync.WaitGroup
}
type watcher[T any] struct {
watchMu sync.Mutex
watchers map[string]opChans[T]
Filter func(Op[T]) string
}
// RegisterWatcher provides a channel of Ops for any key-value changes of an inserted node.
// If the node registered does not exist, no notifications will come through until that node
// is inserted into the ring.
func (ring *watcher[T]) RegisterWatcher(filter Op[T]) chan Op[T] {
ring.watchMu.Lock()
defer ring.watchMu.Unlock()
opChans := opChans[T]{
msg: make(chan Op[T]),
done: make(chan struct{}),
wg: new(sync.WaitGroup),
}
ring.watchers[ring.Filter(filter)] = opChans
return opChans.msg
}
// DeregisterWatcher attempts to close the channel and delete the registration from memory.
// It is a noop if the watcher does not exist.
func (ring *watcher[T]) DeregisterWatcher(op Op[T]) {
ring.watchMu.Lock()
filter := ring.Filter(op)
c, ok := ring.watchers[filter]
if !ok {
ring.watchMu.Unlock()
return
}
delete(ring.watchers, filter)
ring.watchMu.Unlock()
close(c.done)
c.wg.Wait()
close(c.msg)
}
func (ring *watcher[T]) notify(op Op[T]) {
ring.watchMu.Lock()
watcher, ok := ring.watchers[ring.Filter(op)]
if !ok {
ring.watchMu.Unlock()
return
}
watcher.wg.Add(1)
defer watcher.wg.Done()
ring.watchMu.Unlock()
select {
case watcher.msg <- op:
case <-watcher.done:
}
}
// Ring is a hash ring implementation capable of storing key value pairs belonging to member
// nodes in one or more slices belonging to these nodes. The ring can be observed for changes
// of the key value pairs (removal, addition, slice changes).
type Ring[T any] struct {
slices []uint64
hashes []uint64
empty map[uint64]uint64
nodesBySlice map[uint64]string
vFactorByNode map[string]int
slicesByHash map[uint64]uint64
keysByHash map[uint64][]*InnerKey
contentByKey map[string]T
hashesByKey map[string]uint64
mu sync.RWMutex
Hash func(string) uint64
BaseVFactor int
ToSliceName func(string, int) string
watcher[T]
}
// New attempts to create a new ring, given an optional function to modify public fields of the ring.
func New[T any](options ...func(*Ring[T])) (*Ring[T], error) {
ring := &Ring[T]{
nodesBySlice: make(map[uint64]string),
vFactorByNode: make(map[string]int),
slicesByHash: make(map[uint64]uint64),
keysByHash: make(map[uint64][]*InnerKey),
hashesByKey: make(map[string]uint64),
contentByKey: make(map[string]T),
empty: make(map[uint64]uint64),
Hash: MD5,
BaseVFactor: 1,
ToSliceName: func(s string, i int) string {
return fmt.Sprintf("%s%d", s, i)
},
watcher: watcher[T]{
watchers: make(map[string]opChans[T]),
Filter: func(o Op[T]) string {
return o.Node
},
},
}
for _, option := range options {
option(ring)
}
// Throw error if base factor is less than 1.
if ring.BaseVFactor < 1 {
return nil, ErrInvalidBaseVFactor
}
return ring, nil
}
func (ring *Ring[T]) State() *State {
return &State{
NodesBySlice: ring.nodesBySlice,
SlicesByHash: ring.slicesByHash,
HashesByKey: ring.hashesByKey,
}
}
// CreateNode attempts to add a new node to the hash ring, including all of that nodes associated slices.
// The nodes VFactor determines how many slices will be associated with the particular node.
func (ring *Ring[T]) CreateNode(node Node) error {
ring.mu.Lock()
defer ring.mu.Unlock()
// Check to see if node already exists.
_, ok := ring.vFactorByNode[node.Identifier]
if ok {
return ErrNodeAlreadyExists
}
// Save vfactor.
ring.vFactorByNode[node.Identifier] = node.VFactor
// Create all virtual slices.
for idx := 0; idx < node.VFactor*ring.BaseVFactor; idx++ {
// Compute slice hash and insert slice.
slice := ring.Hash(ring.ToSliceName(node.Identifier, idx))
err := ring.insertSlice(slice, node.Identifier)
if err != nil {
return err
}
}
return nil
}
// DeleteNode attempts to remove a node from the hash ring given the node's identifier.
// It is a noop if no node with the given identifier exists.
func (ring *Ring[T]) DeleteNode(identifier string) {
ring.mu.Lock()
defer ring.mu.Unlock()
// Check if the node exists.
vFactor, ok := ring.vFactorByNode[identifier]
if !ok {
return
}
for idx := 0; idx < vFactor*ring.BaseVFactor; idx++ {
ring.removeSlice(ring.Hash(ring.ToSliceName(identifier, idx)))
}
// Delete vFactor.
delete(ring.vFactorByNode, identifier)
}
// UpdateNode attempts to update a node by adding or removing slices based on the new VFactor of that node.
// If the VFactor is the same as it was previously, nothing will change.
func (ring *Ring[T]) UpdateNode(node Node) error {
ring.mu.Lock()
defer ring.mu.Unlock()
vFactor, ok := ring.vFactorByNode[node.Identifier]
if !ok {
return ErrNodeNotFound
}
if node.VFactor == vFactor {
return nil
}
if node.VFactor > vFactor {
for idx := vFactor * ring.BaseVFactor; idx < node.VFactor*ring.BaseVFactor; idx++ {
slice := ring.Hash(ring.ToSliceName(node.Identifier, idx))
err := ring.insertSlice(slice, node.Identifier)
if err == ErrSliceAlreadyExists {
return ErrSliceHashCollision
}
}
} else {
for idx := node.VFactor * ring.BaseVFactor; idx < vFactor*ring.BaseVFactor; idx++ {
slice := ring.Hash(ring.ToSliceName(node.Identifier, idx))
ring.removeSlice(slice)
}
}
ring.vFactorByNode[node.Identifier] = node.VFactor
return nil
}
// GetNode attempts to find the node with the provided identifier.
func (ring *Ring[T]) GetNode(identifier string) (Node, error) {
ring.mu.RLock()
defer ring.mu.RUnlock()
vFactor, ok := ring.vFactorByNode[identifier]
if !ok {
return Node{}, ErrNodeNotFound
}
return Node{
Identifier: identifier,
VFactor: vFactor,
}, nil
}
// ListNodes lists the identifiers of the current nodes of the hash ring.
func (ring *Ring[T]) ListNodes() []string {
ring.mu.RLock()
defer ring.mu.RUnlock()
nodes := make([]string, 0, len(ring.vFactorByNode))
for node := range ring.vFactorByNode {
nodes = append(nodes, node)
}
return nodes
}
func (ring *Ring[T]) insertSlice(slice uint64, node string) error {
// Check to see if slice already exists.
_, ok := ring.nodesBySlice[slice]
if ok {
return ErrSliceAlreadyExists
}
// Insert new slice into slices and retrieve index.
var idx int
ring.slices, idx = insertPreserveOrder(ring.slices, slice, findIndex)
// Add to nodes by slice.
ring.nodesBySlice[slice] = node
// If this is the first slice, attempt to move in keys from the empty container.
if len(ring.slices) == 1 {
for _, hash := range ring.empty {
ring.slicesByHash[hash] = slice
for _, key := range ring.keysByHash[hash] {
ring.notify(Op[T]{
Key: key.Key,
Payload: ring.contentByKey[key.Key],
Node: ring.nodesBySlice[slice],
RingChange: true,
})
}
delete(ring.empty, hash)
}
} else { // Otherwise convert the hashes taken from the next slice.
nextSlice := ring.slices[findNextIndex(ring.slices, idx)]
// Convert ring hashes to new slice starting at the first hash greater than the new slice and ending at the next slice.
ring.convertHashes(
slice,
findIndex(ring.hashes, slice),
findIndex(ring.hashes, nextSlice),
nextSlice < slice,
)
}
return nil
}
func (ring *Ring[T]) removeSlice(slice uint64) {
// Noop if slice doesn't exist.
_, ok := ring.nodesBySlice[slice]
if !ok {
return
}
// Find the current index of the slice.
sliceIdx := findIndex(ring.slices, slice)
// If this is the final slice in the ring, move hashes into the empty container.
if len(ring.slices) == 1 {
for _, hash := range ring.hashes {
for _, key := range ring.keysByHash[hash] {
ring.notify(Op[T]{
Key: key.Key,
Payload: ring.contentByKey[key.Key],
Node: ring.nodesBySlice[slice],
Removed: true,
RingChange: true,
})
}
ring.empty[hash] = hash
}
} else {
// Find the previous slice.
prevSlice := ring.slices[findPrevIndex(ring.slices, sliceIdx)]
nextSlice := ring.slices[findNextIndex(ring.slices, sliceIdx)]
// Convert all of this slice's hashes to belong to the previous slice.
ring.convertHashes(
prevSlice,
findIndex(ring.hashes, slice),
findIndex(ring.hashes, nextSlice),
nextSlice < slice,
)
}
// Remove the slice from the slices array.
ring.slices, _ = removeIndex(ring.slices, sliceIdx)
// Delete from nodes by slice map.
delete(ring.nodesBySlice, slice)
}
func (ring *Ring[T]) convertHashes(
slice uint64,
hashStartIndex int,
hashEndIndex int,
circle bool,
) {
// If start and end are equal, convert the entire ring.
if circle && hashStartIndex == hashEndIndex ||
hashStartIndex == 0 && hashEndIndex == len(ring.hashes) {
for _, hash := range ring.hashes {
ring.convertHash(slice, hash)
}
return
}
if hashEndIndex == len(ring.hashes) {
hashEndIndex = 0
}
// Otherwise convert only the specified range.
for {
if hashStartIndex == len(ring.hashes) {
hashStartIndex = 0
}
if hashStartIndex == hashEndIndex {
return
}
ring.convertHash(slice, ring.hashes[hashStartIndex])
hashStartIndex++
}
}
func (ring *Ring[T]) convertHash(slice uint64, hash uint64) {
// Notify previous node of removals.
prevSlice := ring.slicesByHash[hash]
for _, key := range ring.keysByHash[hash] {
ring.notify(Op[T]{
Key: key.Key,
Payload: ring.contentByKey[key.Key],
Node: ring.nodesBySlice[prevSlice],
Removed: true,
RingChange: true,
})
}
// Reassign hash's slice and notify addition.
ring.slicesByHash[hash] = slice
for _, key := range ring.keysByHash[hash] {
ring.notify(Op[T]{
Key: key.Key,
Payload: ring.contentByKey[key.Key],
Node: ring.nodesBySlice[slice],
RingChange: true,
})
}
}
// Emplace attempts to add the given key to the hash ring.
// If the optional hash key is provided, this will be used to hash the key into the ring.
// Otherwise, the key itself will be used to hash into the ring.
// The key must unique; an error will be thrown otherwise.
func (ring *Ring[T]) Emplace(key *Key[T], hk ...string) error {
if key == nil {
return ErrNilKey
}
ring.mu.Lock()
defer ring.mu.Unlock()
// Check to see if key already exists.
_, ok := ring.hashesByKey[key.InnerKey.Key]
if ok {
return ErrKeyAlreadyExists
}
// Insert key content into keysByKey map.
ring.contentByKey[key.InnerKey.Key] = key.Value
// Identify which key will be used to create the hash.
var hashKey string
if len(hk) == 0 {
hashKey = key.InnerKey.Key
} else {
hashKey = hk[0]
}
// Hash the key.
hash := ring.Hash(hashKey)
// Insert into hash ring.
ring.insertHash(hash)
// Check to see if there are any slices to take the key.
if len(ring.slices) == 0 {
ring.empty[hash] = hash
ring.notify(Op[T]{
Key: key.InnerKey.Key,
Payload: key.Value,
})
} else {
// Find the appropriate slice this hash will belong to.
slice := ring.slices[findPrevIndex(ring.slices, findIndex(ring.slices, hash))]
ring.slicesByHash[hash] = slice
ring.notify(Op[T]{
Key: key.InnerKey.Key,
Node: ring.nodesBySlice[slice],
Payload: key.Value,
})
}
// Insert key into keys array for this hash.
ring.keysByHash[hash], _ = insertPreserveOrder(
ring.keysByHash[hash],
key.InnerKey,
findKeyIndex,
)
// Insert key into hashes by key table.
ring.hashesByKey[key.InnerKey.Key] = hash
return nil
}
// Update attempts to update the key object in the ring without changing
// its position in the ring, or its hash.
func (ring *Ring[T]) Update(key *Key[T]) error {
if key == nil {
return ErrNilKey
}
// Assure key is actually present in ring.
_, ok := ring.contentByKey[key.InnerKey.Key]
if !ok {
return ErrKeyNotFound
}
// Update key in keysByKey map.
ring.contentByKey[key.InnerKey.Key] = key.Value
// Notify subscribers of key update.
ring.notify(Op[T]{
Key: key.InnerKey.Key,
Payload: key.Value,
Node: ring.nodesBySlice[ring.slicesByHash[ring.hashesByKey[key.InnerKey.Key]]],
Updated: true,
})
return nil
}
// Remove will remove a key from the ring, given its unique key.
func (ring *Ring[T]) Remove(key string) {
ring.mu.Lock()
defer ring.mu.Unlock()
// Noop if the key doesn't exist.
hash, ok := ring.hashesByKey[key]
if !ok {
return
}
// Delete from keysByKey map.
delete(ring.contentByKey, key)
// Remove the key from the keys by hash table for this hash.
ring.keysByHash[hash], _ = removeIndex(
ring.keysByHash[hash],
findKeyByName(ring.keysByHash[hash], key),
)
// If the empty container has any elements, remove from the empty container.
if len(ring.empty) > 0 {
delete(ring.empty, hash)
ring.notify(Op[T]{
Key: key,
Removed: true,
})
} else {
// Notify new key removal from ring.
ring.notify(Op[T]{
Key: key,
Node: ring.nodesBySlice[ring.slicesByHash[hash]],
Removed: true,
})
}
// If this was the last key left for this hash, remove the hash.
if len(ring.keysByHash[hash]) == 0 {
// Remove the hash.
ring.removeHash(hash)
// Remove from slices by hash table.
delete(ring.slicesByHash, hash)
}
// Delete key from hashes by key table/
delete(ring.hashesByKey, key)
}
func (ring *Ring[T]) insertHash(hash uint64) {
idx := findIndex(ring.hashes, hash)
if idx >= len(ring.hashes) || ring.hashes[idx] != hash {
ring.hashes, _ = insertPreserveOrder(ring.hashes, hash, findIndex)
}
}
func (ring *Ring[T]) removeHash(hash uint64) {
idx := findIndex(ring.hashes, hash)
if idx < len(ring.hashes) && ring.hashes[idx] == hash {
ring.hashes, _ = removeIndex(ring.hashes, idx)
}
}
func findKeyIndex(t []*InnerKey, k *InnerKey) int {
return sort.Search(
len(t),
func(i int) bool { return t[i].Order >= k.Order },
)
}
func findKeyByName(keys []*InnerKey, name string) int {
for idx, key := range keys {
if key.Key == name {
return idx
}
}
return -1
}
// findIndex will return the index where val is located, or should be inserted (if it is not located in the array).
func findIndex(arr []uint64, val uint64) int {
return sort.Search(len(arr), func(i int) bool { return arr[i] >= val })
}
// findPrevIndex will return the index previous to the current index.
func findPrevIndex(arr []uint64, idx int) int {
if idx == 0 {
return len(arr) - 1
}
return idx - 1
}
func findNextIndex(arr []uint64, idx int) int {
if idx == len(arr)-1 {
return 0
}
return idx + 1
}
func insertPreserveOrder[T any](
arr []T,
val T,
findIndex func([]T, T) int,
) ([]T, int) {
idx := findIndex(arr, val)
if idx == len(arr) {
arr = append(arr, val)
} else {
arr = append(arr[:idx+1], arr[idx:]...)
arr[idx] = val
}
return arr, idx
}
func removeIndex[T any](arr []T, idx int) ([]T, error) {
if len(arr) == 0 {
return nil, nil
}
if idx < 0 || idx >= len(arr) {
return arr, ErrOutOfBounds
}
return append(arr[:idx], arr[idx+1:]...), nil
}