-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashRing.cs
More file actions
266 lines (230 loc) · 8.08 KB
/
Copy pathHashRing.cs
File metadata and controls
266 lines (230 loc) · 8.08 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
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
namespace ConsistentHashing
{
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Represents a consistent hash ring.
/// </summary>
/// <typeparam name="TNode">The type of node to store in the ring.</typeparam>
public class HashRing<TNode> : IConsistentHashRing<TNode>
where TNode : IComparable<TNode>
{
private readonly List<RingItem> ring = new List<RingItem>();
/// <summary>
/// Gets all partitions where a partition is a hash range and the owner node.
/// </summary>
/// <value>An enumeration of all the partitions defined by the hash ring.</value>
public IEnumerable<Partition<TNode>> Partitions => this.GetPartitions();
/// <summary>
/// Gets whether the consistent hash ring is empty or not.
/// </summary>
/// <value>True if the ring is empty and false otherwise.</value>
public bool IsEmpty => this.ring.Count == 0;
/// <summary>
/// Adds the specified node to the hash ring at the specified point.
/// </summary>
/// <param name="node">The node to add.</param>
/// <param name="point">The point at which to add the node to.</param>
public void AddNode(TNode node, uint point)
{
var newNode = new RingItem(node, point);
int index = this.BinarySearch(point, true, node);
if (index < 0)
{
ring.Insert(~index, newNode);
}
}
/// <inheritdoc />
public IEnumerator<(TNode, uint)> GetEnumerator()
{
foreach (var item in this.ring)
{
yield return (item.Node, item.Hash);
}
}
/// <summary>
/// Gets the node that owns the hash.
/// </summary>
/// <param name="hash">The hash.</param>
/// <returns>The node that owns the hash.</returns>
public TNode GetNode(uint hash)
{
if (this.IsEmpty)
{
throw new InvalidOperationException("Ring is empty");
}
int index = this.GetNodeIndex(hash);
return this.ring[index].Node;
}
/// <summary>
/// Gets the node that owns the hash, and the next n - 1 unique nodes
/// on the ring. This method is useful for implementing the concept of
/// replicas.
///
/// If a node appears on the ring multiple times as virtual nodes, the
/// first instance will be returned and the remaining appearances will
/// be ignored. toward the limit.
/// </summary>
/// <param name="hash">The hash.</param>
/// <param name="n">How many nodes to return. May be fewer than n if n is greater than the number of nodes in the ring.</param>
/// <returns>The nodes that owns the hash, and the following n - 1 nodes.</returns>
public List<TNode> GetNodes(uint hash, int n)
{
if (this.IsEmpty)
{
throw new InvalidOperationException("Ring is empty");
}
if (n < 1)
{
throw new InvalidOperationException(
$"GetNodes() parameter n must be greater or equal to 1, but it was {n}");
}
var toReturn = new List<TNode>();
var seen = new List<TNode>(); // Faster for small values of n, which is the expected use case.
int curIndex = this.GetNodeIndex(hash);
n = Math.Min(n, ring.Count);
// Loop over the ring, reading the hash's node and following nodes
for (int tries = 0; tries < ring.Count; tries++)
{
// We need to take the entry's ID.
var curNode = ring[curIndex].Node;
if (!seen.Contains(curNode))
{
seen.Add(curNode);
toReturn.Add(curNode);
n--;
}
else
{
// We've already seen curNode node and added it. It's a
// virtual node. Don't re-add it.
}
if (n == 0)
{
// We've found all of the nodes the caller asked for.
// Return.
break;
}
if (++curIndex == ring.Count)
{
// Wrap around. We're a ring, aren't we? Faster than
// using modulo every loop.
curIndex = 0;
}
}
return toReturn;
}
/// <summary>
/// Removes all instances of the node from the hash ring.
/// </summary>
/// <param name="node">The node to remove.</param>
public void RemoveNode(TNode node)
{
bool RemovePredicate(RingItem n)
{
return node.CompareTo(n.Node) == 0;
}
this.ring.RemoveAll(RemovePredicate);
}
private int BinarySearch(uint hash, bool compareNodes, TNode node)
{
int start = 0;
int end = ring.Count - 1;
while (start <= end)
{
int mid = start + ((end - start) / 2);
uint midHash = ring[mid].Hash;
if (midHash == hash)
{
if (compareNodes)
{
int nodeComparison = node.CompareTo(ring[mid].Node);
if (nodeComparison == 0)
{
return mid;
}
else if (nodeComparison < 0)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
else
{
return mid;
}
}
else if (midHash > hash)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return ~start;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private IEnumerable<Partition<TNode>> GetPartitions()
{
if (this.IsEmpty)
{
yield break;
}
var first = this.ring[0];
uint prevHash = first.Hash;
for (int i = 1; i < this.ring.Count; i++)
{
var curr = this.ring[i];
yield return new Partition<TNode>(curr.Node, new HashRange(prevHash, curr.Hash));
prevHash = curr.Hash;
}
var last = this.ring[this.ring.Count - 1];
yield return new Partition<TNode>(first.Node, new HashRange(last.Hash, first.Hash));
}
private int GetNodeIndex(uint hash)
{
int index = this.BinarySearch(hash, false, default(TNode));
if (index >= 0)
{
int prev = index - 1;
while (prev >= 0 && this.ring[prev].Hash == hash)
{
index = prev;
prev--;
}
}
else
{
index = ~index;
if (index == this.ring.Count)
{
index = 0;
}
}
return index;
}
struct RingItem
{
public RingItem(TNode node, uint hash)
{
this.Node = node;
this.Hash = hash;
}
public TNode Node { get; }
public uint Hash { get; }
public override string ToString()
{
return $"{this.Node} - {this.Hash}";
}
}
}
}