-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsistent_hash.go
More file actions
162 lines (133 loc) · 3.43 KB
/
consistent_hash.go
File metadata and controls
162 lines (133 loc) · 3.43 KB
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
package scale
import (
"fmt"
"hash/crc32"
"sort"
"sync"
)
// ConsistentHash implements a consistent hashing ring for partitioning work
// by tenant or conversation ID across a set of nodes.
type ConsistentHash struct {
mu sync.RWMutex
ring []uint32 // sorted hash values
nodes map[uint32]string // hash -> node name
replicas int // virtual nodes per real node
members map[string]bool // set of real node names
}
// NewConsistentHash creates a new consistent hash ring.
// replicas controls the number of virtual nodes per physical node (higher = more even distribution).
func NewConsistentHash(replicas int) *ConsistentHash {
if replicas <= 0 {
replicas = 100
}
return &ConsistentHash{
nodes: make(map[uint32]string),
replicas: replicas,
members: make(map[string]bool),
}
}
// AddNode adds a node to the hash ring.
func (h *ConsistentHash) AddNode(node string) {
h.mu.Lock()
defer h.mu.Unlock()
if h.members[node] {
return
}
h.members[node] = true
for i := 0; i < h.replicas; i++ {
hash := h.hash(fmt.Sprintf("%s-%d", node, i))
h.ring = append(h.ring, hash)
h.nodes[hash] = node
}
sort.Slice(h.ring, func(i, j int) bool {
return h.ring[i] < h.ring[j]
})
}
// RemoveNode removes a node from the hash ring.
func (h *ConsistentHash) RemoveNode(node string) {
h.mu.Lock()
defer h.mu.Unlock()
if !h.members[node] {
return
}
delete(h.members, node)
// Remove all virtual nodes for this node
newRing := make([]uint32, 0, len(h.ring))
for _, hashVal := range h.ring {
if h.nodes[hashVal] == node {
delete(h.nodes, hashVal)
} else {
newRing = append(newRing, hashVal)
}
}
h.ring = newRing
}
// GetNode returns the node responsible for the given key.
func (h *ConsistentHash) GetNode(key string) (string, error) {
h.mu.RLock()
defer h.mu.RUnlock()
if len(h.ring) == 0 {
return "", fmt.Errorf("empty hash ring")
}
hash := h.hash(key)
// Binary search for the first hash >= key hash
idx := sort.Search(len(h.ring), func(i int) bool {
return h.ring[i] >= hash
})
// Wrap around to the first node if past the end
if idx >= len(h.ring) {
idx = 0
}
return h.nodes[h.ring[idx]], nil
}
// GetNodes returns the N distinct nodes responsible for the given key,
// in ring order. Useful for replication.
func (h *ConsistentHash) GetNodes(key string, count int) ([]string, error) {
h.mu.RLock()
defer h.mu.RUnlock()
if len(h.ring) == 0 {
return nil, fmt.Errorf("empty hash ring")
}
memberCount := len(h.members)
if count > memberCount {
count = memberCount
}
hash := h.hash(key)
idx := sort.Search(len(h.ring), func(i int) bool {
return h.ring[i] >= hash
})
result := make([]string, 0, count)
seen := make(map[string]bool, count)
for len(result) < count {
if idx >= len(h.ring) {
idx = 0
}
node := h.nodes[h.ring[idx]]
if !seen[node] {
seen[node] = true
result = append(result, node)
}
idx++
}
return result, nil
}
// Members returns the set of nodes in the ring.
func (h *ConsistentHash) Members() []string {
h.mu.RLock()
defer h.mu.RUnlock()
result := make([]string, 0, len(h.members))
for node := range h.members {
result = append(result, node)
}
sort.Strings(result)
return result
}
// Size returns the number of nodes in the ring.
func (h *ConsistentHash) Size() int {
h.mu.RLock()
defer h.mu.RUnlock()
return len(h.members)
}
func (h *ConsistentHash) hash(key string) uint32 {
return crc32.ChecksumIEEE([]byte(key))
}