-
Notifications
You must be signed in to change notification settings - Fork 4
/
hashring.go
262 lines (218 loc) · 7.05 KB
/
hashring.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
// Package hashring implements a thread-safe consistent hashring with a
// pluggable hashing algorithm.
//
// This package was developed for use in a gRPC balancer, but nothing precludes
// it from being used for any other purpose.
package hashring
import (
"encoding/binary"
"errors"
"fmt"
"sort"
"strings"
"sync"
"golang.org/x/exp/slices"
)
var (
ErrMemberAlreadyExists = errors.New("member node already exists")
ErrMemberNotFound = errors.New("member node not found")
ErrNotEnoughMembers = errors.New("not enough member nodes to satisfy request")
ErrInvalidReplicationFactor = errors.New("replication factor must be at least 1")
ErrVnodeNotFound = errors.New("vnode not found")
ErrUnexpectedVnodeCount = errors.New("found a different number of vnodes than replication factor")
)
// HashFunc is the signature for any hashing function that can be leveraged by
// the hashring.
type HashFunc func([]byte) uint64
// Member represents a participating member of the hashring.
// In most use cases, you can think of a member as a node or backend.
type Member interface {
Key() string
}
// Ring provides a thread-safe consistent hashring implementation with a
// configurable number of virtual nodes.
type Ring struct {
hashfn HashFunc
replicationFactor uint16
sync.RWMutex
nodes map[string]nodeRecord
virtualNodes []virtualNode
}
// MustNew creates a new Hashring with the specified hasher function and
// replication factor.
//
// If the provided replication factor is less than 1, this function will panic.
func MustNew(hasher HashFunc, replicationFactor uint16) *Ring {
hr, err := New(hasher, replicationFactor)
if err != nil {
panic(err)
}
return hr
}
// New allocates a Ring with the specified hash function and replication factor.
//
// The replication factor must be greater than 0 and ideally be at least 20 or
// higher for quality key distribution. At 100, the standard distribution of
// key->member mapping will be about 10% of the mean. At 1000, it will be about
// 3.2%. This value should be chosen very carefully because a higher value will
// require more memory and decrease member selection performance.
func New(hashfn HashFunc, replicationFactor uint16) (*Ring, error) {
if replicationFactor < 1 {
return nil, ErrInvalidReplicationFactor
}
return &Ring{
hashfn: hashfn,
replicationFactor: replicationFactor,
nodes: map[string]nodeRecord{},
}, nil
}
// Add inserts a member into the hashring.
//
// If a member with the same key is already in the hashring,
// ErrMemberAlreadyExists is returned.
func (h *Ring) Add(member Member) error {
nodeKeyString := member.Key()
nodeHash := h.hashfn([]byte(nodeKeyString))
newNodeRecord := nodeRecord{
nodeHash,
nodeKeyString,
member,
nil,
}
h.Lock()
defer h.Unlock()
if _, ok := h.nodes[nodeKeyString]; ok {
return ErrMemberAlreadyExists
}
// virtualNodeBuffer is a 10-byte array, where 8 bytes are the hash value of
// the member key, and the final 2 bytes are an offset of the virtual node
// itself. This value is then hashed to get the final hash value of the virtual node.
virtualNodeBuffer := make([]byte, 10)
binary.LittleEndian.PutUint64(virtualNodeBuffer, nodeHash)
for i := uint16(0); i < h.replicationFactor; i++ {
binary.LittleEndian.PutUint16(virtualNodeBuffer[8:], i)
virtualNodeHash := h.hashfn(virtualNodeBuffer)
virtualNode := virtualNode{
virtualNodeHash,
newNodeRecord,
}
newNodeRecord.virtualNodes = append(newNodeRecord.virtualNodes, virtualNode)
h.virtualNodes = append(h.virtualNodes, virtualNode)
}
slices.SortFunc(h.virtualNodes, cmpVnode)
// Add the node to our map of nodes
h.nodes[nodeKeyString] = newNodeRecord
return nil
}
// Remove finds and removes the specified member from the hashring.
//
// If no member can be found, ErrMemberNotFound is returned.
func (h *Ring) Remove(member Member) error {
nodeKeyString := member.Key()
h.Lock()
defer h.Unlock()
foundNode, ok := h.nodes[nodeKeyString]
if !ok {
return ErrMemberNotFound
}
indexesToRemove := make([]int, 0, h.replicationFactor)
for _, vnode := range foundNode.virtualNodes {
vnode := vnode
vnodeIndex := sort.Search(len(h.virtualNodes), func(i int) bool {
return cmpVnode(h.virtualNodes[i], vnode) >= 0
})
if vnodeIndex >= len(h.virtualNodes) {
return fmt.Errorf(
"failed to delete vnode %020d/%020d/%s: %w",
vnode.hashvalue,
vnode.members.hashvalue,
vnode.members.nodeKey,
ErrVnodeNotFound,
)
}
indexesToRemove = append(indexesToRemove, vnodeIndex)
}
sort.Slice(indexesToRemove, func(i, j int) bool {
// NOTE: this is a reverse sort!
return indexesToRemove[j] < indexesToRemove[i]
})
if len(indexesToRemove) != int(h.replicationFactor) {
return ErrUnexpectedVnodeCount
}
for i, indexToRemove := range indexesToRemove {
// Swap this index for a later one
h.virtualNodes[indexToRemove] = h.virtualNodes[len(h.virtualNodes)-1-i]
}
// Truncate and sort the nodelist
h.virtualNodes = h.virtualNodes[:len(h.virtualNodes)-len(indexesToRemove)]
slices.SortFunc(h.virtualNodes, cmpVnode)
// Remove the node from our map
delete(h.nodes, nodeKeyString)
return nil
}
// FindN finds the first N members after the specified key.
//
// If there are not enough members to satisfy the request, ErrNotEnoughMembers
// is returned.
func (h *Ring) FindN(key []byte, num uint8) ([]Member, error) {
h.RLock()
defer h.RUnlock()
if int(num) > len(h.nodes) {
return nil, ErrNotEnoughMembers
}
keyHash := h.hashfn(key)
vnodeIndex := sort.Search(len(h.virtualNodes), func(i int) bool {
return h.virtualNodes[i].hashvalue >= keyHash
})
alreadyFoundNodeKeys := map[string]struct{}{}
foundNodes := make([]Member, 0, num)
for i := 0; i < len(h.virtualNodes) && len(foundNodes) < int(num); i++ {
boundedIndex := (i + vnodeIndex) % len(h.virtualNodes)
candidate := h.virtualNodes[boundedIndex]
if _, ok := alreadyFoundNodeKeys[candidate.members.nodeKey]; !ok {
foundNodes = append(foundNodes, candidate.members.member)
alreadyFoundNodeKeys[candidate.members.nodeKey] = struct{}{}
}
}
return foundNodes, nil
}
// Members enumerates the full set of hashring members.
func (h *Ring) Members() []Member {
h.RLock()
defer h.RUnlock()
membersCopy := make([]Member, 0, len(h.nodes))
for _, nodeInfo := range h.nodes {
membersCopy = append(membersCopy, nodeInfo.member)
}
return membersCopy
}
type nodeRecord struct {
hashvalue uint64
nodeKey string
member Member
virtualNodes []virtualNode
}
type virtualNode struct {
hashvalue uint64
members nodeRecord
}
// compareUint64 should be replaced with the standard library's cmp.Compare once
// Go 1.21 is released.
func compareUint64(x, y uint64) int {
if x < y {
return -1
}
if x > y {
return +1
}
return 0
}
func cmpVnode(a, b virtualNode) int {
if a.hashvalue == b.hashvalue {
if a.members.hashvalue == b.members.hashvalue {
return strings.Compare(a.members.nodeKey, b.members.nodeKey)
}
return compareUint64(a.members.hashvalue, b.members.hashvalue)
}
return compareUint64(a.hashvalue, b.hashvalue)
}