/
ConsistentHash.cs
350 lines (317 loc) · 13.7 KB
/
ConsistentHash.cs
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
//-----------------------------------------------------------------------
// <copyright file="ConsistentHash.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Akka.Actor;
using Akka.Util;
using Akka.Util.Internal;
namespace Akka.Routing
{
/// <summary>
/// Consistent Hashing node ring implementation.
///
/// A good explanation of Consistent Hashing:
/// http://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html
///
/// Note that toString of the ring nodes are used for the node
/// hash, i.e. make sure it is different for different nodes.
/// </summary>
/// <typeparam name="T">The type of objects to store in the hash.</typeparam>
public class ConsistentHash<T>
{
private readonly SortedDictionary<int, T> _nodes;
private readonly int _virtualNodesFactor;
/// <summary>
/// Initializes a new instance of the <see cref="ConsistentHash{T}"/> class.
/// </summary>
/// <param name="nodes">TBD</param>
/// <param name="virtualNodesFactor">TBD</param>
/// <exception cref="ArgumentException">
/// This exception is thrown if the given <paramref name="virtualNodesFactor"/> is less than one.
/// </exception>
public ConsistentHash(SortedDictionary<int, T> nodes, int virtualNodesFactor)
{
_nodes = nodes;
if (virtualNodesFactor < 1) throw new ArgumentException("virtualNodesFactor must be >= 1", nameof(virtualNodesFactor));
_virtualNodesFactor = virtualNodesFactor;
}
private (int[], T[])? _ring = null;
private (int[], T[])? RingTuple
{
get { return _ring ??= (_nodes.Keys.ToArray(), _nodes.Values.ToArray()); }
}
private int[] NodeHashRing
{
get { return RingTuple.Value.Item1; }
}
private T[] NodeRing
{
get { return RingTuple.Value.Item2; }
}
/// <summary>
/// Adds a node to the hash ring.
///
/// Note that <see cref="ConsistentHash{T}"/> is immutable and
/// this operation returns a new instance.
/// </summary>
/// <param name="node">The node to add to the hash ring</param>
/// <returns>A new instance of this hash ring with the given node added.</returns>
public ConsistentHash<T> Add(T node)
{
return this + node;
}
/// <summary>
/// Removes a node from the hash ring.
///
/// Note that <see cref="ConsistentHash{T}"/> is immutable and
/// this operation returns a new instance.
/// </summary>
/// <param name="node">The node to remove from the hash ring</param>
/// <returns>A new instance of this hash ring with the given node removed.</returns>
public ConsistentHash<T> Remove(T node)
{
return this - node;
}
private int Idx(int i)
{
if (i >= 0) return i; //exact match
else
{
var j = Math.Abs(i + 1);
if (j >= NodeHashRing.Length) return 0; //after last, use first
else return j; //next node clockwise
}
}
/// <summary>
/// Retrieves the node associated with the data key.
/// </summary>
/// <param name="key">The data key used for lookup.</param>
/// <exception cref="InvalidOperationException">
/// This exception is thrown if the node ring is empty.
/// </exception>
/// <returns>The node associated with the data key</returns>
public T NodeFor(byte[] key)
{
if (IsEmpty) throw new InvalidOperationException($"Can't get node for [{key}] from an empty node ring");
return NodeRing[Idx(Array.BinarySearch(NodeHashRing, ConsistentHash.HashFor(key)))];
}
/// <summary>
/// Retrieves the node associated with the data key.
/// </summary>
/// <param name="key">The data key used for lookup.</param>
/// <exception cref="InvalidOperationException">
/// This exception is thrown if the node ring is empty.
/// </exception>
/// <returns>The node associated with the data key</returns>
public T NodeFor(string key)
{
if (IsEmpty) throw new InvalidOperationException($"Can't get node for [{key}] from an empty node ring");
return NodeRing[Idx(Array.BinarySearch(NodeHashRing, ConsistentHash.HashFor(key)))];
}
/// <summary>
/// Check to determine if the node ring is empty (i.e. no nodes added or all removed)
/// </summary>
public bool IsEmpty
{
get { return !_nodes.Any(); }
}
/// <summary>
/// This class represents a surrogate of a <see cref="ConsistentHashingGroup"/> router.
/// Its main use is to help during the serialization process.
/// </summary>
public class ConsistentHashingGroupSurrogate : ISurrogate
{
/// <summary>
/// Creates a <see cref="ConsistentHashingGroup"/> encapsulated by this surrogate.
/// </summary>
/// <param name="system">The actor system that owns this router.</param>
/// <returns>The <see cref="ConsistentHashingGroup"/> encapsulated by this surrogate.</returns>
public ISurrogated FromSurrogate(ActorSystem system)
{
return new ConsistentHashingGroup(Paths);
}
/// <summary>
/// The actor paths used by this router during routee selection.
/// </summary>
public string[] Paths { get; set; }
}
#region Operator overloads
/// <summary>
/// Adds a node to the hash ring.
///
/// Note that <see cref="ConsistentHash{T}"/> is immutable and
/// this operation returns a new instance.
/// </summary>
/// <param name="hash">The hash ring used to derive a new ring with the given node added.</param>
/// <param name="node">The node to add to the hash ring</param>
/// <returns>A new instance of this hash ring with the given node added.</returns>
public static ConsistentHash<T> operator +(ConsistentHash<T> hash, T node)
{
var nodeHash = ConsistentHash.HashFor(node.ToString());
return new ConsistentHash<T>(hash._nodes.CopyAndAdd(Enumerable.Range(1, hash._virtualNodesFactor).Select(r => new KeyValuePair<int, T>(ConsistentHash.ConcatenateNodeHash(nodeHash, r), node))),
hash._virtualNodesFactor);
}
/// <summary>
/// Removes a node from the hash ring.
///
/// Note that <see cref="ConsistentHash{T}"/> is immutable and
/// this operation returns a new instance.
/// </summary>
/// <param name="hash">The hash ring used to derive a new ring with the given node removed.</param>
/// <param name="node">The node to remove from the hash ring</param>
/// <returns>A new instance of this hash ring with the given node removed.</returns>
public static ConsistentHash<T> operator -(ConsistentHash<T> hash, T node)
{
var nodeHash = ConsistentHash.HashFor(node.ToString());
return new ConsistentHash<T>(hash._nodes.CopyAndRemove(Enumerable.Range(1, hash._virtualNodesFactor).Select(r => new KeyValuePair<int, T>(ConsistentHash.ConcatenateNodeHash(nodeHash, r), node))),
hash._virtualNodesFactor);
}
#endregion
}
/// <summary>
/// Static helper class for creating <see cref="ConsistentHash{T}"/> instances.
/// </summary>
public static class ConsistentHash
{
/// <summary>
/// Factory method to create a <see cref="ConsistentHash{T}"/> instance.
/// </summary>
/// <typeparam name="T">TBD</typeparam>
/// <param name="nodes">TBD</param>
/// <param name="virtualNodesFactor">TBD</param>
/// <returns>TBD</returns>
public static ConsistentHash<T> Create<T>(IEnumerable<T> nodes, int virtualNodesFactor)
{
var sortedDict = new SortedDictionary<int, T>();
foreach (var node in nodes)
{
var nodeHash = HashFor(node.ToString());
var vnodes = Enumerable.Range(1, virtualNodesFactor)
.Select(x => ConcatenateNodeHash(nodeHash, x)).ToList();
foreach(var vnode in vnodes)
sortedDict.Add(vnode, node);
}
return new ConsistentHash<T>(sortedDict, virtualNodesFactor);
}
#region Hashing methods
/// <summary>
/// TBD
/// </summary>
/// <param name="nodeHash">TBD</param>
/// <param name="vnode">TBD</param>
/// <returns>TBD</returns>
internal static int ConcatenateNodeHash(int nodeHash, int vnode)
{
unchecked
{
var h = MurmurHash.StartHash((uint)nodeHash);
h = MurmurHash.ExtendHash(h, (uint)vnode, MurmurHash.StartMagicA, MurmurHash.StartMagicB);
return (int)MurmurHash.FinalizeHash(h);
}
}
/// <summary>
/// This class represents a surrogate of a <see cref="ConsistentHashingPool"/> router.
/// Its main use is to help during the serialization process.
/// </summary>
public class ConsistentHashingPoolSurrogate : ISurrogate
{
/// <summary>
/// Creates a <see cref="ConsistentHashingPool"/> encapsulated by this surrogate.
/// </summary>
/// <param name="system">The actor system that owns this router.</param>
/// <returns>The <see cref="ConsistentHashingPool"/> encapsulated by this surrogate.</returns>
public ISurrogated FromSurrogate(ActorSystem system)
{
return new ConsistentHashingPool(NrOfInstances, Resizer, SupervisorStrategy, RouterDispatcher, UsePoolDispatcher);
}
/// <summary>
/// The number of routees associated with this pool.
/// </summary>
public int NrOfInstances { get; set; }
/// <summary>
/// Determine whether or not to use the pool dispatcher. The dispatcher is defined in the
/// 'pool-dispatcher' configuration property in the deployment section of the router.
/// </summary>
public bool UsePoolDispatcher { get; set; }
/// <summary>
/// The resizer to use when dynamically allocating routees to the pool.
/// </summary>
public Resizer Resizer { get; set; }
/// <summary>
/// The strategy to use when supervising the pool.
/// </summary>
public SupervisorStrategy SupervisorStrategy { get; set; }
/// <summary>
/// The dispatcher to use when passing messages to the routees.
/// </summary>
public string RouterDispatcher { get; set; }
}
/// <summary>
/// Translate the offered object into a byte array, or returns the original object
/// if it needs to be serialized first.
/// </summary>
/// <param name="obj">An arbitrary .NET object</param>
/// <returns>The object encoded into bytes - in the case of custom classes, the hashcode may be used.</returns>
internal static object ToBytesOrObject(object obj)
{
switch (obj)
{
case null:
return new byte[] { 0 };
case byte[] bytes:
return bytes;
case int @int:
return BitConverter.GetBytes(@int);
case uint @uint:
return BitConverter.GetBytes(@uint);
case short @short:
return BitConverter.GetBytes(@short);
case ushort @ushort:
return BitConverter.GetBytes(@ushort);
case bool @bool:
return BitConverter.GetBytes(@bool);
case long @long:
return BitConverter.GetBytes(@long);
case ulong @ulong:
return BitConverter.GetBytes(@ulong);
case char @char:
return BitConverter.GetBytes(@char);
case float @float:
return BitConverter.GetBytes(@float);
case double @double:
return BitConverter.GetBytes(@double);
case decimal @decimal:
return new BitArray(decimal.GetBits(@decimal)).ToBytes();
case Guid guid:
return guid.ToByteArray();
default:
return obj;
}
}
/// <summary>
/// TBD
/// </summary>
/// <param name="bytes">TBD</param>
/// <returns>TBD</returns>
internal static int HashFor(byte[] bytes)
{
return MurmurHash.ByteHash(bytes);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="hashKey">TBD</param>
/// <returns>TBD</returns>
internal static int HashFor(string hashKey)
{
return MurmurHash.StringHash(hashKey);
}
#endregion
}
}